github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/google/resource_compute_instance_group_manager_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/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccInstanceGroupManager_basic(t *testing.T) {
    14  	var manager compute.InstanceGroupManager
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckInstanceGroupManagerDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccInstanceGroupManager_basic,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckInstanceGroupManagerExists(
    25  						"google_compute_instance_group_manager.igm-basic", &manager),
    26  					testAccCheckInstanceGroupManagerExists(
    27  						"google_compute_instance_group_manager.igm-no-tp", &manager),
    28  				),
    29  			},
    30  		},
    31  	})
    32  }
    33  
    34  func TestAccInstanceGroupManager_update(t *testing.T) {
    35  	var manager compute.InstanceGroupManager
    36  
    37  	resource.Test(t, resource.TestCase{
    38  		PreCheck:     func() { testAccPreCheck(t) },
    39  		Providers:    testAccProviders,
    40  		CheckDestroy: testAccCheckInstanceGroupManagerDestroy,
    41  		Steps: []resource.TestStep{
    42  			resource.TestStep{
    43  				Config: testAccInstanceGroupManager_update,
    44  				Check: resource.ComposeTestCheckFunc(
    45  					testAccCheckInstanceGroupManagerExists(
    46  						"google_compute_instance_group_manager.igm-update", &manager),
    47  				),
    48  			},
    49  			resource.TestStep{
    50  				Config: testAccInstanceGroupManager_update2,
    51  				Check: resource.ComposeTestCheckFunc(
    52  					testAccCheckInstanceGroupManagerExists(
    53  						"google_compute_instance_group_manager.igm-update", &manager),
    54  					testAccCheckInstanceGroupManagerUpdated(
    55  						"google_compute_instance_group_manager.igm-update", 3,
    56  						"google_compute_target_pool.igm-update", "terraform-test-igm-update2"),
    57  				),
    58  			},
    59  		},
    60  	})
    61  }
    62  
    63  func testAccCheckInstanceGroupManagerDestroy(s *terraform.State) error {
    64  	config := testAccProvider.Meta().(*Config)
    65  
    66  	for _, rs := range s.RootModule().Resources {
    67  		if rs.Type != "google_compute_instance_group_manager" {
    68  			continue
    69  		}
    70  		_, err := config.clientCompute.InstanceGroupManagers.Get(
    71  			config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
    72  		if err != nil {
    73  			return fmt.Errorf("InstanceGroupManager still exists")
    74  		}
    75  	}
    76  
    77  	return nil
    78  }
    79  
    80  func testAccCheckInstanceGroupManagerExists(n string, manager *compute.InstanceGroupManager) resource.TestCheckFunc {
    81  	return func(s *terraform.State) error {
    82  		rs, ok := s.RootModule().Resources[n]
    83  		if !ok {
    84  			return fmt.Errorf("Not found: %s", n)
    85  		}
    86  
    87  		if rs.Primary.ID == "" {
    88  			return fmt.Errorf("No ID is set")
    89  		}
    90  
    91  		config := testAccProvider.Meta().(*Config)
    92  
    93  		found, err := config.clientCompute.InstanceGroupManagers.Get(
    94  			config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
    95  		if err != nil {
    96  			return err
    97  		}
    98  
    99  		if found.Name != rs.Primary.ID {
   100  			return fmt.Errorf("InstanceGroupManager not found")
   101  		}
   102  
   103  		*manager = *found
   104  
   105  		return nil
   106  	}
   107  }
   108  
   109  func testAccCheckInstanceGroupManagerUpdated(n string, size int64, targetPool string, template string) resource.TestCheckFunc {
   110  	return func(s *terraform.State) error {
   111  		rs, ok := s.RootModule().Resources[n]
   112  		if !ok {
   113  			return fmt.Errorf("Not found: %s", n)
   114  		}
   115  
   116  		if rs.Primary.ID == "" {
   117  			return fmt.Errorf("No ID is set")
   118  		}
   119  
   120  		config := testAccProvider.Meta().(*Config)
   121  
   122  		manager, err := config.clientCompute.InstanceGroupManagers.Get(
   123  			config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do()
   124  		if err != nil {
   125  			return err
   126  		}
   127  
   128  		// Cannot check the target pool as the instance creation is asynchronous.  However, can
   129  		// check the target_size.
   130  		if manager.TargetSize != size {
   131  			return fmt.Errorf("instance count incorrect")
   132  		}
   133  
   134  		// check that the instance template updated
   135  		instanceTemplate, err := config.clientCompute.InstanceTemplates.Get(
   136  			config.Project, template).Do()
   137  		if err != nil {
   138  			return fmt.Errorf("Error reading instance template: %s", err)
   139  		}
   140  
   141  		if instanceTemplate.Name != template {
   142  			return fmt.Errorf("instance template not updated")
   143  		}
   144  
   145  		return nil
   146  	}
   147  }
   148  
   149  const testAccInstanceGroupManager_basic = `
   150  resource "google_compute_instance_template" "igm-basic" {
   151  	name = "terraform-test-igm-basic"
   152  	machine_type = "n1-standard-1"
   153  	can_ip_forward = false
   154  	tags = ["foo", "bar"]
   155  
   156  	disk {
   157  		source_image = "debian-cloud/debian-7-wheezy-v20140814"
   158  		auto_delete = true
   159  		boot = true
   160  	}
   161  
   162  	network_interface {
   163  		network = "default"
   164  	}
   165  
   166  	metadata {
   167  		foo = "bar"
   168  	}
   169  
   170  	service_account {
   171  		scopes = ["userinfo-email", "compute-ro", "storage-ro"]
   172  	}
   173  }
   174  
   175  resource "google_compute_target_pool" "igm-basic" {
   176  	description = "Resource created for Terraform acceptance testing"
   177  	name = "terraform-test-igm-basic"
   178  	session_affinity = "CLIENT_IP_PROTO"
   179  }
   180  
   181  resource "google_compute_instance_group_manager" "igm-basic" {
   182  	description = "Terraform test instance group manager"
   183  	name = "terraform-test-igm-basic"
   184  	instance_template = "${google_compute_instance_template.igm-basic.self_link}"
   185  	target_pools = ["${google_compute_target_pool.igm-basic.self_link}"]
   186  	base_instance_name = "igm-basic"
   187  	zone = "us-central1-c"
   188  	target_size = 2
   189  }
   190  
   191  resource "google_compute_instance_group_manager" "igm-no-tp" {
   192  	description = "Terraform test instance group manager"
   193  	name = "terraform-test-igm-no-tp"
   194  	instance_template = "${google_compute_instance_template.igm-basic.self_link}"
   195  	base_instance_name = "igm-no-tp"
   196  	zone = "us-central1-c"
   197  	target_size = 2
   198  }
   199  `
   200  
   201  const testAccInstanceGroupManager_update = `
   202  resource "google_compute_instance_template" "igm-update" {
   203  	name = "terraform-test-igm-update"
   204  	machine_type = "n1-standard-1"
   205  	can_ip_forward = false
   206  	tags = ["foo", "bar"]
   207  
   208  	disk {
   209  		source_image = "debian-cloud/debian-7-wheezy-v20140814"
   210  		auto_delete = true
   211  		boot = true
   212  	}
   213  
   214  	network_interface {
   215  		network = "default"
   216  	}
   217  
   218  	metadata {
   219  		foo = "bar"
   220  	}
   221  
   222  	service_account {
   223  		scopes = ["userinfo-email", "compute-ro", "storage-ro"]
   224  	}
   225  }
   226  
   227  resource "google_compute_target_pool" "igm-update" {
   228  	description = "Resource created for Terraform acceptance testing"
   229  	name = "terraform-test-igm-update"
   230  	session_affinity = "CLIENT_IP_PROTO"
   231  }
   232  
   233  resource "google_compute_instance_group_manager" "igm-update" {
   234  	description = "Terraform test instance group manager"
   235  	name = "terraform-test-igm-update"
   236  	instance_template = "${google_compute_instance_template.igm-update.self_link}"
   237  	target_pools = ["${google_compute_target_pool.igm-update.self_link}"]
   238  	base_instance_name = "igm-update"
   239  	zone = "us-central1-c"
   240  	target_size = 2
   241  }`
   242  
   243  // Change IGM's instance template and target size
   244  const testAccInstanceGroupManager_update2 = `
   245  resource "google_compute_instance_template" "igm-update" {
   246  	name = "terraform-test-igm-update"
   247  	machine_type = "n1-standard-1"
   248  	can_ip_forward = false
   249  	tags = ["foo", "bar"]
   250  
   251  	disk {
   252  		source_image = "debian-cloud/debian-7-wheezy-v20140814"
   253  		auto_delete = true
   254  		boot = true
   255  	}
   256  
   257  	network_interface {
   258  		network = "default"
   259  	}
   260  
   261  	metadata {
   262  		foo = "bar"
   263  	}
   264  
   265  	service_account {
   266  		scopes = ["userinfo-email", "compute-ro", "storage-ro"]
   267  	}
   268  }
   269  
   270  resource "google_compute_target_pool" "igm-update" {
   271  	description = "Resource created for Terraform acceptance testing"
   272  	name = "terraform-test-igm-update"
   273  	session_affinity = "CLIENT_IP_PROTO"
   274  }
   275  
   276  resource "google_compute_instance_template" "igm-update2" {
   277  	name = "terraform-test-igm-update2"
   278  	machine_type = "n1-standard-1"
   279  	can_ip_forward = false
   280  	tags = ["foo", "bar"]
   281  
   282  	disk {
   283  		source_image = "debian-cloud/debian-7-wheezy-v20140814"
   284  		auto_delete = true
   285  		boot = true
   286  	}
   287  
   288  	network_interface {
   289  		network = "default"
   290  	}
   291  
   292  	metadata {
   293  		foo = "bar"
   294  	}
   295  
   296  	service_account {
   297  		scopes = ["userinfo-email", "compute-ro", "storage-ro"]
   298  	}
   299  }
   300  
   301  resource "google_compute_instance_group_manager" "igm-update" {
   302  	description = "Terraform test instance group manager"
   303  	name = "terraform-test-igm-update"
   304  	instance_template = "${google_compute_instance_template.igm-update2.self_link}"
   305  	target_pools = ["${google_compute_target_pool.igm-update.self_link}"]
   306  	base_instance_name = "igm-update"
   307  	zone = "us-central1-c"
   308  	target_size = 3
   309  }`