Skip to content

Commit

Permalink
Fixes getting output from multiple nested dependencies (#3071)
Browse files Browse the repository at this point in the history
  • Loading branch information
levkohimins committed Apr 19, 2024
1 parent 14b45a9 commit 93c312f
Show file tree
Hide file tree
Showing 19 changed files with 129 additions and 142 deletions.
6 changes: 3 additions & 3 deletions config/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,8 @@ func decodeAndRetrieveOutputs(ctx *ParsingContext, file *hclparse.File) (*cty.Va
for _, dep := range decodedDependency.Dependencies {
depPath := getCleanedTargetConfigPath(dep.ConfigPath, ctx.TerragruntOptions.TerragruntConfigPath)
if dep.isEnabled() && util.FileExists(depPath) {
depCtx := ctx.
WithDecodeList(TerragruntFlags, TerragruntInputs).
WithTerragruntOptions(cloneTerragruntOptionsForDependency(ctx, depPath))
depOpts := cloneTerragruntOptionsForDependency(ctx, depPath)
depCtx := ctx.WithDecodeList(TerragruntFlags, TerragruntInputs).WithTerragruntOptions(depOpts)

if depConfig, err := PartialParseConfigFile(depCtx, depPath, nil); err == nil {
if depConfig.Skip {
Expand Down Expand Up @@ -542,6 +541,7 @@ 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)
// 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
49 changes: 0 additions & 49 deletions test/fixture-inputs-from-dependency/app/main.tf

This file was deleted.

16 changes: 0 additions & 16 deletions test/fixture-inputs-from-dependency/app/terragrunt.hcl

This file was deleted.

17 changes: 17 additions & 0 deletions test/fixture-inputs-from-dependency/apps/a/terragrunt.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
terraform {
source = "${get_terragrunt_dir()}/../../modules/a"
}

dependencies {
paths = ["../b"]
}

dependency "b" {
config_path = "../b"
}

inputs = {
foo = dependency.b.outputs.foo
bar = dependency.b.inputs.bar
baz = dependency.b.inputs.baz
}
3 changes: 3 additions & 0 deletions test/fixture-inputs-from-dependency/apps/b/local.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
inputs = {
baz = "b-baz"
}
19 changes: 19 additions & 0 deletions test/fixture-inputs-from-dependency/apps/b/terragrunt.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
terraform {
source = "${get_terragrunt_dir()}/../../modules/b"
}

include "root" {
path = find_in_parent_folders("terragrunt.hcl")
}

include "local" {
path = "local.hcl"
}

dependency "c" {
config_path = "../c"
}

inputs = {
foo = dependency.c.outputs.foo
}
3 changes: 3 additions & 0 deletions test/fixture-inputs-from-dependency/apps/c/terragrunt.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
terraform {
source = "${get_terragrunt_dir()}/../../modules/c"
}
3 changes: 3 additions & 0 deletions test/fixture-inputs-from-dependency/apps/terragrunt.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
inputs = {
bar = "parent-bar"
}
3 changes: 0 additions & 3 deletions test/fixture-inputs-from-dependency/cluster/main.tf

This file was deleted.

1 change: 0 additions & 1 deletion test/fixture-inputs-from-dependency/cluster/terragrunt.hcl

This file was deleted.

3 changes: 0 additions & 3 deletions test/fixture-inputs-from-dependency/dependency/local.hcl

This file was deleted.

19 changes: 0 additions & 19 deletions test/fixture-inputs-from-dependency/dependency/main.tf

This file was deleted.

16 changes: 0 additions & 16 deletions test/fixture-inputs-from-dependency/dependency/terragrunt.hcl

This file was deleted.

29 changes: 29 additions & 0 deletions test/fixture-inputs-from-dependency/modules/a/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
variable "foo" {
type = string
default = "empty"
}

output "foo" {
value = var.foo
description = "Test foo value"
}

variable "bar" {
type = string
default = "empty"
}

output "bar" {
value = var.bar
description = "Test bar value"
}

variable "baz" {
type = string
default = "empty"
}

output "baz" {
value = var.baz
description = "Test baz value"
}
17 changes: 17 additions & 0 deletions test/fixture-inputs-from-dependency/modules/b/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
variable "baz" {
type = string
default = "empty"
}

output "baz" {
value = var.baz
}

variable "foo" {
type = string
default = "empty"
}

output "foo" {
value = var.foo
}
3 changes: 3 additions & 0 deletions test/fixture-inputs-from-dependency/modules/c/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "foo" {
value = "c-foo"
}
3 changes: 0 additions & 3 deletions test/fixture-inputs-from-dependency/terragrunt.hcl

This file was deleted.

32 changes: 32 additions & 0 deletions test/integration_serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,38 @@ import (

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

func TestTerragruntInputsFromDependency(t *testing.T) {
tmpEnvPath := copyEnvironment(t, TEST_FIXTURE_INPUTS_FROM_DEPENDENCY)
rootTerragruntPath := util.JoinPath(tmpEnvPath, TEST_FIXTURE_INPUTS_FROM_DEPENDENCY)
rootPath := util.JoinPath(rootTerragruntPath, "apps")

var (
stdout bytes.Buffer
stderr bytes.Buffer
)

var appDir string

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()
}

runTerragruntRedirectOutput(t, fmt.Sprintf("terragrunt output --terragrunt-non-interactive --terragrunt-working-dir %s", appDir), &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))
}
}

func TestTerragruntDownloadDir(t *testing.T) {
cleanupTerraformFolder(t, testFixtureLocalRelativeDownloadPath)
tmpEnvPath := copyEnvironment(t, TEST_FIXTURE_GET_OUTPUT)
Expand Down
29 changes: 0 additions & 29 deletions test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1056,35 +1056,6 @@ func TestTerragruntOutputFromDependency(t *testing.T) {
assert.NotContains(t, output, "invalid character")
}

func TestTerragruntInputsFromDependency(t *testing.T) {
t.Parallel()

tmpEnvPath := copyEnvironment(t, TEST_FIXTURE_INPUTS_FROM_DEPENDENCY)
rootTerragruntPath := util.JoinPath(tmpEnvPath, TEST_FIXTURE_INPUTS_FROM_DEPENDENCY)
appTerragruntPath := util.JoinPath(rootTerragruntPath, "app")

var (
stdout bytes.Buffer
stderr bytes.Buffer
)

runTerragrunt(t, fmt.Sprintf("terragrunt run-all apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir %s", rootTerragruntPath))
runTerragruntRedirectOutput(t, fmt.Sprintf("terragrunt output --terragrunt-non-interactive --terragrunt-working-dir %s", appTerragruntPath), &stdout, &stderr)

expectedOutpus := map[string]string{
"bar": "dependency-parent-bar-value",
"baz": "dependency-include-input-baz-value",
"foo": "dependency-input-foo-value",
"dep-cluster-id": "test-id-value",
"dep-output-test": "test-value",
}

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

func TestTerragruntValidateAllCommand(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 93c312f

Please sign in to comment.