Skip to content

Commit a7f6e8e

Browse files
committed
test: improve terraform integration test reliability
1 parent 78ab753 commit a7f6e8e

File tree

3 files changed

+73
-10
lines changed

3 files changed

+73
-10
lines changed

tests/cli_terraform_test.go

+53-8
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ func TestCLITerraformClean(t *testing.T) {
2525
t.Fatalf("Failed to apply updated PATH: %v", err)
2626
}
2727
fmt.Printf("Updated PATH: %s\n", pathManager.GetPath())
28+
29+
// Setup cleanup function to run after test completion
2830
defer func() {
2931
// Change back to the original working directory after the test
3032
if err := os.Chdir(startingDir); err != nil {
@@ -44,29 +46,32 @@ func TestCLITerraformClean(t *testing.T) {
4446
t.Fatalf("Binary not found: %s. Current PATH: %s", "atmos", pathManager.GetPath())
4547
}
4648

49+
// Clean up any existing Terraform state before starting tests
50+
cleanupTerraformState(t)
51+
4752
// Initialize terraform for both environments
4853
runTerraformInit(t, binaryPath, "prod")
4954
runTerraformInit(t, binaryPath, "dev")
5055

5156
// Run terraform apply for prod environment
5257
runTerraformApply(t, binaryPath, "prod")
5358
verifyStateFilesExist(t, []string{
54-
"./components/terraform/mock/.terraform",
55-
"./components/terraform/mock/.terraform.lock.hcl",
59+
"terraform/components/mock/.terraform",
60+
"terraform/components/mock/.terraform.lock.hcl",
5661
})
5762
runCLITerraformCleanComponent(t, binaryPath, "prod")
5863
verifyStateFilesDeleted(t, []string{
59-
"./components/terraform/mock/.terraform",
60-
"./components/terraform/mock/.terraform.lock.hcl",
64+
"terraform/components/mock/.terraform",
65+
"terraform/components/mock/.terraform.lock.hcl",
6166
})
6267

6368
// Run terraform apply for dev environment
6469
runTerraformApply(t, binaryPath, "dev")
6570

6671
// Verify if state files exist before cleaning
6772
stateFiles := []string{
68-
"./components/terraform/mock/.terraform",
69-
"./components/terraform/mock/.terraform.lock.hcl",
73+
"terraform/components/mock/.terraform",
74+
"terraform/components/mock/.terraform.lock.hcl",
7075
}
7176
verifyStateFilesExist(t, stateFiles)
7277

@@ -167,8 +172,8 @@ func runTerraformInit(t *testing.T, binaryPath, environment string) {
167172
// Set environment variables
168173
envVars := os.Environ()
169174
envVars = append(envVars,
170-
"ATMOS_COMPONENTS_TERRAFORM_INIT_RUN_RECONFIGURE=true",
171175
"ATMOS_COMPONENTS_TERRAFORM_DEPLOY_RUN_INIT=true",
176+
"ATMOS_COMPONENTS_TERRAFORM_INIT_RUN_RECONFIGURE=true",
172177
"ATMOS_LOGS_LEVEL=Debug",
173178
"ATMOS_LOGS_FILE=/dev/stderr")
174179
cmd.Env = envVars
@@ -178,16 +183,26 @@ func runTerraformInit(t *testing.T, binaryPath, environment string) {
178183
cmd.Stderr = &stderr
179184

180185
// Log the command being executed
181-
t.Logf("Running command: %s %v", binaryPath, cmd.Args)
186+
t.Logf("Running terraform init for environment %s: %s %v", environment, binaryPath, cmd.Args)
182187

183188
err := cmd.Run()
184189
// Always log both stdout and stderr
185190
t.Logf("Init command stdout:\n%s", stdout.String())
186191
t.Logf("Init command stderr:\n%s", stderr.String())
192+
187193
if err != nil {
188194
t.Fatalf("Failed to run terraform init mock -s %s: %v\nStdout: %s\nStderr: %s",
189195
environment, err, stdout.String(), stderr.String())
190196
}
197+
198+
// Verify that terraform was properly initialized
199+
terraformDir := filepath.Join("terraform", "components", "mock")
200+
if _, err := os.Stat(filepath.Join(terraformDir, ".terraform")); os.IsNotExist(err) {
201+
t.Fatalf("Terraform was not properly initialized: .terraform directory not found in %s", terraformDir)
202+
}
203+
if _, err := os.Stat(filepath.Join(terraformDir, ".terraform.lock.hcl")); os.IsNotExist(err) {
204+
t.Fatalf("Terraform was not properly initialized: .terraform.lock.hcl not found in %s", terraformDir)
205+
}
191206
}
192207

193208
func runTerraformCleanCommand(t *testing.T, binaryPath string, args ...string) {
@@ -202,3 +217,33 @@ func runTerraformCleanCommand(t *testing.T, binaryPath string, args ...string) {
202217
t.Fatalf("Failed to run terraform clean: %v", stderr.String())
203218
}
204219
}
220+
221+
func cleanupTerraformState(t *testing.T) {
222+
t.Helper()
223+
224+
// Define paths to clean
225+
terraformDir := filepath.Join("terraform", "components", "mock")
226+
pathsToClean := []string{
227+
filepath.Join(terraformDir, ".terraform"),
228+
filepath.Join(terraformDir, ".terraform.lock.hcl"),
229+
filepath.Join(terraformDir, "terraform.tfstate"),
230+
filepath.Join(terraformDir, "terraform.tfstate.backup"),
231+
filepath.Join(terraformDir, ".terraform.tfstate.lock.info"),
232+
filepath.Join(terraformDir, "terraform.tfstate.d"),
233+
}
234+
235+
// Remove each path
236+
for _, path := range pathsToClean {
237+
err := os.RemoveAll(path)
238+
if err != nil && !os.IsNotExist(err) {
239+
t.Fatalf("Failed to clean up Terraform state at %s: %v", path, err)
240+
}
241+
}
242+
243+
// Ensure the terraform directory exists
244+
if err := os.MkdirAll(terraformDir, 0755); err != nil {
245+
t.Fatalf("Failed to create terraform directory at %s: %v", terraformDir, err)
246+
}
247+
248+
t.Logf("Successfully cleaned up Terraform state in %s", terraformDir)
249+
}

tests/fixtures/scenarios/mock-terraform/atmos.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
components:
22
terraform:
3-
base_path: "../../"
3+
base_path: "terraform/components"
44
apply:
55
auto_approve: true
66
clean:
@@ -19,4 +19,4 @@ stacks:
1919

2020
logs:
2121
file: "/dev/stderr"
22-
level: "Info"
22+
level: "Info"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
terraform {
2+
required_providers {
3+
local = {
4+
source = "hashicorp/local"
5+
version = ">= 2.0"
6+
}
7+
}
8+
}
9+
10+
variable "environment" {
11+
type = string
12+
}
13+
14+
# Mock resource for testing
15+
resource "local_file" "test" {
16+
content = "test-${var.environment}"
17+
filename = "${path.module}/test.txt"
18+
}

0 commit comments

Comments
 (0)