-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathintegration_download_test.go
605 lines (458 loc) · 25.9 KB
/
integration_download_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
package test_test
import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"github.com/gruntwork-io/terragrunt/cli/commands/run"
"github.com/gruntwork-io/terragrunt/config"
"github.com/gruntwork-io/terragrunt/internal/errors"
"github.com/gruntwork-io/terragrunt/test/helpers"
"github.com/gruntwork-io/terragrunt/tf"
"github.com/gruntwork-io/terragrunt/util"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
testFixtureLocalDownloadPath = "fixtures/download/local"
testFixtureCustomLockFile = "fixtures/download/custom-lock-file"
testFixtureRemoteDownloadPath = "fixtures/download/remote"
testFixtureInvalidRemoteDownloadPath = "fixtures/download/remote-invalid"
testFixtureInvalidRemoteDownloadPathWithRetries = "fixtures/download/remote-invalid-with-retries"
testFixtureOverrideDonwloadPath = "fixtures/download/override"
testFixtureLocalRelativeDownloadPath = "fixtures/download/local-relative"
testFixtureRemoteRelativeDownloadPath = "fixtures/download/remote-relative"
testFixtureRemoteRelativeDownloadPathWithSlash = "fixtures/download/remote-relative-with-slash"
testFixtureLocalWithBackend = "fixtures/download/local-with-backend"
testFixtureLocalWithExcludeDir = "fixtures/download/local-with-exclude-dir"
testFixtureLocalWithIncludeDir = "fixtures/download/local-with-include-dir"
testFixtureRemoteWithBackend = "fixtures/download/remote-with-backend"
testFixtureRemoteModuleInRoot = "fixtures/download/remote-module-in-root"
testFixtureLocalMissingBackend = "fixtures/download/local-with-missing-backend"
testFixtureLocalWithHiddenFolder = "fixtures/download/local-with-hidden-folder"
testFixtureLocalWithAllowedHidden = "fixtures/download/local-with-allowed-hidden"
testFixtureLocalPreventDestroy = "fixtures/download/local-with-prevent-destroy"
testFixtureLocalPreventDestroyDependencies = "fixtures/download/local-with-prevent-destroy-dependencies"
testFixtureLocalIncludePreventDestroyDependencies = "fixtures/download/local-include-with-prevent-destroy-dependencies"
testFixtureNotExistingSource = "fixtures/download/invalid-path"
testFixtureDisableCopyLockFilePath = "fixtures/download/local-disable-copy-terraform-lock-file"
testFixtureIncludeDisableCopyLockFilePath = "fixtures/download/local-include-disable-copy-lock-file/module-b"
)
func TestLocalDownload(t *testing.T) {
t.Parallel()
helpers.CleanupTerraformFolder(t, testFixtureLocalDownloadPath)
helpers.RunTerragrunt(t, "terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir "+testFixtureLocalDownloadPath)
// As of Terraform 0.14.0 we should be copying the lock file from .terragrunt-cache to the working directory
assert.FileExists(t, util.JoinPath(testFixtureLocalDownloadPath, util.TerraformLockFile))
// Run a second time to make sure the temporary folder can be reused without errors
helpers.RunTerragrunt(t, "terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir "+testFixtureLocalDownloadPath)
}
func TestLocalDownloadDisableCopyTerraformLockFile(t *testing.T) {
t.Parallel()
helpers.CleanupTerraformFolder(t, testFixtureDisableCopyLockFilePath)
helpers.RunTerragrunt(t, "terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir "+testFixtureDisableCopyLockFilePath)
// The terraform lock file should not be copied if `copy_terraform_lock_file = false`
assert.NoFileExists(t, util.JoinPath(testFixtureDisableCopyLockFilePath, util.TerraformLockFile))
// Run a second time to make sure the temporary folder can be reused without errors
helpers.RunTerragrunt(t, "terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir "+testFixtureDisableCopyLockFilePath)
}
func TestLocalIncludeDisableCopyTerraformLockFile(t *testing.T) {
t.Parallel()
helpers.CleanupTerraformFolder(t, testFixtureIncludeDisableCopyLockFilePath)
helpers.RunTerragrunt(t, "terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir "+testFixtureIncludeDisableCopyLockFilePath)
// The terraform lock file should not be copied if `copy_terraform_lock_file = false`
assert.NoFileExists(t, util.JoinPath(testFixtureIncludeDisableCopyLockFilePath, util.TerraformLockFile))
// Run a second time to make sure the temporary folder can be reused without errors
helpers.RunTerragrunt(t, "terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir "+testFixtureIncludeDisableCopyLockFilePath)
}
func TestLocalDownloadWithHiddenFolder(t *testing.T) {
t.Parallel()
helpers.CleanupTerraformFolder(t, testFixtureLocalWithHiddenFolder)
helpers.RunTerragrunt(t, "terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir "+testFixtureLocalWithHiddenFolder)
// Run a second time to make sure the temporary folder can be reused without errors
helpers.RunTerragrunt(t, "terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir "+testFixtureLocalWithHiddenFolder)
}
func TestLocalDownloadWithAllowedHiddenFiles(t *testing.T) {
t.Parallel()
helpers.CleanupTerraformFolder(t, testFixtureLocalWithAllowedHidden)
helpers.RunTerragrunt(t, fmt.Sprintf("terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir %s/live", testFixtureLocalWithAllowedHidden))
// Run a second time to make sure the temporary folder can be reused without errors
helpers.RunTerragrunt(t, fmt.Sprintf("terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir %s/live", testFixtureLocalWithAllowedHidden))
// Validate that the hidden file was copied
var (
stdout bytes.Buffer
stderr bytes.Buffer
)
err := helpers.RunTerragruntCommand(t, fmt.Sprintf("terragrunt output -raw text --terragrunt-non-interactive --terragrunt-working-dir %s/live", testFixtureLocalWithAllowedHidden), &stdout, &stderr)
helpers.LogBufferContentsLineByLine(t, stdout, "output stdout")
helpers.LogBufferContentsLineByLine(t, stderr, "output stderr")
require.NoError(t, err)
assert.Equal(t, "Hello world", stdout.String())
}
func TestLocalDownloadWithRelativePath(t *testing.T) {
t.Parallel()
helpers.CleanupTerraformFolder(t, testFixtureLocalRelativeDownloadPath)
helpers.RunTerragrunt(t, "terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir "+testFixtureLocalRelativeDownloadPath)
// Run a second time to make sure the temporary folder can be reused without errors
helpers.RunTerragrunt(t, "terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir "+testFixtureLocalRelativeDownloadPath)
}
func TestLocalWithMissingBackend(t *testing.T) {
t.Parallel()
s3BucketName := "terragrunt-test-bucket-" + strings.ToLower(helpers.UniqueID())
lockTableName := "terragrunt-lock-table-" + strings.ToLower(helpers.UniqueID())
tmpEnvPath := helpers.CopyEnvironment(t, "fixtures/download")
rootPath := util.JoinPath(tmpEnvPath, testFixtureLocalMissingBackend)
rootTerragruntConfigPath := util.JoinPath(rootPath, config.DefaultTerragruntConfigPath)
helpers.CopyTerragruntConfigAndFillPlaceholders(t, rootTerragruntConfigPath, rootTerragruntConfigPath, s3BucketName, lockTableName, "not-used")
err := helpers.RunTerragruntCommand(t, "terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir "+rootPath, os.Stdout, os.Stderr)
if assert.Error(t, err) {
underlying := errors.Unwrap(err)
assert.IsType(t, run.BackendNotDefined{}, underlying)
}
}
func TestRemoteDownload(t *testing.T) {
t.Parallel()
helpers.CleanupTerraformFolder(t, testFixtureRemoteDownloadPath)
helpers.RunTerragrunt(t, "terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir "+testFixtureRemoteDownloadPath)
// Run a second time to make sure the temporary folder can be reused without errors
helpers.RunTerragrunt(t, "terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir "+testFixtureRemoteDownloadPath)
}
func TestInvalidRemoteDownload(t *testing.T) {
t.Parallel()
helpers.CleanupTerraformFolder(t, testFixtureInvalidRemoteDownloadPath)
applyStdout := bytes.Buffer{}
applyStderr := bytes.Buffer{}
err := helpers.RunTerragruntCommand(t, "terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir "+testFixtureInvalidRemoteDownloadPath, &applyStdout, &applyStderr)
helpers.LogBufferContentsLineByLine(t, applyStdout, "apply stdout")
helpers.LogBufferContentsLineByLine(t, applyStderr, "apply stderr")
require.Error(t, err)
errMessage := "downloading source url"
assert.Containsf(t, err.Error(), errMessage, "expected error containing %q, got %s", errMessage, err)
}
func TestInvalidRemoteDownloadWithRetries(t *testing.T) {
t.Parallel()
helpers.CleanupTerraformFolder(t, testFixtureInvalidRemoteDownloadPathWithRetries)
applyStdout := bytes.Buffer{}
applyStderr := bytes.Buffer{}
err := helpers.RunTerragruntCommand(t, "terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir "+testFixtureInvalidRemoteDownloadPathWithRetries, &applyStdout, &applyStderr)
helpers.LogBufferContentsLineByLine(t, applyStdout, "apply stdout")
helpers.LogBufferContentsLineByLine(t, applyStderr, "apply stderr")
require.Error(t, err)
errMessage := "max retry attempts (2) reached for error"
assert.Containsf(t, err.Error(), errMessage, "expected error containing %q, got %s", errMessage, err)
}
func TestRemoteDownloadWithRelativePath(t *testing.T) {
t.Parallel()
helpers.CleanupTerraformFolder(t, testFixtureRemoteRelativeDownloadPath)
helpers.RunTerragrunt(t, "terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir "+testFixtureRemoteRelativeDownloadPath)
// Run a second time to make sure the temporary folder can be reused without errors
helpers.RunTerragrunt(t, "terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir "+testFixtureRemoteRelativeDownloadPath)
}
func TestRemoteDownloadWithRelativePathAndSlashInBranch(t *testing.T) {
t.Parallel()
helpers.CleanupTerraformFolder(t, testFixtureRemoteRelativeDownloadPathWithSlash)
helpers.RunTerragrunt(t, "terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir "+testFixtureRemoteRelativeDownloadPathWithSlash)
// Run a second time to make sure the temporary folder can be reused without errors
helpers.RunTerragrunt(t, "terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir "+testFixtureRemoteRelativeDownloadPathWithSlash)
}
func TestRemoteDownloadOverride(t *testing.T) {
t.Parallel()
helpers.CleanupTerraformFolder(t, testFixtureOverrideDonwloadPath)
helpers.RunTerragrunt(t, fmt.Sprintf("terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir %s --terragrunt-source %s", testFixtureOverrideDonwloadPath, "../hello-world"))
// Run a second time to make sure the temporary folder can be reused without errors
helpers.RunTerragrunt(t, fmt.Sprintf("terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir %s --terragrunt-source %s", testFixtureOverrideDonwloadPath, "../hello-world"))
}
func TestRemoteWithModuleInRoot(t *testing.T) {
t.Parallel()
tmpEnvPath := helpers.CopyEnvironment(t, testFixtureRemoteModuleInRoot)
rootPath := util.JoinPath(tmpEnvPath, testFixtureRemoteModuleInRoot)
helpers.RunTerragrunt(t, "terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir "+rootPath)
// Run a second time to make sure the temporary folder can be reused without errors
helpers.RunTerragrunt(t, "terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir "+rootPath)
}
// As of Terraform 0.14.0, if there's already a lock file in the working directory, we should be copying it into
// .terragrunt-cache
func TestCustomLockFile(t *testing.T) {
t.Parallel()
path := fmt.Sprintf("%s-%s", testFixtureCustomLockFile, wrappedBinary())
tmpEnvPath := helpers.CopyEnvironment(t, filepath.Dir(testFixtureCustomLockFile))
rootPath := util.JoinPath(tmpEnvPath, path)
helpers.RunTerragrunt(t, "terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-log-level trace --terragrunt-working-dir "+rootPath)
source := "../custom-lock-file-module"
downloadDir := util.JoinPath(rootPath, helpers.TerragruntCache)
result, err := tf.NewSource(source, downloadDir, rootPath, createLogger(), false)
require.NoError(t, err)
lockFilePath := util.JoinPath(result.WorkingDir, util.TerraformLockFile)
assert.FileExists(t, lockFilePath)
readFile, err := os.ReadFile(lockFilePath)
require.NoError(t, err)
// In our lock file, we intentionally have hashes for an older version of the AWS provider. If the lock file
// copying works, then Terraform will stick with this older version. If there is a bug, Terraform will end up
// installing a newer version (since the version is not pinned in the .tf code, only in the lock file).
assert.Contains(t, string(readFile), `version = "5.23.0"`)
}
func TestExcludeDirs(t *testing.T) {
t.Parallel()
// Populate module paths.
moduleNames := []string{
"integration-env/aws/module-aws-a",
"integration-env/gce/module-gce-b",
"integration-env/gce/module-gce-c",
"production-env/aws/module-aws-d",
"production-env/gce/module-gce-e",
}
testCases := []struct {
workingDir string
excludeArgs string
excludedModuleOutputs []string
}{
{testFixtureLocalWithExcludeDir, "--terragrunt-exclude-dir **/gce/**/*", []string{"Module GCE B", "Module GCE C", "Module GCE E"}},
{testFixtureLocalWithExcludeDir, "--terragrunt-exclude-dir production-env/**/* --terragrunt-exclude-dir **/module-gce-c", []string{"Module GCE C", "Module AWS D", "Module GCE E"}},
{testFixtureLocalWithExcludeDir, "--terragrunt-exclude-dir integration-env/gce/module-gce-b --terragrunt-exclude-dir integration-env/gce/module-gce-c --terragrunt-exclude-dir **/module-aws*", []string{"Module AWS A", "Module GCE B", "Module GCE C", "Module AWS D"}},
}
modulePaths := make(map[string]string, len(moduleNames))
for _, moduleName := range moduleNames {
modulePaths[moduleName] = util.JoinPath(testFixtureLocalWithExcludeDir, moduleName)
}
for _, testCase := range testCases {
applyAllStdout := bytes.Buffer{}
applyAllStderr := bytes.Buffer{}
// Cleanup all modules directories.
helpers.CleanupTerragruntFolder(t, testFixtureLocalWithExcludeDir)
for _, modulePath := range modulePaths {
helpers.CleanupTerragruntFolder(t, modulePath)
}
// Apply modules according to test cases
err := helpers.RunTerragruntCommand(t, fmt.Sprintf("terragrunt apply-all --terragrunt-non-interactive --terragrunt-log-level trace --terragrunt-working-dir %s %s", testCase.workingDir, testCase.excludeArgs), &applyAllStdout, &applyAllStderr)
helpers.LogBufferContentsLineByLine(t, applyAllStdout, "apply-all stdout")
helpers.LogBufferContentsLineByLine(t, applyAllStderr, "apply-all stderr")
if err != nil {
t.Fatalf("apply-all in TestExcludeDirs failed with error: %v. Full std", err)
}
// Check that the excluded module output is not present
for _, modulePath := range modulePaths {
showStdout := bytes.Buffer{}
showStderr := bytes.Buffer{}
err = helpers.RunTerragruntCommand(t, "terragrunt show --terragrunt-non-interactive --terragrunt-log-level trace --terragrunt-working-dir "+modulePath, &showStdout, &showStderr)
helpers.LogBufferContentsLineByLine(t, showStdout, "show stdout for "+modulePath)
helpers.LogBufferContentsLineByLine(t, showStderr, "show stderr for "+modulePath)
require.NoError(t, err)
output := showStdout.String()
for _, excludedModuleOutput := range testCase.excludedModuleOutputs {
assert.NotContains(t, output, excludedModuleOutput)
}
}
}
}
func TestIncludeDirs(t *testing.T) {
t.Parallel()
// Populate module paths.
moduleNames := []string{
"integration-env/aws/module-aws-a",
"integration-env/gce/module-gce-b",
"integration-env/gce/module-gce-c",
"production-env/aws/module-aws-d",
"production-env/gce/module-gce-e",
}
testCases := []struct {
workingDir string
includeArgs string
includedModuleOutputs []string
}{
{testFixtureLocalWithIncludeDir, "--terragrunt-include-dir xyz", []string{}},
{testFixtureLocalWithIncludeDir, "--terragrunt-include-dir */aws", []string{"Module GCE B", "Module GCE C", "Module GCE E"}},
{testFixtureLocalWithIncludeDir, "--terragrunt-include-dir production-env --terragrunt-include-dir **/module-gce-c", []string{"Module GCE B", "Module AWS A"}},
{testFixtureLocalWithIncludeDir, "--terragrunt-include-dir integration-env/gce/module-gce-b --terragrunt-include-dir integration-env/gce/module-gce-c --terragrunt-include-dir **/module-aws*", []string{"Module GCE E"}},
}
modulePaths := make(map[string]string, len(moduleNames))
for _, moduleName := range moduleNames {
modulePaths[moduleName] = util.JoinPath(testFixtureLocalWithIncludeDir, moduleName)
}
for _, testCase := range testCases {
applyAllStdout := bytes.Buffer{}
applyAllStderr := bytes.Buffer{}
// Cleanup all modules directories.
helpers.CleanupTerragruntFolder(t, testFixtureLocalWithIncludeDir)
for _, modulePath := range modulePaths {
helpers.CleanupTerragruntFolder(t, modulePath)
}
// Apply modules according to test cases
err := helpers.RunTerragruntCommand(t, fmt.Sprintf("terragrunt apply-all --terragrunt-non-interactive --terragrunt-log-level trace --terragrunt-working-dir %s %s", testCase.workingDir, testCase.includeArgs), &applyAllStdout, &applyAllStderr)
helpers.LogBufferContentsLineByLine(t, applyAllStdout, "apply-all stdout")
helpers.LogBufferContentsLineByLine(t, applyAllStderr, "apply-all stderr")
if err != nil {
t.Fatalf("apply-all in TestExcludeDirs failed with error: %v. Full std", err)
}
// Check that the included module output is present
for _, modulePath := range modulePaths {
showStdout := bytes.Buffer{}
showStderr := bytes.Buffer{}
err = helpers.RunTerragruntCommand(t, "terragrunt show --terragrunt-non-interactive --terragrunt-log-level trace --terragrunt-working-dir "+modulePath, &showStdout, &showStderr)
helpers.LogBufferContentsLineByLine(t, showStdout, "show stdout for "+modulePath)
helpers.LogBufferContentsLineByLine(t, showStderr, "show stderr for "+modulePath)
require.NoError(t, err)
output := showStdout.String()
for _, includedModuleOutput := range testCase.includedModuleOutputs {
assert.NotContains(t, output, includedModuleOutput)
}
}
}
}
func TestIncludeDirsDependencyConsistencyRegression(t *testing.T) {
t.Parallel()
modulePaths := []string{
"amazing-app/k8s",
"clusters/eks",
"testapp/k8s",
}
tmpPath, _ := filepath.EvalSymlinks(helpers.CopyEnvironment(t, testFixtureRegressions))
testPath := filepath.Join(tmpPath, testFixtureRegressions, "exclude-dependency")
for _, modulePath := range modulePaths {
helpers.CleanupTerragruntFolder(t, filepath.Join(testPath, modulePath))
}
includedModulesWithNone := helpers.RunValidateAllWithIncludeAndGetIncludedModules(t, testPath, []string{}, false)
assert.NotEmpty(t, includedModulesWithNone)
includedModulesWithAmzApp := helpers.RunValidateAllWithIncludeAndGetIncludedModules(t, testPath, []string{"amazing-app/k8s"}, false)
assert.Equal(t, getPathsRelativeTo(t, testPath, []string{"amazing-app/k8s", "clusters/eks"}), includedModulesWithAmzApp)
includedModulesWithTestApp := helpers.RunValidateAllWithIncludeAndGetIncludedModules(t, testPath, []string{"testapp/k8s"}, false)
assert.Equal(t, getPathsRelativeTo(t, testPath, []string{"clusters/eks", "testapp/k8s"}), includedModulesWithTestApp)
}
func TestIncludeDirsStrict(t *testing.T) {
t.Parallel()
modulePaths := []string{
"amazing-app/k8s",
"clusters/eks",
"testapp/k8s",
}
tmpPath, _ := filepath.EvalSymlinks(helpers.CopyEnvironment(t, testFixtureRegressions))
testPath := filepath.Join(tmpPath, testFixtureRegressions, "exclude-dependency")
helpers.CleanupTerragruntFolder(t, testPath)
for _, modulePath := range modulePaths {
helpers.CleanupTerragruntFolder(t, filepath.Join(testPath, modulePath))
}
includedModulesWithNone := helpers.RunValidateAllWithIncludeAndGetIncludedModules(t, testPath, []string{}, true)
assert.Equal(t, []string{}, includedModulesWithNone)
includedModulesWithAmzApp := helpers.RunValidateAllWithIncludeAndGetIncludedModules(t, testPath, []string{"amazing-app/k8s"}, true)
assert.Equal(t, getPathsRelativeTo(t, testPath, []string{"amazing-app/k8s"}), includedModulesWithAmzApp)
includedModulesWithTestApp := helpers.RunValidateAllWithIncludeAndGetIncludedModules(t, testPath, []string{"testapp/k8s"}, true)
assert.Equal(t, getPathsRelativeTo(t, testPath, []string{"testapp/k8s"}), includedModulesWithTestApp)
}
func TestTerragruntExternalDependencies(t *testing.T) {
t.Parallel()
modules := []string{
"module-a",
"module-b",
}
helpers.CleanupTerraformFolder(t, testFixtureExternalDependence)
for _, module := range modules {
helpers.CleanupTerraformFolder(t, util.JoinPath(testFixtureExternalDependence, module))
}
var (
applyAllStdout bytes.Buffer
applyAllStderr bytes.Buffer
)
rootPath := helpers.CopyEnvironment(t, testFixtureExternalDependence)
modulePath := util.JoinPath(rootPath, testFixtureExternalDependence, "module-b")
err := helpers.RunTerragruntCommand(t, "terragrunt apply-all --terragrunt-non-interactive --terragrunt-include-external-dependencies --terragrunt-forward-tf-stdout --terragrunt-working-dir "+modulePath, &applyAllStdout, &applyAllStderr)
helpers.LogBufferContentsLineByLine(t, applyAllStdout, "apply-all stdout")
helpers.LogBufferContentsLineByLine(t, applyAllStderr, "apply-all stderr")
applyAllStdoutString := applyAllStdout.String()
if err != nil {
t.Errorf("Did not expect to get error: %s", err.Error())
}
for _, module := range modules {
assert.Contains(t, applyAllStdoutString, "Hello World, "+module)
}
}
func TestPreventDestroy(t *testing.T) {
t.Parallel()
tmpEnvPath := helpers.CopyEnvironment(t, "fixtures/download")
fixtureRoot := util.JoinPath(tmpEnvPath, testFixtureLocalPreventDestroy)
helpers.RunTerragrunt(t, "terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir "+fixtureRoot)
err := helpers.RunTerragruntCommand(t, "terragrunt destroy -auto-approve --terragrunt-non-interactive --terragrunt-working-dir "+fixtureRoot, os.Stdout, os.Stderr)
if assert.Error(t, err) {
underlying := errors.Unwrap(err)
assert.IsType(t, run.ModuleIsProtected{}, underlying)
}
}
func TestPreventDestroyApply(t *testing.T) {
t.Parallel()
tmpEnvPath := helpers.CopyEnvironment(t, "fixtures/download")
fixtureRoot := util.JoinPath(tmpEnvPath, testFixtureLocalPreventDestroy)
helpers.RunTerragrunt(t, "terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir "+fixtureRoot)
err := helpers.RunTerragruntCommand(t, "terragrunt apply -destroy -auto-approve --terragrunt-non-interactive --terragrunt-working-dir "+fixtureRoot, os.Stdout, os.Stderr)
if assert.Error(t, err) {
underlying := errors.Unwrap(err)
assert.IsType(t, run.ModuleIsProtected{}, underlying)
}
}
func TestPreventDestroyDependencies(t *testing.T) {
t.Parallel()
// Populate module paths.
moduleNames := []string{
"module-a",
"module-b",
"module-c",
"module-d",
"module-e",
}
modulePaths := make(map[string]string, len(moduleNames))
for _, moduleName := range moduleNames {
modulePaths[moduleName] = util.JoinPath(testFixtureLocalPreventDestroyDependencies, moduleName)
}
// Cleanup all modules directories.
helpers.CleanupTerraformFolder(t, testFixtureLocalPreventDestroyDependencies)
for _, modulePath := range modulePaths {
helpers.CleanupTerraformFolder(t, modulePath)
}
var (
applyAllStdout bytes.Buffer
applyAllStderr bytes.Buffer
)
// Apply and destroy all modules.
err := helpers.RunTerragruntCommand(t, "terragrunt apply-all --terragrunt-non-interactive --terragrunt-working-dir "+testFixtureLocalPreventDestroyDependencies, &applyAllStdout, &applyAllStderr)
helpers.LogBufferContentsLineByLine(t, applyAllStdout, "apply-all stdout")
helpers.LogBufferContentsLineByLine(t, applyAllStderr, "apply-all stderr")
if err != nil {
t.Fatalf("apply-all in TestPreventDestroyDependencies failed with error: %v. Full std", err)
}
var (
destroyAllStdout bytes.Buffer
destroyAllStderr bytes.Buffer
)
err = helpers.RunTerragruntCommand(t, "terragrunt destroy-all --terragrunt-non-interactive --terragrunt-working-dir "+testFixtureLocalPreventDestroyDependencies, &destroyAllStdout, &destroyAllStderr)
helpers.LogBufferContentsLineByLine(t, destroyAllStdout, "destroy-all stdout")
helpers.LogBufferContentsLineByLine(t, destroyAllStderr, "destroy-all stderr")
require.Error(t, err)
var multiErrors *errors.MultiError
if ok := errors.As(err, &multiErrors); ok {
err = multiErrors
}
assert.IsType(t, &errors.MultiError{}, err)
// Check that modules C, D and E were deleted and modules A and B weren't.
for moduleName, modulePath := range modulePaths {
var (
showStdout bytes.Buffer
showStderr bytes.Buffer
)
err = helpers.RunTerragruntCommand(t, "terragrunt show --terragrunt-non-interactive --terragrunt-forward-tf-stdout --terragrunt-working-dir "+modulePath, &showStdout, &showStderr)
helpers.LogBufferContentsLineByLine(t, showStdout, "show stdout for "+modulePath)
helpers.LogBufferContentsLineByLine(t, showStderr, "show stderr for "+modulePath)
require.NoError(t, err)
output := showStdout.String()
switch moduleName {
case "module-a":
assert.Contains(t, output, "Hello, Module A")
case "module-b":
assert.Contains(t, output, "Hello, Module B")
case "module-c":
assert.NotContains(t, output, "Hello, Module C")
case "module-d":
assert.NotContains(t, output, "Hello, Module D")
case "module-e":
assert.NotContains(t, output, "Hello, Module E")
}
}
}