-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Beta support for auto_healing_policies to instance group manager. #249
Changes from 5 commits
819bb9e
400e3ce
0568152
d517a97
8a53eba
7636e6e
fcc4ccf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ import ( | |
"strings" | ||
"testing" | ||
|
||
computeBeta "google.golang.org/api/compute/v0.beta" | ||
"google.golang.org/api/compute/v1" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
|
@@ -185,6 +186,71 @@ func TestAccInstanceGroupManager_separateRegions(t *testing.T) { | |
}) | ||
} | ||
|
||
func TestAccInstanceGroupManager_autoHealingPolicies(t *testing.T) { | ||
var manager computeBeta.InstanceGroupManager | ||
|
||
template := fmt.Sprintf("igm-test-%s", acctest.RandString(10)) | ||
target := fmt.Sprintf("igm-test-%s", acctest.RandString(10)) | ||
igm := fmt.Sprintf("igm-test-%s", acctest.RandString(10)) | ||
hck := fmt.Sprintf("igm-test-%s", acctest.RandString(10)) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckInstanceGroupManagerDestroy, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccInstanceGroupManager_autoHealingPolicies(template, target, igm, hck), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckInstanceGroupManagerBetaExists( | ||
"google_compute_instance_group_manager.igm-basic", &manager), | ||
), | ||
}, | ||
}, | ||
}) | ||
|
||
if len(manager.AutoHealingPolicies) != 1 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you considered moving these in a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
t.Errorf("Expected # of auto healing policies to be 1, got %d", len(manager.AutoHealingPolicies)) | ||
} | ||
autoHealingPolicy := manager.AutoHealingPolicies[0] | ||
|
||
if !strings.Contains(autoHealingPolicy.HealthCheck, hck) { | ||
t.Errorf("Expected string \"%s\" to appear in \"%s\"", hck, autoHealingPolicy.HealthCheck) | ||
} | ||
|
||
if autoHealingPolicy.InitialDelaySec != 10 { | ||
t.Errorf("Expected auto healing policy inital delay to be 10, got %d", autoHealingPolicy.InitialDelaySec) | ||
} | ||
} | ||
|
||
// This test is to make sure that a single version resource can link to a versioned resource | ||
// without perpetual diffs because the self links mismatch. | ||
// Once auto_healing_policies is no longer beta, we will need to use a new field or resource. | ||
func TestAccInstanceGroupManager_selfLinkStability(t *testing.T) { | ||
var manager computeBeta.InstanceGroupManager | ||
|
||
template := fmt.Sprintf("igm-test-%s", acctest.RandString(10)) | ||
target := fmt.Sprintf("igm-test-%s", acctest.RandString(10)) | ||
igm := fmt.Sprintf("igm-test-%s", acctest.RandString(10)) | ||
hck := fmt.Sprintf("igm-test-%s", acctest.RandString(10)) | ||
autoscaler := fmt.Sprintf("igm-test-%s", acctest.RandString(10)) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckInstanceGroupManagerDestroy, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccInstanceGroupManager_selfLinkStability(template, target, igm, hck, autoscaler), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckInstanceGroupManagerBetaExists( | ||
"google_compute_instance_group_manager.igm-basic", &manager), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckInstanceGroupManagerDestroy(s *terraform.State) error { | ||
config := testAccProvider.Meta().(*Config) | ||
|
||
|
@@ -231,6 +297,35 @@ func testAccCheckInstanceGroupManagerExists(n string, manager *compute.InstanceG | |
} | ||
} | ||
|
||
func testAccCheckInstanceGroupManagerBetaExists(n string, manager *computeBeta.InstanceGroupManager) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[n] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", n) | ||
} | ||
|
||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("No ID is set") | ||
} | ||
|
||
config := testAccProvider.Meta().(*Config) | ||
|
||
found, err := config.clientComputeBeta.InstanceGroupManagers.Get( | ||
config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if found.Name != rs.Primary.ID { | ||
return fmt.Errorf("InstanceGroupManager not found") | ||
} | ||
|
||
*manager = *found | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccCheckInstanceGroupManagerUpdated(n string, size int64, targetPool string, template string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[n] | ||
|
@@ -704,6 +799,127 @@ func testAccInstanceGroupManager_separateRegions(igm1, igm2 string) string { | |
`, igm1, igm2) | ||
} | ||
|
||
func testAccInstanceGroupManager_autoHealingPolicies(template, target, igm, hck string) string { | ||
return fmt.Sprintf(` | ||
resource "google_compute_instance_template" "igm-basic" { | ||
name = "%s" | ||
machine_type = "n1-standard-1" | ||
can_ip_forward = false | ||
tags = ["foo", "bar"] | ||
disk { | ||
source_image = "debian-cloud/debian-8-jessie-v20160803" | ||
auto_delete = true | ||
boot = true | ||
} | ||
network_interface { | ||
network = "default" | ||
} | ||
metadata { | ||
foo = "bar" | ||
} | ||
service_account { | ||
scopes = ["userinfo-email", "compute-ro", "storage-ro"] | ||
} | ||
} | ||
|
||
resource "google_compute_target_pool" "igm-basic" { | ||
description = "Resource created for Terraform acceptance testing" | ||
name = "%s" | ||
session_affinity = "CLIENT_IP_PROTO" | ||
} | ||
|
||
resource "google_compute_instance_group_manager" "igm-basic" { | ||
description = "Terraform test instance group manager" | ||
name = "%s" | ||
instance_template = "${google_compute_instance_template.igm-basic.self_link}" | ||
target_pools = ["${google_compute_target_pool.igm-basic.self_link}"] | ||
base_instance_name = "igm-basic" | ||
zone = "us-central1-c" | ||
target_size = 2 | ||
auto_healing_policies { | ||
health_check = "${google_compute_http_health_check.zero.self_link}" | ||
initial_delay_sec = "10" | ||
} | ||
} | ||
|
||
resource "google_compute_http_health_check" "zero" { | ||
name = "%s" | ||
request_path = "/" | ||
check_interval_sec = 1 | ||
timeout_sec = 1 | ||
} | ||
`, template, target, igm, hck) | ||
} | ||
|
||
// This test is to make sure that a single version resource can link to a versioned resource | ||
// without perpetual diffs because the self links mismatch. | ||
// Once auto_healing_policies is no longer beta, we will need to use a new field or resource. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a new field or resource for what? Not super clear. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
func testAccInstanceGroupManager_selfLinkStability(template, target, igm, hck, autoscaler string) string { | ||
return fmt.Sprintf(` | ||
resource "google_compute_instance_template" "igm-basic" { | ||
name = "%s" | ||
machine_type = "n1-standard-1" | ||
can_ip_forward = false | ||
tags = ["foo", "bar"] | ||
disk { | ||
source_image = "debian-cloud/debian-8-jessie-v20160803" | ||
auto_delete = true | ||
boot = true | ||
} | ||
network_interface { | ||
network = "default" | ||
} | ||
metadata { | ||
foo = "bar" | ||
} | ||
service_account { | ||
scopes = ["userinfo-email", "compute-ro", "storage-ro"] | ||
} | ||
} | ||
|
||
resource "google_compute_target_pool" "igm-basic" { | ||
description = "Resource created for Terraform acceptance testing" | ||
name = "%s" | ||
session_affinity = "CLIENT_IP_PROTO" | ||
} | ||
|
||
resource "google_compute_instance_group_manager" "igm-basic" { | ||
description = "Terraform test instance group manager" | ||
name = "%s" | ||
instance_template = "${google_compute_instance_template.igm-basic.self_link}" | ||
target_pools = ["${google_compute_target_pool.igm-basic.self_link}"] | ||
base_instance_name = "igm-basic" | ||
zone = "us-central1-c" | ||
target_size = 2 | ||
auto_healing_policies { | ||
health_check = "${google_compute_http_health_check.zero.self_link}" | ||
initial_delay_sec = "10" | ||
} | ||
} | ||
|
||
resource "google_compute_http_health_check" "zero" { | ||
name = "%s" | ||
request_path = "/" | ||
check_interval_sec = 1 | ||
timeout_sec = 1 | ||
} | ||
|
||
resource "google_compute_autoscaler" "foobar" { | ||
name = "%s" | ||
zone = "us-central1-c" | ||
target = "${google_compute_instance_group_manager.igm-basic.self_link}" | ||
autoscaling_policy = { | ||
max_replicas = 10 | ||
min_replicas = 1 | ||
cooldown_period = 60 | ||
cpu_utilization = { | ||
target = 0.5 | ||
} | ||
} | ||
} | ||
`, template, target, igm, hck, autoscaler) | ||
} | ||
|
||
func resourceSplitter(resource string) string { | ||
splits := strings.Split(resource, "/") | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to use
ComputeTestCheckFunc
if you only have one checkThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.