github.com/swisspost/terratest@v0.0.0-20230214120104-7ec6de2e1ae0/test/gcp/terraform_gcp_ig_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 "testing" 11 "time" 12 13 "github.com/gruntwork-io/terratest/modules/gcp" 14 "github.com/gruntwork-io/terratest/modules/retry" 15 "github.com/gruntwork-io/terratest/modules/terraform" 16 test_structure "github.com/gruntwork-io/terratest/modules/test-structure" 17 ) 18 19 func TestTerraformGcpInstanceGroupExample(t *testing.T) { 20 t.Parallel() 21 22 exampleDir := test_structure.CopyTerraformFolderToTemp(t, "../../", "examples/terraform-gcp-ig-example") 23 24 // Setup values for our Terraform apply 25 projectId := gcp.GetGoogleProjectIDFromEnvVar(t) 26 27 region := gcp.GetRandomRegion(t, projectId, RegionsThatSupportF1Micro, nil) 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 }