|
1 | 1 | package test |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "strings" |
| 4 | + "fmt" |
| 5 | + "math/rand" |
| 6 | + "strconv" |
5 | 7 | "testing" |
| 8 | + "time" |
6 | 9 |
|
7 | 10 | "github.com/gruntwork-io/terratest/modules/terraform" |
8 | 11 | "github.com/stretchr/testify/assert" |
9 | 12 | ) |
10 | 13 |
|
| 14 | +const preExistingKeyFormat = "/production/test/master/preexisting_key_%s" |
| 15 | + |
11 | 16 | // Test the Terraform module in examples/complete using Terratest. |
12 | 17 | func TestExamplesComplete(t *testing.T) { |
| 18 | + terraformOptions := &terraform.Options{ |
| 19 | + // The path to where our Terraform code is located |
| 20 | + TerraformDir: "../../examples/complete", |
| 21 | + Upgrade: true, |
| 22 | + // Variables to pass to our Terraform code using -var-file options |
| 23 | + VarFiles: []string{"fixtures.us-east-2.tfvars"}, |
| 24 | + } |
| 25 | + |
| 26 | + terraform.Init(t, terraformOptions) |
| 27 | + // Run tests in parallel |
| 28 | + t.Run("Disabled", testExamplesCompleteDisabled) |
| 29 | + t.Run("Enabled", testExamplesCompleteEnabled) |
| 30 | +} |
| 31 | + |
| 32 | +func testExamplesCompleteDisabled(t *testing.T) { |
13 | 33 | t.Parallel() |
14 | 34 |
|
| 35 | + attributes := []string{strconv.Itoa(rand.Intn(100000))} |
| 36 | + preExistingKeyName := fmt.Sprintf(preExistingKeyFormat, attributes[0]) |
| 37 | + |
15 | 38 | terraformOptions := &terraform.Options{ |
16 | 39 | // The path to where our Terraform code is located |
17 | 40 | TerraformDir: "../../examples/complete", |
18 | 41 | Upgrade: true, |
| 42 | + EnvVars: map[string]string{ |
| 43 | + "TF_CLI_ARGS": "-state=terraform-disabled-test.tfstate", |
| 44 | + }, |
19 | 45 | // Variables to pass to our Terraform code using -var-file options |
20 | 46 | VarFiles: []string{"fixtures.us-east-2.tfvars"}, |
| 47 | + Vars: map[string]interface{}{ |
| 48 | + "enabled": false, |
| 49 | + "attributes": attributes, |
| 50 | + "parameter_read": []string{ |
| 51 | + preExistingKeyName, |
| 52 | + }, |
| 53 | + }, |
21 | 54 | } |
22 | 55 |
|
23 | 56 | // At the end of the test, run `terraform destroy` to clean up any resources that were created |
24 | 57 | defer terraform.Destroy(t, terraformOptions) |
25 | 58 |
|
26 | | - // This will run `terraform init` and `terraform apply` and fail the test if there are any errors |
27 | | - terraform.InitAndApply(t, terraformOptions) |
| 59 | + // This will run `terraform apply` and fail the test if there are any errors |
| 60 | + terraform.Apply(t, terraformOptions) |
28 | 61 |
|
29 | 62 | // Run `terraform output` to get the value of an output variable |
30 | | - output := terraform.Output(t, terraformOptions, "map") |
| 63 | + output := terraform.OutputMap(t, terraformOptions, "map") |
| 64 | + assert.Equal(t, len(output), 0, "There should be no outputs when the module is disabled.") |
| 65 | +} |
31 | 66 |
|
32 | | - key1Result := strings.Contains(output, "/production/test/master/company") |
33 | | - value1Result := strings.Contains(output, "Amazon") |
34 | | - key2Result := strings.Contains(output, "/production/test/master/users") |
35 | | - value2Result := strings.Contains(output, "John,Todd") |
| 67 | +func testExamplesCompleteEnabled(t *testing.T) { |
| 68 | + t.Parallel() |
| 69 | + |
| 70 | + rand.Seed(time.Now().UnixNano() + 1) // give a slightly different seed than the other parallel test |
| 71 | + attributes := []string{strconv.Itoa(rand.Intn(100000))} |
| 72 | + preExistingKeyName := fmt.Sprintf(preExistingKeyFormat, attributes[0]) |
| 73 | + |
| 74 | + terraformOptions := &terraform.Options{ |
| 75 | + // The path to where our Terraform code is located |
| 76 | + TerraformDir: "../../examples/complete", |
| 77 | + Upgrade: true, |
| 78 | + EnvVars: map[string]string{ |
| 79 | + "TF_CLI_ARGS": "-state=terraform-enabled-test.tfstate", |
| 80 | + }, |
| 81 | + // Variables to pass to our Terraform code using -var-file options |
| 82 | + VarFiles: []string{"fixtures.us-east-2.tfvars"}, |
| 83 | + Vars: map[string]interface{}{ |
| 84 | + "attributes": attributes, |
| 85 | + "parameter_read": []string{ |
| 86 | + preExistingKeyName, |
| 87 | + }, |
| 88 | + }, |
| 89 | + } |
| 90 | + |
| 91 | + // At the end of the test, run `terraform destroy` to clean up any resources that were created |
| 92 | + defer terraform.Destroy(t, terraformOptions) |
| 93 | + |
| 94 | + // This will run `terraform apply` and fail the test if there are any errors |
| 95 | + terraform.Apply(t, terraformOptions) |
| 96 | + |
| 97 | + // Run `terraform output` to get the value of an output variable |
| 98 | + output := terraform.OutputMap(t, terraformOptions, "map") |
| 99 | + |
| 100 | + // Expected values for created parameters |
| 101 | + key1 := "/production/test/master/company" |
| 102 | + value1 := "Amazon" |
| 103 | + key2 := "/production/test/master/users" |
| 104 | + value2 := "John,Todd" |
| 105 | + |
| 106 | + // Expected values for preexisting parameters |
| 107 | + key3 := preExistingKeyName |
| 108 | + value3 := "preexisting_value" |
36 | 109 |
|
37 | 110 | // Verify we're getting back the outputs we expect |
38 | | - assert.True(t, key1Result, "The 'map' output should contain the /production/test/master/company key") |
39 | | - assert.True(t, value1Result, "The /production/test/master/company key's value should be 'Amazon'") |
| 111 | + containsErrorMessageFormat := "The 'map' output should contain the %s key" |
| 112 | + equalsErrorMessageFormat := "The %s's value should be '%s'" |
| 113 | + |
| 114 | + assert.Contains(t, output, key1, fmt.Sprintf(containsErrorMessageFormat, key1)) |
| 115 | + assert.Equal(t, value1 ,output[key1], fmt.Sprintf(equalsErrorMessageFormat, key1, value1)) |
| 116 | + |
| 117 | + assert.Contains(t, output, key2, fmt.Sprintf(containsErrorMessageFormat, key2)) |
| 118 | + assert.Equal(t, value2 ,output[key2], fmt.Sprintf(equalsErrorMessageFormat, key2, value2)) |
40 | 119 |
|
41 | | - assert.True(t, key2Result, "The 'map' output should contain the /production/test/master/users key") |
42 | | - assert.True(t, value2Result, "The /production/test/master/users key's value should be 'John,Todd'") |
| 120 | + assert.Contains(t, output, key3, fmt.Sprintf(containsErrorMessageFormat, key3)) |
| 121 | + assert.Equal(t, value3 ,output[key3], fmt.Sprintf(equalsErrorMessageFormat, key3, value3)) |
43 | 122 | } |
0 commit comments