github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/google/resource_compute_instance_group_manager_test.go (about)

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"strings"
     7  	"testing"
     8  
     9  	"google.golang.org/api/compute/v1"
    10  
    11  	"github.com/hashicorp/terraform/helper/acctest"
    12  	"github.com/hashicorp/terraform/helper/resource"
    13  	"github.com/hashicorp/terraform/terraform"
    14  )
    15  
    16  func TestAccInstanceGroupManager_basic(t *testing.T) {
    17  	var manager compute.InstanceGroupManager
    18  
    19  	template := fmt.Sprintf("igm-test-%s", acctest.RandString(10))
    20  	target := fmt.Sprintf("igm-test-%s", acctest.RandString(10))
    21  	igm1 := fmt.Sprintf("igm-test-%s", acctest.RandString(10))
    22  	igm2 := fmt.Sprintf("igm-test-%s", acctest.RandString(10))
    23  
    24  	resource.Test(t, resource.TestCase{
    25  		PreCheck:     func() { testAccPreCheck(t) },
    26  		Providers:    testAccProviders,
    27  		CheckDestroy: testAccCheckInstanceGroupManagerDestroy,
    28  		Steps: []resource.TestStep{
    29  			resource.TestStep{
    30  				Config: testAccInstanceGroupManager_basic(template, target, igm1, igm2),
    31  				Check: resource.ComposeTestCheckFunc(
    32  					testAccCheckInstanceGroupManagerExists(
    33  						"google_compute_instance_group_manager.igm-basic", &manager),
    34  					testAccCheckInstanceGroupManagerExists(
    35  						"google_compute_instance_group_manager.igm-no-tp", &manager),
    36  				),
    37  			},
    38  		},
    39  	})
    40  }
    41  
    42  func TestAccInstanceGroupManager_update(t *testing.T) {
    43  	var manager compute.InstanceGroupManager
    44  
    45  	template1 := fmt.Sprintf("igm-test-%s", acctest.RandString(10))
    46  	target := fmt.Sprintf("igm-test-%s", acctest.RandString(10))
    47  	template2 := fmt.Sprintf("igm-test-%s", acctest.RandString(10))
    48  	igm := fmt.Sprintf("igm-test-%s", acctest.RandString(10))
    49  
    50  	resource.Test(t, resource.TestCase{
    51  		PreCheck:     func() { testAccPreCheck(t) },
    52  		Providers:    testAccProviders,
    53  		CheckDestroy: testAccCheckInstanceGroupManagerDestroy,
    54  		Steps: []resource.TestStep{
    55  			resource.TestStep{
    56  				Config: testAccInstanceGroupManager_update(template1, target, igm),
    57  				Check: resource.ComposeTestCheckFunc(
    58  					testAccCheckInstanceGroupManagerExists(
    59  						"google_compute_instance_group_manager.igm-update", &manager),
    60  					testAccCheckInstanceGroupManagerNamedPorts(
    61  						"google_compute_instance_group_manager.igm-update",
    62  						map[string]int64{"customhttp": 8080},
    63  						&manager),
    64  				),
    65  			},
    66  			resource.TestStep{
    67  				Config: testAccInstanceGroupManager_update2(template1, target, template2, igm),
    68  				Check: resource.ComposeTestCheckFunc(
    69  					testAccCheckInstanceGroupManagerExists(
    70  						"google_compute_instance_group_manager.igm-update", &manager),
    71  					testAccCheckInstanceGroupManagerUpdated(
    72  						"google_compute_instance_group_manager.igm-update", 3,
    73  						"google_compute_target_pool.igm-update", template2),
    74  					testAccCheckInstanceGroupManagerNamedPorts(
    75  						"google_compute_instance_group_manager.igm-update",
    76  						map[string]int64{"customhttp": 8080, "customhttps": 8443},
    77  						&manager),
    78  				),
    79  			},
    80  		},
    81  	})
    82  }
    83  
    84  func TestAccInstanceGroupManager_updateLifecycle(t *testing.T) {
    85  	var manager compute.InstanceGroupManager
    86  
    87  	tag1 := "tag1"
    88  	tag2 := "tag2"
    89  	igm := fmt.Sprintf("igm-test-%s", acctest.RandString(10))
    90  
    91  	resource.Test(t, resource.TestCase{
    92  		PreCheck:     func() { testAccPreCheck(t) },
    93  		Providers:    testAccProviders,
    94  		CheckDestroy: testAccCheckInstanceGroupManagerDestroy,
    95  		Steps: []resource.TestStep{
    96  			resource.TestStep{
    97  				Config: testAccInstanceGroupManager_updateLifecycle(tag1, igm),
    98  				Check: resource.ComposeTestCheckFunc(
    99  					testAccCheckInstanceGroupManagerExists(
   100  						"google_compute_instance_group_manager.igm-update", &manager),
   101  				),
   102  			},
   103  			resource.TestStep{
   104  				Config: testAccInstanceGroupManager_updateLifecycle(tag2, igm),
   105  				Check: resource.ComposeTestCheckFunc(
   106  					testAccCheckInstanceGroupManagerExists(
   107  						"google_compute_instance_group_manager.igm-update", &manager),
   108  					testAccCheckInstanceGroupManagerTemplateTags(
   109  						"google_compute_instance_group_manager.igm-update", []string{tag2}),
   110  				),
   111  			},
   112  		},
   113  	})
   114  }
   115  func testAccCheckInstanceGroupManagerDestroy(s *terraform.State) error {
   116  	config := testAccProvider.Meta().(*Config)
   117  
   118  	for _, rs := range s.RootModule().Resources {
   119  		if rs.Type != "google_compute_instance_group_manager" {
   120  			continue
   121  		}
   122  		_, err := config.clientCompute.InstanceGroupManagers.Get(
   123  			config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
   124  		if err == nil {
   125  			return fmt.Errorf("InstanceGroupManager still exists")
   126  		}
   127  	}
   128  
   129  	return nil
   130  }
   131  
   132  func testAccCheckInstanceGroupManagerExists(n string, manager *compute.InstanceGroupManager) resource.TestCheckFunc {
   133  	return func(s *terraform.State) error {
   134  		rs, ok := s.RootModule().Resources[n]
   135  		if !ok {
   136  			return fmt.Errorf("Not found: %s", n)
   137  		}
   138  
   139  		if rs.Primary.ID == "" {
   140  			return fmt.Errorf("No ID is set")
   141  		}
   142  
   143  		config := testAccProvider.Meta().(*Config)
   144  
   145  		found, err := config.clientCompute.InstanceGroupManagers.Get(
   146  			config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
   147  		if err != nil {
   148  			return err
   149  		}
   150  
   151  		if found.Name != rs.Primary.ID {
   152  			return fmt.Errorf("InstanceGroupManager not found")
   153  		}
   154  
   155  		*manager = *found
   156  
   157  		return nil
   158  	}
   159  }
   160  
   161  func testAccCheckInstanceGroupManagerUpdated(n string, size int64, targetPool string, template string) resource.TestCheckFunc {
   162  	return func(s *terraform.State) error {
   163  		rs, ok := s.RootModule().Resources[n]
   164  		if !ok {
   165  			return fmt.Errorf("Not found: %s", n)
   166  		}
   167  
   168  		if rs.Primary.ID == "" {
   169  			return fmt.Errorf("No ID is set")
   170  		}
   171  
   172  		config := testAccProvider.Meta().(*Config)
   173  
   174  		manager, err := config.clientCompute.InstanceGroupManagers.Get(
   175  			config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
   176  		if err != nil {
   177  			return err
   178  		}
   179  
   180  		// Cannot check the target pool as the instance creation is asynchronous.  However, can
   181  		// check the target_size.
   182  		if manager.TargetSize != size {
   183  			return fmt.Errorf("instance count incorrect")
   184  		}
   185  
   186  		// check that the instance template updated
   187  		instanceTemplate, err := config.clientCompute.InstanceTemplates.Get(
   188  			config.Project, template).Do()
   189  		if err != nil {
   190  			return fmt.Errorf("Error reading instance template: %s", err)
   191  		}
   192  
   193  		if instanceTemplate.Name != template {
   194  			return fmt.Errorf("instance template not updated")
   195  		}
   196  
   197  		return nil
   198  	}
   199  }
   200  
   201  func testAccCheckInstanceGroupManagerNamedPorts(n string, np map[string]int64, instanceGroupManager *compute.InstanceGroupManager) resource.TestCheckFunc {
   202  	return func(s *terraform.State) error {
   203  		rs, ok := s.RootModule().Resources[n]
   204  		if !ok {
   205  			return fmt.Errorf("Not found: %s", n)
   206  		}
   207  
   208  		if rs.Primary.ID == "" {
   209  			return fmt.Errorf("No ID is set")
   210  		}
   211  
   212  		config := testAccProvider.Meta().(*Config)
   213  
   214  		manager, err := config.clientCompute.InstanceGroupManagers.Get(
   215  			config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
   216  		if err != nil {
   217  			return err
   218  		}
   219  
   220  		var found bool
   221  		for _, namedPort := range manager.NamedPorts {
   222  			found = false
   223  			for name, port := range np {
   224  				if namedPort.Name == name && namedPort.Port == port {
   225  					found = true
   226  				}
   227  			}
   228  			if !found {
   229  				return fmt.Errorf("named port incorrect")
   230  			}
   231  		}
   232  
   233  		return nil
   234  	}
   235  }
   236  
   237  func testAccCheckInstanceGroupManagerTemplateTags(n string, tags []string) resource.TestCheckFunc {
   238  	return func(s *terraform.State) error {
   239  		rs, ok := s.RootModule().Resources[n]
   240  		if !ok {
   241  			return fmt.Errorf("Not found: %s", n)
   242  		}
   243  
   244  		if rs.Primary.ID == "" {
   245  			return fmt.Errorf("No ID is set")
   246  		}
   247  
   248  		config := testAccProvider.Meta().(*Config)
   249  
   250  		manager, err := config.clientCompute.InstanceGroupManagers.Get(
   251  			config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
   252  		if err != nil {
   253  			return err
   254  		}
   255  
   256  		// check that the instance template updated
   257  		instanceTemplate, err := config.clientCompute.InstanceTemplates.Get(
   258  			config.Project, resourceSplitter(manager.InstanceTemplate)).Do()
   259  		if err != nil {
   260  			return fmt.Errorf("Error reading instance template: %s", err)
   261  		}
   262  
   263  		if !reflect.DeepEqual(instanceTemplate.Properties.Tags.Items, tags) {
   264  			return fmt.Errorf("instance template not updated")
   265  		}
   266  
   267  		return nil
   268  	}
   269  }
   270  
   271  func testAccInstanceGroupManager_basic(template, target, igm1, igm2 string) string {
   272  	return fmt.Sprintf(`
   273  	resource "google_compute_instance_template" "igm-basic" {
   274  		name = "%s"
   275  		machine_type = "n1-standard-1"
   276  		can_ip_forward = false
   277  		tags = ["foo", "bar"]
   278  
   279  		disk {
   280  			source_image = "debian-cloud/debian-8-jessie-v20160803"
   281  			auto_delete = true
   282  			boot = true
   283  		}
   284  
   285  		network_interface {
   286  			network = "default"
   287  		}
   288  
   289  		metadata {
   290  			foo = "bar"
   291  		}
   292  
   293  		service_account {
   294  			scopes = ["userinfo-email", "compute-ro", "storage-ro"]
   295  		}
   296  	}
   297  
   298  	resource "google_compute_target_pool" "igm-basic" {
   299  		description = "Resource created for Terraform acceptance testing"
   300  		name = "%s"
   301  		session_affinity = "CLIENT_IP_PROTO"
   302  	}
   303  
   304  	resource "google_compute_instance_group_manager" "igm-basic" {
   305  		description = "Terraform test instance group manager"
   306  		name = "%s"
   307  		instance_template = "${google_compute_instance_template.igm-basic.self_link}"
   308  		target_pools = ["${google_compute_target_pool.igm-basic.self_link}"]
   309  		base_instance_name = "igm-basic"
   310  		zone = "us-central1-c"
   311  		target_size = 2
   312  	}
   313  
   314  	resource "google_compute_instance_group_manager" "igm-no-tp" {
   315  		description = "Terraform test instance group manager"
   316  		name = "%s"
   317  		instance_template = "${google_compute_instance_template.igm-basic.self_link}"
   318  		base_instance_name = "igm-no-tp"
   319  		zone = "us-central1-c"
   320  		target_size = 2
   321  	}
   322  	`, template, target, igm1, igm2)
   323  }
   324  
   325  func testAccInstanceGroupManager_update(template, target, igm string) string {
   326  	return fmt.Sprintf(`
   327  	resource "google_compute_instance_template" "igm-update" {
   328  		name = "%s"
   329  		machine_type = "n1-standard-1"
   330  		can_ip_forward = false
   331  		tags = ["foo", "bar"]
   332  
   333  		disk {
   334  			source_image = "debian-cloud/debian-8-jessie-v20160803"
   335  			auto_delete = true
   336  			boot = true
   337  		}
   338  
   339  		network_interface {
   340  			network = "default"
   341  		}
   342  
   343  		metadata {
   344  			foo = "bar"
   345  		}
   346  
   347  		service_account {
   348  			scopes = ["userinfo-email", "compute-ro", "storage-ro"]
   349  		}
   350  	}
   351  
   352  	resource "google_compute_target_pool" "igm-update" {
   353  		description = "Resource created for Terraform acceptance testing"
   354  		name = "%s"
   355  		session_affinity = "CLIENT_IP_PROTO"
   356  	}
   357  
   358  	resource "google_compute_instance_group_manager" "igm-update" {
   359  		description = "Terraform test instance group manager"
   360  		name = "%s"
   361  		instance_template = "${google_compute_instance_template.igm-update.self_link}"
   362  		target_pools = ["${google_compute_target_pool.igm-update.self_link}"]
   363  		base_instance_name = "igm-update"
   364  		zone = "us-central1-c"
   365  		target_size = 2
   366  		named_port {
   367  			name = "customhttp"
   368  			port = 8080
   369  		}
   370  	}`, template, target, igm)
   371  }
   372  
   373  // Change IGM's instance template and target size
   374  func testAccInstanceGroupManager_update2(template1, target, template2, igm string) string {
   375  	return fmt.Sprintf(`
   376  	resource "google_compute_instance_template" "igm-update" {
   377  		name = "%s"
   378  		machine_type = "n1-standard-1"
   379  		can_ip_forward = false
   380  		tags = ["foo", "bar"]
   381  
   382  		disk {
   383  			source_image = "debian-cloud/debian-8-jessie-v20160803"
   384  			auto_delete = true
   385  			boot = true
   386  		}
   387  
   388  		network_interface {
   389  			network = "default"
   390  		}
   391  
   392  		metadata {
   393  			foo = "bar"
   394  		}
   395  
   396  		service_account {
   397  			scopes = ["userinfo-email", "compute-ro", "storage-ro"]
   398  		}
   399  	}
   400  
   401  	resource "google_compute_target_pool" "igm-update" {
   402  		description = "Resource created for Terraform acceptance testing"
   403  		name = "%s"
   404  		session_affinity = "CLIENT_IP_PROTO"
   405  	}
   406  
   407  	resource "google_compute_instance_template" "igm-update2" {
   408  		name = "%s"
   409  		machine_type = "n1-standard-1"
   410  		can_ip_forward = false
   411  		tags = ["foo", "bar"]
   412  
   413  		disk {
   414  			source_image = "debian-cloud/debian-8-jessie-v20160803"
   415  			auto_delete = true
   416  			boot = true
   417  		}
   418  
   419  		network_interface {
   420  			network = "default"
   421  		}
   422  
   423  		metadata {
   424  			foo = "bar"
   425  		}
   426  
   427  		service_account {
   428  			scopes = ["userinfo-email", "compute-ro", "storage-ro"]
   429  		}
   430  	}
   431  
   432  	resource "google_compute_instance_group_manager" "igm-update" {
   433  		description = "Terraform test instance group manager"
   434  		name = "%s"
   435  		instance_template = "${google_compute_instance_template.igm-update2.self_link}"
   436  		target_pools = ["${google_compute_target_pool.igm-update.self_link}"]
   437  		base_instance_name = "igm-update"
   438  		zone = "us-central1-c"
   439  		target_size = 3
   440  		named_port {
   441  			name = "customhttp"
   442  			port = 8080
   443  		}
   444  		named_port {
   445  			name = "customhttps"
   446  			port = 8443
   447  		}
   448  	}`, template1, target, template2, igm)
   449  }
   450  
   451  func testAccInstanceGroupManager_updateLifecycle(tag, igm string) string {
   452  	return fmt.Sprintf(`
   453  	resource "google_compute_instance_template" "igm-update" {
   454  		machine_type = "n1-standard-1"
   455  		can_ip_forward = false
   456  		tags = ["%s"]
   457  
   458  		disk {
   459  			source_image = "debian-cloud/debian-8-jessie-v20160803"
   460  			auto_delete = true
   461  			boot = true
   462  		}
   463  
   464  		network_interface {
   465  			network = "default"
   466  		}
   467  
   468  		service_account {
   469  			scopes = ["userinfo-email", "compute-ro", "storage-ro"]
   470  		}
   471  
   472  		lifecycle {
   473  			create_before_destroy = true
   474  		}
   475  	}
   476  
   477  	resource "google_compute_instance_group_manager" "igm-update" {
   478  		description = "Terraform test instance group manager"
   479  		name = "%s"
   480  		instance_template = "${google_compute_instance_template.igm-update.self_link}"
   481  		base_instance_name = "igm-update"
   482  		zone = "us-central1-c"
   483  		target_size = 2
   484  		named_port {
   485  			name = "customhttp"
   486  			port = 8080
   487  		}
   488  	}`, tag, igm)
   489  }
   490  
   491  func resourceSplitter(resource string) string {
   492  	splits := strings.Split(resource, "/")
   493  
   494  	return splits[len(splits)-1]
   495  }