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