github.com/darmach/terratest@v0.34.8-0.20210517103231-80931f95e3ff/test/gcp/terraform_gcp_ig_example_test.go (about) 1 // +build gcp 2 3 // NOTE: We use build tags to differentiate GCP testing for better isolation and parallelism when executing our tests. 4 5 package test 6 7 import ( 8 "fmt" 9 "testing" 10 "time" 11 12 "github.com/gruntwork-io/terratest/modules/gcp" 13 "github.com/gruntwork-io/terratest/modules/retry" 14 "github.com/gruntwork-io/terratest/modules/terraform" 15 test_structure "github.com/gruntwork-io/terratest/modules/test-structure" 16 ) 17 18 func TestTerraformGcpInstanceGroupExample(t *testing.T) { 19 t.Parallel() 20 21 exampleDir := test_structure.CopyTerraformFolderToTemp(t, "../../", "examples/terraform-gcp-ig-example") 22 23 // Setup values for our Terraform apply 24 projectId := gcp.GetGoogleProjectIDFromEnvVar(t) 25 26 // On October 22, 2018, GCP launched the asia-east2 region, which promptly failed all our tests, so blacklist asia-east2. 27 region := gcp.GetRandomRegion(t, projectId, nil, []string{"asia-east2"}) 28 29 randomValidGcpName := gcp.RandomValidGcpName() 30 clusterSize := 3 31 32 terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{ 33 // The path to where our Terraform code instances located 34 TerraformDir: exampleDir, 35 36 // Variables to pass to our Terraform code using -var options 37 Vars: map[string]interface{}{ 38 "gcp_project_id": projectId, 39 "gcp_region": region, 40 "cluster_name": randomValidGcpName, 41 }, 42 }) 43 44 // 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 // This will run `terraform init` and `terraform apply` and fail the test if there are any errors 48 terraform.InitAndApply(t, terraformOptions) 49 50 instanceGroupName := terraform.Output(t, terraformOptions, "instance_group_name") 51 52 instanceGroup := gcp.FetchRegionalInstanceGroup(t, projectId, region, instanceGroupName) 53 54 // Validate that GetInstances() returns a non-zero number of Instances 55 maxRetries := 40 56 sleepBetweenRetries := 3 * time.Second 57 58 retry.DoWithRetry(t, "Attempting to fetch Instances from Instance Group", maxRetries, sleepBetweenRetries, func() (string, error) { 59 instances, err := instanceGroup.GetInstancesE(t, projectId) 60 if err != nil { 61 return "", fmt.Errorf("Failed to get Instances: %s", err) 62 } 63 64 if len(instances) != clusterSize { 65 return "", fmt.Errorf("Expected to find exactly %d Compute Instances in Instance Group but found %d.", clusterSize, len(instances)) 66 } 67 68 return "", nil 69 }) 70 71 // Validate that we get the right number of IP addresses 72 retry.DoWithRetry(t, "Attempting to fetch Public IP addresses from Instance Group", maxRetries, sleepBetweenRetries, func() (string, error) { 73 ips, err := instanceGroup.GetPublicIpsE(t, projectId) 74 if err != nil { 75 return "", fmt.Errorf("Failed to get public IPs from Instance Group") 76 } 77 78 if len(ips) != clusterSize { 79 return "", fmt.Errorf("Expected to get exactly %d public IP addresses but found %d.", clusterSize, len(ips)) 80 } 81 82 return "", nil 83 }) 84 }