github.com/simonswine/terraform@v0.9.0-beta2/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 testAccCheckAWSTaskDefinitionConstraintsAttrs(def *ecs.TaskDefinition) resource.TestCheckFunc { 139 return func(s *terraform.State) error { 140 if len(def.PlacementConstraints) != 1 { 141 return fmt.Errorf("Expected (1) placement_constraints, got (%d)", len(def.PlacementConstraints)) 142 } 143 return nil 144 } 145 } 146 func TestValidateAwsEcsTaskDefinitionNetworkMode(t *testing.T) { 147 validNames := []string{ 148 "bridge", 149 "host", 150 "none", 151 } 152 for _, v := range validNames { 153 _, errors := validateAwsEcsTaskDefinitionNetworkMode(v, "network_mode") 154 if len(errors) != 0 { 155 t.Fatalf("%q should be a valid AWS ECS Task Definition Network Mode: %q", v, errors) 156 } 157 } 158 159 invalidNames := []string{ 160 "bridged", 161 "-docker", 162 } 163 for _, v := range invalidNames { 164 _, errors := validateAwsEcsTaskDefinitionNetworkMode(v, "network_mode") 165 if len(errors) == 0 { 166 t.Fatalf("%q should be an invalid AWS ECS Task Definition Network Mode", v) 167 } 168 } 169 } 170 171 func testAccCheckAWSEcsTaskDefinitionDestroy(s *terraform.State) error { 172 conn := testAccProvider.Meta().(*AWSClient).ecsconn 173 174 for _, rs := range s.RootModule().Resources { 175 if rs.Type != "aws_ecs_task_definition" { 176 continue 177 } 178 179 input := ecs.DescribeTaskDefinitionInput{ 180 TaskDefinition: aws.String(rs.Primary.Attributes["arn"]), 181 } 182 183 out, err := conn.DescribeTaskDefinition(&input) 184 185 if err != nil { 186 return err 187 } 188 189 if out.TaskDefinition != nil && *out.TaskDefinition.Status != "INACTIVE" { 190 return fmt.Errorf("ECS task definition still exists:\n%#v", *out.TaskDefinition) 191 } 192 } 193 194 return nil 195 } 196 197 func testAccCheckAWSEcsTaskDefinitionExists(name string, def *ecs.TaskDefinition) resource.TestCheckFunc { 198 return func(s *terraform.State) error { 199 rs, ok := s.RootModule().Resources[name] 200 if !ok { 201 return fmt.Errorf("Not found: %s", name) 202 } 203 204 conn := testAccProvider.Meta().(*AWSClient).ecsconn 205 206 out, err := conn.DescribeTaskDefinition(&ecs.DescribeTaskDefinitionInput{ 207 TaskDefinition: aws.String(rs.Primary.Attributes["arn"]), 208 }) 209 if err != nil { 210 return err 211 } 212 213 *def = *out.TaskDefinition 214 215 return nil 216 } 217 } 218 219 var testAccAWSEcsTaskDefinition_constraint = ` 220 resource "aws_ecs_task_definition" "jenkins" { 221 family = "terraform-acc-test" 222 container_definitions = <<TASK_DEFINITION 223 [ 224 { 225 "cpu": 10, 226 "command": ["sleep", "10"], 227 "entryPoint": ["/"], 228 "environment": [ 229 {"name": "VARNAME", "value": "VARVAL"} 230 ], 231 "essential": true, 232 "image": "jenkins", 233 "links": ["mongodb"], 234 "memory": 128, 235 "name": "jenkins", 236 "portMappings": [ 237 { 238 "containerPort": 80, 239 "hostPort": 8080 240 } 241 ] 242 }, 243 { 244 "cpu": 10, 245 "command": ["sleep", "10"], 246 "entryPoint": ["/"], 247 "essential": true, 248 "image": "mongodb", 249 "memory": 128, 250 "name": "mongodb", 251 "portMappings": [ 252 { 253 "containerPort": 28017, 254 "hostPort": 28017 255 } 256 ] 257 } 258 ] 259 TASK_DEFINITION 260 261 volume { 262 name = "jenkins-home" 263 host_path = "/ecs/jenkins-home" 264 } 265 266 placement_constraints { 267 type = "memberOf" 268 expression = "attribute:ecs.availability-zone in [us-west-2a, us-west-2b]" 269 } 270 } 271 ` 272 273 var testAccAWSEcsTaskDefinition = ` 274 resource "aws_ecs_task_definition" "jenkins" { 275 family = "terraform-acc-test" 276 container_definitions = <<TASK_DEFINITION 277 [ 278 { 279 "cpu": 10, 280 "command": ["sleep", "10"], 281 "entryPoint": ["/"], 282 "environment": [ 283 {"name": "VARNAME", "value": "VARVAL"} 284 ], 285 "essential": true, 286 "image": "jenkins", 287 "links": ["mongodb"], 288 "memory": 128, 289 "name": "jenkins", 290 "portMappings": [ 291 { 292 "containerPort": 80, 293 "hostPort": 8080 294 } 295 ] 296 }, 297 { 298 "cpu": 10, 299 "command": ["sleep", "10"], 300 "entryPoint": ["/"], 301 "essential": true, 302 "image": "mongodb", 303 "memory": 128, 304 "name": "mongodb", 305 "portMappings": [ 306 { 307 "containerPort": 28017, 308 "hostPort": 28017 309 } 310 ] 311 } 312 ] 313 TASK_DEFINITION 314 315 volume { 316 name = "jenkins-home" 317 host_path = "/ecs/jenkins-home" 318 } 319 } 320 ` 321 322 var testAccAWSEcsTaskDefinitionWithScratchVolume = ` 323 resource "aws_ecs_task_definition" "sleep" { 324 family = "terraform-acc-sc-volume-test" 325 container_definitions = <<TASK_DEFINITION 326 [ 327 { 328 "name": "sleep", 329 "image": "busybox", 330 "cpu": 10, 331 "command": ["sleep","360"], 332 "memory": 10, 333 "essential": true 334 } 335 ] 336 TASK_DEFINITION 337 338 volume { 339 name = "database_scratch" 340 } 341 } 342 ` 343 344 func testAccAWSEcsTaskDefinitionWithTaskRoleArn(rInt int) string { 345 return fmt.Sprintf(` 346 resource "aws_iam_role" "role_test" { 347 name = "tf_old_name-%d" 348 path = "/test/" 349 assume_role_policy = <<EOF 350 { 351 "Version": "2012-10-17", 352 "Statement": [ 353 { 354 "Action": "sts:AssumeRole", 355 "Principal": { 356 "Service": "ec2.amazonaws.com" 357 }, 358 "Effect": "Allow", 359 "Sid": "" 360 } 361 ] 362 } 363 EOF 364 } 365 366 resource "aws_iam_role_policy" "role_test" { 367 name = "role_update_test-%d" 368 role = "${aws_iam_role.role_test.id}" 369 policy = <<EOF 370 { 371 "Version": "2012-10-17", 372 "Statement": [ 373 { 374 "Effect": "Allow", 375 "Action": [ 376 "s3:GetBucketLocation", 377 "s3:ListAllMyBuckets" 378 ], 379 "Resource": "arn:aws:s3:::*" 380 } 381 ] 382 } 383 EOF 384 } 385 386 resource "aws_ecs_task_definition" "sleep" { 387 family = "terraform-acc-sc-volume-test" 388 task_role_arn = "${aws_iam_role.role_test.arn}" 389 container_definitions = <<TASK_DEFINITION 390 [ 391 { 392 "name": "sleep", 393 "image": "busybox", 394 "cpu": 10, 395 "command": ["sleep","360"], 396 "memory": 10, 397 "essential": true 398 } 399 ] 400 TASK_DEFINITION 401 volume { 402 name = "database_scratch" 403 } 404 }`, rInt, rInt) 405 } 406 407 func testAccAWSEcsTaskDefinitionWithNetworkMode(rInt int) string { 408 return fmt.Sprintf(` 409 resource "aws_iam_role" "role_test" { 410 name = "tf_old_name-%d" 411 path = "/test/" 412 assume_role_policy = <<EOF 413 { 414 "Version": "2012-10-17", 415 "Statement": [ 416 { 417 "Action": "sts:AssumeRole", 418 "Principal": { 419 "Service": "ec2.amazonaws.com" 420 }, 421 "Effect": "Allow", 422 "Sid": "" 423 } 424 ] 425 } 426 EOF 427 } 428 429 resource "aws_iam_role_policy" "role_test" { 430 name = "role_update_test-%d" 431 role = "${aws_iam_role.role_test.id}" 432 policy = <<EOF 433 { 434 "Version": "2012-10-17", 435 "Statement": [ 436 { 437 "Effect": "Allow", 438 "Action": [ 439 "s3:GetBucketLocation", 440 "s3:ListAllMyBuckets" 441 ], 442 "Resource": "arn:aws:s3:::*" 443 } 444 ] 445 } 446 EOF 447 } 448 449 resource "aws_ecs_task_definition" "sleep" { 450 family = "terraform-acc-sc-volume-test-network-mode" 451 task_role_arn = "${aws_iam_role.role_test.arn}" 452 network_mode = "bridge" 453 container_definitions = <<TASK_DEFINITION 454 [ 455 { 456 "name": "sleep", 457 "image": "busybox", 458 "cpu": 10, 459 "command": ["sleep","360"], 460 "memory": 10, 461 "essential": true 462 } 463 ] 464 TASK_DEFINITION 465 466 volume { 467 name = "database_scratch" 468 } 469 }`, rInt, rInt) 470 } 471 472 var testAccAWSEcsTaskDefinitionWithEcsService = ` 473 resource "aws_ecs_cluster" "default" { 474 name = "terraform-acc-test" 475 } 476 477 resource "aws_ecs_service" "sleep-svc" { 478 name = "tf-acc-ecs-svc" 479 cluster = "${aws_ecs_cluster.default.id}" 480 task_definition = "${aws_ecs_task_definition.sleep.arn}" 481 desired_count = 1 482 } 483 484 resource "aws_ecs_task_definition" "sleep" { 485 family = "terraform-acc-sc-volume-test" 486 container_definitions = <<TASK_DEFINITION 487 [ 488 { 489 "name": "sleep", 490 "image": "busybox", 491 "cpu": 10, 492 "command": ["sleep","360"], 493 "memory": 10, 494 "essential": true 495 } 496 ] 497 TASK_DEFINITION 498 499 volume { 500 name = "database_scratch" 501 } 502 } 503 ` 504 var testAccAWSEcsTaskDefinitionWithEcsServiceModified = ` 505 resource "aws_ecs_cluster" "default" { 506 name = "terraform-acc-test" 507 } 508 509 resource "aws_ecs_service" "sleep-svc" { 510 name = "tf-acc-ecs-svc" 511 cluster = "${aws_ecs_cluster.default.id}" 512 task_definition = "${aws_ecs_task_definition.sleep.arn}" 513 desired_count = 1 514 } 515 516 resource "aws_ecs_task_definition" "sleep" { 517 family = "terraform-acc-sc-volume-test" 518 container_definitions = <<TASK_DEFINITION 519 [ 520 { 521 "name": "sleep", 522 "image": "busybox", 523 "cpu": 20, 524 "command": ["sleep","360"], 525 "memory": 50, 526 "essential": true 527 } 528 ] 529 TASK_DEFINITION 530 531 volume { 532 name = "database_scratch" 533 } 534 } 535 ` 536 537 var testAccAWSEcsTaskDefinitionModified = ` 538 resource "aws_ecs_task_definition" "jenkins" { 539 family = "terraform-acc-test" 540 container_definitions = <<TASK_DEFINITION 541 [ 542 { 543 "cpu": 10, 544 "command": ["sleep", "10"], 545 "entryPoint": ["/"], 546 "environment": [ 547 {"name": "VARNAME", "value": "VARVAL"} 548 ], 549 "essential": true, 550 "image": "jenkins", 551 "links": ["mongodb"], 552 "memory": 128, 553 "name": "jenkins", 554 "portMappings": [ 555 { 556 "containerPort": 80, 557 "hostPort": 8080 558 } 559 ] 560 }, 561 { 562 "cpu": 20, 563 "command": ["sleep", "10"], 564 "entryPoint": ["/"], 565 "essential": true, 566 "image": "mongodb", 567 "memory": 128, 568 "name": "mongodb", 569 "portMappings": [ 570 { 571 "containerPort": 28017, 572 "hostPort": 28017 573 } 574 ] 575 } 576 ] 577 TASK_DEFINITION 578 579 volume { 580 name = "jenkins-home" 581 host_path = "/ecs/jenkins-home" 582 } 583 } 584 `