Skip to content

Commit 86a0953

Browse files
author
jamoroso-caylent
authored
CA-196: Replicate cleanup functions needed on Jenkins pipeline on all tamr-AWS-modules (#26)
* Added test functions for cleanup stage * Modified tests to decrease retry attempts
1 parent 48b1892 commit 86a0953

File tree

5 files changed

+433
-17
lines changed

5 files changed

+433
-17
lines changed

test/create_db_test.go

+28-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package test
22

33
import (
4+
"os"
45
"strings"
56
"testing"
67

8+
terratestutils "github.com/Datatamer/go-terratest-functions/pkg/terratest_utils"
9+
"github.com/Datatamer/go-terratest-functions/pkg/types"
710
"github.com/gruntwork-io/terratest/modules/aws"
11+
"github.com/gruntwork-io/terratest/modules/logger"
812
"github.com/gruntwork-io/terratest/modules/random"
913
"github.com/gruntwork-io/terratest/modules/terraform"
1014
test_structure "github.com/gruntwork-io/terratest/modules/test-structure"
@@ -23,6 +27,7 @@ func initTestCases() []RdsTestCase {
2327
return []RdsTestCase{
2428
{
2529
testName: "minimal",
30+
tfDir: "test_examples/minimal",
2631
expectApplyError: false,
2732
vars: map[string]interface{}{
2833
"vpc_cidr": "172.18.0.0/18",
@@ -37,9 +42,11 @@ func initTestCases() []RdsTestCase {
3742

3843
// TestTerraformCreateRDS runs all test cases
3944
func TestTerraformCreateRDS(t *testing.T) {
40-
45+
const MODULE_NAME = "terraform-aws-rds-postgres"
4146
testCases := initTestCases()
42-
47+
// Generate file containing GCS URL to be used on Jenkins.
48+
// TERRATEST_BACKEND_BUCKET_NAME and TERRATEST_URL_FILE_NAME are both set on Jenkins declaration.
49+
gcsTestExamplesURL := terratestutils.GenerateUrlFile(t, MODULE_NAME, os.Getenv("TERRATEST_BACKEND_BUCKET_NAME"), os.Getenv("TERRATEST_URL_FILE_NAME"))
4350
for _, testCase := range testCases {
4451
testCase := testCase
4552

@@ -60,13 +67,20 @@ func TestTerraformCreateRDS(t *testing.T) {
6067
})
6168

6269
defer test_structure.RunTestStage(t, "teardown", func() {
63-
teraformOptions := test_structure.LoadTerraformOptions(t, tempTestFolder)
64-
terraform.Destroy(t, teraformOptions)
70+
terraformOptions := test_structure.LoadTerraformOptions(t, tempTestFolder)
71+
terraformOptions.MaxRetries = 5
72+
73+
_, err := terraform.DestroyE(t, terraformOptions)
74+
if err != nil {
75+
// If there is an error on destroy, it will be logged.
76+
logger.Log(t, err)
77+
}
6578
})
6679

6780
test_structure.RunTestStage(t, "setup_options", func() {
6881
awsRegion := test_structure.LoadString(t, tempTestFolder, "region")
6982
uniqueID := test_structure.LoadString(t, tempTestFolder, "unique_id")
83+
backendConfig := terratestutils.ParseBackendConfig(t, gcsTestExamplesURL, testCase.testName, testCase.tfDir)
7084

7185
testCase.vars["name_prefix"] = uniqueID
7286

@@ -76,6 +90,8 @@ func TestTerraformCreateRDS(t *testing.T) {
7690
EnvVars: map[string]string{
7791
"AWS_REGION": awsRegion,
7892
},
93+
BackendConfig: backendConfig,
94+
MaxRetries: 2,
7995
})
8096

8197
test_structure.SaveTerraformOptions(t, tempTestFolder, terraformOptions)
@@ -84,7 +100,14 @@ func TestTerraformCreateRDS(t *testing.T) {
84100
test_structure.RunTestStage(t, "create_rds", func() {
85101
terraformOptions := test_structure.LoadTerraformOptions(t, tempTestFolder)
86102
_, err := terraform.InitAndApplyE(t, terraformOptions)
87-
103+
terraformConfig := &types.TerraformData{
104+
TerraformBackendConfig: terraformOptions.BackendConfig,
105+
TerraformVars: terraformOptions.Vars,
106+
TerraformEnvVars: terraformOptions.EnvVars,
107+
}
108+
if _, err := terratestutils.UploadFilesE(t, terraformConfig); err != nil {
109+
logger.Log(t, err)
110+
}
88111
if testCase.expectApplyError {
89112
require.Error(t, err)
90113
// If it failed as expected, we should skip the rest (validate function).

test/go.mod

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ module github.com/Datatamer/terraform-aws-rds-postgres
33
go 1.16
44

55
require (
6+
github.com/Datatamer/go-terratest-functions v0.2.0
67
github.com/aws/aws-sdk-go v1.40.40
78
github.com/gruntwork-io/terratest v0.37.8
8-
github.com/stretchr/testify v1.7.0
9+
github.com/stretchr/testify v1.7.1
910
)

0 commit comments

Comments
 (0)