From 826c506312fc3df79c49e886300054d208c47ad2 Mon Sep 17 00:00:00 2001 From: Bibek Rauniyar Date: Mon, 17 Jun 2024 23:33:46 +0700 Subject: [PATCH] fix: terratest --- .github/workflows/terratest.yml | 37 ++++++++++++++++++------ test/gcp_sql_test.go | 51 ++++++++++++++++++++++++++++----- 2 files changed, 72 insertions(+), 16 deletions(-) diff --git a/.github/workflows/terratest.yml b/.github/workflows/terratest.yml index ecbdc1f..3597d83 100644 --- a/.github/workflows/terratest.yml +++ b/.github/workflows/terratest.yml @@ -25,30 +25,45 @@ env: TF_VAR_google_credentials: ${{ secrets.TERRATEST_GOOGLE_CREDENTIALS }} TF_VAR_shared_vpc_host_google_credentials: ${{ secrets.TERRATEST_GOOGLE_CREDENTIALS }} TF_VAR_google_region: ${{ secrets.TERRATEST_GOOGLE_REGION }} + jobs: terratest: - name: terratest + name: Terratest runs-on: ubuntu-latest + steps: - name: Check out code into the Go module directory - uses: actions/checkout@v3 + uses: actions/checkout@v4 # Updated to latest version with: submodules: true - - name: Set up Go (1.17) - uses: actions/setup-go@v2 + + - name: Cache Go modules + uses: actions/cache@v3 # Use caching to speed up Go modules installation + with: + path: | + ~/.cache/go-build + ~/go/pkg/mod + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go- + + - name: Set up Go (1.21) + uses: actions/setup-go@v4 # Updated to latest version with: - go-version: 1.17 - id: go + go-version: 1.21 + - name: Login to Google Cloud - uses: google-github-actions/auth@v0 + uses: google-github-actions/auth@v2 # Updated to latest version with: credentials_json: ${{ env.TERRATEST_GOOGLE_CREDENTIALS }} - name: Set Google Cloud project - run: gcloud config set project $GOOGLE_PROJECT - - name: Run terratest + run: gcloud config set project ${{ env.GOOGLE_PROJECT }} + + - name: Run Terratest run: | make tests + - name: Release uses: cycjimmy/semantic-release-action@v3 env: @@ -58,3 +73,7 @@ jobs: @semantic-release/git@10.0.1 @semantic-release/exec@6.0.3 @semantic-release/changelog@6.0.1 + + - name: Clear GCloud Config + if: always() + run: gcloud config unset project diff --git a/test/gcp_sql_test.go b/test/gcp_sql_test.go index 02a76a8..e7d3be0 100644 --- a/test/gcp_sql_test.go +++ b/test/gcp_sql_test.go @@ -3,12 +3,35 @@ package test import ( "strings" "testing" + "time" "github.com/gruntwork-io/terratest/modules/terraform" test_structure "github.com/gruntwork-io/terratest/modules/test-structure" "github.com/stretchr/testify/assert" ) +const ( + maxRetries = 3 + retryInterval = 10 * time.Second +) + +// retryTerraformDestroy attempts to destroy the Terraform configuration with retries. +func retryTerraformDestroy(t *testing.T, terraformOptions *terraform.Options) error { + var err error + for i := 0; i < maxRetries; i++ { + // Run terraform.DestroyE and check for nil indicating success + err = terraform.DestroyE(t, terraformOptions) + if err == nil { + return nil // Success, no error + } + t.Logf("Retry %d/%d: Terraform destroy failed with error: %v", i+1, maxRetries, err) + + // Wait before retrying + time.Sleep(retryInterval) + } + return err // Return the last error encountered +} + func TestTerraformCreateGCPSQL(t *testing.T) { t.Parallel() @@ -17,14 +40,21 @@ func TestTerraformCreateGCPSQL(t *testing.T) { testDirectory := test_structure.CopyTerraformFolderToTemp(t, "..", "examples/mysql_instance_with_read_replica") - // retryable errors in terraform testing. terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{ TerraformDir: testDirectory, }) - defer terraform.Destroy(t, terraformOptions) + // Use retryTerraformDestroy to ensure destruction with retries + defer func() { + err := retryTerraformDestroy(t, terraformOptions) + if err != nil { + t.Fatalf("Failed to destroy resources: %v", err) + } + }() - terraform.InitAndApply(t, terraformOptions) + // Apply without retries + err := terraform.InitAndApplyE(t, terraformOptions) + assert.NoError(t, err, "Terraform apply failed") var output string @@ -75,14 +105,21 @@ func TestTerraformCreateGCPSQL(t *testing.T) { testDirectory := test_structure.CopyTerraformFolderToTemp(t, "..", "examples/postgres_instance_with_read_replica") - // retryable errors in terraform testing. terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{ TerraformDir: testDirectory, }) - defer terraform.Destroy(t, terraformOptions) - - terraform.InitAndApply(t, terraformOptions) + // Use retryTerraformDestroy to ensure destruction with retries + defer func() { + err := retryTerraformDestroy(t, terraformOptions) + if err != nil { + t.Fatalf("Failed to destroy resources: %v", err) + } + }() + + // Apply without retries + err := terraform.InitAndApplyE(t, terraformOptions) + assert.NoError(t, err, "Terraform apply failed") var output string