github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/resource_aws_alb_target_group_test.go (about) 1 package aws 2 3 import ( 4 "errors" 5 "fmt" 6 "testing" 7 8 "github.com/aws/aws-sdk-go/aws" 9 "github.com/aws/aws-sdk-go/service/elbv2" 10 "github.com/hashicorp/errwrap" 11 "github.com/hashicorp/terraform/helper/acctest" 12 "github.com/hashicorp/terraform/helper/resource" 13 "github.com/hashicorp/terraform/terraform" 14 ) 15 16 func TestAccAWSALBTargetGroup_basic(t *testing.T) { 17 var conf elbv2.TargetGroup 18 targetGroupName := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) 19 20 resource.Test(t, resource.TestCase{ 21 PreCheck: func() { testAccPreCheck(t) }, 22 IDRefreshName: "aws_alb_target_group.test", 23 Providers: testAccProviders, 24 CheckDestroy: testAccCheckAWSALBTargetGroupDestroy, 25 Steps: []resource.TestStep{ 26 { 27 Config: testAccAWSALBTargetGroupConfig_basic(targetGroupName), 28 Check: resource.ComposeAggregateTestCheckFunc( 29 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf), 30 resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "arn"), 31 resource.TestCheckResourceAttr("aws_alb_target_group.test", "name", targetGroupName), 32 resource.TestCheckResourceAttr("aws_alb_target_group.test", "port", "443"), 33 resource.TestCheckResourceAttr("aws_alb_target_group.test", "protocol", "HTTPS"), 34 resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "vpc_id"), 35 resource.TestCheckResourceAttr("aws_alb_target_group.test", "deregistration_delay", "200"), 36 resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.#", "1"), 37 resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.type", "lb_cookie"), 38 resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.cookie_duration", "10000"), 39 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.#", "1"), 40 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.path", "/health"), 41 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.interval", "60"), 42 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.port", "8081"), 43 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.protocol", "HTTP"), 44 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.timeout", "3"), 45 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.healthy_threshold", "3"), 46 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.unhealthy_threshold", "3"), 47 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.matcher", "200-299"), 48 ), 49 }, 50 }, 51 }) 52 } 53 54 func TestAccAWSALBTargetGroup_changeNameForceNew(t *testing.T) { 55 var before, after elbv2.TargetGroup 56 targetGroupNameBefore := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) 57 targetGroupNameAfter := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(4, acctest.CharSetAlphaNum)) 58 59 resource.Test(t, resource.TestCase{ 60 PreCheck: func() { testAccPreCheck(t) }, 61 IDRefreshName: "aws_alb_target_group.test", 62 Providers: testAccProviders, 63 CheckDestroy: testAccCheckAWSALBTargetGroupDestroy, 64 Steps: []resource.TestStep{ 65 { 66 Config: testAccAWSALBTargetGroupConfig_basic(targetGroupNameBefore), 67 Check: resource.ComposeAggregateTestCheckFunc( 68 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &before), 69 resource.TestCheckResourceAttr("aws_alb_target_group.test", "name", targetGroupNameBefore), 70 ), 71 }, 72 { 73 Config: testAccAWSALBTargetGroupConfig_basic(targetGroupNameAfter), 74 Check: resource.ComposeAggregateTestCheckFunc( 75 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &after), 76 resource.TestCheckResourceAttr("aws_alb_target_group.test", "name", targetGroupNameAfter), 77 ), 78 }, 79 }, 80 }) 81 } 82 83 func TestAccAWSALBTargetGroup_changeProtocolForceNew(t *testing.T) { 84 var before, after elbv2.TargetGroup 85 targetGroupName := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) 86 87 resource.Test(t, resource.TestCase{ 88 PreCheck: func() { testAccPreCheck(t) }, 89 IDRefreshName: "aws_alb_target_group.test", 90 Providers: testAccProviders, 91 CheckDestroy: testAccCheckAWSALBTargetGroupDestroy, 92 Steps: []resource.TestStep{ 93 { 94 Config: testAccAWSALBTargetGroupConfig_basic(targetGroupName), 95 Check: resource.ComposeAggregateTestCheckFunc( 96 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &before), 97 resource.TestCheckResourceAttr("aws_alb_target_group.test", "protocol", "HTTPS"), 98 ), 99 }, 100 { 101 Config: testAccAWSALBTargetGroupConfig_updatedProtocol(targetGroupName), 102 Check: resource.ComposeAggregateTestCheckFunc( 103 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &after), 104 resource.TestCheckResourceAttr("aws_alb_target_group.test", "protocol", "HTTP"), 105 ), 106 }, 107 }, 108 }) 109 } 110 111 func TestAccAWSALBTargetGroup_changePortForceNew(t *testing.T) { 112 var before, after elbv2.TargetGroup 113 targetGroupName := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) 114 115 resource.Test(t, resource.TestCase{ 116 PreCheck: func() { testAccPreCheck(t) }, 117 IDRefreshName: "aws_alb_target_group.test", 118 Providers: testAccProviders, 119 CheckDestroy: testAccCheckAWSALBTargetGroupDestroy, 120 Steps: []resource.TestStep{ 121 { 122 Config: testAccAWSALBTargetGroupConfig_basic(targetGroupName), 123 Check: resource.ComposeAggregateTestCheckFunc( 124 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &before), 125 resource.TestCheckResourceAttr("aws_alb_target_group.test", "port", "443"), 126 ), 127 }, 128 { 129 Config: testAccAWSALBTargetGroupConfig_updatedPort(targetGroupName), 130 Check: resource.ComposeAggregateTestCheckFunc( 131 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &after), 132 resource.TestCheckResourceAttr("aws_alb_target_group.test", "port", "442"), 133 ), 134 }, 135 }, 136 }) 137 } 138 139 func TestAccAWSALBTargetGroup_changeVpcForceNew(t *testing.T) { 140 var before, after elbv2.TargetGroup 141 targetGroupName := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) 142 143 resource.Test(t, resource.TestCase{ 144 PreCheck: func() { testAccPreCheck(t) }, 145 IDRefreshName: "aws_alb_target_group.test", 146 Providers: testAccProviders, 147 CheckDestroy: testAccCheckAWSALBTargetGroupDestroy, 148 Steps: []resource.TestStep{ 149 { 150 Config: testAccAWSALBTargetGroupConfig_basic(targetGroupName), 151 Check: resource.ComposeAggregateTestCheckFunc( 152 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &before), 153 ), 154 }, 155 { 156 Config: testAccAWSALBTargetGroupConfig_updatedVpc(targetGroupName), 157 Check: resource.ComposeAggregateTestCheckFunc( 158 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &after), 159 ), 160 }, 161 }, 162 }) 163 } 164 165 func TestAccAWSALBTargetGroup_tags(t *testing.T) { 166 var conf elbv2.TargetGroup 167 targetGroupName := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) 168 169 resource.Test(t, resource.TestCase{ 170 PreCheck: func() { testAccPreCheck(t) }, 171 IDRefreshName: "aws_alb_target_group.test", 172 Providers: testAccProviders, 173 CheckDestroy: testAccCheckAWSALBTargetGroupDestroy, 174 Steps: []resource.TestStep{ 175 { 176 Config: testAccAWSALBTargetGroupConfig_basic(targetGroupName), 177 Check: resource.ComposeAggregateTestCheckFunc( 178 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf), 179 resource.TestCheckResourceAttr("aws_alb_target_group.test", "tags.%", "1"), 180 resource.TestCheckResourceAttr("aws_alb_target_group.test", "tags.TestName", "TestAccAWSALBTargetGroup_basic"), 181 ), 182 }, 183 { 184 Config: testAccAWSALBTargetGroupConfig_updateTags(targetGroupName), 185 Check: resource.ComposeAggregateTestCheckFunc( 186 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf), 187 resource.TestCheckResourceAttr("aws_alb_target_group.test", "tags.%", "2"), 188 resource.TestCheckResourceAttr("aws_alb_target_group.test", "tags.Environment", "Production"), 189 resource.TestCheckResourceAttr("aws_alb_target_group.test", "tags.Type", "ALB Target Group"), 190 ), 191 }, 192 }, 193 }) 194 } 195 196 func TestAccAWSALBTargetGroup_updateHealthCheck(t *testing.T) { 197 var conf elbv2.TargetGroup 198 targetGroupName := fmt.Sprintf("test-target-group-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)) 199 200 resource.Test(t, resource.TestCase{ 201 PreCheck: func() { testAccPreCheck(t) }, 202 IDRefreshName: "aws_alb_target_group.test", 203 Providers: testAccProviders, 204 CheckDestroy: testAccCheckAWSALBTargetGroupDestroy, 205 Steps: []resource.TestStep{ 206 { 207 Config: testAccAWSALBTargetGroupConfig_basic(targetGroupName), 208 Check: resource.ComposeAggregateTestCheckFunc( 209 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf), 210 resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "arn"), 211 resource.TestCheckResourceAttr("aws_alb_target_group.test", "name", targetGroupName), 212 resource.TestCheckResourceAttr("aws_alb_target_group.test", "port", "443"), 213 resource.TestCheckResourceAttr("aws_alb_target_group.test", "protocol", "HTTPS"), 214 resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "vpc_id"), 215 resource.TestCheckResourceAttr("aws_alb_target_group.test", "deregistration_delay", "200"), 216 resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.#", "1"), 217 resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.type", "lb_cookie"), 218 resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.cookie_duration", "10000"), 219 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.#", "1"), 220 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.path", "/health"), 221 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.interval", "60"), 222 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.port", "8081"), 223 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.protocol", "HTTP"), 224 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.timeout", "3"), 225 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.healthy_threshold", "3"), 226 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.unhealthy_threshold", "3"), 227 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.matcher", "200-299"), 228 ), 229 }, 230 { 231 Config: testAccAWSALBTargetGroupConfig_updateHealthCheck(targetGroupName), 232 Check: resource.ComposeAggregateTestCheckFunc( 233 testAccCheckAWSALBTargetGroupExists("aws_alb_target_group.test", &conf), 234 resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "arn"), 235 resource.TestCheckResourceAttr("aws_alb_target_group.test", "name", targetGroupName), 236 resource.TestCheckResourceAttr("aws_alb_target_group.test", "port", "443"), 237 resource.TestCheckResourceAttr("aws_alb_target_group.test", "protocol", "HTTPS"), 238 resource.TestCheckResourceAttrSet("aws_alb_target_group.test", "vpc_id"), 239 resource.TestCheckResourceAttr("aws_alb_target_group.test", "deregistration_delay", "200"), 240 resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.#", "1"), 241 resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.type", "lb_cookie"), 242 resource.TestCheckResourceAttr("aws_alb_target_group.test", "stickiness.0.cookie_duration", "10000"), 243 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.#", "1"), 244 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.path", "/health2"), 245 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.interval", "30"), 246 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.port", "8082"), 247 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.protocol", "HTTPS"), 248 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.timeout", "4"), 249 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.healthy_threshold", "4"), 250 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.unhealthy_threshold", "4"), 251 resource.TestCheckResourceAttr("aws_alb_target_group.test", "health_check.0.matcher", "200"), 252 ), 253 }, 254 }, 255 }) 256 } 257 258 func testAccCheckAWSALBTargetGroupExists(n string, res *elbv2.TargetGroup) resource.TestCheckFunc { 259 return func(s *terraform.State) error { 260 rs, ok := s.RootModule().Resources[n] 261 if !ok { 262 return fmt.Errorf("Not found: %s", n) 263 } 264 265 if rs.Primary.ID == "" { 266 return errors.New("No Target Group ID is set") 267 } 268 269 conn := testAccProvider.Meta().(*AWSClient).elbv2conn 270 271 describe, err := conn.DescribeTargetGroups(&elbv2.DescribeTargetGroupsInput{ 272 TargetGroupArns: []*string{aws.String(rs.Primary.ID)}, 273 }) 274 275 if err != nil { 276 return err 277 } 278 279 if len(describe.TargetGroups) != 1 || 280 *describe.TargetGroups[0].TargetGroupArn != rs.Primary.ID { 281 return errors.New("Target Group not found") 282 } 283 284 *res = *describe.TargetGroups[0] 285 return nil 286 } 287 } 288 289 func testAccCheckAWSALBTargetGroupDestroy(s *terraform.State) error { 290 conn := testAccProvider.Meta().(*AWSClient).elbv2conn 291 292 for _, rs := range s.RootModule().Resources { 293 if rs.Type != "aws_alb_target_group" { 294 continue 295 } 296 297 describe, err := conn.DescribeTargetGroups(&elbv2.DescribeTargetGroupsInput{ 298 TargetGroupArns: []*string{aws.String(rs.Primary.ID)}, 299 }) 300 301 if err == nil { 302 if len(describe.TargetGroups) != 0 && 303 *describe.TargetGroups[0].TargetGroupArn == rs.Primary.ID { 304 return fmt.Errorf("Target Group %q still exists", rs.Primary.ID) 305 } 306 } 307 308 // Verify the error 309 if isTargetGroupNotFound(err) { 310 return nil 311 } else { 312 return errwrap.Wrapf("Unexpected error checking ALB destroyed: {{err}}", err) 313 } 314 } 315 316 return nil 317 } 318 319 func testAccAWSALBTargetGroupConfig_basic(targetGroupName string) string { 320 return fmt.Sprintf(`resource "aws_alb_target_group" "test" { 321 name = "%s" 322 port = 443 323 protocol = "HTTPS" 324 vpc_id = "${aws_vpc.test.id}" 325 326 deregistration_delay = 200 327 328 stickiness { 329 type = "lb_cookie" 330 cookie_duration = 10000 331 } 332 333 health_check { 334 path = "/health" 335 interval = 60 336 port = 8081 337 protocol = "HTTP" 338 timeout = 3 339 healthy_threshold = 3 340 unhealthy_threshold = 3 341 matcher = "200-299" 342 } 343 344 tags { 345 TestName = "TestAccAWSALBTargetGroup_basic" 346 } 347 } 348 349 resource "aws_vpc" "test" { 350 cidr_block = "10.0.0.0/16" 351 352 tags { 353 TestName = "TestAccAWSALBTargetGroup_basic" 354 } 355 }`, targetGroupName) 356 } 357 358 func testAccAWSALBTargetGroupConfig_updatedPort(targetGroupName string) string { 359 return fmt.Sprintf(`resource "aws_alb_target_group" "test" { 360 name = "%s" 361 port = 442 362 protocol = "HTTPS" 363 vpc_id = "${aws_vpc.test.id}" 364 365 deregistration_delay = 200 366 367 stickiness { 368 type = "lb_cookie" 369 cookie_duration = 10000 370 } 371 372 health_check { 373 path = "/health" 374 interval = 60 375 port = 8081 376 protocol = "HTTP" 377 timeout = 3 378 healthy_threshold = 3 379 unhealthy_threshold = 3 380 matcher = "200-299" 381 } 382 383 tags { 384 TestName = "TestAccAWSALBTargetGroup_basic" 385 } 386 } 387 388 resource "aws_vpc" "test" { 389 cidr_block = "10.0.0.0/16" 390 391 tags { 392 TestName = "TestAccAWSALBTargetGroup_basic" 393 } 394 }`, targetGroupName) 395 } 396 397 func testAccAWSALBTargetGroupConfig_updatedProtocol(targetGroupName string) string { 398 return fmt.Sprintf(`resource "aws_alb_target_group" "test" { 399 name = "%s" 400 port = 443 401 protocol = "HTTP" 402 vpc_id = "${aws_vpc.test2.id}" 403 404 deregistration_delay = 200 405 406 stickiness { 407 type = "lb_cookie" 408 cookie_duration = 10000 409 } 410 411 health_check { 412 path = "/health" 413 interval = 60 414 port = 8081 415 protocol = "HTTP" 416 timeout = 3 417 healthy_threshold = 3 418 unhealthy_threshold = 3 419 matcher = "200-299" 420 } 421 422 tags { 423 TestName = "TestAccAWSALBTargetGroup_basic" 424 } 425 } 426 427 resource "aws_vpc" "test2" { 428 cidr_block = "10.10.0.0/16" 429 430 tags { 431 TestName = "TestAccAWSALBTargetGroup_basic" 432 } 433 } 434 435 resource "aws_vpc" "test" { 436 cidr_block = "10.0.0.0/16" 437 438 tags { 439 TestName = "TestAccAWSALBTargetGroup_basic" 440 } 441 }`, targetGroupName) 442 } 443 444 func testAccAWSALBTargetGroupConfig_updatedVpc(targetGroupName string) string { 445 return fmt.Sprintf(`resource "aws_alb_target_group" "test" { 446 name = "%s" 447 port = 443 448 protocol = "HTTPS" 449 vpc_id = "${aws_vpc.test.id}" 450 451 deregistration_delay = 200 452 453 stickiness { 454 type = "lb_cookie" 455 cookie_duration = 10000 456 } 457 458 health_check { 459 path = "/health" 460 interval = 60 461 port = 8081 462 protocol = "HTTP" 463 timeout = 3 464 healthy_threshold = 3 465 unhealthy_threshold = 3 466 matcher = "200-299" 467 } 468 469 tags { 470 TestName = "TestAccAWSALBTargetGroup_basic" 471 } 472 } 473 474 resource "aws_vpc" "test" { 475 cidr_block = "10.0.0.0/16" 476 477 tags { 478 TestName = "TestAccAWSALBTargetGroup_basic" 479 } 480 }`, targetGroupName) 481 } 482 483 func testAccAWSALBTargetGroupConfig_updateTags(targetGroupName string) string { 484 return fmt.Sprintf(`resource "aws_alb_target_group" "test" { 485 name = "%s" 486 port = 443 487 protocol = "HTTPS" 488 vpc_id = "${aws_vpc.test.id}" 489 490 deregistration_delay = 200 491 492 stickiness { 493 type = "lb_cookie" 494 cookie_duration = 10000 495 } 496 497 health_check { 498 path = "/health" 499 interval = 60 500 port = 8081 501 protocol = "HTTP" 502 timeout = 3 503 healthy_threshold = 3 504 unhealthy_threshold = 3 505 matcher = "200-299" 506 } 507 508 tags { 509 Environment = "Production" 510 Type = "ALB Target Group" 511 } 512 } 513 514 resource "aws_vpc" "test" { 515 cidr_block = "10.0.0.0/16" 516 517 tags { 518 TestName = "TestAccAWSALBTargetGroup_basic" 519 } 520 }`, targetGroupName) 521 } 522 523 func testAccAWSALBTargetGroupConfig_updateHealthCheck(targetGroupName string) string { 524 return fmt.Sprintf(`resource "aws_alb_target_group" "test" { 525 name = "%s" 526 port = 443 527 protocol = "HTTPS" 528 vpc_id = "${aws_vpc.test.id}" 529 530 deregistration_delay = 200 531 532 stickiness { 533 type = "lb_cookie" 534 cookie_duration = 10000 535 } 536 537 health_check { 538 path = "/health2" 539 interval = 30 540 port = 8082 541 protocol = "HTTPS" 542 timeout = 4 543 healthy_threshold = 4 544 unhealthy_threshold = 4 545 matcher = "200" 546 } 547 } 548 549 resource "aws_vpc" "test" { 550 cidr_block = "10.0.0.0/16" 551 552 tags { 553 TestName = "TestAccAWSALBTargetGroup_basic" 554 } 555 }`, targetGroupName) 556 }