-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
terraform_gcp_hello_world_example_test.go
49 lines (37 loc) · 1.67 KB
/
terraform_gcp_hello_world_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
//go:build gcp
// +build gcp
// NOTE: We use build tags to differentiate GCP testing for better isolation and parallelism when executing our tests.
package test
import (
"fmt"
"strings"
"testing"
"github.com/gruntwork-io/terratest/modules/gcp"
"github.com/gruntwork-io/terratest/modules/random"
"github.com/gruntwork-io/terratest/modules/terraform"
)
func TestTerraformGcpHelloWorldExample(t *testing.T) {
t.Parallel()
// website::tag::1:: Get the Project Id to use
projectId := gcp.GetGoogleProjectIDFromEnvVar(t)
// website::tag::2:: Give the example instance a unique name
instanceName := fmt.Sprintf("gcp-hello-world-example-%s", strings.ToLower(random.UniqueId()))
// website::tag::6:: Construct the terraform options with default retryable errors to handle the most common
// retryable errors in terraform testing.
terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
// website::tag::3:: The path to where our Terraform code is located
TerraformDir: "../../examples/terraform-gcp-hello-world-example",
// website::tag::4:: Variables to pass to our Terraform code using -var options
Vars: map[string]interface{}{
"instance_name": instanceName,
},
// website::tag::5:: Variables to pass to our Terraform code using TF_VAR_xxx environment variables
EnvVars: map[string]string{
"GOOGLE_CLOUD_PROJECT": projectId,
},
})
// website::tag::8:: At the end of the test, run `terraform destroy` to clean up any resources that were created
defer terraform.Destroy(t, terraformOptions)
// website::tag::7:: Run `terraform init` and `terraform apply`. Fail the test if there are any errors.
terraform.InitAndApply(t, terraformOptions)
}