@@ -25,6 +25,8 @@ func TestCLITerraformClean(t *testing.T) {
25
25
t .Fatalf ("Failed to apply updated PATH: %v" , err )
26
26
}
27
27
fmt .Printf ("Updated PATH: %s\n " , pathManager .GetPath ())
28
+
29
+ // Setup cleanup function to run after test completion
28
30
defer func () {
29
31
// Change back to the original working directory after the test
30
32
if err := os .Chdir (startingDir ); err != nil {
@@ -44,29 +46,32 @@ func TestCLITerraformClean(t *testing.T) {
44
46
t .Fatalf ("Binary not found: %s. Current PATH: %s" , "atmos" , pathManager .GetPath ())
45
47
}
46
48
49
+ // Clean up any existing Terraform state before starting tests
50
+ cleanupTerraformState (t )
51
+
47
52
// Initialize terraform for both environments
48
53
runTerraformInit (t , binaryPath , "prod" )
49
54
runTerraformInit (t , binaryPath , "dev" )
50
55
51
56
// Run terraform apply for prod environment
52
57
runTerraformApply (t , binaryPath , "prod" )
53
58
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" ,
56
61
})
57
62
runCLITerraformCleanComponent (t , binaryPath , "prod" )
58
63
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" ,
61
66
})
62
67
63
68
// Run terraform apply for dev environment
64
69
runTerraformApply (t , binaryPath , "dev" )
65
70
66
71
// Verify if state files exist before cleaning
67
72
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" ,
70
75
}
71
76
verifyStateFilesExist (t , stateFiles )
72
77
@@ -167,8 +172,8 @@ func runTerraformInit(t *testing.T, binaryPath, environment string) {
167
172
// Set environment variables
168
173
envVars := os .Environ ()
169
174
envVars = append (envVars ,
170
- "ATMOS_COMPONENTS_TERRAFORM_INIT_RUN_RECONFIGURE=true" ,
171
175
"ATMOS_COMPONENTS_TERRAFORM_DEPLOY_RUN_INIT=true" ,
176
+ "ATMOS_COMPONENTS_TERRAFORM_INIT_RUN_RECONFIGURE=true" ,
172
177
"ATMOS_LOGS_LEVEL=Debug" ,
173
178
"ATMOS_LOGS_FILE=/dev/stderr" )
174
179
cmd .Env = envVars
@@ -178,16 +183,26 @@ func runTerraformInit(t *testing.T, binaryPath, environment string) {
178
183
cmd .Stderr = & stderr
179
184
180
185
// 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 )
182
187
183
188
err := cmd .Run ()
184
189
// Always log both stdout and stderr
185
190
t .Logf ("Init command stdout:\n %s" , stdout .String ())
186
191
t .Logf ("Init command stderr:\n %s" , stderr .String ())
192
+
187
193
if err != nil {
188
194
t .Fatalf ("Failed to run terraform init mock -s %s: %v\n Stdout: %s\n Stderr: %s" ,
189
195
environment , err , stdout .String (), stderr .String ())
190
196
}
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
+ }
191
206
}
192
207
193
208
func runTerraformCleanCommand (t * testing.T , binaryPath string , args ... string ) {
@@ -202,3 +217,33 @@ func runTerraformCleanCommand(t *testing.T, binaryPath string, args ...string) {
202
217
t .Fatalf ("Failed to run terraform clean: %v" , stderr .String ())
203
218
}
204
219
}
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
+ }
0 commit comments