github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/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 116 func TestAccInstanceGroupManager_updateStrategy(t *testing.T) { 117 var manager compute.InstanceGroupManager 118 igm := fmt.Sprintf("igm-test-%s", acctest.RandString(10)) 119 120 resource.Test(t, resource.TestCase{ 121 PreCheck: func() { testAccPreCheck(t) }, 122 Providers: testAccProviders, 123 CheckDestroy: testAccCheckInstanceGroupManagerDestroy, 124 Steps: []resource.TestStep{ 125 resource.TestStep{ 126 Config: testAccInstanceGroupManager_updateStrategy(igm), 127 Check: resource.ComposeTestCheckFunc( 128 testAccCheckInstanceGroupManagerExists( 129 "google_compute_instance_group_manager.igm-update-strategy", &manager), 130 testAccCheckInstanceGroupManagerUpdateStrategy( 131 "google_compute_instance_group_manager.igm-update-strategy", "NONE"), 132 ), 133 }, 134 }, 135 }) 136 } 137 138 func testAccCheckInstanceGroupManagerDestroy(s *terraform.State) error { 139 config := testAccProvider.Meta().(*Config) 140 141 for _, rs := range s.RootModule().Resources { 142 if rs.Type != "google_compute_instance_group_manager" { 143 continue 144 } 145 _, err := config.clientCompute.InstanceGroupManagers.Get( 146 config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do() 147 if err == nil { 148 return fmt.Errorf("InstanceGroupManager still exists") 149 } 150 } 151 152 return nil 153 } 154 155 func testAccCheckInstanceGroupManagerExists(n string, manager *compute.InstanceGroupManager) resource.TestCheckFunc { 156 return func(s *terraform.State) error { 157 rs, ok := s.RootModule().Resources[n] 158 if !ok { 159 return fmt.Errorf("Not found: %s", n) 160 } 161 162 if rs.Primary.ID == "" { 163 return fmt.Errorf("No ID is set") 164 } 165 166 config := testAccProvider.Meta().(*Config) 167 168 found, err := config.clientCompute.InstanceGroupManagers.Get( 169 config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do() 170 if err != nil { 171 return err 172 } 173 174 if found.Name != rs.Primary.ID { 175 return fmt.Errorf("InstanceGroupManager not found") 176 } 177 178 *manager = *found 179 180 return nil 181 } 182 } 183 184 func testAccCheckInstanceGroupManagerUpdated(n string, size int64, targetPool string, template string) resource.TestCheckFunc { 185 return func(s *terraform.State) error { 186 rs, ok := s.RootModule().Resources[n] 187 if !ok { 188 return fmt.Errorf("Not found: %s", n) 189 } 190 191 if rs.Primary.ID == "" { 192 return fmt.Errorf("No ID is set") 193 } 194 195 config := testAccProvider.Meta().(*Config) 196 197 manager, err := config.clientCompute.InstanceGroupManagers.Get( 198 config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do() 199 if err != nil { 200 return err 201 } 202 203 // Cannot check the target pool as the instance creation is asynchronous. However, can 204 // check the target_size. 205 if manager.TargetSize != size { 206 return fmt.Errorf("instance count incorrect") 207 } 208 209 // check that the instance template updated 210 instanceTemplate, err := config.clientCompute.InstanceTemplates.Get( 211 config.Project, template).Do() 212 if err != nil { 213 return fmt.Errorf("Error reading instance template: %s", err) 214 } 215 216 if instanceTemplate.Name != template { 217 return fmt.Errorf("instance template not updated") 218 } 219 220 return nil 221 } 222 } 223 224 func testAccCheckInstanceGroupManagerNamedPorts(n string, np map[string]int64, instanceGroupManager *compute.InstanceGroupManager) resource.TestCheckFunc { 225 return func(s *terraform.State) error { 226 rs, ok := s.RootModule().Resources[n] 227 if !ok { 228 return fmt.Errorf("Not found: %s", n) 229 } 230 231 if rs.Primary.ID == "" { 232 return fmt.Errorf("No ID is set") 233 } 234 235 config := testAccProvider.Meta().(*Config) 236 237 manager, err := config.clientCompute.InstanceGroupManagers.Get( 238 config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do() 239 if err != nil { 240 return err 241 } 242 243 var found bool 244 for _, namedPort := range manager.NamedPorts { 245 found = false 246 for name, port := range np { 247 if namedPort.Name == name && namedPort.Port == port { 248 found = true 249 } 250 } 251 if !found { 252 return fmt.Errorf("named port incorrect") 253 } 254 } 255 256 return nil 257 } 258 } 259 260 func testAccCheckInstanceGroupManagerTemplateTags(n string, tags []string) resource.TestCheckFunc { 261 return func(s *terraform.State) error { 262 rs, ok := s.RootModule().Resources[n] 263 if !ok { 264 return fmt.Errorf("Not found: %s", n) 265 } 266 267 if rs.Primary.ID == "" { 268 return fmt.Errorf("No ID is set") 269 } 270 271 config := testAccProvider.Meta().(*Config) 272 273 manager, err := config.clientCompute.InstanceGroupManagers.Get( 274 config.Project, rs.Primary.Attributes["zone"], rs.Primary.ID).Do() 275 if err != nil { 276 return err 277 } 278 279 // check that the instance template updated 280 instanceTemplate, err := config.clientCompute.InstanceTemplates.Get( 281 config.Project, resourceSplitter(manager.InstanceTemplate)).Do() 282 if err != nil { 283 return fmt.Errorf("Error reading instance template: %s", err) 284 } 285 286 if !reflect.DeepEqual(instanceTemplate.Properties.Tags.Items, tags) { 287 return fmt.Errorf("instance template not updated") 288 } 289 290 return nil 291 } 292 } 293 294 func testAccCheckInstanceGroupManagerUpdateStrategy(n, strategy string) resource.TestCheckFunc { 295 return func(s *terraform.State) error { 296 rs, ok := s.RootModule().Resources[n] 297 if !ok { 298 return fmt.Errorf("Not found: %s", n) 299 } 300 301 if rs.Primary.ID == "" { 302 return fmt.Errorf("No ID is set") 303 } 304 305 if rs.Primary.Attributes["update_strategy"] != strategy { 306 return fmt.Errorf("Expected strategy to be %s, got %s", 307 strategy, rs.Primary.Attributes["update_strategy"]) 308 } 309 return nil 310 } 311 } 312 313 func testAccInstanceGroupManager_basic(template, target, igm1, igm2 string) string { 314 return fmt.Sprintf(` 315 resource "google_compute_instance_template" "igm-basic" { 316 name = "%s" 317 machine_type = "n1-standard-1" 318 can_ip_forward = false 319 tags = ["foo", "bar"] 320 321 disk { 322 source_image = "debian-cloud/debian-8-jessie-v20160803" 323 auto_delete = true 324 boot = true 325 } 326 327 network_interface { 328 network = "default" 329 } 330 331 metadata { 332 foo = "bar" 333 } 334 335 service_account { 336 scopes = ["userinfo-email", "compute-ro", "storage-ro"] 337 } 338 } 339 340 resource "google_compute_target_pool" "igm-basic" { 341 description = "Resource created for Terraform acceptance testing" 342 name = "%s" 343 session_affinity = "CLIENT_IP_PROTO" 344 } 345 346 resource "google_compute_instance_group_manager" "igm-basic" { 347 description = "Terraform test instance group manager" 348 name = "%s" 349 instance_template = "${google_compute_instance_template.igm-basic.self_link}" 350 target_pools = ["${google_compute_target_pool.igm-basic.self_link}"] 351 base_instance_name = "igm-basic" 352 zone = "us-central1-c" 353 target_size = 2 354 } 355 356 resource "google_compute_instance_group_manager" "igm-no-tp" { 357 description = "Terraform test instance group manager" 358 name = "%s" 359 instance_template = "${google_compute_instance_template.igm-basic.self_link}" 360 base_instance_name = "igm-no-tp" 361 zone = "us-central1-c" 362 target_size = 2 363 } 364 `, template, target, igm1, igm2) 365 } 366 367 func testAccInstanceGroupManager_update(template, target, igm string) string { 368 return fmt.Sprintf(` 369 resource "google_compute_instance_template" "igm-update" { 370 name = "%s" 371 machine_type = "n1-standard-1" 372 can_ip_forward = false 373 tags = ["foo", "bar"] 374 375 disk { 376 source_image = "debian-cloud/debian-8-jessie-v20160803" 377 auto_delete = true 378 boot = true 379 } 380 381 network_interface { 382 network = "default" 383 } 384 385 metadata { 386 foo = "bar" 387 } 388 389 service_account { 390 scopes = ["userinfo-email", "compute-ro", "storage-ro"] 391 } 392 } 393 394 resource "google_compute_target_pool" "igm-update" { 395 description = "Resource created for Terraform acceptance testing" 396 name = "%s" 397 session_affinity = "CLIENT_IP_PROTO" 398 } 399 400 resource "google_compute_instance_group_manager" "igm-update" { 401 description = "Terraform test instance group manager" 402 name = "%s" 403 instance_template = "${google_compute_instance_template.igm-update.self_link}" 404 target_pools = ["${google_compute_target_pool.igm-update.self_link}"] 405 base_instance_name = "igm-update" 406 zone = "us-central1-c" 407 target_size = 2 408 named_port { 409 name = "customhttp" 410 port = 8080 411 } 412 }`, template, target, igm) 413 } 414 415 // Change IGM's instance template and target size 416 func testAccInstanceGroupManager_update2(template1, target, template2, igm string) string { 417 return fmt.Sprintf(` 418 resource "google_compute_instance_template" "igm-update" { 419 name = "%s" 420 machine_type = "n1-standard-1" 421 can_ip_forward = false 422 tags = ["foo", "bar"] 423 424 disk { 425 source_image = "debian-cloud/debian-8-jessie-v20160803" 426 auto_delete = true 427 boot = true 428 } 429 430 network_interface { 431 network = "default" 432 } 433 434 metadata { 435 foo = "bar" 436 } 437 438 service_account { 439 scopes = ["userinfo-email", "compute-ro", "storage-ro"] 440 } 441 } 442 443 resource "google_compute_target_pool" "igm-update" { 444 description = "Resource created for Terraform acceptance testing" 445 name = "%s" 446 session_affinity = "CLIENT_IP_PROTO" 447 } 448 449 resource "google_compute_instance_template" "igm-update2" { 450 name = "%s" 451 machine_type = "n1-standard-1" 452 can_ip_forward = false 453 tags = ["foo", "bar"] 454 455 disk { 456 source_image = "debian-cloud/debian-8-jessie-v20160803" 457 auto_delete = true 458 boot = true 459 } 460 461 network_interface { 462 network = "default" 463 } 464 465 metadata { 466 foo = "bar" 467 } 468 469 service_account { 470 scopes = ["userinfo-email", "compute-ro", "storage-ro"] 471 } 472 } 473 474 resource "google_compute_instance_group_manager" "igm-update" { 475 description = "Terraform test instance group manager" 476 name = "%s" 477 instance_template = "${google_compute_instance_template.igm-update2.self_link}" 478 target_pools = ["${google_compute_target_pool.igm-update.self_link}"] 479 base_instance_name = "igm-update" 480 zone = "us-central1-c" 481 target_size = 3 482 named_port { 483 name = "customhttp" 484 port = 8080 485 } 486 named_port { 487 name = "customhttps" 488 port = 8443 489 } 490 }`, template1, target, template2, igm) 491 } 492 493 func testAccInstanceGroupManager_updateLifecycle(tag, igm string) string { 494 return fmt.Sprintf(` 495 resource "google_compute_instance_template" "igm-update" { 496 machine_type = "n1-standard-1" 497 can_ip_forward = false 498 tags = ["%s"] 499 500 disk { 501 source_image = "debian-cloud/debian-8-jessie-v20160803" 502 auto_delete = true 503 boot = true 504 } 505 506 network_interface { 507 network = "default" 508 } 509 510 service_account { 511 scopes = ["userinfo-email", "compute-ro", "storage-ro"] 512 } 513 514 lifecycle { 515 create_before_destroy = true 516 } 517 } 518 519 resource "google_compute_instance_group_manager" "igm-update" { 520 description = "Terraform test instance group manager" 521 name = "%s" 522 instance_template = "${google_compute_instance_template.igm-update.self_link}" 523 base_instance_name = "igm-update" 524 zone = "us-central1-c" 525 target_size = 2 526 named_port { 527 name = "customhttp" 528 port = 8080 529 } 530 }`, tag, igm) 531 } 532 533 func testAccInstanceGroupManager_updateStrategy(igm string) string { 534 return fmt.Sprintf(` 535 resource "google_compute_instance_template" "igm-update-strategy" { 536 machine_type = "n1-standard-1" 537 can_ip_forward = false 538 tags = ["terraform-testing"] 539 540 disk { 541 source_image = "debian-cloud/debian-8-jessie-v20160803" 542 auto_delete = true 543 boot = true 544 } 545 546 network_interface { 547 network = "default" 548 } 549 550 service_account { 551 scopes = ["userinfo-email", "compute-ro", "storage-ro"] 552 } 553 554 lifecycle { 555 create_before_destroy = true 556 } 557 } 558 559 resource "google_compute_instance_group_manager" "igm-update-strategy" { 560 description = "Terraform test instance group manager" 561 name = "%s" 562 instance_template = "${google_compute_instance_template.igm-update-strategy.self_link}" 563 base_instance_name = "igm-update-strategy" 564 zone = "us-central1-c" 565 target_size = 2 566 update_strategy = "NONE" 567 named_port { 568 name = "customhttp" 569 port = 8080 570 } 571 }`, igm) 572 } 573 574 func resourceSplitter(resource string) string { 575 splits := strings.Split(resource, "/") 576 577 return splits[len(splits)-1] 578 }