github.com/swisspost/terratest@v0.0.0-20230214120104-7ec6de2e1ae0/test/gcp/terraform_gcp_hello_world_example_test.go (about) 1 //go:build gcp 2 // +build gcp 3 4 // NOTE: We use build tags to differentiate GCP testing for better isolation and parallelism when executing our tests. 5 6 package test 7 8 import ( 9 "fmt" 10 "strings" 11 "testing" 12 13 "github.com/gruntwork-io/terratest/modules/gcp" 14 "github.com/gruntwork-io/terratest/modules/random" 15 "github.com/gruntwork-io/terratest/modules/terraform" 16 ) 17 18 func TestTerraformGcpHelloWorldExample(t *testing.T) { 19 t.Parallel() 20 21 // website::tag::1:: Get the Project Id to use 22 projectId := gcp.GetGoogleProjectIDFromEnvVar(t) 23 24 // website::tag::2:: Give the example instance a unique name 25 instanceName := fmt.Sprintf("gcp-hello-world-example-%s", strings.ToLower(random.UniqueId())) 26 27 // website::tag::6:: Construct the terraform options with default retryable errors to handle the most common 28 // retryable errors in terraform testing. 29 terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{ 30 // website::tag::3:: The path to where our Terraform code is located 31 TerraformDir: "../../examples/terraform-gcp-hello-world-example", 32 33 // website::tag::4:: Variables to pass to our Terraform code using -var options 34 Vars: map[string]interface{}{ 35 "instance_name": instanceName, 36 }, 37 38 // website::tag::5:: Variables to pass to our Terraform code using TF_VAR_xxx environment variables 39 EnvVars: map[string]string{ 40 "GOOGLE_CLOUD_PROJECT": projectId, 41 }, 42 }) 43 44 // website::tag::8:: At the end of the test, run `terraform destroy` to clean up any resources that were created 45 defer terraform.Destroy(t, terraformOptions) 46 47 // website::tag::7:: Run `terraform init` and `terraform apply`. Fail the test if there are any errors. 48 terraform.InitAndApply(t, terraformOptions) 49 }