|
| 1 | +package tests |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "path/filepath" |
| 10 | + "testing" |
| 11 | +) |
| 12 | + |
| 13 | +func TestCLITerraformClean(t *testing.T) { |
| 14 | + // Capture the starting working directory |
| 15 | + startingDir, err := os.Getwd() |
| 16 | + if err != nil { |
| 17 | + t.Fatalf("Failed to get the current working directory: %v", err) |
| 18 | + } |
| 19 | + |
| 20 | + // Initialize PathManager and update PATH |
| 21 | + pathManager := NewPathManager() |
| 22 | + pathManager.Prepend("../build", "..") |
| 23 | + err = pathManager.Apply() |
| 24 | + if err != nil { |
| 25 | + t.Fatalf("Failed to apply updated PATH: %v", err) |
| 26 | + } |
| 27 | + fmt.Printf("Updated PATH: %s\n", pathManager.GetPath()) |
| 28 | + defer func() { |
| 29 | + // Change back to the original working directory after the test |
| 30 | + if err := os.Chdir(startingDir); err != nil { |
| 31 | + t.Fatalf("Failed to change back to the starting directory: %v", err) |
| 32 | + } |
| 33 | + }() |
| 34 | + |
| 35 | + // Define the work directory and change to it |
| 36 | + workDir := "../examples/quick-start-simple" |
| 37 | + if err := os.Chdir(workDir); err != nil { |
| 38 | + t.Fatalf("Failed to change directory to %q: %v", workDir, err) |
| 39 | + } |
| 40 | + |
| 41 | + // Find the binary path for "atmos" |
| 42 | + binaryPath, err := exec.LookPath("atmos") |
| 43 | + if err != nil { |
| 44 | + t.Fatalf("Binary not found: %s. Current PATH: %s", "atmos", pathManager.GetPath()) |
| 45 | + } |
| 46 | + |
| 47 | + // Force clean everything |
| 48 | + runTerraformCleanCommand(t, binaryPath, "--force") |
| 49 | + // Clean everything |
| 50 | + runTerraformCleanCommand(t, binaryPath) |
| 51 | + // Clean specific component |
| 52 | + runTerraformCleanCommand(t, binaryPath, "station") |
| 53 | + // Clean component with stack |
| 54 | + runTerraformCleanCommand(t, binaryPath, "station", "-s", "dev") |
| 55 | + |
| 56 | + // Run terraform apply for prod environment |
| 57 | + runTerraformApply(t, binaryPath, "prod") |
| 58 | + verifyStateFilesExist(t, []string{"./components/terraform/weather/terraform.tfstate.d/prod-station"}) |
| 59 | + runCLITerraformCleanComponent(t, binaryPath, "prod") |
| 60 | + verifyStateFilesDeleted(t, []string{"./components/terraform/weather/terraform.tfstate.d/prod-station"}) |
| 61 | + |
| 62 | + // Run terraform apply for dev environment |
| 63 | + runTerraformApply(t, binaryPath, "dev") |
| 64 | + |
| 65 | + // Verify if state files exist before cleaning |
| 66 | + stateFiles := []string{ |
| 67 | + "./components/terraform/weather/.terraform", |
| 68 | + "./components/terraform/weather/terraform.tfstate.d", |
| 69 | + "./components/terraform/weather/.terraform.lock.hcl", |
| 70 | + } |
| 71 | + verifyStateFilesExist(t, stateFiles) |
| 72 | + |
| 73 | + // Run terraform clean |
| 74 | + runTerraformClean(t, binaryPath) |
| 75 | + |
| 76 | + // Verify if state files have been deleted after clean |
| 77 | + verifyStateFilesDeleted(t, stateFiles) |
| 78 | + |
| 79 | +} |
| 80 | + |
| 81 | +// runTerraformApply runs the terraform apply command for a given environment. |
| 82 | +func runTerraformApply(t *testing.T, binaryPath, environment string) { |
| 83 | + cmd := exec.Command(binaryPath, "terraform", "apply", "station", "-s", environment) |
| 84 | + envVars := os.Environ() |
| 85 | + envVars = append(envVars, "ATMOS_COMPONENTS_TERRAFORM_APPLY_AUTO_APPROVE=true") |
| 86 | + cmd.Env = envVars |
| 87 | + |
| 88 | + var stdout, stderr bytes.Buffer |
| 89 | + cmd.Stdout = &stdout |
| 90 | + cmd.Stderr = &stderr |
| 91 | + err := cmd.Run() |
| 92 | + t.Log(stdout.String()) |
| 93 | + if err != nil { |
| 94 | + t.Fatalf("Failed to run terraform apply station -s %s: %v", environment, stderr.String()) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +// verifyStateFilesExist checks if the state files exist before cleaning. |
| 99 | +func verifyStateFilesExist(t *testing.T, stateFiles []string) { |
| 100 | + for _, file := range stateFiles { |
| 101 | + fileAbs, err := filepath.Abs(file) |
| 102 | + if err != nil { |
| 103 | + t.Fatalf("Failed to resolve absolute path for %q: %v", file, err) |
| 104 | + } |
| 105 | + if _, err := os.Stat(fileAbs); errors.Is(err, os.ErrNotExist) { |
| 106 | + t.Errorf("Expected file to exist before cleaning: %q", fileAbs) |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// runTerraformClean runs the terraform clean command. |
| 112 | +func runTerraformClean(t *testing.T, binaryPath string) { |
| 113 | + cmd := exec.Command(binaryPath, "terraform", "clean", "--force") |
| 114 | + var stdout, stderr bytes.Buffer |
| 115 | + cmd.Stdout = &stdout |
| 116 | + cmd.Stderr = &stderr |
| 117 | + err := cmd.Run() |
| 118 | + t.Logf("Clean command output:\n%s", stdout.String()) |
| 119 | + if err != nil { |
| 120 | + t.Fatalf("Failed to run terraform clean: %v", stderr.String()) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +// verifyStateFilesDeleted checks if the state files have been deleted after cleaning. |
| 125 | +func verifyStateFilesDeleted(t *testing.T, stateFiles []string) { |
| 126 | + for _, file := range stateFiles { |
| 127 | + fileAbs, err := filepath.Abs(file) |
| 128 | + if err != nil { |
| 129 | + t.Fatalf("Failed to resolve absolute path for %q: %v", file, err) |
| 130 | + } |
| 131 | + _, err = os.Stat(fileAbs) |
| 132 | + if err == nil { |
| 133 | + t.Errorf("Expected Terraform state file to be deleted: %q", fileAbs) |
| 134 | + } else if !errors.Is(err, os.ErrNotExist) { |
| 135 | + t.Errorf("Unexpected error checking file %q: %v", fileAbs, err) |
| 136 | + } |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +func runCLITerraformCleanComponent(t *testing.T, binaryPath, environment string) { |
| 141 | + cmd := exec.Command(binaryPath, "terraform", "clean", "station", "-s", environment, "--force") |
| 142 | + var stdout, stderr bytes.Buffer |
| 143 | + cmd.Stdout = &stdout |
| 144 | + cmd.Stderr = &stderr |
| 145 | + err := cmd.Run() |
| 146 | + t.Logf("Clean command output:\n%s", stdout.String()) |
| 147 | + if err != nil { |
| 148 | + t.Fatalf("Failed to run terraform clean: %v", stderr.String()) |
| 149 | + } |
| 150 | +} |
| 151 | +func runCLITerraformClean(t *testing.T, binaryPath string) { |
| 152 | + cmd := exec.Command(binaryPath, "terraform", "clean") |
| 153 | + var stdout, stderr bytes.Buffer |
| 154 | + cmd.Stdout = &stdout |
| 155 | + cmd.Stderr = &stderr |
| 156 | + err := cmd.Run() |
| 157 | + t.Logf("Clean command output:\n%s", stdout.String()) |
| 158 | + if err != nil { |
| 159 | + t.Fatalf("Failed to run terraform clean: %v", stderr.String()) |
| 160 | + } |
| 161 | + |
| 162 | +} |
| 163 | + |
| 164 | +func runTerraformCleanCommand(t *testing.T, binaryPath string, args ...string) { |
| 165 | + cmdArgs := append([]string{"terraform", "clean"}, args...) |
| 166 | + cmd := exec.Command(binaryPath, cmdArgs...) |
| 167 | + var stdout, stderr bytes.Buffer |
| 168 | + cmd.Stdout = &stdout |
| 169 | + cmd.Stderr = &stderr |
| 170 | + err := cmd.Run() |
| 171 | + t.Logf("Clean command output:\n%s", stdout.String()) |
| 172 | + if err != nil { |
| 173 | + t.Fatalf("Failed to run terraform clean: %v", stderr.String()) |
| 174 | + } |
| 175 | +} |
0 commit comments