@@ -20,29 +20,44 @@ func RandStringRunes(n int) string {
20
20
return string (b )
21
21
}
22
22
23
- // Test the Terraform module in examples/complete using Terratest.
24
23
func TestExamplesComplete (t * testing.T ) {
24
+ terraformOptions := & terraform.Options {
25
+ // The path to where our Terraform code is located
26
+ TerraformDir : "../../examples/complete" ,
27
+ Upgrade : true ,
28
+ // Variables to pass to our Terraform code using -var-file options
29
+ VarFiles : []string {"fixtures.us-west-1.tfvars" },
30
+ }
31
+
32
+ terraform .Init (t , terraformOptions )
33
+ // Run tests in parallel
34
+ t .Run ("Enabled" , testExamplesCompleteEnabled )
35
+ t .Run ("Disabled" , testExamplesCompleteDisabled )
36
+ }
37
+
38
+ // Test the Terraform module in examples/complete using Terratest.
39
+ func testExamplesCompleteEnabled (t * testing.T ) {
25
40
t .Parallel ()
26
41
27
- testName := "s3-website-test-" + RandStringRunes (10 )
42
+ testName := "s3-website-test-" + RandStringRunes (10 )
28
43
29
44
terraformOptions := & terraform.Options {
30
45
// The path to where our Terraform code is located
31
46
TerraformDir : "../../examples/complete" ,
32
47
Upgrade : true ,
33
48
// Variables to pass to our Terraform code using -var-file options
34
49
VarFiles : []string {"fixtures.us-west-1.tfvars" },
35
- Vars : map [string ]interface {} {
36
- "name" : testName ,
37
- "hostname" : testName + ".testing.cloudposse.co" ,
50
+ Vars : map [string ]interface {}{
51
+ "name" : testName ,
52
+ "hostname" : testName + ".testing.cloudposse.co" ,
38
53
},
39
54
}
40
55
41
56
// At the end of the test, run `terraform destroy` to clean up any resources that were created
42
57
defer terraform .Destroy (t , terraformOptions )
43
58
44
59
// This will run `terraform init` and `terraform apply` and fail the test if there are any errors
45
- terraform .InitAndApply (t , terraformOptions )
60
+ terraform .Apply (t , terraformOptions )
46
61
47
62
// Run `terraform output` to get the value of an output variable
48
63
hostname := terraform .Output (t , terraformOptions , "hostname" )
@@ -59,3 +74,37 @@ func TestExamplesComplete(t *testing.T) {
59
74
// Verify we're getting back the outputs we expect
60
75
assert .Equal (t , testName + ".testing.cloudposse.co.s3.amazonaws.com" , s3BucketDomainName )
61
76
}
77
+
78
+ // Test the Terraform module in examples/complete using Terratest, but with the root module disabled.
79
+ func testExamplesCompleteDisabled (t * testing.T ) {
80
+ t .Parallel ()
81
+
82
+ testName := "s3-website-test-" + RandStringRunes (10 )
83
+
84
+ terraformOptions := & terraform.Options {
85
+ // The path to where our Terraform code is located
86
+ TerraformDir : "../../examples/complete" ,
87
+ Upgrade : true ,
88
+ EnvVars : map [string ]string {
89
+ "TF_CLI_ARGS" : "-state=terraform-disabled-test.tfstate" ,
90
+ },
91
+ // Variables to pass to our Terraform code using -var-file options
92
+ VarFiles : []string {"fixtures.us-west-1.tfvars" },
93
+ Vars : map [string ]interface {}{
94
+ "enabled" : "false" ,
95
+ "name" : testName ,
96
+ "hostname" : testName + ".testing.cloudposse.co" ,
97
+ },
98
+ }
99
+
100
+ // At the end of the test, run `terraform destroy` to clean up any resources that were created
101
+ defer terraform .Destroy (t , terraformOptions )
102
+
103
+ // This will run `terraform init` and `terraform apply` and fail the test if there are any errors
104
+ terraform .Apply (t , terraformOptions )
105
+
106
+ // Run `terraform output` to get the value of an output variable
107
+ s3BucketDomainName := terraform .Output (t , terraformOptions , "s3_bucket_domain_name" )
108
+ // Verify we're getting back the outputs we expect
109
+ assert .Empty (t , s3BucketDomainName )
110
+ }
0 commit comments