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