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