Skip to content

Commit

Permalink
Merge branch 'main' into DEV-2964
Browse files Browse the repository at this point in the history
  • Loading branch information
Listener430 authored Jan 31, 2025
2 parents ed1af3d + 4a129d0 commit f920318
Show file tree
Hide file tree
Showing 14 changed files with 302 additions and 55 deletions.
46 changes: 46 additions & 0 deletions .github/workflows/autofix.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
# The workflow name **must** be "autofix.ci" for Autofix CI to function correctly.
# Any deviation from this name will cause Autofix CI to fail, as it relies on this
# specific identifier for execution. This is a strict requirement of Autofix CI.
name: autofix.ci
on: pull_request
permissions: {}
jobs:
autofix:
runs-on: ubuntu-24.04
permissions: {}
timeout-minutes: 15
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
persist-credentials: false
- uses: aquaproj/aqua-installer@e2d0136abcf70b7a2f6f505720640750557c4b33 # v3.1.1
with:
aqua_version: v2.43.0

- name: Update aqua-checksums.json
run: aqua upc -prune

# go mod tidy
- uses: actions/setup-go@f111f3307d8850f501ac008e886eec1fd1932a34 # v5.3.0
with:
go-version-file: go.mod
- run: go mod tidy

# gofumpt
- name: Get changed Go files
id: changed-files
uses: tj-actions/changed-files@d6e91a2266cdb9d62096cebf1e8546899c6aa18f # v45.0.6
with:
use_rest_api: "true"
files: |
**/*.go
- if: steps.changed-files.outputs.all_changed_files_count != '0'
env:
ALL_CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }}
run: |
# shellcheck disable=SC2086
gofumpt -l -w $ALL_CHANGED_FILES
- uses: autofix-ci/action@2891949f3779a1cafafae1523058501de3d4e944 # v1.3.1
19 changes: 0 additions & 19 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,6 @@ jobs:
path: |
./build/
tidy:
name: Tidy Go Modules
runs-on: ubuntu-latest
steps:
- name: Check out code into the Go module directory
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: "go.mod"
id: go

- uses: j0hnsmith/go-mod-check@v1
with:
working-directory: ${{ github.workspace }}
# optional, only if you're happy to have `replace ` lines in your go.mod file
skip-replace-check: true

# run acceptance tests
test:
name: Acceptance Tests
Expand Down
11 changes: 4 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@ SHELL := /bin/bash
#GOARCH=amd64
VERSION=test

# List of targets the `readme` target should call before generating the readme
export README_DEPS ?= docs/targets.md
export CGO_ENABLED=0

-include $(shell curl -sSL -o .build-harness "https://cloudposse.tools/build-harness"; echo .build-harness)

## Lint terraform code
lint:
$(SELF) terraform/install terraform/get-modules terraform/get-plugins terraform/lint terraform/validate
readme:
@echo "README.md generation temporarily disabled."
@exit 0

get:
go get
Expand Down
34 changes: 34 additions & 0 deletions aqua/aqua-checksums.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"checksums": [
{
"id": "github_release/github.com/mvdan/gofumpt/v0.7.0/gofumpt_v0.7.0_darwin_amd64",
"checksum": "B7D05E092DA45C5EC96344AB635B1D6547C3E27C840BA39BC76989934EFD7CE3",
"algorithm": "sha256"
},
{
"id": "github_release/github.com/mvdan/gofumpt/v0.7.0/gofumpt_v0.7.0_darwin_arm64",
"checksum": "08F23114760A090B090706D92B8C52B9875B9EB352D76C77AA354D6AA20B045A",
"algorithm": "sha256"
},
{
"id": "github_release/github.com/mvdan/gofumpt/v0.7.0/gofumpt_v0.7.0_linux_amd64",
"checksum": "6FF459C1DCAE3B0B00844C1A5A4A5B0F547237D8A4F3624AAEA8D424AEEF24C6",
"algorithm": "sha256"
},
{
"id": "github_release/github.com/mvdan/gofumpt/v0.7.0/gofumpt_v0.7.0_linux_arm64",
"checksum": "00C18C88EF50437629626BA20D677F4648684CB280952814CDD887677D42CBD3",
"algorithm": "sha256"
},
{
"id": "github_release/github.com/mvdan/gofumpt/v0.7.0/gofumpt_v0.7.0_windows_amd64.exe",
"checksum": "65F5B9EA7723AA936FA6880E184624747E9E6481802B62D4CB5B774EF2350CEC",
"algorithm": "sha256"
},
{
"id": "registries/github_content/github.com/aquaproj/aqua-registry/v4.302.1/registry.yaml",
"checksum": "908B44E94A3583AA2D6F0B17F53E49D01ACD57E4ECDFEDB29EB082D335D9445478426E591AB57D37BAA02A2EF9581A71A3AA66C825CC787323D65A1D89B53072",
"algorithm": "sha512"
}
]
}
12 changes: 12 additions & 0 deletions aqua/aqua.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
# yaml-language-server: $schema=https://raw.githubusercontent.com/aquaproj/aqua/main/json-schema/aqua-yaml.json
# aqua - Declarative CLI Version Manager
# https://aquaproj.github.io/
checksum:
enabled: true
require_checksum: true
registries:
- type: standard
ref: v4.302.1 # renovate: depName=aquaproj/aqua-registry
packages:
- import: imports/*.yaml
2 changes: 2 additions & 0 deletions aqua/imports/gofumpt.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
packages:
- name: mvdan/[email protected]
8 changes: 2 additions & 6 deletions internal/exec/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const (
outFlag = "-out"
varFileFlag = "-var-file"
skipTerraformLockFileFlag = "--skip-lock-file"
everythingFlag = "--everything"
forceFlag = "--force"
)

Expand All @@ -37,13 +36,10 @@ func shouldProcessStacks(info *schema.ConfigAndStacksInfo) (bool, bool) {
shouldProcessStacks := true
shouldCheckStack := true

if info.SubCommand == "clean" &&
(u.SliceContainsString(info.AdditionalArgsAndFlags, everythingFlag) ||
u.SliceContainsString(info.AdditionalArgsAndFlags, forceFlag)) {
if info.SubCommand == "clean" {
if info.ComponentFromArg == "" {
shouldProcessStacks = false
}

shouldCheckStack = info.Stack != ""

}
Expand Down Expand Up @@ -128,7 +124,7 @@ func ExecuteTerraform(info schema.ConfigAndStacksInfo) error {
}
}

if !info.ComponentIsEnabled {
if !info.ComponentIsEnabled && info.SubCommand != "clean" {
u.LogInfo(atmosConfig, fmt.Sprintf("component '%s' is not enabled and skipped", info.ComponentFromArg))
return nil
}
Expand Down
9 changes: 4 additions & 5 deletions internal/exec/terraform_clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,8 @@ func handleTFDataDir(componentPath string, relativePath string, atmosConfig sche
}

}
func initializeFilesToClear(info schema.ConfigAndStacksInfo, atmosConfig schema.AtmosConfiguration, everything bool) []string {
if everything && info.Stack == "" {
func initializeFilesToClear(info schema.ConfigAndStacksInfo, atmosConfig schema.AtmosConfiguration) []string {
if info.ComponentFromArg == "" {
return []string{".terraform", ".terraform.lock.hcl", "*.tfvar.json", "terraform.tfstate.d"}
}
varFile := constructTerraformComponentVarfileName(info)
Expand Down Expand Up @@ -407,8 +407,7 @@ func handleCleanSubCommand(info schema.ConfigAndStacksInfo, componentPath string
}

force := u.SliceContainsString(info.AdditionalArgsAndFlags, forceFlag)
everything := u.SliceContainsString(info.AdditionalArgsAndFlags, everythingFlag)
filesToClear := initializeFilesToClear(info, atmosConfig, everything)
filesToClear := initializeFilesToClear(info, atmosConfig)
folders, err := CollectDirectoryObjects(cleanPath, filesToClear)
if err != nil {
u.LogTrace(atmosConfig, fmt.Errorf("error collecting folders and files: %v", err).Error())
Expand Down Expand Up @@ -456,7 +455,7 @@ func handleCleanSubCommand(info schema.ConfigAndStacksInfo, componentPath string
u.PrintMessage(fmt.Sprintf("Do you want to delete the folder '%s'? ", tfDataDir))
}
var message string
if everything && info.ComponentFromArg == "" {
if info.ComponentFromArg == "" {
message = fmt.Sprintf("This will delete %v local terraform state files affecting all components", total)
} else if info.Component != "" && info.Stack != "" {
message = fmt.Sprintf("This will delete %v local terraform state files for component '%s' in stack '%s'", total, info.Component, info.Stack)
Expand Down
7 changes: 5 additions & 2 deletions internal/exec/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,9 +688,12 @@ func processArgsAndFlags(
var additionalArgsAndFlags []string
var globalOptions []string
var indexesToRemove []int
if len(inputArgsAndFlags) == 1 && inputArgsAndFlags[0] == "clean" {
info.SubCommand = inputArgsAndFlags[0]
}

// For commands like `atmos terraform clean` and `atmos terraform plan`, show the command help
if len(inputArgsAndFlags) == 1 && inputArgsAndFlags[0] != "version" {
// For commands like `atmos terraform plan`, show the command help
if len(inputArgsAndFlags) == 1 && inputArgsAndFlags[0] != "version" && info.SubCommand == "" {
info.SubCommand = inputArgsAndFlags[0]
info.NeedHelp = true
return info, nil
Expand Down
175 changes: 175 additions & 0 deletions tests/cli_terraform_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
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())
}

// Force clean everything
runTerraformCleanCommand(t, binaryPath, "--force")
// Clean everything
runTerraformCleanCommand(t, binaryPath)
// Clean specific component
runTerraformCleanCommand(t, binaryPath, "station")
// Clean component with stack
runTerraformCleanCommand(t, binaryPath, "station", "-s", "dev")

// 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")

// 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)

}

// 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())
}
}

// 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())
}
}
func runCLITerraformClean(t *testing.T, binaryPath string) {
cmd := exec.Command(binaryPath, "terraform", "clean")
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())
}

}

func runTerraformCleanCommand(t *testing.T, binaryPath string, args ...string) {
cmdArgs := append([]string{"terraform", "clean"}, args...)
cmd := exec.Command(binaryPath, cmdArgs...)
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())
}
}
Loading

0 comments on commit f920318

Please sign in to comment.