-
-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathterraform_clean_test.go
More file actions
108 lines (98 loc) · 3.66 KB
/
terraform_clean_test.go
File metadata and controls
108 lines (98 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package exec
import (
"os"
"testing"
"github.com/stretchr/testify/require"
cfg "github.com/cloudposse/atmos/pkg/config"
"github.com/cloudposse/atmos/pkg/schema"
tfclean "github.com/cloudposse/atmos/pkg/terraform/clean"
"github.com/cloudposse/atmos/tests"
)
// verifyFileExists checks that all files in the list exist.
// Returns true if all files exist, false otherwise with the first missing file path.
func verifyFileExists(t *testing.T, files []string) (bool, string) {
t.Helper()
for _, file := range files {
if _, err := os.Stat(file); err != nil {
return false, file
}
}
return true, ""
}
// verifyFileDeleted checks that all files in the list have been deleted.
// Returns true if all files are deleted, false otherwise with the first existing file path.
func verifyFileDeleted(t *testing.T, files []string) (bool, string) {
t.Helper()
for _, file := range files {
if _, err := os.Stat(file); err == nil {
return false, file
}
}
return true, ""
}
// TestCLITerraformClean is an integration test that verifies the clean command works
// correctly with ExecuteTerraform. This test must remain in internal/exec because it
// depends on ExecuteTerraform and other internal/exec functions.
func TestCLITerraformClean(t *testing.T) {
tests.RequireTerraform(t)
// Use t.Setenv for automatic cleanup and better test isolation.
t.Setenv("ATMOS_CLI_CONFIG_PATH", "")
t.Setenv("ATMOS_BASE_PATH", "")
// Define the work directory and change to it.
workDir := "../../tests/fixtures/scenarios/terraform-sub-components"
t.Chdir(workDir)
var infoApply schema.ConfigAndStacksInfo
infoApply.SubCommand = "apply"
infoApply.ComponentType = "terraform"
infoApply.Stack = "staging"
infoApply.Component = "component-1"
infoApply.ComponentFromArg = "component-1"
err := ExecuteTerraform(infoApply)
require.NoError(t, err)
infoApply.Component = "component-2"
infoApply.ComponentFromArg = "component-2"
err = ExecuteTerraform(infoApply)
require.NoError(t, err)
files := []string{
"../../components/terraform/mock-subcomponents/component-1/.terraform",
"../../components/terraform/mock-subcomponents/component-1/terraform.tfstate.d/staging-component-1/terraform.tfstate",
"../../components/terraform/mock-subcomponents/component-1/component-2/.terraform",
"../../components/terraform/mock-subcomponents/component-1/component-2/terraform.tfstate.d/staging-component-2/terraform.tfstate",
}
// Verify that expected files exist after apply.
exists, missingFile := verifyFileExists(t, files)
if !exists {
t.Fatalf("Expected file does not exist: %s", missingFile)
}
// Initialize atmosConfig for ExecuteClean.
atmosConfig, err := cfg.InitCliConfig(schema.ConfigAndStacksInfo{}, true)
require.NoError(t, err)
// Call clean service directly with typed parameters (no component, no stack, force=true, dryRun=false).
// This cleans ALL components since component="" and stack="".
opts := &tfclean.Options{
Component: "",
Stack: "",
Force: true,
Everything: false,
SkipLockFile: false,
DryRun: false,
}
// Create adapter and service.
adapter := tfclean.NewExecAdapter(
ProcessStacksForClean,
ExecuteDescribeStacksForClean,
GetGenerateFilenamesForComponent,
CollectComponentsDirectoryObjectsForClean,
ConstructTerraformComponentVarfileNameForClean,
ConstructTerraformComponentPlanfileNameForClean,
GetAllStacksComponentsPathsForClean,
)
service := tfclean.NewService(adapter)
err = service.Execute(opts, &atmosConfig)
require.NoError(t, err)
// Verify that files were deleted after clean.
deleted, existingFile := verifyFileDeleted(t, files)
if !deleted {
t.Fatalf("File should have been deleted but still exists: %s", existingFile)
}
}