Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Updated handling of empty mock command list #2655

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
16 changes: 12 additions & 4 deletions config/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"`
Expand Down Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 5 additions & 0 deletions test/integration_include_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
Expand Down