-
-
Notifications
You must be signed in to change notification settings - Fork 108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix terraform clean
bugs
#870
Open
haitham911
wants to merge
24
commits into
main
Choose a base branch
from
DEV-2859
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+165
−27
Open
Changes from 20 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
6832ddd
fix clean cmd
haitham911 0c90df5
feat --everything is the default behavior
haitham911 a2b820e
remove debug print
haitham911 1649cd9
Merge branch 'main' into DEV-2859
osterman 4a9bde6
Update website/docs/cli/commands/terraform/usage.mdx
osterman 755e5c6
Update website/docs/cli/commands/terraform/usage.mdx
osterman 13126b2
Apply suggestions from code review
osterman 7e2f054
Update internal/exec/help.go
osterman f4a81b3
Merge branch 'main' into DEV-2859
osterman 8d1292b
Merge branch 'main' into DEV-2859
haitham911 1acd1d0
remove --everything
haitham911 338d859
add integration test for terraform apply and terraform clean
haitham911 6735525
chang dev with prod
haitham911 d9f4ff6
move TestCLITerraformClean to new file
haitham911 e983196
Merge branch 'main' into DEV-2859
haitham911 2ad7321
add clean help
haitham911 b87a851
improve TestCLITerraformClean
haitham911 e3b16d9
improve TestCLITerraformClean
haitham911 24f7385
improve clean test
haitham911 87f9bc4
add clean runCLITerraformCleanComponent
haitham911 d5cca32
modify docs
haitham911 73c6a80
improve test
haitham911 dfd8ada
remove clean help from help.go
haitham911 76d5cda
add line
haitham911 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package tests | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
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) | ||
} | ||
}() | ||
|
||
// Define the work directory and change to it | ||
workDir := "../examples/quick-start-simple" | ||
if err := os.Chdir(workDir); err != nil { | ||
t.Fatalf("Failed to change directory to %q: %v", workDir, err) | ||
} | ||
|
||
// Find the binary path for "atmos" | ||
binaryPath, err := exec.LookPath("atmos") | ||
if err != nil { | ||
t.Fatalf("Binary not found: %s. Current PATH: %s", "atmos", pathManager.GetPath()) | ||
} | ||
// Run terraform apply for prod environment | ||
runTerraformApply(t, binaryPath, "prod") | ||
verifyStateFilesExist(t, []string{"./components/terraform/weather/terraform.tfstate.d/prod-station"}) | ||
runCLITerraformCleanComponent(t, binaryPath, "prod") | ||
verifyStateFilesDeleted(t, []string{"./components/terraform/weather/terraform.tfstate.d/prod-station"}) | ||
|
||
// Run terraform apply for dev environment | ||
runTerraformApply(t, binaryPath, "dev") | ||
|
||
// Run terraform apply for prod environment | ||
runTerraformApply(t, binaryPath, "prod") | ||
haitham911 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Verify if state files exist before cleaning | ||
stateFiles := []string{ | ||
"./components/terraform/weather/.terraform", | ||
"./components/terraform/weather/terraform.tfstate.d", | ||
"./components/terraform/weather/.terraform.lock.hcl", | ||
} | ||
verifyStateFilesExist(t, stateFiles) | ||
|
||
// Run terraform clean | ||
runTerraformClean(t, binaryPath) | ||
|
||
// Verify if state files have been deleted after clean | ||
verifyStateFilesDeleted(t, stateFiles) | ||
|
||
} | ||
haitham911 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// runTerraformApply runs the terraform apply command for a given environment. | ||
func runTerraformApply(t *testing.T, binaryPath, environment string) { | ||
cmd := exec.Command(binaryPath, "terraform", "apply", "station", "-s", environment) | ||
envVars := os.Environ() | ||
envVars = append(envVars, "ATMOS_COMPONENTS_TERRAFORM_APPLY_AUTO_APPROVE=true") | ||
cmd.Env = envVars | ||
|
||
var stdout, stderr bytes.Buffer | ||
cmd.Stdout = &stdout | ||
cmd.Stderr = &stderr | ||
err := cmd.Run() | ||
t.Log(stdout.String()) | ||
if err != nil { | ||
t.Fatalf("Failed to run terraform apply station -s %s: %v", environment, stderr.String()) | ||
} | ||
} | ||
|
||
// verifyStateFilesExist checks if the state files exist before cleaning. | ||
func verifyStateFilesExist(t *testing.T, stateFiles []string) { | ||
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("Expected file to exist before cleaning: %q", fileAbs) | ||
} | ||
} | ||
} | ||
|
||
// runTerraformClean runs the terraform clean command. | ||
func runTerraformClean(t *testing.T, binaryPath string) { | ||
cmd := exec.Command(binaryPath, "terraform", "clean", "--force") | ||
var stdout, stderr bytes.Buffer | ||
cmd.Stdout = &stdout | ||
cmd.Stderr = &stderr | ||
err := cmd.Run() | ||
t.Logf("Clean command output:\n%s", stdout.String()) | ||
if err != nil { | ||
t.Fatalf("Failed to run terraform clean: %v", stderr.String()) | ||
} | ||
} | ||
osterman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// verifyStateFilesDeleted checks if the state files have been deleted after cleaning. | ||
func verifyStateFilesDeleted(t *testing.T, stateFiles []string) { | ||
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 { | ||
t.Errorf("Expected Terraform state file to be deleted: %q", fileAbs) | ||
} else if !errors.Is(err, os.ErrNotExist) { | ||
t.Errorf("Unexpected error checking file %q: %v", fileAbs, err) | ||
} | ||
} | ||
} | ||
|
||
func runCLITerraformCleanComponent(t *testing.T, binaryPath, environment string) { | ||
cmd := exec.Command(binaryPath, "terraform", "clean", "station", "-s", environment, "--force") | ||
var stdout, stderr bytes.Buffer | ||
cmd.Stdout = &stdout | ||
cmd.Stderr = &stderr | ||
err := cmd.Run() | ||
t.Logf("Clean command output:\n%s", stdout.String()) | ||
if err != nil { | ||
t.Fatalf("Failed to run terraform clean: %v", stderr.String()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
website/src/components/Screengrabs/atmos-terraform--help.html
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
4 changes: 2 additions & 2 deletions
4
website/src/components/Screengrabs/atmos-terraform-clean--help.html
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we're regressing here. This was heavily refactored in #914
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should go away from this file. Is there something we are missing that the code handle comes here?