Skip to content

Commit

Permalink
add integration test for terraform apply and terraform clean
Browse files Browse the repository at this point in the history
  • Loading branch information
haitham911 committed Jan 4, 2025
1 parent 1acd1d0 commit 338d859
Showing 1 changed file with 97 additions and 0 deletions.
97 changes: 97 additions & 0 deletions tests/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,100 @@ func verifyFileContains(t *testing.T, filePatterns map[string][]string) bool {
}
return success
}
func TestCLITerraformClean(t *testing.T) {
// Capture the starting working directory
startingDir, err := os.Getwd()
if err != nil {
t.Fatalf("Failed to get the current working directory: %v", err)
}

// Initialize PathManager and update PATH
pathManager := NewPathManager()
pathManager.Prepend("../build", "..")
err = pathManager.Apply()
if err != nil {
t.Fatalf("Failed to apply updated PATH: %v", err)
}
fmt.Printf("Updated PATH: %s\n", pathManager.GetPath())
defer func() {
// Change back to the original working directory after the test
if err := os.Chdir(startingDir); err != nil {
t.Fatalf("Failed to change back to the starting directory: %v", err)
}
}()
workDir := "../examples/quick-start-simple"
err = os.Chdir(workDir)
if err != nil {
t.Fatalf("Failed to change directory to %q: %v", workDir, err)
}
binaryPath, err := exec.LookPath("atmos")
if err != nil {
t.Fatalf("Binary not found: %s. Current PATH: %s", "atmos", pathManager.GetPath())
}
cmd := exec.Command(binaryPath, "terraform", "apply", "station", "-s", "dev")
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
// ATMOS_COMPONENTS_TERRAFORM_APPLY_AUTO_APPROVE
envVars := os.Environ()
envVars = append(envVars, "ATMOS_COMPONENTS_TERRAFORM_APPLY_AUTO_APPROVE=true")
cmd.Env = envVars

// run terraform apply station -s dev and terraform apply station -s prod
err = cmd.Run()
if err != nil {
t.Log(stdout.String())
t.Fatalf("Failed to run terraform apply station -s dev: %v", stderr.String())
return
}
cmd = exec.Command(binaryPath, "terraform", "apply", "station", "-s", "dev")
err = cmd.Run()
if err != nil {
t.Log(stdout.String())
t.Fatalf("Failed to run terraform apply station -s prod: %v", stderr.String())
return
}
// get command error sta
// check if the state files and directories for the component and stack are exist
stateFiles := []string{
"./components/terraform/weather/.terraform",
"./components/terraform/weather/terraform.tfstate.d",
"./components/terraform/weather/.terraform.lock.hcl",
}
for _, file := range stateFiles {
fileAbs, err := filepath.Abs(file)
if err != nil {
t.Fatalf("Failed to resolve absolute path for %q: %v", file, err)
}
if _, err := os.Stat(fileAbs); errors.Is(err, os.ErrNotExist) {
t.Errorf("Reason: Expected file exist: %q", fileAbs)
return
}
}

// run atmos terraform clean
cmd = exec.Command(binaryPath, "terraform", "clean", "--force")
err = cmd.Run()
if err != nil {
t.Log(stdout.String())
t.Fatalf("Failed to run atmos terraform clean: %v", stderr.String())
}
// check if the state files and directories for the component and stack are deleted
for _, file := range stateFiles {
fileAbs, err := filepath.Abs(file)
if err != nil {
t.Fatalf("Failed to resolve absolute path for %q: %v", file, err)
}
_, err = os.Stat(fileAbs)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
continue
}
t.Errorf("Reason: error %q", fileAbs)
return

}
return
}

}

0 comments on commit 338d859

Please sign in to comment.