|
| 1 | +package test |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/gruntwork-io/terratest/modules/aws" |
| 8 | + "github.com/gruntwork-io/terratest/modules/random" |
| 9 | + "github.com/gruntwork-io/terratest/modules/terraform" |
| 10 | + test_structure "github.com/gruntwork-io/terratest/modules/test-structure" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +// These const are declared according to what is found at "../../examples/minimal/main.tf" |
| 15 | +const ( |
| 16 | + expectedPw = "examplePassword" |
| 17 | + expectedUsername = "exampleUsername" |
| 18 | + expectedDBName = "example0" |
| 19 | +) |
| 20 | + |
| 21 | +// initTestCases initializes a list of RdsTestCase |
| 22 | +func initTestCases() []RdsTestCase { |
| 23 | + return []RdsTestCase{ |
| 24 | + { |
| 25 | + testName: "minimal", |
| 26 | + expectApplyError: false, |
| 27 | + vars: map[string]interface{}{ |
| 28 | + "vpc_cidr": "172.18.0.0/18", |
| 29 | + "database_subnets": []string{"172.18.0.0/24", "172.18.1.0/24"}, |
| 30 | + "egress_cidr_blocks": []string{"0.0.0.0/0"}, |
| 31 | + "ingress_cidr_blocks": []string{"0.0.0.0/0"}, |
| 32 | + "name_prefix": "", |
| 33 | + }, |
| 34 | + }, |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// TestTerraformCreateRDS runs all test cases |
| 39 | +func TestTerraformCreateRDS(t *testing.T) { |
| 40 | + |
| 41 | + testCases := initTestCases() |
| 42 | + |
| 43 | + for _, testCase := range testCases { |
| 44 | + testCase := testCase |
| 45 | + |
| 46 | + t.Run(testCase.testName, func(t *testing.T) { |
| 47 | + t.Parallel() |
| 48 | + |
| 49 | + // These will create a tempTestFolder for each bucketTestCase. |
| 50 | + tempTestFolder := test_structure.CopyTerraformFolderToTemp(t, "..", "test_examples/minimal") |
| 51 | + |
| 52 | + // this stage will generate a random `awsRegion` and a `uniqueId` to be used in tests. |
| 53 | + test_structure.RunTestStage(t, "pick_new_randoms", func() { |
| 54 | + usRegions := []string{"us-east-1", "us-east-2", "us-west-1", "us-west-2"} |
| 55 | + // This function will first check for the Env Var TERRATEST_REGION and return its value if != "" |
| 56 | + awsRegion := aws.GetRandomStableRegion(t, usRegions, nil) |
| 57 | + |
| 58 | + test_structure.SaveString(t, tempTestFolder, "region", awsRegion) |
| 59 | + test_structure.SaveString(t, tempTestFolder, "unique_id", strings.ToLower(random.UniqueId())) |
| 60 | + }) |
| 61 | + |
| 62 | + defer test_structure.RunTestStage(t, "teardown", func() { |
| 63 | + teraformOptions := test_structure.LoadTerraformOptions(t, tempTestFolder) |
| 64 | + terraform.Destroy(t, teraformOptions) |
| 65 | + }) |
| 66 | + |
| 67 | + test_structure.RunTestStage(t, "setup_options", func() { |
| 68 | + awsRegion := test_structure.LoadString(t, tempTestFolder, "region") |
| 69 | + uniqueID := test_structure.LoadString(t, tempTestFolder, "unique_id") |
| 70 | + |
| 71 | + testCase.vars["name_prefix"] = uniqueID |
| 72 | + |
| 73 | + terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{ |
| 74 | + TerraformDir: tempTestFolder, |
| 75 | + Vars: testCase.vars, |
| 76 | + EnvVars: map[string]string{ |
| 77 | + "AWS_REGION": awsRegion, |
| 78 | + }, |
| 79 | + }) |
| 80 | + |
| 81 | + test_structure.SaveTerraformOptions(t, tempTestFolder, terraformOptions) |
| 82 | + }) |
| 83 | + |
| 84 | + test_structure.RunTestStage(t, "create_rds", func() { |
| 85 | + terraformOptions := test_structure.LoadTerraformOptions(t, tempTestFolder) |
| 86 | + _, err := terraform.InitAndApplyE(t, terraformOptions) |
| 87 | + |
| 88 | + if testCase.expectApplyError { |
| 89 | + require.Error(t, err) |
| 90 | + // If it failed as expected, we should skip the rest (validate function). |
| 91 | + t.SkipNow() |
| 92 | + } |
| 93 | + }) |
| 94 | + |
| 95 | + test_structure.RunTestStage(t, "validate", func() { |
| 96 | + awsRegion := test_structure.LoadString(t, tempTestFolder, "region") |
| 97 | + terraformOptions := test_structure.LoadTerraformOptions(t, tempTestFolder) |
| 98 | + validateModuleOutputs(t, |
| 99 | + terraformOptions, |
| 100 | + awsRegion, |
| 101 | + int64(5432), |
| 102 | + expectedUsername, |
| 103 | + expectedDBName, |
| 104 | + ) |
| 105 | + }) |
| 106 | + }) |
| 107 | + } |
| 108 | +} |
0 commit comments