From 8b2eb99afdcaad324b5fe6cb47179355b63f31ba Mon Sep 17 00:00:00 2001 From: Denis O Date: Thu, 3 Aug 2023 22:23:11 +0300 Subject: [PATCH 1/8] Updated handling of empty command list --- config/dependency.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/dependency.go b/config/dependency.go index d2f48d061..bddfe2b03 100644 --- a/config/dependency.go +++ b/config/dependency.go @@ -381,8 +381,8 @@ func getTerragruntOutputIfAppliedElseConfiguredDefault(dependencyConfig Dependen func (dependencyConfig Dependency) shouldReturnMockOutputs(terragruntOptions *options.TerragruntOptions) bool { defaultOutputsSet := dependencyConfig.MockOutputs != nil allowedCommand := - dependencyConfig.MockOutputsAllowedTerraformCommands == nil || - len(*dependencyConfig.MockOutputsAllowedTerraformCommands) == 0 || + dependencyConfig.MockOutputsAllowedTerraformCommands != nil && + len(*dependencyConfig.MockOutputsAllowedTerraformCommands) != 0 && util.ListContainsElement(*dependencyConfig.MockOutputsAllowedTerraformCommands, terragruntOptions.OriginalTerraformCommand) return defaultOutputsSet && allowedCommand || isRenderJsonCommand(terragruntOptions) } From 2a54ad5d42fadae2420b8402b8d5ddf37fd4d0b4 Mon Sep 17 00:00:00 2001 From: Denis O Date: Fri, 4 Aug 2023 00:46:24 +0300 Subject: [PATCH 2/8] Validate testing --- .../exclude-dependency/amazing-app/k8s/terragrunt.hcl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/fixture-regressions/exclude-dependency/amazing-app/k8s/terragrunt.hcl b/test/fixture-regressions/exclude-dependency/amazing-app/k8s/terragrunt.hcl index 0d35b1318..e15507a57 100644 --- a/test/fixture-regressions/exclude-dependency/amazing-app/k8s/terragrunt.hcl +++ b/test/fixture-regressions/exclude-dependency/amazing-app/k8s/terragrunt.hcl @@ -9,6 +9,7 @@ terraform { dependency "eks" { config_path = "${get_terragrunt_dir()}/../../clusters/eks" skip_outputs = true + mock_outputs_allowed_terraform_commands = ["validate"] mock_outputs = { random_string = "foo" } From 204a3c9dc2d370f4b5b9654c09f2f0210243454c Mon Sep 17 00:00:00 2001 From: Denis O Date: Fri, 4 Aug 2023 00:49:24 +0300 Subject: [PATCH 3/8] Validate phase --- .../exclude-dependency/testapp/k8s/terragrunt.hcl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/fixture-regressions/exclude-dependency/testapp/k8s/terragrunt.hcl b/test/fixture-regressions/exclude-dependency/testapp/k8s/terragrunt.hcl index b43099351..83b3850b1 100644 --- a/test/fixture-regressions/exclude-dependency/testapp/k8s/terragrunt.hcl +++ b/test/fixture-regressions/exclude-dependency/testapp/k8s/terragrunt.hcl @@ -9,6 +9,7 @@ terraform { dependency "eks" { config_path = "${get_terragrunt_dir()}/../../clusters/eks" skip_outputs = true + mock_outputs_allowed_terraform_commands = ["validate"] mock_outputs = { random_string = "foo" } From e15f87ecf34232d87bc6716cd0295fbec1b669f9 Mon Sep 17 00:00:00 2001 From: Denis O Date: Mon, 7 Aug 2023 02:07:04 -0700 Subject: [PATCH 4/8] Validation checks --- test/fixture-destroy-warning/app/terragrunt.hcl | 2 +- test/fixture-destroy-warning/vpc/main.tf | 3 +++ test/fixture-get-output/mock-outputs/dependent1/terragrunt.hcl | 1 + test/fixture-get-output/mock-outputs/dependent3/terragrunt.hcl | 1 + .../shallow-merge/child/terragrunt.hcl | 1 + test/fixture-include-parent/dependency/main.tf | 3 +++ test/fixture-include-parent/parent.hcl | 2 +- 7 files changed, 11 insertions(+), 2 deletions(-) diff --git a/test/fixture-destroy-warning/app/terragrunt.hcl b/test/fixture-destroy-warning/app/terragrunt.hcl index ad5ab705a..e74542cc4 100644 --- a/test/fixture-destroy-warning/app/terragrunt.hcl +++ b/test/fixture-destroy-warning/app/terragrunt.hcl @@ -1,6 +1,6 @@ dependency "vpc" { config_path = "../vpc" - + mock_outputs_allowed_terraform_commands = ["validate"] mock_outputs = { vpc = "mock" } diff --git a/test/fixture-destroy-warning/vpc/main.tf b/test/fixture-destroy-warning/vpc/main.tf index e69de29bb..1e383a33f 100644 --- a/test/fixture-destroy-warning/vpc/main.tf +++ b/test/fixture-destroy-warning/vpc/main.tf @@ -0,0 +1,3 @@ +output "vpc" { + value = "123" +} \ No newline at end of file diff --git a/test/fixture-get-output/mock-outputs/dependent1/terragrunt.hcl b/test/fixture-get-output/mock-outputs/dependent1/terragrunt.hcl index 447e32920..08306decd 100644 --- a/test/fixture-get-output/mock-outputs/dependent1/terragrunt.hcl +++ b/test/fixture-get-output/mock-outputs/dependent1/terragrunt.hcl @@ -1,5 +1,6 @@ dependency "source" { config_path = "../source" + mock_outputs_allowed_terraform_commands = ["validate"] mock_outputs = { the_answer = "0" } diff --git a/test/fixture-get-output/mock-outputs/dependent3/terragrunt.hcl b/test/fixture-get-output/mock-outputs/dependent3/terragrunt.hcl index f8a06227b..d2deaee19 100644 --- a/test/fixture-get-output/mock-outputs/dependent3/terragrunt.hcl +++ b/test/fixture-get-output/mock-outputs/dependent3/terragrunt.hcl @@ -4,6 +4,7 @@ dependency "source" { the_answer = "0" } skip_outputs = true + mock_outputs_allowed_terraform_commands = ["validate"] } inputs = { diff --git a/test/fixture-include-multiple/shallow-merge/child/terragrunt.hcl b/test/fixture-include-multiple/shallow-merge/child/terragrunt.hcl index 554e5793b..6fb1c84eb 100644 --- a/test/fixture-include-multiple/shallow-merge/child/terragrunt.hcl +++ b/test/fixture-include-multiple/shallow-merge/child/terragrunt.hcl @@ -16,6 +16,7 @@ include "vpc_dep" { dependency "vpc" { config_path = "../vpc" + mock_outputs_allowed_terraform_commands = ["validate"] mock_outputs = { attribute = "mock" old_attribute = "old val" diff --git a/test/fixture-include-parent/dependency/main.tf b/test/fixture-include-parent/dependency/main.tf index e69de29bb..c1f13513c 100644 --- a/test/fixture-include-parent/dependency/main.tf +++ b/test/fixture-include-parent/dependency/main.tf @@ -0,0 +1,3 @@ +output "value" { + value = "test" +} \ No newline at end of file diff --git a/test/fixture-include-parent/parent.hcl b/test/fixture-include-parent/parent.hcl index 1cd92e89e..f828074ef 100644 --- a/test/fixture-include-parent/parent.hcl +++ b/test/fixture-include-parent/parent.hcl @@ -4,7 +4,7 @@ locals { dependency "dependency" { config_path = "../dependency" - + mock_outputs_allowed_terraform_commands = ["validate"] mock_outputs = { mock_key = "mock_value" } From bc61c396b500c7ac53bd1a6a1ce98f8c79230980 Mon Sep 17 00:00:00 2001 From: Denis O Date: Fri, 11 Aug 2023 00:04:58 +0300 Subject: [PATCH 5/8] Add support for commands which can use mocks --- config/dependency.go | 16 ++++++++++++---- test/fixture-destroy-warning/app/terragrunt.hcl | 2 +- test/fixture-destroy-warning/vpc/main.tf | 3 --- .../mock-outputs/dependent1/terragrunt.hcl | 1 - .../mock-outputs/dependent3/terragrunt.hcl | 1 - .../shallow-merge/child/terragrunt.hcl | 1 - test/fixture-include-parent/dependency/main.tf | 3 --- 7 files changed, 13 insertions(+), 14 deletions(-) delete mode 100644 test/fixture-destroy-warning/vpc/main.tf delete mode 100644 test/fixture-include-parent/dependency/main.tf diff --git a/config/dependency.go b/config/dependency.go index 31ee7f9a8..2898c27fa 100644 --- a/config/dependency.go +++ b/config/dependency.go @@ -12,6 +12,8 @@ import ( "strings" "sync" + "github.com/gruntwork-io/terratest/modules/collections" + "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/s3" "github.com/hashicorp/go-getter" @@ -33,6 +35,9 @@ import ( const renderJsonCommand = "render-json" +// commandsWhichCanUseMocks - list of commands which can use mocks if mock command is not explicitly defined +var commandsWhichCanUseMocks = []string{"init", "validate", "plan"} + type Dependency struct { Name string `hcl:",label" cty:"name"` ConfigPath string `hcl:"config_path,attr" cty:"config_path"` @@ -380,10 +385,13 @@ func getTerragruntOutputIfAppliedElseConfiguredDefault(dependencyConfig Dependen // allowed commands when `mock_outputs_allowed_terraform_commands` is set as well. func (dependencyConfig Dependency) shouldReturnMockOutputs(terragruntOptions *options.TerragruntOptions) bool { defaultOutputsSet := dependencyConfig.MockOutputs != nil - allowedCommand := - dependencyConfig.MockOutputsAllowedTerraformCommands != nil && - len(*dependencyConfig.MockOutputsAllowedTerraformCommands) != 0 && - util.ListContainsElement(*dependencyConfig.MockOutputsAllowedTerraformCommands, terragruntOptions.OriginalTerraformCommand) + var mockOutputCommands []string + if dependencyConfig.MockOutputsAllowedTerraformCommands != nil { + mockOutputCommands = *dependencyConfig.MockOutputsAllowedTerraformCommands + } + // check if can be used mocks if allowed commands are empty + mockOnEmptyCommands := len(mockOutputCommands) == 0 && len(collections.ListIntersection([]string{terragruntOptions.TerraformCommand}, commandsWhichCanUseMocks)) != 0 + allowedCommand := mockOnEmptyCommands || util.ListContainsElement(mockOutputCommands, terragruntOptions.OriginalTerraformCommand) return defaultOutputsSet && allowedCommand || isRenderJsonCommand(terragruntOptions) } diff --git a/test/fixture-destroy-warning/app/terragrunt.hcl b/test/fixture-destroy-warning/app/terragrunt.hcl index e74542cc4..ad5ab705a 100644 --- a/test/fixture-destroy-warning/app/terragrunt.hcl +++ b/test/fixture-destroy-warning/app/terragrunt.hcl @@ -1,6 +1,6 @@ dependency "vpc" { config_path = "../vpc" - mock_outputs_allowed_terraform_commands = ["validate"] + mock_outputs = { vpc = "mock" } diff --git a/test/fixture-destroy-warning/vpc/main.tf b/test/fixture-destroy-warning/vpc/main.tf deleted file mode 100644 index 1e383a33f..000000000 --- a/test/fixture-destroy-warning/vpc/main.tf +++ /dev/null @@ -1,3 +0,0 @@ -output "vpc" { - value = "123" -} \ No newline at end of file diff --git a/test/fixture-get-output/mock-outputs/dependent1/terragrunt.hcl b/test/fixture-get-output/mock-outputs/dependent1/terragrunt.hcl index 08306decd..447e32920 100644 --- a/test/fixture-get-output/mock-outputs/dependent1/terragrunt.hcl +++ b/test/fixture-get-output/mock-outputs/dependent1/terragrunt.hcl @@ -1,6 +1,5 @@ dependency "source" { config_path = "../source" - mock_outputs_allowed_terraform_commands = ["validate"] mock_outputs = { the_answer = "0" } diff --git a/test/fixture-get-output/mock-outputs/dependent3/terragrunt.hcl b/test/fixture-get-output/mock-outputs/dependent3/terragrunt.hcl index d2deaee19..f8a06227b 100644 --- a/test/fixture-get-output/mock-outputs/dependent3/terragrunt.hcl +++ b/test/fixture-get-output/mock-outputs/dependent3/terragrunt.hcl @@ -4,7 +4,6 @@ dependency "source" { the_answer = "0" } skip_outputs = true - mock_outputs_allowed_terraform_commands = ["validate"] } inputs = { diff --git a/test/fixture-include-multiple/shallow-merge/child/terragrunt.hcl b/test/fixture-include-multiple/shallow-merge/child/terragrunt.hcl index 6fb1c84eb..554e5793b 100644 --- a/test/fixture-include-multiple/shallow-merge/child/terragrunt.hcl +++ b/test/fixture-include-multiple/shallow-merge/child/terragrunt.hcl @@ -16,7 +16,6 @@ include "vpc_dep" { dependency "vpc" { config_path = "../vpc" - mock_outputs_allowed_terraform_commands = ["validate"] mock_outputs = { attribute = "mock" old_attribute = "old val" diff --git a/test/fixture-include-parent/dependency/main.tf b/test/fixture-include-parent/dependency/main.tf deleted file mode 100644 index c1f13513c..000000000 --- a/test/fixture-include-parent/dependency/main.tf +++ /dev/null @@ -1,3 +0,0 @@ -output "value" { - value = "test" -} \ No newline at end of file From ed1b8a9723613d0299a7557c4f4250f5a17de6ec Mon Sep 17 00:00:00 2001 From: Denis O Date: Fri, 11 Aug 2023 00:06:05 +0300 Subject: [PATCH 6/8] Cleanup --- .../exclude-dependency/amazing-app/k8s/terragrunt.hcl | 1 - .../exclude-dependency/testapp/k8s/terragrunt.hcl | 1 - 2 files changed, 2 deletions(-) diff --git a/test/fixture-regressions/exclude-dependency/amazing-app/k8s/terragrunt.hcl b/test/fixture-regressions/exclude-dependency/amazing-app/k8s/terragrunt.hcl index e15507a57..0d35b1318 100644 --- a/test/fixture-regressions/exclude-dependency/amazing-app/k8s/terragrunt.hcl +++ b/test/fixture-regressions/exclude-dependency/amazing-app/k8s/terragrunt.hcl @@ -9,7 +9,6 @@ terraform { dependency "eks" { config_path = "${get_terragrunt_dir()}/../../clusters/eks" skip_outputs = true - mock_outputs_allowed_terraform_commands = ["validate"] mock_outputs = { random_string = "foo" } diff --git a/test/fixture-regressions/exclude-dependency/testapp/k8s/terragrunt.hcl b/test/fixture-regressions/exclude-dependency/testapp/k8s/terragrunt.hcl index 83b3850b1..b43099351 100644 --- a/test/fixture-regressions/exclude-dependency/testapp/k8s/terragrunt.hcl +++ b/test/fixture-regressions/exclude-dependency/testapp/k8s/terragrunt.hcl @@ -9,7 +9,6 @@ terraform { dependency "eks" { config_path = "${get_terragrunt_dir()}/../../clusters/eks" skip_outputs = true - mock_outputs_allowed_terraform_commands = ["validate"] mock_outputs = { random_string = "foo" } From c7628042e95f593823719a7012b54a9f61902d09 Mon Sep 17 00:00:00 2001 From: Denis O Date: Fri, 11 Aug 2023 00:07:00 +0300 Subject: [PATCH 7/8] Fixture test update --- test/fixture-destroy-warning/vpc/main.tf | 0 test/fixture-include-parent/dependency/main.tf | 0 test/fixture-include-parent/parent.hcl | 2 +- 3 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 test/fixture-destroy-warning/vpc/main.tf create mode 100644 test/fixture-include-parent/dependency/main.tf diff --git a/test/fixture-destroy-warning/vpc/main.tf b/test/fixture-destroy-warning/vpc/main.tf new file mode 100644 index 000000000..e69de29bb diff --git a/test/fixture-include-parent/dependency/main.tf b/test/fixture-include-parent/dependency/main.tf new file mode 100644 index 000000000..e69de29bb diff --git a/test/fixture-include-parent/parent.hcl b/test/fixture-include-parent/parent.hcl index f828074ef..1cd92e89e 100644 --- a/test/fixture-include-parent/parent.hcl +++ b/test/fixture-include-parent/parent.hcl @@ -4,7 +4,7 @@ locals { dependency "dependency" { config_path = "../dependency" - mock_outputs_allowed_terraform_commands = ["validate"] + mock_outputs = { mock_key = "mock_value" } From 3c9edec62b4365292161f5e368af10ef00894612 Mon Sep 17 00:00:00 2001 From: Denis O Date: Fri, 11 Aug 2023 11:02:51 -0700 Subject: [PATCH 8/8] Tests update --- .../shallow-merge/child/terragrunt.hcl | 1 + test/integration_include_test.go | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/test/fixture-include-multiple/shallow-merge/child/terragrunt.hcl b/test/fixture-include-multiple/shallow-merge/child/terragrunt.hcl index 554e5793b..1a5a954c8 100644 --- a/test/fixture-include-multiple/shallow-merge/child/terragrunt.hcl +++ b/test/fixture-include-multiple/shallow-merge/child/terragrunt.hcl @@ -16,6 +16,7 @@ include "vpc_dep" { dependency "vpc" { config_path = "../vpc" + mock_outputs_allowed_terraform_commands = ["apply", "output"] mock_outputs = { attribute = "mock" old_attribute = "old val" diff --git a/test/integration_include_test.go b/test/integration_include_test.go index 60a016c96..bd91ad91f 100644 --- a/test/integration_include_test.go +++ b/test/integration_include_test.go @@ -18,6 +18,7 @@ import ( const ( includeDeepFixturePath = "fixture-include-deep/" includeDeepFixtureChildPath = "child" + includeDeepFixtureVpcPath = "vpc" includeFixturePath = "fixture-include/" includeShallowFixturePath = "stage/my-app" includeNoMergeFixturePath = "qa/my-app" @@ -210,6 +211,10 @@ func TestTerragruntWorksWithMultipleInclude(t *testing.T) { t.Run(filepath.Base(testCase), func(t *testing.T) { t.Parallel() + vpcPath := filepath.Join(includeMultipleFixturePath, testCase, includeDeepFixtureVpcPath) + cleanupTerraformFolder(t, vpcPath) + runTerragrunt(t, fmt.Sprintf("terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-log-level debug --terragrunt-working-dir %s", vpcPath)) + childPath := filepath.Join(includeMultipleFixturePath, testCase, includeDeepFixtureChildPath) cleanupTerraformFolder(t, childPath) runTerragrunt(t, fmt.Sprintf("terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-log-level debug --terragrunt-working-dir %s", childPath))