Skip to content

Commit

Permalink
Fixed ignoring TERRAGRUNT_DOWNLOAD for dependent modules (#3158)
Browse files Browse the repository at this point in the history
* fix: using of explicitly specified working directory

* chore: test improvements

* fix: disable sonar for integration test code

* chore: changes to trigger the sonar
  • Loading branch information
levkohimins authored May 24, 2024
1 parent 89dd2b5 commit 904fa71
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 25 deletions.
5 changes: 5 additions & 0 deletions cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,11 @@ func initialSetup(cliCtx *cli.Context, opts *options.TerragruntOptions) error {
opts.TerragruntConfigPath = util.JoinPath(opts.WorkingDir, opts.TerragruntConfigPath)
}

opts.TerragruntConfigPath, err = filepath.Abs(opts.TerragruntConfigPath)
if err != nil {
return errors.WithStackTrace(err)
}

opts.TerraformPath = filepath.ToSlash(opts.TerraformPath)

opts.ExcludeDirs, err = util.GlobCanonicalPath(opts.WorkingDir, opts.ExcludeDirs...)
Expand Down
8 changes: 7 additions & 1 deletion config/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,13 @@ func getOutputJsonWithCaching(ctx *ParsingContext, targetConfig string) ([]byte,
func cloneTerragruntOptionsForDependency(ctx *ParsingContext, targetConfigPath string) *options.TerragruntOptions {
targetOptions := ctx.TerragruntOptions.Clone(targetConfigPath)
targetOptions.OriginalTerragruntConfigPath = targetConfigPath
targetOptions.DownloadDir = filepath.Join(filepath.Dir(targetConfigPath), util.TerragruntCacheDir)

// `needUpdateDownloadDir` is true if `DownloadDir` was not explicitly specified by the user and we need to assign the default download dir, otherwise leave as is.
needUpdateDownloadDir := filepath.Join(filepath.Dir(ctx.TerragruntOptions.TerragruntConfigPath), util.TerragruntCacheDir) == ctx.TerragruntOptions.DownloadDir
if needUpdateDownloadDir {
targetOptions.DownloadDir = filepath.Join(filepath.Dir(targetConfigPath), util.TerragruntCacheDir)
}

// Clear IAMRoleOptions in case if it is different from one passed through CLI to allow dependencies to define own iam roles
// https://github.com/gruntwork-io/terragrunt/issues/1853#issuecomment-940102676
if targetOptions.IAMRoleOptions != targetOptions.OriginalIAMRoleOptions {
Expand Down
72 changes: 54 additions & 18 deletions test/integration_serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
"github.com/gruntwork-io/terragrunt/util"
)

// @SONAR_STOP@

// NOTE: We don't run these tests in parallel because it modifies the environment variable, so it can affect other tests

func TestTerragruntProviderCacheWithFilesystemMirror(t *testing.T) {
Expand Down Expand Up @@ -117,30 +119,62 @@ func TestTerragruntInputsFromDependency(t *testing.T) {
rootTerragruntPath := util.JoinPath(tmpEnvPath, TEST_FIXTURE_INPUTS_FROM_DEPENDENCY)
rootPath := util.JoinPath(rootTerragruntPath, "apps")

var (
stdout bytes.Buffer
stderr bytes.Buffer
)
curDir, err := os.Getwd()
require.NoError(t, err)

var appDir string
relRootPath, err := filepath.Rel(curDir, rootPath)
require.NoError(t, err)

for _, app := range []string{"c", "b", "a"} {
appDir = filepath.Join(rootPath, app)
runTerragrunt(t, fmt.Sprintf("terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir %s", appDir))
config.ClearOutputCache()
testCases := []struct {
rootPath string
downloadDir string
}{
{
rootPath: rootPath,
downloadDir: "",
},
{
rootPath: relRootPath,
downloadDir: filepath.Join(rootTerragruntPath, "download-dir"),
},
}

runTerragruntRedirectOutput(t, fmt.Sprintf("terragrunt output --terragrunt-non-interactive --terragrunt-working-dir %s", appDir), &stdout, &stderr)
for _, testCase := range testCases {
var (
stdout bytes.Buffer
stderr bytes.Buffer
)

expectedOutpus := map[string]string{
"bar": "parent-bar",
"baz": "b-baz",
"foo": "c-foo",
}
var (
appDir string
appDirs = []string{"c", "b", "a"}
)

output := stdout.String()
for key, value := range expectedOutpus {
assert.Contains(t, output, fmt.Sprintf("%s = %q\n", key, value))
for _, app := range appDirs {
appDir = filepath.Join(testCase.rootPath, app)

runTerragrunt(t, fmt.Sprintf("terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir %s --terragrunt-download-dir=%s", appDir, testCase.downloadDir))
config.ClearOutputCache()
}

if testCase.downloadDir != "" {
entries, err := os.ReadDir(testCase.downloadDir)
require.NoError(t, err)
assert.Equal(t, len(appDirs), len(entries))
}

runTerragruntRedirectOutput(t, fmt.Sprintf("terragrunt output --terragrunt-non-interactive --terragrunt-working-dir %s --terragrunt-download-dir=%s", appDir, testCase.downloadDir), &stdout, &stderr)

expectedOutpus := map[string]string{
"bar": "parent-bar",
"baz": "b-baz",
"foo": "c-foo",
}

output := stdout.String()
for key, value := range expectedOutpus {
assert.Contains(t, output, fmt.Sprintf("%s = %q\n", key, value))
}
}
}

Expand Down Expand Up @@ -691,3 +725,5 @@ func TestTerragruntProduceTelemetryInCasOfError(t *testing.T) {
assert.Contains(t, output, "exception.message")
assert.Contains(t, output, "\"Name\":\"exception\"")
}

// @SONAR_START@
22 changes: 16 additions & 6 deletions test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ import (
"github.com/gruntwork-io/terragrunt/util"
)

// @SONAR_STOP@

// hard-code this to match the test fixture for now
const (
TERRAFORM_REMOTE_STATE_S3_REGION = "us-west-2"
Expand Down Expand Up @@ -4189,10 +4191,13 @@ func TestLogFailedLocalsEvaluation(t *testing.T) {
)

err := runTerragruntCommand(t, fmt.Sprintf("terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir %s --terragrunt-log-level debug", TEST_FIXTURE_BROKEN_LOCALS), &stdout, &stderr)
output := stderr.String()

assert.Error(t, err)
assert.Contains(t, output, "Encountered error while evaluating locals in file fixture-broken-locals/terragrunt.hcl")

testdataDir, err := filepath.Abs(TEST_FIXTURE_BROKEN_LOCALS)
require.NoError(t, err)

output := stderr.String()
assert.Contains(t, output, fmt.Sprintf("Encountered error while evaluating locals in file %s", filepath.Join(testdataDir, "terragrunt.hcl")))
}

func TestLogFailingDependencies(t *testing.T) {
Expand All @@ -4204,10 +4209,13 @@ func TestLogFailingDependencies(t *testing.T) {
path := filepath.Join(TEST_FIXTURE_BROKEN_DEPENDENCY, "app")

err := runTerragruntCommand(t, fmt.Sprintf("terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir %s --terragrunt-log-level debug", path), &stdout, &stderr)
output := stderr.String()

assert.Error(t, err)
assert.Contains(t, output, wrappedBinary()+" invocation failed in fixture-broken-dependency/dependency")

testdataDir, err := filepath.Abs(TEST_FIXTURE_BROKEN_DEPENDENCY)
require.NoError(t, err)

output := stderr.String()
assert.Contains(t, output, fmt.Sprintf("%s invocation failed in %s", wrappedBinary(), testdataDir))
}

func cleanupTerraformFolder(t *testing.T, templatesPath string) {
Expand Down Expand Up @@ -7115,3 +7123,5 @@ func findFilesWithExtension(dir string, ext string) ([]string, error) {

return files, err
}

// @SONAR_START@

0 comments on commit 904fa71

Please sign in to comment.