2015-03-04 10:15:26 +00:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2016-01-05 21:47:10 +00:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2015-03-04 10:15:26 +00:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2015-08-14 11:06:06 +00:00
|
|
|
"google.golang.org/api/compute/v1"
|
2015-03-04 10:15:26 +00:00
|
|
|
)
|
|
|
|
|
2017-06-06 12:20:29 +00:00
|
|
|
func TestAccComputeAutoscaler_basic(t *testing.T) {
|
2017-10-12 22:07:29 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2015-07-28 00:47:10 +00:00
|
|
|
var ascaler compute.Autoscaler
|
2015-03-04 10:15:26 +00:00
|
|
|
|
2017-06-06 12:20:29 +00:00
|
|
|
var it_name = fmt.Sprintf("autoscaler-test-%s", acctest.RandString(10))
|
|
|
|
var tp_name = fmt.Sprintf("autoscaler-test-%s", acctest.RandString(10))
|
|
|
|
var igm_name = fmt.Sprintf("autoscaler-test-%s", acctest.RandString(10))
|
|
|
|
var autoscaler_name = fmt.Sprintf("autoscaler-test-%s", acctest.RandString(10))
|
|
|
|
|
2015-03-04 10:15:26 +00:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
2017-06-06 12:20:29 +00:00
|
|
|
CheckDestroy: testAccCheckComputeAutoscalerDestroy,
|
2015-03-04 10:15:26 +00:00
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2017-06-06 12:20:29 +00:00
|
|
|
Config: testAccComputeAutoscaler_basic(it_name, tp_name, igm_name, autoscaler_name),
|
2015-03-04 10:15:26 +00:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2017-06-06 12:20:29 +00:00
|
|
|
testAccCheckComputeAutoscalerExists(
|
2015-07-28 00:47:10 +00:00
|
|
|
"google_compute_autoscaler.foobar", &ascaler),
|
2015-03-04 10:15:26 +00:00
|
|
|
),
|
|
|
|
},
|
2017-12-19 20:43:30 +00:00
|
|
|
|
|
|
|
resource.TestStep{
|
|
|
|
ResourceName: "google_compute_autoscaler.foobar",
|
|
|
|
ImportState: true,
|
|
|
|
ImportStateVerify: true,
|
|
|
|
},
|
2015-03-04 10:15:26 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-06-06 12:20:29 +00:00
|
|
|
func TestAccComputeAutoscaler_update(t *testing.T) {
|
2017-10-12 22:07:29 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2015-07-28 00:47:10 +00:00
|
|
|
var ascaler compute.Autoscaler
|
2015-03-04 10:15:26 +00:00
|
|
|
|
2017-06-06 12:20:29 +00:00
|
|
|
var it_name = fmt.Sprintf("autoscaler-test-%s", acctest.RandString(10))
|
|
|
|
var tp_name = fmt.Sprintf("autoscaler-test-%s", acctest.RandString(10))
|
|
|
|
var igm_name = fmt.Sprintf("autoscaler-test-%s", acctest.RandString(10))
|
|
|
|
var autoscaler_name = fmt.Sprintf("autoscaler-test-%s", acctest.RandString(10))
|
|
|
|
|
2015-03-04 10:15:26 +00:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
2017-06-06 12:20:29 +00:00
|
|
|
CheckDestroy: testAccCheckComputeAutoscalerDestroy,
|
2015-03-04 10:15:26 +00:00
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2017-06-06 12:20:29 +00:00
|
|
|
Config: testAccComputeAutoscaler_basic(it_name, tp_name, igm_name, autoscaler_name),
|
2015-03-04 10:15:26 +00:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2017-06-06 12:20:29 +00:00
|
|
|
testAccCheckComputeAutoscalerExists(
|
2015-07-28 00:47:10 +00:00
|
|
|
"google_compute_autoscaler.foobar", &ascaler),
|
2015-03-04 10:15:26 +00:00
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
2017-06-06 12:20:29 +00:00
|
|
|
Config: testAccComputeAutoscaler_update(it_name, tp_name, igm_name, autoscaler_name),
|
2015-03-04 10:15:26 +00:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2017-06-06 12:20:29 +00:00
|
|
|
testAccCheckComputeAutoscalerExists(
|
2015-07-28 00:47:10 +00:00
|
|
|
"google_compute_autoscaler.foobar", &ascaler),
|
2017-06-06 12:20:29 +00:00
|
|
|
testAccCheckComputeAutoscalerUpdated(
|
2015-07-28 00:47:10 +00:00
|
|
|
"google_compute_autoscaler.foobar", 10),
|
2015-03-04 10:15:26 +00:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-01-17 01:10:13 +00:00
|
|
|
func TestAccComputeAutoscaler_multicondition(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
var it_name = fmt.Sprintf("autoscaler-test-%s", acctest.RandString(10))
|
|
|
|
var tp_name = fmt.Sprintf("autoscaler-test-%s", acctest.RandString(10))
|
|
|
|
var igm_name = fmt.Sprintf("autoscaler-test-%s", acctest.RandString(10))
|
|
|
|
var autoscaler_name = fmt.Sprintf("autoscaler-test-%s", acctest.RandString(10))
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckComputeAutoscalerDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccComputeAutoscaler_multicondition(it_name, tp_name, igm_name, autoscaler_name),
|
|
|
|
Check: testAccCheckComputeAutoscalerMultifunction("google_compute_autoscaler.foobar"),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
ResourceName: "google_compute_autoscaler.foobar",
|
|
|
|
ImportState: true,
|
|
|
|
ImportStateVerify: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-06-06 12:20:29 +00:00
|
|
|
func testAccCheckComputeAutoscalerDestroy(s *terraform.State) error {
|
2015-03-04 10:15:26 +00:00
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
2015-07-28 00:47:10 +00:00
|
|
|
if rs.Type != "google_compute_autoscaler" {
|
2015-03-04 10:15:26 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-07-28 00:47:10 +00:00
|
|
|
_, err := config.clientCompute.Autoscalers.Get(
|
2015-03-04 10:15:26 +00:00
|
|
|
config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
|
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("Autoscaler still exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-06 12:20:29 +00:00
|
|
|
func testAccCheckComputeAutoscalerExists(n string, ascaler *compute.Autoscaler) resource.TestCheckFunc {
|
2015-03-04 10:15:26 +00:00
|
|
|
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)
|
|
|
|
|
2015-07-28 00:47:10 +00:00
|
|
|
found, err := config.clientCompute.Autoscalers.Get(
|
2015-03-04 10:15:26 +00:00
|
|
|
config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if found.Name != rs.Primary.ID {
|
|
|
|
return fmt.Errorf("Autoscaler not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
*ascaler = *found
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-17 01:10:13 +00:00
|
|
|
func testAccCheckComputeAutoscalerMultifunction(n string) 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.clientCompute.Autoscalers.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("Autoscaler not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
if found.AutoscalingPolicy.CpuUtilization.UtilizationTarget == 0.5 && found.AutoscalingPolicy.LoadBalancingUtilization.UtilizationTarget == 0.5 {
|
|
|
|
return nil
|
|
|
|
}
|
2018-01-17 17:52:20 +00:00
|
|
|
return fmt.Errorf("Util target for CPU: %f, for LB: %f - should have been 0.5 for each.",
|
2018-01-17 01:10:13 +00:00
|
|
|
found.AutoscalingPolicy.CpuUtilization.UtilizationTarget,
|
|
|
|
found.AutoscalingPolicy.LoadBalancingUtilization.UtilizationTarget)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-06 12:20:29 +00:00
|
|
|
func testAccCheckComputeAutoscalerUpdated(n string, max int64) resource.TestCheckFunc {
|
2015-03-04 10:15:26 +00:00
|
|
|
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)
|
|
|
|
|
2015-07-28 00:47:10 +00:00
|
|
|
ascaler, err := config.clientCompute.Autoscalers.Get(
|
2015-03-04 10:15:26 +00:00
|
|
|
config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ascaler.AutoscalingPolicy.MaxNumReplicas != max {
|
|
|
|
return fmt.Errorf("maximum replicas incorrect")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-17 01:10:13 +00:00
|
|
|
func testAccComputeAutoscaler_scaffolding(it_name, tp_name, igm_name string) string {
|
2017-06-06 12:20:29 +00:00
|
|
|
return fmt.Sprintf(`
|
2015-03-04 10:15:26 +00:00
|
|
|
resource "google_compute_instance_template" "foobar" {
|
2017-06-06 12:20:29 +00:00
|
|
|
name = "%s"
|
2015-03-04 10:15:26 +00:00
|
|
|
machine_type = "n1-standard-1"
|
|
|
|
can_ip_forward = false
|
|
|
|
tags = ["foo", "bar"]
|
|
|
|
|
2017-10-01 02:42:15 +00:00
|
|
|
disk {
|
2016-08-25 13:39:03 +00:00
|
|
|
source_image = "debian-cloud/debian-8-jessie-v20160803"
|
2015-03-04 10:15:26 +00:00
|
|
|
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" "foobar" {
|
|
|
|
description = "Resource created for Terraform acceptance testing"
|
2017-06-06 12:20:29 +00:00
|
|
|
name = "%s"
|
2015-03-04 10:15:26 +00:00
|
|
|
session_affinity = "CLIENT_IP_PROTO"
|
|
|
|
}
|
|
|
|
|
2015-07-28 00:47:10 +00:00
|
|
|
resource "google_compute_instance_group_manager" "foobar" {
|
2015-03-04 10:15:26 +00:00
|
|
|
description = "Terraform test instance group manager"
|
2017-06-06 12:20:29 +00:00
|
|
|
name = "%s"
|
2015-03-04 10:15:26 +00:00
|
|
|
instance_template = "${google_compute_instance_template.foobar.self_link}"
|
|
|
|
target_pools = ["${google_compute_target_pool.foobar.self_link}"]
|
|
|
|
base_instance_name = "foobar"
|
|
|
|
zone = "us-central1-a"
|
|
|
|
}
|
2018-01-17 01:10:13 +00:00
|
|
|
`, it_name, tp_name, igm_name)
|
|
|
|
|
|
|
|
}
|
2015-03-04 10:15:26 +00:00
|
|
|
|
2018-01-17 01:10:13 +00:00
|
|
|
func testAccComputeAutoscaler_basic(it_name, tp_name, igm_name, autoscaler_name string) string {
|
|
|
|
return testAccComputeAutoscaler_scaffolding(it_name, tp_name, igm_name) + fmt.Sprintf(`
|
2015-07-28 00:47:10 +00:00
|
|
|
resource "google_compute_autoscaler" "foobar" {
|
2015-03-04 10:15:26 +00:00
|
|
|
description = "Resource created for Terraform acceptance testing"
|
2017-06-06 12:20:29 +00:00
|
|
|
name = "%s"
|
2015-03-04 10:15:26 +00:00
|
|
|
zone = "us-central1-a"
|
2015-07-28 00:47:10 +00:00
|
|
|
target = "${google_compute_instance_group_manager.foobar.self_link}"
|
2015-03-04 10:15:26 +00:00
|
|
|
autoscaling_policy = {
|
|
|
|
max_replicas = 5
|
2016-08-04 20:14:43 +00:00
|
|
|
min_replicas = 1
|
2015-03-04 10:15:26 +00:00
|
|
|
cooldown_period = 60
|
|
|
|
cpu_utilization = {
|
|
|
|
target = 0.5
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-06 12:20:29 +00:00
|
|
|
}
|
2018-01-17 01:10:13 +00:00
|
|
|
`, autoscaler_name)
|
2017-06-06 12:20:29 +00:00
|
|
|
}
|
2015-03-04 10:15:26 +00:00
|
|
|
|
2017-06-06 12:20:29 +00:00
|
|
|
func testAccComputeAutoscaler_update(it_name, tp_name, igm_name, autoscaler_name string) string {
|
2018-01-17 01:10:13 +00:00
|
|
|
return testAccComputeAutoscaler_scaffolding(it_name, tp_name, igm_name) + fmt.Sprintf(`
|
|
|
|
resource "google_compute_autoscaler" "foobar" {
|
|
|
|
description = "Resource created for Terraform acceptance testing"
|
2017-06-06 12:20:29 +00:00
|
|
|
name = "%s"
|
2018-01-17 01:10:13 +00:00
|
|
|
zone = "us-central1-a"
|
|
|
|
target = "${google_compute_instance_group_manager.foobar.self_link}"
|
|
|
|
autoscaling_policy = {
|
|
|
|
max_replicas = 10
|
|
|
|
min_replicas = 1
|
|
|
|
cooldown_period = 60
|
|
|
|
cpu_utilization = {
|
|
|
|
target = 0.5
|
|
|
|
}
|
2015-03-04 10:15:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-01-17 01:10:13 +00:00
|
|
|
`, autoscaler_name)
|
2015-03-04 10:15:26 +00:00
|
|
|
}
|
|
|
|
|
2018-01-17 01:10:13 +00:00
|
|
|
func testAccComputeAutoscaler_multicondition(it_name, tp_name, igm_name, autoscaler_name string) string {
|
|
|
|
return testAccComputeAutoscaler_scaffolding(it_name, tp_name, igm_name) + fmt.Sprintf(`
|
2015-07-28 00:47:10 +00:00
|
|
|
resource "google_compute_autoscaler" "foobar" {
|
2015-03-04 10:15:26 +00:00
|
|
|
description = "Resource created for Terraform acceptance testing"
|
2017-06-06 12:20:29 +00:00
|
|
|
name = "%s"
|
2015-03-04 10:15:26 +00:00
|
|
|
zone = "us-central1-a"
|
2015-07-28 00:47:10 +00:00
|
|
|
target = "${google_compute_instance_group_manager.foobar.self_link}"
|
2015-03-04 10:15:26 +00:00
|
|
|
autoscaling_policy = {
|
|
|
|
max_replicas = 10
|
2016-08-04 20:14:43 +00:00
|
|
|
min_replicas = 1
|
2015-03-04 10:15:26 +00:00
|
|
|
cooldown_period = 60
|
|
|
|
cpu_utilization = {
|
|
|
|
target = 0.5
|
|
|
|
}
|
2018-01-17 01:10:13 +00:00
|
|
|
load_balancing_utilization = {
|
|
|
|
target = 0.5
|
|
|
|
}
|
|
|
|
metric {
|
|
|
|
name = "compute.googleapis.com/instance/network/received_bytes_count"
|
|
|
|
target = 75
|
|
|
|
type = "GAUGE"
|
|
|
|
}
|
|
|
|
metric {
|
|
|
|
name = "compute.googleapis.com/instance/network/sent_bytes_count"
|
|
|
|
target = 50
|
|
|
|
type = "GAUGE"
|
|
|
|
}
|
2015-03-04 10:15:26 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 12:20:29 +00:00
|
|
|
}
|
2018-01-17 01:10:13 +00:00
|
|
|
`, autoscaler_name)
|
2017-06-06 12:20:29 +00:00
|
|
|
}
|