github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/google/resource_compute_instance_group_test.go (about)

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"google.golang.org/api/compute/v1"
     8  
     9  	"github.com/hashicorp/terraform/helper/acctest"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestAccComputeInstanceGroup_basic(t *testing.T) {
    15  	var instanceGroup compute.InstanceGroup
    16  	var instanceName = fmt.Sprintf("instancegroup-test-%s", acctest.RandString(10))
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testAccComputeInstanceGroup_destroy,
    22  		Steps: []resource.TestStep{
    23  			resource.TestStep{
    24  				Config: testAccComputeInstanceGroup_basic(instanceName),
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccComputeInstanceGroup_exists(
    27  						"google_compute_instance_group.basic", &instanceGroup),
    28  					testAccComputeInstanceGroup_exists(
    29  						"google_compute_instance_group.empty", &instanceGroup),
    30  				),
    31  			},
    32  		},
    33  	})
    34  }
    35  
    36  func TestAccComputeInstanceGroup_update(t *testing.T) {
    37  	var instanceGroup compute.InstanceGroup
    38  	var instanceName = fmt.Sprintf("instancegroup-test-%s", acctest.RandString(10))
    39  
    40  	resource.Test(t, resource.TestCase{
    41  		PreCheck:     func() { testAccPreCheck(t) },
    42  		Providers:    testAccProviders,
    43  		CheckDestroy: testAccComputeInstanceGroup_destroy,
    44  		Steps: []resource.TestStep{
    45  			resource.TestStep{
    46  				Config: testAccComputeInstanceGroup_update(instanceName),
    47  				Check: resource.ComposeTestCheckFunc(
    48  					testAccComputeInstanceGroup_exists(
    49  						"google_compute_instance_group.update", &instanceGroup),
    50  					testAccComputeInstanceGroup_named_ports(
    51  						"google_compute_instance_group.update",
    52  						map[string]int64{"http": 8080, "https": 8443},
    53  						&instanceGroup),
    54  				),
    55  			},
    56  			resource.TestStep{
    57  				Config: testAccComputeInstanceGroup_update2(instanceName),
    58  				Check: resource.ComposeTestCheckFunc(
    59  					testAccComputeInstanceGroup_exists(
    60  						"google_compute_instance_group.update", &instanceGroup),
    61  					testAccComputeInstanceGroup_updated(
    62  						"google_compute_instance_group.update", 3, &instanceGroup),
    63  					testAccComputeInstanceGroup_named_ports(
    64  						"google_compute_instance_group.update",
    65  						map[string]int64{"http": 8081, "test": 8444},
    66  						&instanceGroup),
    67  				),
    68  			},
    69  		},
    70  	})
    71  }
    72  
    73  func TestAccComputeInstanceGroup_outOfOrderInstances(t *testing.T) {
    74  	var instanceGroup compute.InstanceGroup
    75  	var instanceName = fmt.Sprintf("instancegroup-test-%s", acctest.RandString(10))
    76  
    77  	resource.Test(t, resource.TestCase{
    78  		PreCheck:     func() { testAccPreCheck(t) },
    79  		Providers:    testAccProviders,
    80  		CheckDestroy: testAccComputeInstanceGroup_destroy,
    81  		Steps: []resource.TestStep{
    82  			resource.TestStep{
    83  				Config: testAccComputeInstanceGroup_outOfOrderInstances(instanceName),
    84  				Check: resource.ComposeTestCheckFunc(
    85  					testAccComputeInstanceGroup_exists(
    86  						"google_compute_instance_group.group", &instanceGroup),
    87  				),
    88  			},
    89  		},
    90  	})
    91  }
    92  
    93  func testAccComputeInstanceGroup_destroy(s *terraform.State) error {
    94  	config := testAccProvider.Meta().(*Config)
    95  
    96  	for _, rs := range s.RootModule().Resources {
    97  		if rs.Type != "google_compute_instance_group" {
    98  			continue
    99  		}
   100  		_, err := config.clientCompute.InstanceGroups.Get(
   101  			config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
   102  		if err == nil {
   103  			return fmt.Errorf("InstanceGroup still exists")
   104  		}
   105  	}
   106  
   107  	return nil
   108  }
   109  
   110  func testAccComputeInstanceGroup_exists(n string, instanceGroup *compute.InstanceGroup) resource.TestCheckFunc {
   111  	return func(s *terraform.State) error {
   112  		rs, ok := s.RootModule().Resources[n]
   113  		if !ok {
   114  			return fmt.Errorf("Not found: %s", n)
   115  		}
   116  
   117  		if rs.Primary.ID == "" {
   118  			return fmt.Errorf("No ID is set")
   119  		}
   120  
   121  		config := testAccProvider.Meta().(*Config)
   122  
   123  		found, err := config.clientCompute.InstanceGroups.Get(
   124  			config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
   125  		if err != nil {
   126  			return err
   127  		}
   128  
   129  		if found.Name != rs.Primary.ID {
   130  			return fmt.Errorf("InstanceGroup not found")
   131  		}
   132  
   133  		*instanceGroup = *found
   134  
   135  		return nil
   136  	}
   137  }
   138  
   139  func testAccComputeInstanceGroup_updated(n string, size int64, instanceGroup *compute.InstanceGroup) resource.TestCheckFunc {
   140  	return func(s *terraform.State) error {
   141  		rs, ok := s.RootModule().Resources[n]
   142  		if !ok {
   143  			return fmt.Errorf("Not found: %s", n)
   144  		}
   145  
   146  		if rs.Primary.ID == "" {
   147  			return fmt.Errorf("No ID is set")
   148  		}
   149  
   150  		config := testAccProvider.Meta().(*Config)
   151  
   152  		instanceGroup, err := config.clientCompute.InstanceGroups.Get(
   153  			config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
   154  		if err != nil {
   155  			return err
   156  		}
   157  
   158  		// Cannot check the target pool as the instance creation is asynchronous.  However, can
   159  		// check the target_size.
   160  		if instanceGroup.Size != size {
   161  			return fmt.Errorf("instance count incorrect")
   162  		}
   163  
   164  		return nil
   165  	}
   166  }
   167  
   168  func testAccComputeInstanceGroup_named_ports(n string, np map[string]int64, instanceGroup *compute.InstanceGroup) resource.TestCheckFunc {
   169  	return func(s *terraform.State) error {
   170  		rs, ok := s.RootModule().Resources[n]
   171  		if !ok {
   172  			return fmt.Errorf("Not found: %s", n)
   173  		}
   174  
   175  		if rs.Primary.ID == "" {
   176  			return fmt.Errorf("No ID is set")
   177  		}
   178  
   179  		config := testAccProvider.Meta().(*Config)
   180  
   181  		instanceGroup, err := config.clientCompute.InstanceGroups.Get(
   182  			config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
   183  		if err != nil {
   184  			return err
   185  		}
   186  
   187  		var found bool
   188  		for _, namedPort := range instanceGroup.NamedPorts {
   189  			found = false
   190  			for name, port := range np {
   191  				if namedPort.Name == name && namedPort.Port == port {
   192  					found = true
   193  				}
   194  			}
   195  			if !found {
   196  				return fmt.Errorf("named port incorrect")
   197  			}
   198  		}
   199  
   200  		return nil
   201  	}
   202  }
   203  
   204  func testAccComputeInstanceGroup_basic(instance string) string {
   205  	return fmt.Sprintf(`
   206  	resource "google_compute_instance" "ig_instance" {
   207  		name = "%s"
   208  		machine_type = "n1-standard-1"
   209  		can_ip_forward = false
   210  		zone = "us-central1-c"
   211  
   212  		disk {
   213  			image = "debian-8-jessie-v20160803"
   214  		}
   215  
   216  		network_interface {
   217  			network = "default"
   218  		}
   219  	}
   220  
   221  	resource "google_compute_instance_group" "basic" {
   222  		description = "Terraform test instance group"
   223  		name = "%s"
   224  		zone = "us-central1-c"
   225  		instances = [ "${google_compute_instance.ig_instance.self_link}" ]
   226  		named_port {
   227  			name = "http"
   228  			port = "8080"
   229  		}
   230  		named_port {
   231  			name = "https"
   232  			port = "8443"
   233  		}
   234  	}
   235  
   236  	resource "google_compute_instance_group" "empty" {
   237  		description = "Terraform test instance group empty"
   238  		name = "%s-empty"
   239  		zone = "us-central1-c"
   240  			named_port {
   241  			name = "http"
   242  			port = "8080"
   243  		}
   244  		named_port {
   245  			name = "https"
   246  			port = "8443"
   247  		}
   248  	}`, instance, instance, instance)
   249  }
   250  
   251  func testAccComputeInstanceGroup_update(instance string) string {
   252  	return fmt.Sprintf(`
   253  	resource "google_compute_instance" "ig_instance" {
   254  		name = "%s-${count.index}"
   255  		machine_type = "n1-standard-1"
   256  		can_ip_forward = false
   257  		zone = "us-central1-c"
   258  		count = 1
   259  
   260  		disk {
   261  			image = "debian-8-jessie-v20160803"
   262  		}
   263  
   264  		network_interface {
   265  			network = "default"
   266  		}
   267  	}
   268  
   269  	resource "google_compute_instance_group" "update" {
   270  		description = "Terraform test instance group"
   271  		name = "%s"
   272  		zone = "us-central1-c"
   273  		instances = [ "${google_compute_instance.ig_instance.self_link}" ]
   274  		named_port {
   275  			name = "http"
   276  			port = "8080"
   277  		}
   278  		named_port {
   279  			name = "https"
   280  			port = "8443"
   281  		}
   282  	}`, instance, instance)
   283  }
   284  
   285  // Change IGM's instance template and target size
   286  func testAccComputeInstanceGroup_update2(instance string) string {
   287  	return fmt.Sprintf(`
   288  	resource "google_compute_instance" "ig_instance" {
   289  		name = "%s-${count.index}"
   290  		machine_type = "n1-standard-1"
   291  		can_ip_forward = false
   292  		zone = "us-central1-c"
   293  		count = 3
   294  
   295  		disk {
   296  			image = "debian-8-jessie-v20160803"
   297  		}
   298  
   299  		network_interface {
   300  			network = "default"
   301  		}
   302  	}
   303  
   304  	resource "google_compute_instance_group" "update" {
   305  		description = "Terraform test instance group"
   306  		name = "%s"
   307  		zone = "us-central1-c"
   308  		instances = [ "${google_compute_instance.ig_instance.*.self_link}" ]
   309  
   310  		named_port {
   311  			name = "http"
   312  			port = "8081"
   313  		}
   314  		named_port {
   315  			name = "test"
   316  			port = "8444"
   317  		}
   318  	}`, instance, instance)
   319  }
   320  
   321  func testAccComputeInstanceGroup_outOfOrderInstances(instance string) string {
   322  	return fmt.Sprintf(`
   323  	resource "google_compute_instance" "ig_instance" {
   324  		name = "%s-1"
   325  		machine_type = "n1-standard-1"
   326  		can_ip_forward = false
   327  		zone = "us-central1-c"
   328  
   329  		disk {
   330  			image = "debian-8-jessie-v20160803"
   331  		}
   332  
   333  		network_interface {
   334  			network = "default"
   335  		}
   336  	}
   337  
   338  	resource "google_compute_instance" "ig_instance_2" {
   339  		name = "%s-2"
   340  		machine_type = "n1-standard-1"
   341  		can_ip_forward = false
   342  		zone = "us-central1-c"
   343  
   344  		disk {
   345  			image = "debian-8-jessie-v20160803"
   346  		}
   347  
   348  		network_interface {
   349  			network = "default"
   350  		}
   351  	}
   352  
   353  	resource "google_compute_instance_group" "group" {
   354  		description = "Terraform test instance group"
   355  		name = "%s"
   356  		zone = "us-central1-c"
   357  		instances = [ "${google_compute_instance.ig_instance_2.self_link}", "${google_compute_instance.ig_instance.self_link}" ]
   358  		named_port {
   359  			name = "http"
   360  			port = "8080"
   361  		}
   362  		named_port {
   363  			name = "https"
   364  			port = "8443"
   365  		}
   366  	}`, instance, instance, instance)
   367  }