github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/aws/resource_aws_ecs_task_definition_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/aws/aws-sdk-go/service/ecs" 9 "github.com/hashicorp/terraform/helper/acctest" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestAccAWSEcsTaskDefinition_basic(t *testing.T) { 15 var def ecs.TaskDefinition 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { testAccPreCheck(t) }, 18 Providers: testAccProviders, 19 CheckDestroy: testAccCheckAWSEcsTaskDefinitionDestroy, 20 Steps: []resource.TestStep{ 21 { 22 Config: testAccAWSEcsTaskDefinition, 23 Check: resource.ComposeTestCheckFunc( 24 testAccCheckAWSEcsTaskDefinitionExists("aws_ecs_task_definition.jenkins", &def), 25 ), 26 }, 27 { 28 Config: testAccAWSEcsTaskDefinitionModified, 29 Check: resource.ComposeTestCheckFunc( 30 testAccCheckAWSEcsTaskDefinitionExists("aws_ecs_task_definition.jenkins", &def), 31 ), 32 }, 33 }, 34 }) 35 } 36 37 // Regression for https://github.com/hashicorp/terraform/issues/2370 38 func TestAccAWSEcsTaskDefinition_withScratchVolume(t *testing.T) { 39 var def ecs.TaskDefinition 40 resource.Test(t, resource.TestCase{ 41 PreCheck: func() { testAccPreCheck(t) }, 42 Providers: testAccProviders, 43 CheckDestroy: testAccCheckAWSEcsTaskDefinitionDestroy, 44 Steps: []resource.TestStep{ 45 { 46 Config: testAccAWSEcsTaskDefinitionWithScratchVolume, 47 Check: resource.ComposeTestCheckFunc( 48 testAccCheckAWSEcsTaskDefinitionExists("aws_ecs_task_definition.sleep", &def), 49 ), 50 }, 51 }, 52 }) 53 } 54 55 // Regression for https://github.com/hashicorp/terraform/issues/2694 56 func TestAccAWSEcsTaskDefinition_withEcsService(t *testing.T) { 57 var def ecs.TaskDefinition 58 resource.Test(t, resource.TestCase{ 59 PreCheck: func() { testAccPreCheck(t) }, 60 Providers: testAccProviders, 61 CheckDestroy: testAccCheckAWSEcsTaskDefinitionDestroy, 62 Steps: []resource.TestStep{ 63 { 64 Config: testAccAWSEcsTaskDefinitionWithEcsService, 65 Check: resource.ComposeTestCheckFunc( 66 testAccCheckAWSEcsTaskDefinitionExists("aws_ecs_task_definition.sleep", &def), 67 testAccCheckAWSEcsServiceExists("aws_ecs_service.sleep-svc"), 68 ), 69 }, 70 { 71 Config: testAccAWSEcsTaskDefinitionWithEcsServiceModified, 72 Check: resource.ComposeTestCheckFunc( 73 testAccCheckAWSEcsTaskDefinitionExists("aws_ecs_task_definition.sleep", &def), 74 testAccCheckAWSEcsServiceExists("aws_ecs_service.sleep-svc"), 75 ), 76 }, 77 }, 78 }) 79 } 80 81 func TestAccAWSEcsTaskDefinition_withTaskRoleArn(t *testing.T) { 82 var def ecs.TaskDefinition 83 rInt := acctest.RandInt() 84 resource.Test(t, resource.TestCase{ 85 PreCheck: func() { testAccPreCheck(t) }, 86 Providers: testAccProviders, 87 CheckDestroy: testAccCheckAWSEcsTaskDefinitionDestroy, 88 Steps: []resource.TestStep{ 89 { 90 Config: testAccAWSEcsTaskDefinitionWithTaskRoleArn(rInt), 91 Check: resource.ComposeTestCheckFunc( 92 testAccCheckAWSEcsTaskDefinitionExists("aws_ecs_task_definition.sleep", &def), 93 ), 94 }, 95 }, 96 }) 97 } 98 99 func TestAccAWSEcsTaskDefinition_withNetworkMode(t *testing.T) { 100 var def ecs.TaskDefinition 101 rInt := acctest.RandInt() 102 resource.Test(t, resource.TestCase{ 103 PreCheck: func() { testAccPreCheck(t) }, 104 Providers: testAccProviders, 105 CheckDestroy: testAccCheckAWSEcsTaskDefinitionDestroy, 106 Steps: []resource.TestStep{ 107 { 108 Config: testAccAWSEcsTaskDefinitionWithNetworkMode(rInt), 109 Check: resource.ComposeTestCheckFunc( 110 testAccCheckAWSEcsTaskDefinitionExists("aws_ecs_task_definition.sleep", &def), 111 resource.TestCheckResourceAttr( 112 "aws_ecs_task_definition.sleep", "network_mode", "bridge"), 113 ), 114 }, 115 }, 116 }) 117 } 118 119 func TestAccAWSEcsTaskDefinition_constraint(t *testing.T) { 120 var def ecs.TaskDefinition 121 resource.Test(t, resource.TestCase{ 122 PreCheck: func() { testAccPreCheck(t) }, 123 Providers: testAccProviders, 124 CheckDestroy: testAccCheckAWSEcsTaskDefinitionDestroy, 125 Steps: []resource.TestStep{ 126 { 127 Config: testAccAWSEcsTaskDefinition_constraint, 128 Check: resource.ComposeTestCheckFunc( 129 testAccCheckAWSEcsTaskDefinitionExists("aws_ecs_task_definition.jenkins", &def), 130 resource.TestCheckResourceAttr("aws_ecs_task_definition.jenkins", "placement_constraints.#", "1"), 131 testAccCheckAWSTaskDefinitionConstraintsAttrs(&def), 132 ), 133 }, 134 }, 135 }) 136 } 137 138 func TestAccAWSEcsTaskDefinition_changeVolumesForcesNewResource(t *testing.T) { 139 var before ecs.TaskDefinition 140 var after ecs.TaskDefinition 141 resource.Test(t, resource.TestCase{ 142 PreCheck: func() { testAccPreCheck(t) }, 143 Providers: testAccProviders, 144 CheckDestroy: testAccCheckAWSEcsTaskDefinitionDestroy, 145 Steps: []resource.TestStep{ 146 { 147 Config: testAccAWSEcsTaskDefinition, 148 Check: resource.ComposeTestCheckFunc( 149 testAccCheckAWSEcsTaskDefinitionExists("aws_ecs_task_definition.jenkins", &before), 150 ), 151 }, 152 { 153 Config: testAccAWSEcsTaskDefinitionUpdatedVolume, 154 Check: resource.ComposeTestCheckFunc( 155 testAccCheckAWSEcsTaskDefinitionExists("aws_ecs_task_definition.jenkins", &after), 156 testAccCheckEcsTaskDefinitionRecreated(t, &before, &after), 157 ), 158 }, 159 }, 160 }) 161 } 162 163 func testAccCheckEcsTaskDefinitionRecreated(t *testing.T, 164 before, after *ecs.TaskDefinition) resource.TestCheckFunc { 165 return func(s *terraform.State) error { 166 if *before.Revision == *after.Revision { 167 t.Fatalf("Expected change of TaskDefinition Revisions, but both were %v", before.Revision) 168 } 169 return nil 170 } 171 } 172 173 func testAccCheckAWSTaskDefinitionConstraintsAttrs(def *ecs.TaskDefinition) resource.TestCheckFunc { 174 return func(s *terraform.State) error { 175 if len(def.PlacementConstraints) != 1 { 176 return fmt.Errorf("Expected (1) placement_constraints, got (%d)", len(def.PlacementConstraints)) 177 } 178 return nil 179 } 180 } 181 func TestValidateAwsEcsTaskDefinitionNetworkMode(t *testing.T) { 182 validNames := []string{ 183 "bridge", 184 "host", 185 "none", 186 } 187 for _, v := range validNames { 188 _, errors := validateAwsEcsTaskDefinitionNetworkMode(v, "network_mode") 189 if len(errors) != 0 { 190 t.Fatalf("%q should be a valid AWS ECS Task Definition Network Mode: %q", v, errors) 191 } 192 } 193 194 invalidNames := []string{ 195 "bridged", 196 "-docker", 197 } 198 for _, v := range invalidNames { 199 _, errors := validateAwsEcsTaskDefinitionNetworkMode(v, "network_mode") 200 if len(errors) == 0 { 201 t.Fatalf("%q should be an invalid AWS ECS Task Definition Network Mode", v) 202 } 203 } 204 } 205 206 func testAccCheckAWSEcsTaskDefinitionDestroy(s *terraform.State) error { 207 conn := testAccProvider.Meta().(*AWSClient).ecsconn 208 209 for _, rs := range s.RootModule().Resources { 210 if rs.Type != "aws_ecs_task_definition" { 211 continue 212 } 213 214 input := ecs.DescribeTaskDefinitionInput{ 215 TaskDefinition: aws.String(rs.Primary.Attributes["arn"]), 216 } 217 218 out, err := conn.DescribeTaskDefinition(&input) 219 220 if err != nil { 221 return err 222 } 223 224 if out.TaskDefinition != nil && *out.TaskDefinition.Status != "INACTIVE" { 225 return fmt.Errorf("ECS task definition still exists:\n%#v", *out.TaskDefinition) 226 } 227 } 228 229 return nil 230 } 231 232 func testAccCheckAWSEcsTaskDefinitionExists(name string, def *ecs.TaskDefinition) resource.TestCheckFunc { 233 return func(s *terraform.State) error { 234 rs, ok := s.RootModule().Resources[name] 235 if !ok { 236 return fmt.Errorf("Not found: %s", name) 237 } 238 239 conn := testAccProvider.Meta().(*AWSClient).ecsconn 240 241 out, err := conn.DescribeTaskDefinition(&ecs.DescribeTaskDefinitionInput{ 242 TaskDefinition: aws.String(rs.Primary.Attributes["arn"]), 243 }) 244 if err != nil { 245 return err 246 } 247 248 *def = *out.TaskDefinition 249 250 return nil 251 } 252 } 253 254 var testAccAWSEcsTaskDefinition_constraint = ` 255 resource "aws_ecs_task_definition" "jenkins" { 256 family = "terraform-acc-test" 257 container_definitions = <<TASK_DEFINITION 258 [ 259 { 260 "cpu": 10, 261 "command": ["sleep", "10"], 262 "entryPoint": ["/"], 263 "environment": [ 264 {"name": "VARNAME", "value": "VARVAL"} 265 ], 266 "essential": true, 267 "image": "jenkins", 268 "links": ["mongodb"], 269 "memory": 128, 270 "name": "jenkins", 271 "portMappings": [ 272 { 273 "containerPort": 80, 274 "hostPort": 8080 275 } 276 ] 277 }, 278 { 279 "cpu": 10, 280 "command": ["sleep", "10"], 281 "entryPoint": ["/"], 282 "essential": true, 283 "image": "mongodb", 284 "memory": 128, 285 "name": "mongodb", 286 "portMappings": [ 287 { 288 "containerPort": 28017, 289 "hostPort": 28017 290 } 291 ] 292 } 293 ] 294 TASK_DEFINITION 295 296 volume { 297 name = "jenkins-home" 298 host_path = "/ecs/jenkins-home" 299 } 300 301 placement_constraints { 302 type = "memberOf" 303 expression = "attribute:ecs.availability-zone in [us-west-2a, us-west-2b]" 304 } 305 } 306 ` 307 308 var testAccAWSEcsTaskDefinition = ` 309 resource "aws_ecs_task_definition" "jenkins" { 310 family = "terraform-acc-test" 311 container_definitions = <<TASK_DEFINITION 312 [ 313 { 314 "cpu": 10, 315 "command": ["sleep", "10"], 316 "entryPoint": ["/"], 317 "environment": [ 318 {"name": "VARNAME", "value": "VARVAL"} 319 ], 320 "essential": true, 321 "image": "jenkins", 322 "links": ["mongodb"], 323 "memory": 128, 324 "name": "jenkins", 325 "portMappings": [ 326 { 327 "containerPort": 80, 328 "hostPort": 8080 329 } 330 ] 331 }, 332 { 333 "cpu": 10, 334 "command": ["sleep", "10"], 335 "entryPoint": ["/"], 336 "essential": true, 337 "image": "mongodb", 338 "memory": 128, 339 "name": "mongodb", 340 "portMappings": [ 341 { 342 "containerPort": 28017, 343 "hostPort": 28017 344 } 345 ] 346 } 347 ] 348 TASK_DEFINITION 349 350 volume { 351 name = "jenkins-home" 352 host_path = "/ecs/jenkins-home" 353 } 354 } 355 ` 356 357 var testAccAWSEcsTaskDefinitionUpdatedVolume = ` 358 resource "aws_ecs_task_definition" "jenkins" { 359 family = "terraform-acc-test" 360 container_definitions = <<TASK_DEFINITION 361 [ 362 { 363 "cpu": 10, 364 "command": ["sleep", "10"], 365 "entryPoint": ["/"], 366 "environment": [ 367 {"name": "VARNAME", "value": "VARVAL"} 368 ], 369 "essential": true, 370 "image": "jenkins", 371 "links": ["mongodb"], 372 "memory": 128, 373 "name": "jenkins", 374 "portMappings": [ 375 { 376 "containerPort": 80, 377 "hostPort": 8080 378 } 379 ] 380 }, 381 { 382 "cpu": 10, 383 "command": ["sleep", "10"], 384 "entryPoint": ["/"], 385 "essential": true, 386 "image": "mongodb", 387 "memory": 128, 388 "name": "mongodb", 389 "portMappings": [ 390 { 391 "containerPort": 28017, 392 "hostPort": 28017 393 } 394 ] 395 } 396 ] 397 TASK_DEFINITION 398 399 volume { 400 name = "jenkins-home" 401 host_path = "/ecs/jenkins" 402 } 403 } 404 ` 405 406 var testAccAWSEcsTaskDefinitionWithScratchVolume = ` 407 resource "aws_ecs_task_definition" "sleep" { 408 family = "terraform-acc-sc-volume-test" 409 container_definitions = <<TASK_DEFINITION 410 [ 411 { 412 "name": "sleep", 413 "image": "busybox", 414 "cpu": 10, 415 "command": ["sleep","360"], 416 "memory": 10, 417 "essential": true 418 } 419 ] 420 TASK_DEFINITION 421 422 volume { 423 name = "database_scratch" 424 } 425 } 426 ` 427 428 func testAccAWSEcsTaskDefinitionWithTaskRoleArn(rInt int) string { 429 return fmt.Sprintf(` 430 resource "aws_iam_role" "role_test" { 431 name = "tf_old_name-%d" 432 path = "/test/" 433 assume_role_policy = <<EOF 434 { 435 "Version": "2012-10-17", 436 "Statement": [ 437 { 438 "Action": "sts:AssumeRole", 439 "Principal": { 440 "Service": "ec2.amazonaws.com" 441 }, 442 "Effect": "Allow", 443 "Sid": "" 444 } 445 ] 446 } 447 EOF 448 } 449 450 resource "aws_iam_role_policy" "role_test" { 451 name = "role_update_test-%d" 452 role = "${aws_iam_role.role_test.id}" 453 policy = <<EOF 454 { 455 "Version": "2012-10-17", 456 "Statement": [ 457 { 458 "Effect": "Allow", 459 "Action": [ 460 "s3:GetBucketLocation", 461 "s3:ListAllMyBuckets" 462 ], 463 "Resource": "arn:aws:s3:::*" 464 } 465 ] 466 } 467 EOF 468 } 469 470 resource "aws_ecs_task_definition" "sleep" { 471 family = "terraform-acc-sc-volume-test" 472 task_role_arn = "${aws_iam_role.role_test.arn}" 473 container_definitions = <<TASK_DEFINITION 474 [ 475 { 476 "name": "sleep", 477 "image": "busybox", 478 "cpu": 10, 479 "command": ["sleep","360"], 480 "memory": 10, 481 "essential": true 482 } 483 ] 484 TASK_DEFINITION 485 volume { 486 name = "database_scratch" 487 } 488 }`, rInt, rInt) 489 } 490 491 func testAccAWSEcsTaskDefinitionWithNetworkMode(rInt int) string { 492 return fmt.Sprintf(` 493 resource "aws_iam_role" "role_test" { 494 name = "tf_old_name-%d" 495 path = "/test/" 496 assume_role_policy = <<EOF 497 { 498 "Version": "2012-10-17", 499 "Statement": [ 500 { 501 "Action": "sts:AssumeRole", 502 "Principal": { 503 "Service": "ec2.amazonaws.com" 504 }, 505 "Effect": "Allow", 506 "Sid": "" 507 } 508 ] 509 } 510 EOF 511 } 512 513 resource "aws_iam_role_policy" "role_test" { 514 name = "role_update_test-%d" 515 role = "${aws_iam_role.role_test.id}" 516 policy = <<EOF 517 { 518 "Version": "2012-10-17", 519 "Statement": [ 520 { 521 "Effect": "Allow", 522 "Action": [ 523 "s3:GetBucketLocation", 524 "s3:ListAllMyBuckets" 525 ], 526 "Resource": "arn:aws:s3:::*" 527 } 528 ] 529 } 530 EOF 531 } 532 533 resource "aws_ecs_task_definition" "sleep" { 534 family = "terraform-acc-sc-volume-test-network-mode" 535 task_role_arn = "${aws_iam_role.role_test.arn}" 536 network_mode = "bridge" 537 container_definitions = <<TASK_DEFINITION 538 [ 539 { 540 "name": "sleep", 541 "image": "busybox", 542 "cpu": 10, 543 "command": ["sleep","360"], 544 "memory": 10, 545 "essential": true 546 } 547 ] 548 TASK_DEFINITION 549 550 volume { 551 name = "database_scratch" 552 } 553 }`, rInt, rInt) 554 } 555 556 var testAccAWSEcsTaskDefinitionWithEcsService = ` 557 resource "aws_ecs_cluster" "default" { 558 name = "terraform-acc-test" 559 } 560 561 resource "aws_ecs_service" "sleep-svc" { 562 name = "tf-acc-ecs-svc" 563 cluster = "${aws_ecs_cluster.default.id}" 564 task_definition = "${aws_ecs_task_definition.sleep.arn}" 565 desired_count = 1 566 } 567 568 resource "aws_ecs_task_definition" "sleep" { 569 family = "terraform-acc-sc-volume-test" 570 container_definitions = <<TASK_DEFINITION 571 [ 572 { 573 "name": "sleep", 574 "image": "busybox", 575 "cpu": 10, 576 "command": ["sleep","360"], 577 "memory": 10, 578 "essential": true 579 } 580 ] 581 TASK_DEFINITION 582 583 volume { 584 name = "database_scratch" 585 } 586 } 587 ` 588 var testAccAWSEcsTaskDefinitionWithEcsServiceModified = ` 589 resource "aws_ecs_cluster" "default" { 590 name = "terraform-acc-test" 591 } 592 593 resource "aws_ecs_service" "sleep-svc" { 594 name = "tf-acc-ecs-svc" 595 cluster = "${aws_ecs_cluster.default.id}" 596 task_definition = "${aws_ecs_task_definition.sleep.arn}" 597 desired_count = 1 598 } 599 600 resource "aws_ecs_task_definition" "sleep" { 601 family = "terraform-acc-sc-volume-test" 602 container_definitions = <<TASK_DEFINITION 603 [ 604 { 605 "name": "sleep", 606 "image": "busybox", 607 "cpu": 20, 608 "command": ["sleep","360"], 609 "memory": 50, 610 "essential": true 611 } 612 ] 613 TASK_DEFINITION 614 615 volume { 616 name = "database_scratch" 617 } 618 } 619 ` 620 621 var testAccAWSEcsTaskDefinitionModified = ` 622 resource "aws_ecs_task_definition" "jenkins" { 623 family = "terraform-acc-test" 624 container_definitions = <<TASK_DEFINITION 625 [ 626 { 627 "cpu": 10, 628 "command": ["sleep", "10"], 629 "entryPoint": ["/"], 630 "environment": [ 631 {"name": "VARNAME", "value": "VARVAL"} 632 ], 633 "essential": true, 634 "image": "jenkins", 635 "links": ["mongodb"], 636 "memory": 128, 637 "name": "jenkins", 638 "portMappings": [ 639 { 640 "containerPort": 80, 641 "hostPort": 8080 642 } 643 ] 644 }, 645 { 646 "cpu": 20, 647 "command": ["sleep", "10"], 648 "entryPoint": ["/"], 649 "essential": true, 650 "image": "mongodb", 651 "memory": 128, 652 "name": "mongodb", 653 "portMappings": [ 654 { 655 "containerPort": 28017, 656 "hostPort": 28017 657 } 658 ] 659 } 660 ] 661 TASK_DEFINITION 662 663 volume { 664 name = "jenkins-home" 665 host_path = "/ecs/jenkins-home" 666 } 667 } 668 `