github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/google/resource_compute_autoscaler_test.go (about)

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/acctest"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  	"google.golang.org/api/compute/v1"
    11  )
    12  
    13  func TestAccComputeAutoscaler_basic(t *testing.T) {
    14  	var ascaler compute.Autoscaler
    15  
    16  	var it_name = fmt.Sprintf("autoscaler-test-%s", acctest.RandString(10))
    17  	var tp_name = fmt.Sprintf("autoscaler-test-%s", acctest.RandString(10))
    18  	var igm_name = fmt.Sprintf("autoscaler-test-%s", acctest.RandString(10))
    19  	var autoscaler_name = fmt.Sprintf("autoscaler-test-%s", acctest.RandString(10))
    20  
    21  	resource.Test(t, resource.TestCase{
    22  		PreCheck:     func() { testAccPreCheck(t) },
    23  		Providers:    testAccProviders,
    24  		CheckDestroy: testAccCheckComputeAutoscalerDestroy,
    25  		Steps: []resource.TestStep{
    26  			resource.TestStep{
    27  				Config: testAccComputeAutoscaler_basic(it_name, tp_name, igm_name, autoscaler_name),
    28  				Check: resource.ComposeTestCheckFunc(
    29  					testAccCheckComputeAutoscalerExists(
    30  						"google_compute_autoscaler.foobar", &ascaler),
    31  				),
    32  			},
    33  		},
    34  	})
    35  }
    36  
    37  func TestAccComputeAutoscaler_update(t *testing.T) {
    38  	var ascaler compute.Autoscaler
    39  
    40  	var it_name = fmt.Sprintf("autoscaler-test-%s", acctest.RandString(10))
    41  	var tp_name = fmt.Sprintf("autoscaler-test-%s", acctest.RandString(10))
    42  	var igm_name = fmt.Sprintf("autoscaler-test-%s", acctest.RandString(10))
    43  	var autoscaler_name = fmt.Sprintf("autoscaler-test-%s", acctest.RandString(10))
    44  
    45  	resource.Test(t, resource.TestCase{
    46  		PreCheck:     func() { testAccPreCheck(t) },
    47  		Providers:    testAccProviders,
    48  		CheckDestroy: testAccCheckComputeAutoscalerDestroy,
    49  		Steps: []resource.TestStep{
    50  			resource.TestStep{
    51  				Config: testAccComputeAutoscaler_basic(it_name, tp_name, igm_name, autoscaler_name),
    52  				Check: resource.ComposeTestCheckFunc(
    53  					testAccCheckComputeAutoscalerExists(
    54  						"google_compute_autoscaler.foobar", &ascaler),
    55  				),
    56  			},
    57  			resource.TestStep{
    58  				Config: testAccComputeAutoscaler_update(it_name, tp_name, igm_name, autoscaler_name),
    59  				Check: resource.ComposeTestCheckFunc(
    60  					testAccCheckComputeAutoscalerExists(
    61  						"google_compute_autoscaler.foobar", &ascaler),
    62  					testAccCheckComputeAutoscalerUpdated(
    63  						"google_compute_autoscaler.foobar", 10),
    64  				),
    65  			},
    66  		},
    67  	})
    68  }
    69  
    70  func testAccCheckComputeAutoscalerDestroy(s *terraform.State) error {
    71  	config := testAccProvider.Meta().(*Config)
    72  
    73  	for _, rs := range s.RootModule().Resources {
    74  		if rs.Type != "google_compute_autoscaler" {
    75  			continue
    76  		}
    77  
    78  		_, err := config.clientCompute.Autoscalers.Get(
    79  			config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
    80  		if err == nil {
    81  			return fmt.Errorf("Autoscaler still exists")
    82  		}
    83  	}
    84  
    85  	return nil
    86  }
    87  
    88  func testAccCheckComputeAutoscalerExists(n string, ascaler *compute.Autoscaler) resource.TestCheckFunc {
    89  	return func(s *terraform.State) error {
    90  		rs, ok := s.RootModule().Resources[n]
    91  		if !ok {
    92  			return fmt.Errorf("Not found: %s", n)
    93  		}
    94  
    95  		if rs.Primary.ID == "" {
    96  			return fmt.Errorf("No ID is set")
    97  		}
    98  
    99  		config := testAccProvider.Meta().(*Config)
   100  
   101  		found, err := config.clientCompute.Autoscalers.Get(
   102  			config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
   103  		if err != nil {
   104  			return err
   105  		}
   106  
   107  		if found.Name != rs.Primary.ID {
   108  			return fmt.Errorf("Autoscaler not found")
   109  		}
   110  
   111  		*ascaler = *found
   112  
   113  		return nil
   114  	}
   115  }
   116  
   117  func testAccCheckComputeAutoscalerUpdated(n string, max int64) resource.TestCheckFunc {
   118  	return func(s *terraform.State) error {
   119  		rs, ok := s.RootModule().Resources[n]
   120  		if !ok {
   121  			return fmt.Errorf("Not found: %s", n)
   122  		}
   123  
   124  		if rs.Primary.ID == "" {
   125  			return fmt.Errorf("No ID is set")
   126  		}
   127  
   128  		config := testAccProvider.Meta().(*Config)
   129  
   130  		ascaler, err := config.clientCompute.Autoscalers.Get(
   131  			config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
   132  		if err != nil {
   133  			return err
   134  		}
   135  
   136  		if ascaler.AutoscalingPolicy.MaxNumReplicas != max {
   137  			return fmt.Errorf("maximum replicas incorrect")
   138  		}
   139  
   140  		return nil
   141  	}
   142  }
   143  
   144  func testAccComputeAutoscaler_basic(it_name, tp_name, igm_name, autoscaler_name string) string {
   145  	return fmt.Sprintf(`
   146  resource "google_compute_instance_template" "foobar" {
   147  	name = "%s"
   148  	machine_type = "n1-standard-1"
   149  	can_ip_forward = false
   150  	tags = ["foo", "bar"]
   151  
   152  	disk {
   153  		source_image = "debian-cloud/debian-8-jessie-v20160803"
   154  		auto_delete = true
   155  		boot = true
   156  	}
   157  
   158  	network_interface {
   159  		network = "default"
   160  	}
   161  
   162  	metadata {
   163  		foo = "bar"
   164  	}
   165  
   166  	service_account {
   167  		scopes = ["userinfo-email", "compute-ro", "storage-ro"]
   168  	}
   169  }
   170  
   171  resource "google_compute_target_pool" "foobar" {
   172  	description = "Resource created for Terraform acceptance testing"
   173  	name = "%s"
   174  	session_affinity = "CLIENT_IP_PROTO"
   175  }
   176  
   177  resource "google_compute_instance_group_manager" "foobar" {
   178  	description = "Terraform test instance group manager"
   179  	name = "%s"
   180  	instance_template = "${google_compute_instance_template.foobar.self_link}"
   181  	target_pools = ["${google_compute_target_pool.foobar.self_link}"]
   182  	base_instance_name = "foobar"
   183  	zone = "us-central1-a"
   184  }
   185  
   186  resource "google_compute_autoscaler" "foobar" {
   187  	description = "Resource created for Terraform acceptance testing"
   188  	name = "%s"
   189  	zone = "us-central1-a"
   190  	target = "${google_compute_instance_group_manager.foobar.self_link}"
   191  	autoscaling_policy = {
   192  		max_replicas = 5
   193  		min_replicas = 1
   194  		cooldown_period = 60
   195  		cpu_utilization = {
   196  			target = 0.5
   197  		}
   198  	}
   199  
   200  }
   201  `, it_name, tp_name, igm_name, autoscaler_name)
   202  }
   203  
   204  func testAccComputeAutoscaler_update(it_name, tp_name, igm_name, autoscaler_name string) string {
   205  	return fmt.Sprintf(`
   206  resource "google_compute_instance_template" "foobar" {
   207  	name = "%s"
   208  	machine_type = "n1-standard-1"
   209  	can_ip_forward = false
   210  	tags = ["foo", "bar"]
   211  
   212  	disk {
   213  		source_image = "debian-cloud/debian-8-jessie-v20160803"
   214  		auto_delete = true
   215  		boot = true
   216  	}
   217  
   218  	network_interface {
   219  		network = "default"
   220  	}
   221  
   222  	metadata {
   223  		foo = "bar"
   224  	}
   225  
   226  	service_account {
   227  		scopes = ["userinfo-email", "compute-ro", "storage-ro"]
   228  	}
   229  }
   230  
   231  resource "google_compute_target_pool" "foobar" {
   232  	description = "Resource created for Terraform acceptance testing"
   233  	name = "%s"
   234  	session_affinity = "CLIENT_IP_PROTO"
   235  }
   236  
   237  resource "google_compute_instance_group_manager" "foobar" {
   238  	description = "Terraform test instance group manager"
   239  	name = "%s"
   240  	instance_template = "${google_compute_instance_template.foobar.self_link}"
   241  	target_pools = ["${google_compute_target_pool.foobar.self_link}"]
   242  	base_instance_name = "foobar"
   243  	zone = "us-central1-a"
   244  }
   245  
   246  resource "google_compute_autoscaler" "foobar" {
   247  	description = "Resource created for Terraform acceptance testing"
   248  	name = "%s"
   249  	zone = "us-central1-a"
   250  	target = "${google_compute_instance_group_manager.foobar.self_link}"
   251  	autoscaling_policy = {
   252  		max_replicas = 10
   253  		min_replicas = 1
   254  		cooldown_period = 60
   255  		cpu_utilization = {
   256  			target = 0.5
   257  		}
   258  	}
   259  
   260  }
   261  `, it_name, tp_name, igm_name, autoscaler_name)
   262  }