Skip to content

Commit

Permalink
fix: terratest
Browse files Browse the repository at this point in the history
  • Loading branch information
bibek4699 committed Jun 17, 2024
1 parent bf3c00c commit 91c0df6
Showing 1 changed file with 159 additions and 4 deletions.
163 changes: 159 additions & 4 deletions test/gcp_sql_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,169 @@
// package test
//
// import (
//
// "strings"
// "testing"
//
// "github.com/gruntwork-io/terratest/modules/terraform"
// test_structure "github.com/gruntwork-io/terratest/modules/test-structure"
// "github.com/stretchr/testify/assert"
//
// )
//
// func TestTerraformCreateGCPSQL(t *testing.T) {
// t.Parallel()
//
// t.Run("create mysql with read replica", func(t *testing.T) {
// t.Parallel()
//
// 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)
//
// terraform.InitAndApply(t, terraformOptions)
//
// var output string
//
// output = terraform.Output(t, terraformOptions, "random_string")
// assert.NotEmpty(t, output)
//
// output = terraform.Output(t, terraformOptions, "sql_database_instance_master_instance_name")
// assert.Contains(t, output, "sql-rr")
//
// output = terraform.Output(t, terraformOptions, "sql_database_instance_master_self_link")
// assert.Contains(t, output, "sql-rr")
//
// output = terraform.Output(t, terraformOptions, "sql_database_instance_read_replica_instance_name")
// assert.Contains(t, output, "sql-rr")
// assert.Contains(t, output, "-read-replica")
//
// output = terraform.Output(t, terraformOptions, "sql_database_instance_read_replica_self_link")
// assert.Contains(t, output, "sql-rr")
// assert.Contains(t, output, "-read-replica")
//
// output = terraform.Output(t, terraformOptions, "sql_database_instance_master_service_account_email_address")
// assert.NotEmpty(t, output)
//
// output = terraform.Output(t, terraformOptions, "sql_database_instance_master_connection_name")
// assert.NotEmpty(t, output)
//
// output = terraform.Output(t, terraformOptions, "sql_user_name")
// assert.Equal(t, output, "sql-user")
//
// output = terraform.Output(t, terraformOptions, "sql_database_master_id")
// assert.Contains(t, output, "db-first")
//
// output = terraform.Output(t, terraformOptions, "sql_database_instance_master_public_ip_address")
// assert.Empty(t, output)
//
// output = terraform.Output(t, terraformOptions, "sql_database_instance_master_private_ip_address")
// assert.NotEmpty(t, output)
//
// output = terraform.Output(t, terraformOptions, "sql_database_instance_read_replica_public_ip_address")
// assert.NotEmpty(t, output)
//
// output = terraform.Output(t, terraformOptions, "sql_database_instance_read_replica_private_ip_address")
// assert.NotEmpty(t, output)
// })
//
// t.Run("create PostgreSQL with read replica", func(t *testing.T) {
// t.Parallel()
//
// 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)
//
// var output string
//
// output = terraform.Output(t, terraformOptions, "random_string")
// assert.NotEmpty(t, output)
//
// output = terraform.Output(t, terraformOptions, "sql_database_instance_master_instance_name")
// assert.Contains(t, output, "sql-rr")
//
// output = terraform.Output(t, terraformOptions, "sql_database_instance_master_self_link")
// assert.Contains(t, output, "sql-rr")
//
// output = terraform.Output(t, terraformOptions, "sql_database_instance_read_replica_instance_name")
// assert.Contains(t, output, "sql-rr")
// assert.Contains(t, output, "-read-replica")
//
// output = terraform.Output(t, terraformOptions, "sql_database_instance_read_replica_self_link")
// assert.Contains(t, output, "sql-rr")
// assert.Contains(t, output, "-read-replica")
//
// output = terraform.Output(t, terraformOptions, "sql_database_instance_master_service_account_email_address")
// assert.NotEmpty(t, output)
//
// output = terraform.Output(t, terraformOptions, "sql_database_instance_master_connection_name")
// assert.NotEmpty(t, output)
//
// output = terraform.Output(t, terraformOptions, "sql_user_name")
// assert.Equal(t, output, "sql-user")
//
// output = terraform.Output(t, terraformOptions, "sql_database_master_id")
// assert.Contains(t, output, "db-first")
//
// output = terraform.Output(t, terraformOptions, "sql_database_instance_master_public_ip_address")
// assert.Empty(t, output)
//
// output = terraform.Output(t, terraformOptions, "sql_database_instance_master_private_ip_address")
// assert.NotEmpty(t, output)
//
// output = terraform.Output(t, terraformOptions, "sql_database_instance_read_replica_public_ip_address")
// assert.NotEmpty(t, output)
//
// output = terraform.Output(t, terraformOptions, "sql_database_instance_read_replica_private_ip_address")
// assert.NotEmpty(t, output)
//
// output = terraform.Output(t, terraformOptions, "database_version")
// assert.True(t, strings.HasPrefix(output, "POSTGRES_"))
// })
// }
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
)

func retryTerraformApply(t *testing.T, terraformOptions *terraform.Options) error {
var err error
for i := 0; i < maxRetries; i++ {
err = terraform.InitAndApply(t, terraformOptions)
if err == nil {
break
}

t.Logf("Terraform apply failed on attempt %d: %v, retrying in %v...\n", i+1, err, retryInterval)
time.Sleep(retryInterval)
}
return err
}

func TestTerraformCreateGCPSQL(t *testing.T) {
t.Parallel()

Expand All @@ -17,14 +172,14 @@ 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)

terraform.InitAndApply(t, terraformOptions)
err := retryTerraformApply(t, terraformOptions)
assert.NoError(t, err, "Terraform apply failed after multiple retries")

var output string

Expand Down Expand Up @@ -75,14 +230,14 @@ 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)
err := retryTerraformApply(t, terraformOptions)
assert.NoError(t, err, "Terraform apply failed after multiple retries")

var output string

Expand Down

0 comments on commit 91c0df6

Please sign in to comment.