github.com/kwoods/terraform@v0.6.11-0.20160809170336-13497db7138e/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 resource.Test(t, resource.TestCase{ 15 PreCheck: func() { testAccPreCheck(t) }, 16 Providers: testAccProviders, 17 CheckDestroy: testAccCheckAWSEcsTaskDefinitionDestroy, 18 Steps: []resource.TestStep{ 19 resource.TestStep{ 20 Config: testAccAWSEcsTaskDefinition, 21 Check: resource.ComposeTestCheckFunc( 22 testAccCheckAWSEcsTaskDefinitionExists("aws_ecs_task_definition.jenkins"), 23 ), 24 }, 25 resource.TestStep{ 26 Config: testAccAWSEcsTaskDefinitionModified, 27 Check: resource.ComposeTestCheckFunc( 28 testAccCheckAWSEcsTaskDefinitionExists("aws_ecs_task_definition.jenkins"), 29 ), 30 }, 31 }, 32 }) 33 } 34 35 // Regression for https://github.com/hashicorp/terraform/issues/2370 36 func TestAccAWSEcsTaskDefinition_withScratchVolume(t *testing.T) { 37 resource.Test(t, resource.TestCase{ 38 PreCheck: func() { testAccPreCheck(t) }, 39 Providers: testAccProviders, 40 CheckDestroy: testAccCheckAWSEcsTaskDefinitionDestroy, 41 Steps: []resource.TestStep{ 42 resource.TestStep{ 43 Config: testAccAWSEcsTaskDefinitionWithScratchVolume, 44 Check: resource.ComposeTestCheckFunc( 45 testAccCheckAWSEcsTaskDefinitionExists("aws_ecs_task_definition.sleep"), 46 ), 47 }, 48 }, 49 }) 50 } 51 52 // Regression for https://github.com/hashicorp/terraform/issues/2694 53 func TestAccAWSEcsTaskDefinition_withEcsService(t *testing.T) { 54 resource.Test(t, resource.TestCase{ 55 PreCheck: func() { testAccPreCheck(t) }, 56 Providers: testAccProviders, 57 CheckDestroy: testAccCheckAWSEcsTaskDefinitionDestroy, 58 Steps: []resource.TestStep{ 59 resource.TestStep{ 60 Config: testAccAWSEcsTaskDefinitionWithEcsService, 61 Check: resource.ComposeTestCheckFunc( 62 testAccCheckAWSEcsTaskDefinitionExists("aws_ecs_task_definition.sleep"), 63 testAccCheckAWSEcsServiceExists("aws_ecs_service.sleep-svc"), 64 ), 65 }, 66 resource.TestStep{ 67 Config: testAccAWSEcsTaskDefinitionWithEcsServiceModified, 68 Check: resource.ComposeTestCheckFunc( 69 testAccCheckAWSEcsTaskDefinitionExists("aws_ecs_task_definition.sleep"), 70 testAccCheckAWSEcsServiceExists("aws_ecs_service.sleep-svc"), 71 ), 72 }, 73 }, 74 }) 75 } 76 77 func TestAccAWSEcsTaskDefinition_withTaskRoleArn(t *testing.T) { 78 resource.Test(t, resource.TestCase{ 79 PreCheck: func() { testAccPreCheck(t) }, 80 Providers: testAccProviders, 81 CheckDestroy: testAccCheckAWSEcsTaskDefinitionDestroy, 82 Steps: []resource.TestStep{ 83 resource.TestStep{ 84 Config: testAccAWSEcsTaskDefinitionWithTaskRoleArn, 85 Check: resource.ComposeTestCheckFunc( 86 testAccCheckAWSEcsTaskDefinitionExists("aws_ecs_task_definition.sleep"), 87 ), 88 }, 89 }, 90 }) 91 } 92 93 func testAccCheckAWSEcsTaskDefinitionDestroy(s *terraform.State) error { 94 conn := testAccProvider.Meta().(*AWSClient).ecsconn 95 96 for _, rs := range s.RootModule().Resources { 97 if rs.Type != "aws_ecs_task_definition" { 98 continue 99 } 100 101 input := ecs.DescribeTaskDefinitionInput{ 102 TaskDefinition: aws.String(rs.Primary.Attributes["arn"]), 103 } 104 105 out, err := conn.DescribeTaskDefinition(&input) 106 107 if err != nil { 108 return err 109 } 110 111 if out.TaskDefinition != nil && *out.TaskDefinition.Status != "INACTIVE" { 112 return fmt.Errorf("ECS task definition still exists:\n%#v", *out.TaskDefinition) 113 } 114 } 115 116 return nil 117 } 118 119 func testAccCheckAWSEcsTaskDefinitionExists(name string) resource.TestCheckFunc { 120 return func(s *terraform.State) error { 121 _, ok := s.RootModule().Resources[name] 122 if !ok { 123 return fmt.Errorf("Not found: %s", name) 124 } 125 126 return nil 127 } 128 } 129 130 var testAccAWSEcsTaskDefinition = ` 131 resource "aws_ecs_task_definition" "jenkins" { 132 family = "terraform-acc-test" 133 container_definitions = <<TASK_DEFINITION 134 [ 135 { 136 "cpu": 10, 137 "command": ["sleep", "10"], 138 "entryPoint": ["/"], 139 "environment": [ 140 {"name": "VARNAME", "value": "VARVAL"} 141 ], 142 "essential": true, 143 "image": "jenkins", 144 "links": ["mongodb"], 145 "memory": 128, 146 "name": "jenkins", 147 "portMappings": [ 148 { 149 "containerPort": 80, 150 "hostPort": 8080 151 } 152 ] 153 }, 154 { 155 "cpu": 10, 156 "command": ["sleep", "10"], 157 "entryPoint": ["/"], 158 "essential": true, 159 "image": "mongodb", 160 "memory": 128, 161 "name": "mongodb", 162 "portMappings": [ 163 { 164 "containerPort": 28017, 165 "hostPort": 28017 166 } 167 ] 168 } 169 ] 170 TASK_DEFINITION 171 172 volume { 173 name = "jenkins-home" 174 host_path = "/ecs/jenkins-home" 175 } 176 } 177 ` 178 179 var testAccAWSEcsTaskDefinitionWithScratchVolume = ` 180 resource "aws_ecs_task_definition" "sleep" { 181 family = "terraform-acc-sc-volume-test" 182 container_definitions = <<TASK_DEFINITION 183 [ 184 { 185 "name": "sleep", 186 "image": "busybox", 187 "cpu": 10, 188 "command": ["sleep","360"], 189 "memory": 10, 190 "essential": true 191 } 192 ] 193 TASK_DEFINITION 194 195 volume { 196 name = "database_scratch" 197 } 198 } 199 ` 200 201 var testAccAWSEcsTaskDefinitionWithTaskRoleArn = ` 202 resource "aws_iam_role" "role_test" { 203 name = "tf_old_name" 204 path = "/test/" 205 assume_role_policy = <<EOF 206 { 207 "Version": "2012-10-17", 208 "Statement": [ 209 { 210 "Action": "sts:AssumeRole", 211 "Principal": { 212 "Service": "ec2.amazonaws.com" 213 }, 214 "Effect": "Allow", 215 "Sid": "" 216 } 217 ] 218 } 219 EOF 220 } 221 222 resource "aws_iam_role_policy" "role_test" { 223 name = "role_update_test" 224 role = "${aws_iam_role.role_test.id}" 225 policy = <<EOF 226 { 227 "Version": "2012-10-17", 228 "Statement": [ 229 { 230 "Effect": "Allow", 231 "Action": [ 232 "s3:GetBucketLocation", 233 "s3:ListAllMyBuckets" 234 ], 235 "Resource": "arn:aws:s3:::*" 236 } 237 ] 238 } 239 EOF 240 } 241 242 resource "aws_ecs_task_definition" "sleep" { 243 family = "terraform-acc-sc-volume-test" 244 task_role_arn = "${aws_iam_role.role_test.arn}" 245 container_definitions = <<TASK_DEFINITION 246 [ 247 { 248 "name": "sleep", 249 "image": "busybox", 250 "cpu": 10, 251 "command": ["sleep","360"], 252 "memory": 10, 253 "essential": true 254 } 255 ] 256 TASK_DEFINITION 257 258 volume { 259 name = "database_scratch" 260 } 261 } 262 ` 263 264 var testAccAWSEcsTaskDefinitionWithEcsService = ` 265 resource "aws_ecs_cluster" "default" { 266 name = "terraform-acc-test" 267 } 268 269 resource "aws_ecs_service" "sleep-svc" { 270 name = "tf-acc-ecs-svc" 271 cluster = "${aws_ecs_cluster.default.id}" 272 task_definition = "${aws_ecs_task_definition.sleep.arn}" 273 desired_count = 1 274 } 275 276 resource "aws_ecs_task_definition" "sleep" { 277 family = "terraform-acc-sc-volume-test" 278 container_definitions = <<TASK_DEFINITION 279 [ 280 { 281 "name": "sleep", 282 "image": "busybox", 283 "cpu": 10, 284 "command": ["sleep","360"], 285 "memory": 10, 286 "essential": true 287 } 288 ] 289 TASK_DEFINITION 290 291 volume { 292 name = "database_scratch" 293 } 294 } 295 ` 296 var testAccAWSEcsTaskDefinitionWithEcsServiceModified = ` 297 resource "aws_ecs_cluster" "default" { 298 name = "terraform-acc-test" 299 } 300 301 resource "aws_ecs_service" "sleep-svc" { 302 name = "tf-acc-ecs-svc" 303 cluster = "${aws_ecs_cluster.default.id}" 304 task_definition = "${aws_ecs_task_definition.sleep.arn}" 305 desired_count = 1 306 } 307 308 resource "aws_ecs_task_definition" "sleep" { 309 family = "terraform-acc-sc-volume-test" 310 container_definitions = <<TASK_DEFINITION 311 [ 312 { 313 "name": "sleep", 314 "image": "busybox", 315 "cpu": 20, 316 "command": ["sleep","360"], 317 "memory": 50, 318 "essential": true 319 } 320 ] 321 TASK_DEFINITION 322 323 volume { 324 name = "database_scratch" 325 } 326 } 327 ` 328 329 var testAccAWSEcsTaskDefinitionModified = ` 330 resource "aws_ecs_task_definition" "jenkins" { 331 family = "terraform-acc-test" 332 container_definitions = <<TASK_DEFINITION 333 [ 334 { 335 "cpu": 10, 336 "command": ["sleep", "10"], 337 "entryPoint": ["/"], 338 "environment": [ 339 {"name": "VARNAME", "value": "VARVAL"} 340 ], 341 "essential": true, 342 "image": "jenkins", 343 "links": ["mongodb"], 344 "memory": 128, 345 "name": "jenkins", 346 "portMappings": [ 347 { 348 "containerPort": 80, 349 "hostPort": 8080 350 } 351 ] 352 }, 353 { 354 "cpu": 20, 355 "command": ["sleep", "10"], 356 "entryPoint": ["/"], 357 "essential": true, 358 "image": "mongodb", 359 "memory": 128, 360 "name": "mongodb", 361 "portMappings": [ 362 { 363 "containerPort": 28017, 364 "hostPort": 28017 365 } 366 ] 367 } 368 ] 369 TASK_DEFINITION 370 371 volume { 372 name = "jenkins-home" 373 host_path = "/ecs/jenkins-home" 374 } 375 } 376 `