11package test
22
33import (
4+ "github.com/gruntwork-io/terratest/modules/logger"
5+ "github.com/gruntwork-io/terratest/modules/random"
6+ test_structure "github.com/gruntwork-io/terratest/modules/test-structure"
7+ "github.com/stretchr/testify/require"
8+ "os"
9+ "strings"
410 "testing"
511
612 "github.com/gruntwork-io/terratest/modules/terraform"
713 "github.com/stretchr/testify/assert"
814)
915
16+ func cleanup (t * testing.T , terraformOptions * terraform.Options , tempTestFolder string ) {
17+ terraform .Destroy (t , terraformOptions )
18+ os .RemoveAll (tempTestFolder )
19+ }
20+
1021// Test the Terraform module in examples/complete using Terratest.
1122func TestExamplesComplete (t * testing.T ) {
1223 t .Parallel ()
24+ randID := strings .ToLower (random .UniqueId ())
25+ attributes := []string {randID }
26+
27+ rootFolder := "../../"
28+ terraformFolderRelativeToRoot := "examples/complete"
29+ varFiles := []string {"fixtures.us-west-1.tfvars" }
30+
31+ tempTestFolder := test_structure .CopyTerraformFolderToTemp (t , rootFolder , terraformFolderRelativeToRoot )
1332
1433 terraformOptions := & terraform.Options {
1534 // The path to where our Terraform code is located
16- TerraformDir : "../../examples/complete" ,
35+ TerraformDir : tempTestFolder ,
1736 Upgrade : true ,
1837 // Variables to pass to our Terraform code using -var-file options
19- VarFiles : []string {"fixtures.us-west-1.tfvars" },
38+ VarFiles : varFiles ,
39+ Vars : map [string ]interface {}{
40+ "attributes" : attributes ,
41+ },
42+ }
43+ // Keep the output quiet
44+ if ! testing .Verbose () {
45+ terraformOptions .Logger = logger .Discard
2046 }
2147
2248 // At the end of the test, run `terraform destroy` to clean up any resources that were created
23- defer terraform . Destroy (t , terraformOptions )
49+ defer cleanup (t , terraformOptions , tempTestFolder )
2450
2551 // This will run `terraform init` and `terraform apply` and fail the test if there are any errors
26- terraform .InitAndApply (t , terraformOptions )
52+ if _ , err := terraform .InitAndApplyE (t , terraformOptions ); err != nil {
53+ require .FailNow (t , "Terraform \" apply\" failed, skipping the rest of the tests" )
54+ }
2755
2856 // Run `terraform output` to get the value of an output variable
2957 zoneName := terraform .Output (t , terraformOptions , "zone_name" )
@@ -38,3 +66,57 @@ func TestExamplesComplete(t *testing.T) {
3866 // Verify we're getting back the outputs we expect
3967 assert .Contains (t , certificateArn , "arn:aws:acm:us-west-1:126450723953:certificate/" )
4068}
69+
70+ func TestExamplesCompleteDisabled (t * testing.T ) {
71+ t .Parallel ()
72+ randID := strings .ToLower (random .UniqueId ())
73+ attributes := []string {randID }
74+
75+ rootFolder := "../../"
76+ terraformFolderRelativeToRoot := "examples/complete"
77+ varFiles := []string {"fixtures.us-west-1.tfvars" }
78+
79+ tempTestFolder := test_structure .CopyTerraformFolderToTemp (t , rootFolder , terraformFolderRelativeToRoot )
80+
81+ terraformOptions := & terraform.Options {
82+ // The path to where our Terraform code is located
83+ TerraformDir : tempTestFolder ,
84+ Upgrade : true ,
85+ // Variables to pass to our Terraform code using -var-file options
86+ VarFiles : varFiles ,
87+ Vars : map [string ]interface {}{
88+ "attributes" : attributes ,
89+ "enabled" : "false" ,
90+ },
91+ }
92+ // Keep the output quiet
93+ if ! testing .Verbose () {
94+ terraformOptions .Logger = logger .Discard
95+ }
96+
97+ // At the end of the test, run `terraform destroy` to clean up any resources that were created
98+ defer cleanup (t , terraformOptions , tempTestFolder )
99+
100+ // This will run `terraform init` and `terraform apply` and fail the test if there are any errors
101+ terraform .InitAndApply (t , terraformOptions )
102+
103+ // Get all the output and lookup a field. Pass if the field is missing or empty.
104+ // Get all the output and lookup a field. Pass if the field is missing or empty.
105+ allOutputs := terraform .OutputAll (t , terraformOptions )
106+
107+ // Verify we're getting back the outputs we expect
108+ assert .Empty (t , allOutputs ["id" ], "When disabled, 'id' output should be empty." )
109+ assert .Empty (t , allOutputs ["arn" ], "When disabled, 'arn' output should be empty." )
110+ assert .Empty (t , allOutputs ["domain_validation_options" ], "When disabled, 'domain_validation_options' output should be empty." )
111+ assert .Empty (t , allOutputs ["validation_id" ], "When disabled, 'validation_id' output should be empty." )
112+ assert .Empty (t , allOutputs ["validation_certificate_arn" ], "When disabled, 'validation_certificate_arn' output should be empty." )
113+ assert .Empty (t , allOutputs ["parent_zone_id" ], "When disabled, 'parent_zone_id' output should be empty." )
114+ assert .Empty (t , allOutputs ["parent_zone_name" ], "When disabled, 'parent_zone_name' output should be empty." )
115+ assert .Empty (t , allOutputs ["zone_id" ], "When disabled, 'zone_id' output should be empty." )
116+ assert .Empty (t , allOutputs ["zone_name" ], "When disabled, 'zone_name' output should be empty." )
117+ assert .Empty (t , allOutputs ["zone_fqdn" ], "When disabled, 'zone_fqdn' output should be empty." )
118+ assert .Empty (t , allOutputs ["certificate_id" ], "When disabled, 'certificate_id' output should be empty." )
119+ assert .Empty (t , allOutputs ["certificate_arn" ], "When disabled, 'certificate_arn' output should be empty." )
120+ assert .Empty (t , allOutputs ["certificate_domain_validation_options" ], "When disabled, 'certificate_domain_validation_options' output should be empty." )
121+ assert .Empty (t , allOutputs ["validation_certificate_arn" ], "When disabled, 'validation_certificate_arn' output should be empty." )
122+ }
0 commit comments