1
1
package test
2
2
3
3
import (
4
+ "os"
4
5
"strings"
5
6
"testing"
6
7
8
+ terratestutils "github.com/Datatamer/go-terratest-functions/pkg/terratest_utils"
9
+ "github.com/Datatamer/go-terratest-functions/pkg/types"
7
10
"github.com/gruntwork-io/terratest/modules/aws"
11
+ "github.com/gruntwork-io/terratest/modules/logger"
8
12
"github.com/gruntwork-io/terratest/modules/random"
9
13
"github.com/gruntwork-io/terratest/modules/terraform"
10
14
test_structure "github.com/gruntwork-io/terratest/modules/test-structure"
@@ -23,6 +27,7 @@ func initTestCases() []RdsTestCase {
23
27
return []RdsTestCase {
24
28
{
25
29
testName : "minimal" ,
30
+ tfDir : "test_examples/minimal" ,
26
31
expectApplyError : false ,
27
32
vars : map [string ]interface {}{
28
33
"vpc_cidr" : "172.18.0.0/18" ,
@@ -37,9 +42,11 @@ func initTestCases() []RdsTestCase {
37
42
38
43
// TestTerraformCreateRDS runs all test cases
39
44
func TestTerraformCreateRDS (t * testing.T ) {
40
-
45
+ const MODULE_NAME = "terraform-aws-rds-postgres"
41
46
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" ))
43
50
for _ , testCase := range testCases {
44
51
testCase := testCase
45
52
@@ -60,13 +67,20 @@ func TestTerraformCreateRDS(t *testing.T) {
60
67
})
61
68
62
69
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
+ }
65
78
})
66
79
67
80
test_structure .RunTestStage (t , "setup_options" , func () {
68
81
awsRegion := test_structure .LoadString (t , tempTestFolder , "region" )
69
82
uniqueID := test_structure .LoadString (t , tempTestFolder , "unique_id" )
83
+ backendConfig := terratestutils .ParseBackendConfig (t , gcsTestExamplesURL , testCase .testName , testCase .tfDir )
70
84
71
85
testCase .vars ["name_prefix" ] = uniqueID
72
86
@@ -76,6 +90,8 @@ func TestTerraformCreateRDS(t *testing.T) {
76
90
EnvVars : map [string ]string {
77
91
"AWS_REGION" : awsRegion ,
78
92
},
93
+ BackendConfig : backendConfig ,
94
+ MaxRetries : 2 ,
79
95
})
80
96
81
97
test_structure .SaveTerraformOptions (t , tempTestFolder , terraformOptions )
@@ -84,7 +100,14 @@ func TestTerraformCreateRDS(t *testing.T) {
84
100
test_structure .RunTestStage (t , "create_rds" , func () {
85
101
terraformOptions := test_structure .LoadTerraformOptions (t , tempTestFolder )
86
102
_ , 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
+ }
88
111
if testCase .expectApplyError {
89
112
require .Error (t , err )
90
113
// If it failed as expected, we should skip the rest (validate function).
0 commit comments