Skip to content

Commit e743258

Browse files
authored
Merge pull request #1627 from gruntwork-io/update-terragrunt-test-version
Upgrade terragrunt module tests to v0.93.10
2 parents c45e788 + 3ca2be6 commit e743258

File tree

7 files changed

+130
-82
lines changed

7 files changed

+130
-82
lines changed

.circleci/config.yml

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ env: &env
88
TOFU_VERSION: 1.8.0
99
PACKER_VERSION: 1.10.0
1010
TERRAGRUNT_VERSION: v0.80.4
11+
TERRAGRUNT_TEST_VERSION: v0.93.10 # Version used for terragrunt module tests
1112
OPA_VERSION: v1.1.0
1213
GO_VERSION: 1.21.1
1314
GO111MODULE: auto
@@ -80,6 +81,15 @@ install_tofu: &install_tofu
8081
rm -rf tofu.zip
8182
tofu --version
8283
84+
install_terragrunt_latest: &install_terragrunt_latest
85+
name: Install Terragrunt (latest test version)
86+
command: |
87+
echo "Installing Terragrunt ${TERRAGRUNT_TEST_VERSION}..."
88+
curl -sL "https://github.com/gruntwork-io/terragrunt/releases/download/${TERRAGRUNT_TEST_VERSION}/terragrunt_linux_amd64" -o /tmp/terragrunt
89+
chmod +x /tmp/terragrunt
90+
sudo mv /tmp/terragrunt /usr/local/bin/terragrunt
91+
terragrunt --version
92+
8393
install_docker_buildx: &install_docker_buildx
8494
name: install docker buildx
8595
command: |
@@ -177,7 +187,7 @@ jobs:
177187
- project
178188

179189
# run tests with terraform binary
180-
test_terraform:
190+
terraform_test:
181191
<<: *defaults
182192
resource_class: xlarge
183193
steps:
@@ -218,7 +228,7 @@ jobs:
218228
path: /tmp/logs
219229

220230
# run tests with tofu binary
221-
test_tofu:
231+
terraform_test_tofu:
222232
<<: *defaults
223233
resource_class: large
224234
steps:
@@ -243,10 +253,13 @@ jobs:
243253
# to kill the build after more than 10 minutes without log output.
244254
# NOTE: because this doesn't build with the kubernetes tag, it will not run the kubernetes tests. See
245255
# kubernetes_test build steps.
256+
# NOTE: terragrunt tests are excluded here and run in a separate terragrunt_test job.
246257
- run: mkdir -p /tmp/logs
247258
# check we can compile the azure code, but don't actually run the tests
248259
- run: run-go-tests --packages "-p 1 -tags=azure -run IDontExist ./modules/azure"
249-
- run: run-go-tests --packages "-p 1 ./..." | tee /tmp/logs/test_output.log
260+
- run: |
261+
# Run all tests except terragrunt module (which has its own dedicated job)
262+
run-go-tests --packages "-p 1 $(go list ./... | grep -v './modules/terragrunt' | tr '\n' ' ')" | tee /tmp/logs/test_output.log
250263
251264
- run:
252265
command: |
@@ -381,7 +394,7 @@ jobs:
381394
- store_test_results:
382395
path: /tmp/logs
383396

384-
# Dedicated terragrunt tests that require terragrunt binary to be available
397+
# Dedicated terragrunt tests with terraform as the underlying IaC binary
385398
terragrunt_test:
386399
<<: *defaults
387400
resource_class: large
@@ -391,6 +404,8 @@ jobs:
391404

392405
- run:
393406
<<: *install_gruntwork_utils
407+
- run:
408+
<<: *install_terragrunt_latest
394409
- run:
395410
<<: *install_docker_buildx
396411

@@ -417,6 +432,50 @@ jobs:
417432
- store_test_results:
418433
path: /tmp/logs
419434

435+
# Dedicated terragrunt tests with tofu as the underlying IaC binary
436+
terragrunt_test_tofu:
437+
<<: *defaults
438+
resource_class: large
439+
steps:
440+
- attach_workspace:
441+
at: /home/circleci
442+
443+
- run:
444+
<<: *install_gruntwork_utils
445+
- run:
446+
<<: *install_tofu
447+
- run:
448+
<<: *install_terragrunt_latest
449+
- run:
450+
<<: *install_docker_buildx
451+
452+
# The weird way you have to set PATH in Circle 2.0
453+
- run: |
454+
echo 'export PATH=$HOME/.local/bin:$HOME/terraform:$HOME/packer:$PATH' >> $BASH_ENV
455+
# Remove terraform binary so tofu will be used by terragrunt
456+
sudo rm -f $(which terraform)
457+
# Verify tofu is available
458+
which tofu
459+
tofu --version
460+
461+
# Run the terragrunt-specific tests with tofu as the backend
462+
- run:
463+
command: |
464+
mkdir -p /tmp/logs
465+
# Run only the terragrunt module tests
466+
run-go-tests --packages "-p 1 ./modules/terragrunt" | tee /tmp/logs/test_output.log
467+
468+
- run:
469+
command: |
470+
./cmd/bin/terratest_log_parser_linux_amd64 --testlog /tmp/logs/test_output.log --outputdir /tmp/logs
471+
when: always
472+
473+
# Store test result and log artifacts for browsing purposes
474+
- store_artifacts:
475+
path: /tmp/logs
476+
- store_test_results:
477+
path: /tmp/logs
478+
420479
deploy:
421480
<<: *defaults
422481
steps:
@@ -473,7 +532,17 @@ workflows:
473532
tags:
474533
only: /^v.*/
475534

476-
- test_terraform:
535+
- terragrunt_test_tofu:
536+
context:
537+
- AWS__PHXDEVOPS__circle-ci-test
538+
- GITHUB__PAT__gruntwork-ci
539+
requires:
540+
- setup
541+
filters:
542+
tags:
543+
only: /^v.*/
544+
545+
- terraform_test:
477546
context:
478547
- AWS__PHXDEVOPS__circle-ci-test
479548
- GITHUB__PAT__gruntwork-ci
@@ -486,7 +555,7 @@ workflows:
486555
tags:
487556
only: /^v.*/
488557

489-
- test_tofu:
558+
- terraform_test_tofu:
490559
context:
491560
- AWS__PHXDEVOPS__circle-ci-test
492561
- GITHUB__PAT__gruntwork-ci

modules/terragrunt/cmd_args_test.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package terragrunt
22

33
import (
4+
"os/exec"
45
"path/filepath"
56
"testing"
67

@@ -249,11 +250,21 @@ func TestEnvVarsPropagation(t *testing.T) {
249250
"../../test/fixtures/terragrunt/terragrunt-stack-init", t.Name())
250251
require.NoError(t, err)
251252

253+
// Detect which IaC binary is available (terraform or tofu)
254+
tfBinary := "terraform"
255+
if _, err := exec.LookPath("terraform"); err != nil {
256+
// terraform not found, try tofu
257+
if _, err := exec.LookPath("tofu"); err != nil {
258+
t.Skip("Neither terraform nor tofu found in PATH")
259+
}
260+
tfBinary = "tofu"
261+
}
262+
252263
options := &Options{
253264
TerragruntDir: filepath.Join(testFolder, "live"),
254265
EnvVars: map[string]string{
255-
"TERRAGRUNT_TFPATH": "terraform", // Explicitly set terraform binary
256-
"TG_LOG_LEVEL": "error", // Alternative to --log-level flag
266+
"TERRAGRUNT_TFPATH": tfBinary, // Use whichever binary is available
267+
"TG_LOG_LEVEL": "error", // Alternative to --log-level flag
257268
},
258269
}
259270

modules/terragrunt/format.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ import (
55
"github.com/stretchr/testify/require"
66
)
77

8-
// FormatAll runs terragrunt hclfmt to format all terragrunt.hcl files and returns stdout/stderr
8+
// FormatAll runs terragrunt hcl format to format all terragrunt.hcl files and returns stdout/stderr
99
func FormatAll(t testing.TestingT, options *Options) string {
1010
out, err := FormatAllE(t, options)
1111
require.NoError(t, err)
1212
return out
1313
}
1414

15-
// FormatAllE runs terragrunt hclfmt to format all terragrunt.hcl files and returns stdout/stderr
15+
// FormatAllE runs terragrunt hcl format to format all terragrunt.hcl files and returns stdout/stderr
1616
func FormatAllE(t testing.TestingT, options *Options) (string, error) {
17-
return runTerragruntCommandE(t, options, "hclfmt")
17+
return runTerragruntCommandE(t, options, "hcl", "format")
1818
}

modules/terragrunt/format_test.go

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,20 @@ foo="bar"
3131
TerragruntBinary: "terragrunt",
3232
}
3333

34-
out := FormatAll(t, options)
35-
require.NotEmpty(t, out)
34+
// Run format command
35+
FormatAll(t, options)
36+
37+
// Read the formatted file to verify it was actually formatted
38+
formattedContent, err := os.ReadFile(tgFile)
39+
require.NoError(t, err)
40+
41+
// Verify the file was formatted (should have proper spacing now)
42+
require.Contains(t, string(formattedContent), `source = "git::[email protected]:foo/modules.git//app"`,
43+
"Expected file to be formatted with proper spacing")
44+
require.Contains(t, string(formattedContent), `inputs = {`,
45+
"Expected inputs block to be formatted with spaces around =")
46+
require.Contains(t, string(formattedContent), `foo = "bar"`,
47+
"Expected key-value pairs to be formatted with spaces around =")
3648
}
3749

3850
func TestFormatAllE(t *testing.T) {
@@ -41,12 +53,29 @@ func TestFormatAllE(t *testing.T) {
4153
testFolder, err := files.CopyTerragruntFolderToTemp("../../test/fixtures/terragrunt/terragrunt-multi-plan", t.Name())
4254
require.NoError(t, err)
4355

56+
// Create an unformatted file to ensure the command actually does something
57+
unformattedContent := `terraform {
58+
source = "git::[email protected]:foo/modules.git//app"
59+
}
60+
inputs={
61+
foo="bar"
62+
}`
63+
tgFile := filepath.Join(testFolder, "foo", "terragrunt.hcl")
64+
err = os.WriteFile(tgFile, []byte(unformattedContent), 0644)
65+
require.NoError(t, err)
66+
4467
options := &Options{
4568
TerragruntDir: testFolder,
4669
TerragruntBinary: "terragrunt",
4770
}
4871

49-
out, err := FormatAllE(t, options)
72+
// Run format command - should succeed
73+
_, err = FormatAllE(t, options)
74+
require.NoError(t, err)
75+
76+
// Verify the file was actually formatted by reading it
77+
formattedContent, err := os.ReadFile(tgFile)
5078
require.NoError(t, err)
51-
require.NotEmpty(t, out)
79+
require.Contains(t, string(formattedContent), `inputs = {`,
80+
"File should be formatted with proper spacing")
5281
}

modules/terragrunt/init_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ func TestInit(t *testing.T) {
2020
TerragruntBinary: "terragrunt",
2121
TerraformArgs: []string{"-upgrade=true"},
2222
})
23-
require.Contains(t, out, "Terraform has been successfully initialized!")
23+
// Check for common success indicator (works with both Terraform and OpenTofu)
24+
require.Contains(t, out, "successfully initialized")
2425
}
2526

2627
func TestInitE(t *testing.T) {
@@ -36,7 +37,8 @@ func TestInitE(t *testing.T) {
3637
TerraformArgs: []string{"-upgrade=true"}, // Common terraform init flag
3738
})
3839
require.NoError(t, err)
39-
require.Contains(t, out, "Terraform has been successfully initialized!")
40+
// Check for common success indicator (works with both Terraform and OpenTofu)
41+
require.Contains(t, out, "successfully initialized")
4042
}
4143

4244
func TestInitWithInvalidConfig(t *testing.T) {

modules/terragrunt/stack_generate_test.go

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -69,70 +69,6 @@ func TestStackGenerateE(t *testing.T) {
6969
}
7070
}
7171

72-
func TestStackGenerateWithNoColor(t *testing.T) {
73-
t.Parallel()
74-
75-
testFolder, err := files.CopyTerraformFolderToTemp(
76-
"../../test/fixtures/terragrunt/terragrunt-stack-init", t.Name())
77-
require.NoError(t, err)
78-
79-
// First initialize the stack
80-
_, err = InitE(t, &Options{
81-
TerragruntDir: path.Join(testFolder, "live"),
82-
TerragruntBinary: "terragrunt",
83-
TerraformArgs: []string{"-upgrade=true"},
84-
})
85-
require.NoError(t, err)
86-
87-
// Generate with no-color option
88-
out, err := StackGenerateE(t, &Options{
89-
TerragruntDir: path.Join(testFolder, "live"),
90-
TerragruntBinary: "terragrunt",
91-
TerragruntArgs: []string{"--no-color"},
92-
})
93-
require.NoError(t, err)
94-
95-
// Validate that generate command produced output
96-
// Terragrunt v0.80.4+ outputs "Processing unit", older versions output "Generating unit"
97-
require.True(t, containsEitherString(out, "Processing unit", "Generating unit"), "Output should contain either 'Processing unit' or 'Generating unit'")
98-
99-
// Verify that the .terragrunt-stack directory was created
100-
stackDir := path.Join(testFolder, "live", ".terragrunt-stack")
101-
require.DirExists(t, stackDir)
102-
}
103-
104-
func TestStackGenerateWithExtraArgs(t *testing.T) {
105-
t.Parallel()
106-
107-
testFolder, err := files.CopyTerraformFolderToTemp(
108-
"../../test/fixtures/terragrunt/terragrunt-stack-init", t.Name())
109-
require.NoError(t, err)
110-
111-
// First initialize the stack
112-
_, err = InitE(t, &Options{
113-
TerragruntDir: path.Join(testFolder, "live"),
114-
TerragruntBinary: "terragrunt",
115-
TerraformArgs: []string{"-upgrade=true"},
116-
})
117-
require.NoError(t, err)
118-
119-
// Generate with extra args
120-
out, err := StackGenerateE(t, &Options{
121-
TerragruntDir: path.Join(testFolder, "live"),
122-
TerragruntBinary: "terragrunt",
123-
TerragruntArgs: []string{"--log-level", "info"},
124-
})
125-
require.NoError(t, err)
126-
127-
// Validate that generate command produced output
128-
// Terragrunt v0.80.4+ outputs "Processing unit", older versions output "Generating unit"
129-
require.True(t, containsEitherString(out, "Processing unit", "Generating unit"), "Output should contain either 'Processing unit' or 'Generating unit'")
130-
131-
// Verify that the .terragrunt-stack directory was created
132-
stackDir := path.Join(testFolder, "live", ".terragrunt-stack")
133-
require.DirExists(t, stackDir)
134-
}
135-
13672
func TestStackGenerateNonExistentDir(t *testing.T) {
13773
t.Parallel()
13874

modules/terragrunt/terragrunt_e2e_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ func TestStackEndToEndIntegration(t *testing.T) {
8888
runOptions.TerraformArgs = []string{"plan"}
8989
planOutput, err := StackRunE(t, &runOptions)
9090
require.NoError(t, err)
91-
require.Contains(t, planOutput, "Terraform will perform", "Stack run should execute plan")
91+
// Check for common plan indicator (works with both Terraform and OpenTofu)
92+
require.Contains(t, planOutput, "will perform")
9293

9394
// Step 4: Clean stack
9495
t.Log("Step 4: Cleaning stack")

0 commit comments

Comments
 (0)