Skip to content

Commit 654ec21

Browse files
committed
fix: Adding integration test for stack find_in_parent_folders
1 parent a43b30a commit 654ec21

File tree

8 files changed

+124
-9
lines changed

8 files changed

+124
-9
lines changed

config/config_helpers.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,6 @@ func getPathToRepoRoot(ctx *ParsingContext, l log.Logger) (string, error) {
272272
// GetTerragruntDir returns the directory where the Terragrunt configuration file lives.
273273
func GetTerragruntDir(ctx *ParsingContext, l log.Logger) (string, error) {
274274
path := ctx.TerragruntOptions.TerragruntConfigPath
275-
if val, ok := ctx.Context.Value(stackParserContext{}).(stackParserContext); ok {
276-
path = val.stackConfigFile
277-
}
278275

279276
terragruntConfigFileAbsPath, err := filepath.Abs(path)
280277
if err != nil {

config/config_helpers_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package config_test
22

33
import (
4+
"context"
45
"fmt"
56
"os"
67
"path/filepath"
@@ -333,6 +334,49 @@ func TestFindInParentFolders(t *testing.T) {
333334
}
334335
}
335336

337+
func TestFindInParentFoldersWithStackFile(t *testing.T) {
338+
t.Parallel()
339+
340+
tempDir := t.TempDir()
341+
342+
regionHclPath := filepath.Join(tempDir, "region.hcl")
343+
regionHclContent := `locals {
344+
aws_region = "us-east-1"
345+
}`
346+
err := os.WriteFile(regionHclPath, []byte(regionHclContent), 0644)
347+
require.NoError(t, err)
348+
349+
stackDir := filepath.Join(tempDir, "stack")
350+
err = os.MkdirAll(stackDir, 0755)
351+
require.NoError(t, err)
352+
353+
stackHclPath := filepath.Join(stackDir, "terragrunt.stack.hcl")
354+
stackHclContent := `locals {
355+
regions_vars = read_terragrunt_config(find_in_parent_folders("region.hcl"))
356+
region = local.regions_vars.locals.aws_region
357+
}
358+
359+
unit "test" {
360+
source = "."
361+
path = "test"
362+
}`
363+
err = os.WriteFile(stackHclPath, []byte(stackHclContent), 0644)
364+
require.NoError(t, err)
365+
366+
l := logger.CreateLogger()
367+
opts, err := options.NewTerragruntOptionsForTest(stackHclPath)
368+
require.NoError(t, err)
369+
opts.WorkingDir = tempDir
370+
371+
stackConfig, err := config.ReadStackConfigFile(context.Background(), l, opts, stackHclPath, nil)
372+
require.NoError(t, err)
373+
require.NotNil(t, stackConfig)
374+
375+
region, exists := stackConfig.Locals["region"]
376+
require.True(t, exists, "Expected 'region' local to be parsed")
377+
require.Equal(t, "us-east-1", region)
378+
}
379+
336380
func TestResolveTerragruntInterpolation(t *testing.T) {
337381
t.Parallel()
338382

config/stack.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,6 @@ type Stack struct {
7474
Path string `hcl:"path,attr"`
7575
}
7676

77-
type stackParserContext struct {
78-
stackConfigFile string
79-
}
80-
8177
// GenerateStacks generates the stack files.
8278
func GenerateStacks(ctx context.Context, l log.Logger, opts *options.TerragruntOptions) error {
8379
processedFiles := make(map[string]bool)
@@ -693,8 +689,10 @@ func (u *Unit) ReadOutputs(ctx context.Context, l log.Logger, opts *options.Terr
693689
func ReadStackConfigFile(ctx context.Context, l log.Logger, opts *options.TerragruntOptions, filePath string, values *cty.Value) (*StackConfig, error) {
694690
l.Debugf("Reading Terragrunt stack config file at %s", filePath)
695691

696-
ctx = context.WithValue(ctx, stackParserContext{}, stackParserContext{stackConfigFile: filePath})
697-
parser := NewParsingContext(ctx, l, opts)
692+
stackOpts := opts.Clone()
693+
stackOpts.TerragruntConfigPath = filePath
694+
695+
parser := NewParsingContext(ctx, l, stackOpts)
698696

699697
file, err := hclparse.NewParser(parser.ParserOptions...).ParseFromFile(filePath)
700698
if err != nil {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
locals {
2+
mock_vars = read_terragrunt_config(find_in_parent_folders("mock.hcl"))
3+
mock = local.mock_vars.locals.mock
4+
}
5+
6+
unit "foo" {
7+
source = "../../units/foo"
8+
path = "foo"
9+
10+
values = {
11+
mock = local.mock
12+
}
13+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
locals {
2+
mock = "mock"
3+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
variable "mock" {
2+
description = "Mock value to be passed through"
3+
type = string
4+
}
5+
6+
output "mock" {
7+
value = var.mock
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
terraform {
2+
source = "."
3+
}
4+
5+
inputs = {
6+
mock = values.mock
7+
}

test/integration_stacks_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const (
5050
testFixtureStackTerragruntDir = "fixtures/stacks/terragrunt-dir"
5151
testFixtureStacksAllNoStackDir = "fixtures/stacks/all-no-stack-dir"
5252
testFixtureStackNoDotTerragruntStackOutput = "fixtures/stacks/no-dot-terragrunt-stack-output"
53+
testFixtureStackFindInParentFolders = "fixtures/stacks/find-in-parent-folders"
5354
)
5455

5556
func TestStacksGenerateBasicWithQueueIncludeDirFlag(t *testing.T) {
@@ -1393,3 +1394,47 @@ func TestStackOutputWithNoDotTerragruntStack(t *testing.T) {
13931394

13941395
assert.Contains(t, stdout, "name = \"app1\"")
13951396
}
1397+
1398+
func TestStackFindInParentFolders(t *testing.T) {
1399+
t.Parallel()
1400+
1401+
helpers.CleanupTerraformFolder(t, testFixtureStackFindInParentFolders)
1402+
tmpEnvPath := helpers.CopyEnvironment(t, testFixtureStackFindInParentFolders)
1403+
rootPath := util.JoinPath(tmpEnvPath, testFixtureStackFindInParentFolders, "live")
1404+
1405+
// Test that stack generation works from the live directory
1406+
helpers.RunTerragrunt(t, "terragrunt stack generate --working-dir "+rootPath)
1407+
1408+
// Verify that the stack directory was created and contains the expected unit
1409+
stackDir := util.JoinPath(rootPath, "stack", ".terragrunt-stack")
1410+
validateStackDir(t, stackDir)
1411+
1412+
// Verify that the foo unit was generated
1413+
fooUnitPath := util.JoinPath(stackDir, "foo")
1414+
assert.True(t, util.FileExists(fooUnitPath), "Expected foo unit to exist in .terragrunt-stack directory")
1415+
assert.True(t, util.FileExists(util.JoinPath(fooUnitPath, "terragrunt.hcl")), "Expected terragrunt.hcl to exist in foo unit")
1416+
1417+
// Test that stack generation works from the parent directory (this tests our fix)
1418+
parentPath := util.JoinPath(tmpEnvPath, testFixtureStackFindInParentFolders)
1419+
helpers.RunTerragrunt(t, "terragrunt stack generate --working-dir "+parentPath)
1420+
1421+
// Verify that the stack directory was created from the parent directory
1422+
parentStackDir := util.JoinPath(parentPath, "live", "stack", ".terragrunt-stack")
1423+
validateStackDir(t, parentStackDir)
1424+
1425+
// Verify that the foo unit was generated from the parent directory
1426+
parentFooUnitPath := util.JoinPath(parentStackDir, "foo")
1427+
assert.True(t, util.FileExists(parentFooUnitPath), "Expected foo unit to exist when generating from parent directory")
1428+
assert.True(t, util.FileExists(util.JoinPath(parentFooUnitPath, "terragrunt.hcl")), "Expected terragrunt.hcl to exist in foo unit when generating from parent directory")
1429+
1430+
stackPath := util.JoinPath(rootPath, "stack")
1431+
1432+
_, _, err := helpers.RunTerragruntCommandWithOutput(t, "terragrunt stack run apply --non-interactive --working-dir "+stackPath)
1433+
require.NoError(t, err, "Expected stack run apply to succeed")
1434+
1435+
outputStdout, _, err := helpers.RunTerragruntCommandWithOutput(t, "terragrunt stack output --raw foo.mock --non-interactive --working-dir "+stackPath)
1436+
require.NoError(t, err, "Expected stack output to succeed")
1437+
1438+
// Verify that the mock value from mock.hcl was correctly passed through
1439+
assert.Equal(t, "mock", strings.TrimSpace(outputStdout), "Expected raw output to contain just the mock value")
1440+
}

0 commit comments

Comments
 (0)