github.com/econnell/terraform@v0.5.4-0.20150722160631-78eb236786a4/builtin/providers/aws/resource_aws_ecs_service_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "regexp" 6 "testing" 7 8 "github.com/aws/aws-sdk-go/aws" 9 "github.com/aws/aws-sdk-go/service/ecs" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestParseTaskDefinition(t *testing.T) { 15 cases := map[string]map[string]interface{}{ 16 "invalid": map[string]interface{}{ 17 "family": "", 18 "revision": "", 19 "isValid": false, 20 }, 21 "invalidWithColon:": map[string]interface{}{ 22 "family": "", 23 "revision": "", 24 "isValid": false, 25 }, 26 "1234": map[string]interface{}{ 27 "family": "", 28 "revision": "", 29 "isValid": false, 30 }, 31 "invalid:aaa": map[string]interface{}{ 32 "family": "", 33 "revision": "", 34 "isValid": false, 35 }, 36 "invalid=family:1": map[string]interface{}{ 37 "family": "", 38 "revision": "", 39 "isValid": false, 40 }, 41 "invalid:name:1": map[string]interface{}{ 42 "family": "", 43 "revision": "", 44 "isValid": false, 45 }, 46 "valid:1": map[string]interface{}{ 47 "family": "valid", 48 "revision": "1", 49 "isValid": true, 50 }, 51 "abc12-def:54": map[string]interface{}{ 52 "family": "abc12-def", 53 "revision": "54", 54 "isValid": true, 55 }, 56 "lorem_ip-sum:123": map[string]interface{}{ 57 "family": "lorem_ip-sum", 58 "revision": "123", 59 "isValid": true, 60 }, 61 "lorem-ipsum:1": map[string]interface{}{ 62 "family": "lorem-ipsum", 63 "revision": "1", 64 "isValid": true, 65 }, 66 } 67 68 for input, expectedOutput := range cases { 69 family, revision, err := parseTaskDefinition(input) 70 isValid := expectedOutput["isValid"].(bool) 71 if !isValid && err == nil { 72 t.Fatalf("Task definition %s should fail", input) 73 } 74 75 expectedFamily := expectedOutput["family"].(string) 76 if family != expectedFamily { 77 t.Fatalf("Unexpected family (%#v) for task definition %s\n%#v", family, input, err) 78 } 79 expectedRevision := expectedOutput["revision"].(string) 80 if revision != expectedRevision { 81 t.Fatalf("Unexpected revision (%#v) for task definition %s\n%#v", revision, input, err) 82 } 83 } 84 } 85 86 func TestAccAWSEcsServiceWithARN(t *testing.T) { 87 resource.Test(t, resource.TestCase{ 88 PreCheck: func() { testAccPreCheck(t) }, 89 Providers: testAccProviders, 90 CheckDestroy: testAccCheckAWSEcsServiceDestroy, 91 Steps: []resource.TestStep{ 92 resource.TestStep{ 93 Config: testAccAWSEcsService, 94 Check: resource.ComposeTestCheckFunc( 95 testAccCheckAWSEcsServiceExists("aws_ecs_service.mongo"), 96 ), 97 }, 98 99 resource.TestStep{ 100 Config: testAccAWSEcsServiceModified, 101 Check: resource.ComposeTestCheckFunc( 102 testAccCheckAWSEcsServiceExists("aws_ecs_service.mongo"), 103 ), 104 }, 105 }, 106 }) 107 } 108 109 func TestAccAWSEcsServiceWithFamilyAndRevision(t *testing.T) { 110 resource.Test(t, resource.TestCase{ 111 PreCheck: func() { testAccPreCheck(t) }, 112 Providers: testAccProviders, 113 CheckDestroy: testAccCheckAWSEcsServiceDestroy, 114 Steps: []resource.TestStep{ 115 resource.TestStep{ 116 Config: testAccAWSEcsServiceWithFamilyAndRevision, 117 Check: resource.ComposeTestCheckFunc( 118 testAccCheckAWSEcsServiceExists("aws_ecs_service.jenkins"), 119 ), 120 }, 121 122 resource.TestStep{ 123 Config: testAccAWSEcsServiceWithFamilyAndRevisionModified, 124 Check: resource.ComposeTestCheckFunc( 125 testAccCheckAWSEcsServiceExists("aws_ecs_service.jenkins"), 126 ), 127 }, 128 }, 129 }) 130 } 131 132 // Regression for https://github.com/hashicorp/terraform/issues/2427 133 func TestAccAWSEcsServiceWithRenamedCluster(t *testing.T) { 134 originalRegexp := regexp.MustCompile( 135 "^arn:aws:ecs:[^:]+:[0-9]+:cluster/terraformecstest3$") 136 modifiedRegexp := regexp.MustCompile( 137 "^arn:aws:ecs:[^:]+:[0-9]+:cluster/terraformecstest3modified$") 138 139 resource.Test(t, resource.TestCase{ 140 PreCheck: func() { testAccPreCheck(t) }, 141 Providers: testAccProviders, 142 CheckDestroy: testAccCheckAWSEcsServiceDestroy, 143 Steps: []resource.TestStep{ 144 resource.TestStep{ 145 Config: testAccAWSEcsServiceWithRenamedCluster, 146 Check: resource.ComposeTestCheckFunc( 147 testAccCheckAWSEcsServiceExists("aws_ecs_service.ghost"), 148 resource.TestMatchResourceAttr( 149 "aws_ecs_service.ghost", "cluster", originalRegexp), 150 ), 151 }, 152 153 resource.TestStep{ 154 Config: testAccAWSEcsServiceWithRenamedClusterModified, 155 Check: resource.ComposeTestCheckFunc( 156 testAccCheckAWSEcsServiceExists("aws_ecs_service.ghost"), 157 resource.TestMatchResourceAttr( 158 "aws_ecs_service.ghost", "cluster", modifiedRegexp), 159 ), 160 }, 161 }, 162 }) 163 } 164 165 func testAccCheckAWSEcsServiceDestroy(s *terraform.State) error { 166 conn := testAccProvider.Meta().(*AWSClient).ecsconn 167 168 for _, rs := range s.RootModule().Resources { 169 if rs.Type != "aws_ecs_service" { 170 continue 171 } 172 173 out, err := conn.DescribeServices(&ecs.DescribeServicesInput{ 174 Services: []*string{aws.String(rs.Primary.ID)}, 175 }) 176 177 if err == nil { 178 if len(out.Services) > 0 { 179 return fmt.Errorf("ECS service still exists:\n%#v", out.Services) 180 } 181 } 182 183 return err 184 } 185 186 return nil 187 } 188 189 func testAccCheckAWSEcsServiceExists(name string) resource.TestCheckFunc { 190 return func(s *terraform.State) error { 191 _, ok := s.RootModule().Resources[name] 192 if !ok { 193 return fmt.Errorf("Not found: %s", name) 194 } 195 196 return nil 197 } 198 } 199 200 var testAccAWSEcsService = ` 201 resource "aws_ecs_cluster" "default" { 202 name = "terraformecstest1" 203 } 204 205 resource "aws_ecs_task_definition" "mongo" { 206 family = "mongodb" 207 container_definitions = <<DEFINITION 208 [ 209 { 210 "cpu": 128, 211 "essential": true, 212 "image": "mongo:latest", 213 "memory": 128, 214 "name": "mongodb" 215 } 216 ] 217 DEFINITION 218 } 219 220 resource "aws_ecs_service" "mongo" { 221 name = "mongodb" 222 cluster = "${aws_ecs_cluster.default.id}" 223 task_definition = "${aws_ecs_task_definition.mongo.arn}" 224 desired_count = 1 225 } 226 ` 227 228 var testAccAWSEcsServiceModified = ` 229 resource "aws_ecs_cluster" "default" { 230 name = "terraformecstest1" 231 } 232 233 resource "aws_ecs_task_definition" "mongo" { 234 family = "mongodb" 235 container_definitions = <<DEFINITION 236 [ 237 { 238 "cpu": 128, 239 "essential": true, 240 "image": "mongo:latest", 241 "memory": 128, 242 "name": "mongodb" 243 } 244 ] 245 DEFINITION 246 } 247 248 resource "aws_ecs_service" "mongo" { 249 name = "mongodb" 250 cluster = "${aws_ecs_cluster.default.id}" 251 task_definition = "${aws_ecs_task_definition.mongo.arn}" 252 desired_count = 2 253 } 254 ` 255 256 var testAccAWSEcsServiceWithFamilyAndRevision = ` 257 resource "aws_ecs_cluster" "default" { 258 name = "terraformecstest2" 259 } 260 261 resource "aws_ecs_task_definition" "jenkins" { 262 family = "jenkins" 263 container_definitions = <<DEFINITION 264 [ 265 { 266 "cpu": 128, 267 "essential": true, 268 "image": "jenkins:latest", 269 "memory": 128, 270 "name": "jenkins" 271 } 272 ] 273 DEFINITION 274 } 275 276 resource "aws_ecs_service" "jenkins" { 277 name = "jenkins" 278 cluster = "${aws_ecs_cluster.default.id}" 279 task_definition = "${aws_ecs_task_definition.jenkins.family}:${aws_ecs_task_definition.jenkins.revision}" 280 desired_count = 1 281 } 282 ` 283 284 var testAccAWSEcsServiceWithFamilyAndRevisionModified = ` 285 resource "aws_ecs_cluster" "default" { 286 name = "terraformecstest2" 287 } 288 289 resource "aws_ecs_task_definition" "jenkins" { 290 family = "jenkins" 291 container_definitions = <<DEFINITION 292 [ 293 { 294 "cpu": 128, 295 "essential": true, 296 "image": "jenkins:latest", 297 "memory": 128, 298 "name": "jenkins" 299 } 300 ] 301 DEFINITION 302 } 303 304 resource "aws_ecs_service" "jenkins" { 305 name = "jenkins" 306 cluster = "${aws_ecs_cluster.default.id}" 307 task_definition = "${aws_ecs_task_definition.jenkins.family}:${aws_ecs_task_definition.jenkins.revision}" 308 desired_count = 1 309 } 310 ` 311 312 var testAccAWSEcsServiceWithRenamedCluster = ` 313 resource "aws_ecs_cluster" "default" { 314 name = "terraformecstest3" 315 } 316 resource "aws_ecs_task_definition" "ghost" { 317 family = "ghost" 318 container_definitions = <<DEFINITION 319 [ 320 { 321 "cpu": 128, 322 "essential": true, 323 "image": "ghost:latest", 324 "memory": 128, 325 "name": "ghost" 326 } 327 ] 328 DEFINITION 329 } 330 resource "aws_ecs_service" "ghost" { 331 name = "ghost" 332 cluster = "${aws_ecs_cluster.default.id}" 333 task_definition = "${aws_ecs_task_definition.ghost.family}:${aws_ecs_task_definition.ghost.revision}" 334 desired_count = 1 335 } 336 ` 337 338 var testAccAWSEcsServiceWithRenamedClusterModified = ` 339 resource "aws_ecs_cluster" "default" { 340 name = "terraformecstest3modified" 341 } 342 resource "aws_ecs_task_definition" "ghost" { 343 family = "ghost" 344 container_definitions = <<DEFINITION 345 [ 346 { 347 "cpu": 128, 348 "essential": true, 349 "image": "ghost:latest", 350 "memory": 128, 351 "name": "ghost" 352 } 353 ] 354 DEFINITION 355 } 356 resource "aws_ecs_service" "ghost" { 357 name = "ghost" 358 cluster = "${aws_ecs_cluster.default.id}" 359 task_definition = "${aws_ecs_task_definition.ghost.family}:${aws_ecs_task_definition.ghost.revision}" 360 desired_count = 1 361 } 362 `