forked from gruntwork-io/terratest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
terraform_basic_example_test.go
55 lines (42 loc) · 1.93 KB
/
terraform_basic_example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package test
import (
"testing"
"github.com/gruntwork-io/terratest/modules/terraform"
"github.com/stretchr/testify/assert"
)
// An example of how to test the simple Terraform module in examples/terraform-basic-example using Terratest.
func TestTerraformBasicExample(t *testing.T) {
t.Parallel()
expectedText := "test"
expectedList := []string{expectedText}
expectedMap := map[string]string{"expected": expectedText}
terraformOptions := &terraform.Options{
// The path to where our Terraform code is located
TerraformDir: "../examples/terraform-basic-example",
// Variables to pass to our Terraform code using -var options
Vars: map[string]interface{}{
"example": expectedText,
// We also can see how lists and maps translate between terratest and terraform.
"example_list": expectedList,
"example_map": expectedMap,
},
// Variables to pass to our Terraform code using -var-file options
VarFiles: []string{"varfile.tfvars"},
// Disable colors in Terraform commands so its easier to parse stdout/stderr
NoColor: true,
}
// At the end of the test, run `terraform destroy` to clean up any resources that were created
defer terraform.Destroy(t, terraformOptions)
// This will run `terraform init` and `terraform apply` and fail the test if there are any errors
terraform.InitAndApply(t, terraformOptions)
// Run `terraform output` to get the values of output variables
actualTextExample := terraform.Output(t, terraformOptions, "example")
actualTextExample2 := terraform.Output(t, terraformOptions, "example2")
actualExampleList := terraform.OutputList(t, terraformOptions, "example_list")
actualExampleMap := terraform.OutputMap(t, terraformOptions, "example_map")
// Verify we're getting back the outputs we expect
assert.Equal(t, expectedText, actualTextExample)
assert.Equal(t, expectedText, actualTextExample2)
assert.Equal(t, expectedList, actualExampleList)
assert.Equal(t, expectedMap, actualExampleMap)
}