11package test
22
33import (
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
3944func 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).
0 commit comments