Skip to content

Commit

Permalink
fix: terratest
Browse files Browse the repository at this point in the history
  • Loading branch information
bibek4699 committed Jun 18, 2024
1 parent bf3c00c commit 2159825
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 16 deletions.
37 changes: 28 additions & 9 deletions .github/workflows/terratest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
52 changes: 45 additions & 7 deletions test/gcp_sql_test.go
Original file line number Diff line number Diff line change
@@ -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()

Expand All @@ -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

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 2159825

Please sign in to comment.