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..922c20d 100644 --- a/test/gcp_sql_test.go +++ b/test/gcp_sql_test.go @@ -1,14 +1,38 @@ package test import ( + _ "errors" "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 +41,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 +106,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