forked from gruntwork-io/terratest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterraform_gcp_ig_example_test.go
80 lines (60 loc) · 2.68 KB
/
terraform_gcp_ig_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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package test
import (
"fmt"
"testing"
"time"
"github.com/gruntwork-io/terratest/modules/gcp"
"github.com/gruntwork-io/terratest/modules/retry"
"github.com/gruntwork-io/terratest/modules/terraform"
"github.com/gruntwork-io/terratest/modules/test-structure"
)
func TestTerraformGcpInstanceGroupExample(t *testing.T) {
t.Parallel()
exampleDir := test_structure.CopyTerraformFolderToTemp(t, "../", "examples/terraform-gcp-ig-example")
// Setup values for our Terraform apply
projectId := gcp.GetGoogleProjectIDFromEnvVar(t)
// On October 22, 2018, GCP launched the asia-east2 region, which promptly failed all our tests, so blacklist asia-east2.
region := gcp.GetRandomRegion(t, projectId, nil, []string{"asia-east2"})
randomValidGcpName := gcp.RandomValidGcpName()
cluster_size := 3
terraformOptions := &terraform.Options{
// The path to where our Terraform code instances located
TerraformDir: exampleDir,
// Variables to pass to our Terraform code using -var options
Vars: map[string]interface{}{
"gcp_project_id": projectId,
"gcp_region": region,
"cluster_name": randomValidGcpName,
},
}
// At the end of the test, run `terraform destroy` to clean up any resources that were created
defer terraform.Destroy(t, terraformOptions)
// This will run `terraform init` and `terraform apply` and fail the test if there are any errors
terraform.InitAndApply(t, terraformOptions)
instance_group_name := terraform.Output(t, terraformOptions, "instance_group_name")
instanceGroup := gcp.FetchRegionalInstanceGroup(t, projectId, region, instance_group_name)
// Validate that GetInstances() returns a non-zero number of Instances
maxRetries := 40
sleepBetweenRetries := 3 * time.Second
retry.DoWithRetry(t, "Attempting to fetch Instances from Instance Group", maxRetries, sleepBetweenRetries, func() (string, error) {
instances, err := instanceGroup.GetInstancesE(t, projectId)
if err != nil {
return "", fmt.Errorf("Failed to get Instances: %s", err)
}
if len(instances) != cluster_size {
return "", fmt.Errorf("Expected to find exactly %d Compute Instances in Instance Group but found %d.", cluster_size, len(instances))
}
return "", nil
})
// Validate that we get the right number of IP addresses
retry.DoWithRetry(t, "Attempting to fetch Public IP addresses from Instance Group", maxRetries, sleepBetweenRetries, func() (string, error) {
ips, err := instanceGroup.GetPublicIpsE(t, projectId)
if err != nil {
return "", fmt.Errorf("Failed to get public IPs from Instance Group")
}
if len(ips) != cluster_size {
return "", fmt.Errorf("Expected to get exactly %d public IP addresses but found %d.", cluster_size, len(ips))
}
return "", nil
})
}