github.com/jsoriano/terraform@v0.6.7-0.20151026070445-8b70867fdd95/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 TestAccAWSEcsService_withIamRole(t *testing.T) {
   166  	resource.Test(t, resource.TestCase{
   167  		PreCheck:     func() { testAccPreCheck(t) },
   168  		Providers:    testAccProviders,
   169  		CheckDestroy: testAccCheckAWSEcsServiceDestroy,
   170  		Steps: []resource.TestStep{
   171  			resource.TestStep{
   172  				Config: testAccAWSEcsService_withIamRole,
   173  				Check: resource.ComposeTestCheckFunc(
   174  					testAccCheckAWSEcsServiceExists("aws_ecs_service.ghost"),
   175  				),
   176  			},
   177  		},
   178  	})
   179  }
   180  
   181  func testAccCheckAWSEcsServiceDestroy(s *terraform.State) error {
   182  	conn := testAccProvider.Meta().(*AWSClient).ecsconn
   183  
   184  	for _, rs := range s.RootModule().Resources {
   185  		if rs.Type != "aws_ecs_service" {
   186  			continue
   187  		}
   188  
   189  		out, err := conn.DescribeServices(&ecs.DescribeServicesInput{
   190  			Services: []*string{aws.String(rs.Primary.ID)},
   191  		})
   192  
   193  		if err == nil {
   194  			if len(out.Services) > 0 {
   195  				return fmt.Errorf("ECS service still exists:\n%#v", out.Services)
   196  			}
   197  		}
   198  
   199  		return err
   200  	}
   201  
   202  	return nil
   203  }
   204  
   205  func testAccCheckAWSEcsServiceExists(name string) resource.TestCheckFunc {
   206  	return func(s *terraform.State) error {
   207  		_, ok := s.RootModule().Resources[name]
   208  		if !ok {
   209  			return fmt.Errorf("Not found: %s", name)
   210  		}
   211  
   212  		return nil
   213  	}
   214  }
   215  
   216  var testAccAWSEcsService = `
   217  resource "aws_ecs_cluster" "default" {
   218  	name = "terraformecstest1"
   219  }
   220  
   221  resource "aws_ecs_task_definition" "mongo" {
   222    family = "mongodb"
   223    container_definitions = <<DEFINITION
   224  [
   225    {
   226      "cpu": 128,
   227      "essential": true,
   228      "image": "mongo:latest",
   229      "memory": 128,
   230      "name": "mongodb"
   231    }
   232  ]
   233  DEFINITION
   234  }
   235  
   236  resource "aws_ecs_service" "mongo" {
   237    name = "mongodb"
   238    cluster = "${aws_ecs_cluster.default.id}"
   239    task_definition = "${aws_ecs_task_definition.mongo.arn}"
   240    desired_count = 1
   241  }
   242  `
   243  
   244  var testAccAWSEcsServiceModified = `
   245  resource "aws_ecs_cluster" "default" {
   246  	name = "terraformecstest1"
   247  }
   248  
   249  resource "aws_ecs_task_definition" "mongo" {
   250    family = "mongodb"
   251    container_definitions = <<DEFINITION
   252  [
   253    {
   254      "cpu": 128,
   255      "essential": true,
   256      "image": "mongo:latest",
   257      "memory": 128,
   258      "name": "mongodb"
   259    }
   260  ]
   261  DEFINITION
   262  }
   263  
   264  resource "aws_ecs_service" "mongo" {
   265    name = "mongodb"
   266    cluster = "${aws_ecs_cluster.default.id}"
   267    task_definition = "${aws_ecs_task_definition.mongo.arn}"
   268    desired_count = 2
   269  }
   270  `
   271  
   272  var testAccAWSEcsService_withIamRole = `
   273  resource "aws_ecs_cluster" "main" {
   274  	name = "terraformecstest11"
   275  }
   276  
   277  resource "aws_ecs_task_definition" "ghost" {
   278    family = "ghost_service"
   279    container_definitions = <<DEFINITION
   280  [
   281    {
   282      "cpu": 128,
   283      "essential": true,
   284      "image": "ghost:latest",
   285      "memory": 128,
   286      "name": "ghost",
   287      "portMappings": [
   288        {
   289          "containerPort": 2368,
   290          "hostPort": 8080
   291        }
   292      ]
   293    }
   294  ]
   295  DEFINITION
   296  }
   297  
   298  resource "aws_iam_role" "ecs_service" {
   299      name = "EcsService"
   300      assume_role_policy = <<EOF
   301  {
   302      "Version": "2008-10-17",
   303      "Statement": [
   304          {
   305              "Action": "sts:AssumeRole",
   306              "Principal": {"AWS": "*"},
   307              "Effect": "Allow",
   308              "Sid": ""
   309          }
   310      ]
   311  }
   312  EOF
   313  }
   314  
   315  resource "aws_iam_role_policy" "ecs_service" {
   316      name = "EcsService"
   317      role = "${aws_iam_role.ecs_service.name}"
   318      policy = <<EOF
   319  {
   320    "Version": "2012-10-17",
   321    "Statement": [
   322      {
   323        "Effect": "Allow",
   324        "Action": [
   325          "elasticloadbalancing:*",
   326          "ec2:*",
   327          "ecs:*"
   328        ],
   329        "Resource": [
   330          "*"
   331        ]
   332      }
   333    ]
   334  }
   335  EOF
   336  }
   337  
   338  resource "aws_elb" "main" {
   339    name = "foobar-terraform-test"
   340    availability_zones = ["us-west-2a"]
   341  
   342    listener {
   343      instance_port = 8080
   344      instance_protocol = "http"
   345      lb_port = 80
   346      lb_protocol = "http"
   347    }
   348  }
   349  
   350  resource "aws_ecs_service" "ghost" {
   351    name = "ghost"
   352    cluster = "${aws_ecs_cluster.main.id}"
   353    task_definition = "${aws_ecs_task_definition.ghost.arn}"
   354    desired_count = 1
   355    iam_role = "${aws_iam_role.ecs_service.name}"
   356  
   357    load_balancer {
   358      elb_name = "${aws_elb.main.id}"
   359      container_name = "ghost"
   360      container_port = "2368"
   361    }
   362  
   363    depends_on = ["aws_iam_role_policy.ecs_service"]
   364  }
   365  `
   366  
   367  var testAccAWSEcsServiceWithFamilyAndRevision = `
   368  resource "aws_ecs_cluster" "default" {
   369  	name = "terraformecstest2"
   370  }
   371  
   372  resource "aws_ecs_task_definition" "jenkins" {
   373    family = "jenkins"
   374    container_definitions = <<DEFINITION
   375  [
   376    {
   377      "cpu": 128,
   378      "essential": true,
   379      "image": "jenkins:latest",
   380      "memory": 128,
   381      "name": "jenkins"
   382    }
   383  ]
   384  DEFINITION
   385  }
   386  
   387  resource "aws_ecs_service" "jenkins" {
   388    name = "jenkins"
   389    cluster = "${aws_ecs_cluster.default.id}"
   390    task_definition = "${aws_ecs_task_definition.jenkins.family}:${aws_ecs_task_definition.jenkins.revision}"
   391    desired_count = 1
   392  }
   393  `
   394  
   395  var testAccAWSEcsServiceWithFamilyAndRevisionModified = `
   396  resource "aws_ecs_cluster" "default" {
   397  	name = "terraformecstest2"
   398  }
   399  
   400  resource "aws_ecs_task_definition" "jenkins" {
   401    family = "jenkins"
   402    container_definitions = <<DEFINITION
   403  [
   404    {
   405      "cpu": 128,
   406      "essential": true,
   407      "image": "jenkins:latest",
   408      "memory": 128,
   409      "name": "jenkins"
   410    }
   411  ]
   412  DEFINITION
   413  }
   414  
   415  resource "aws_ecs_service" "jenkins" {
   416    name = "jenkins"
   417    cluster = "${aws_ecs_cluster.default.id}"
   418    task_definition = "${aws_ecs_task_definition.jenkins.family}:${aws_ecs_task_definition.jenkins.revision}"
   419    desired_count = 1
   420  }
   421  `
   422  
   423  var testAccAWSEcsServiceWithRenamedCluster = `
   424  resource "aws_ecs_cluster" "default" {
   425  	name = "terraformecstest3"
   426  }
   427  resource "aws_ecs_task_definition" "ghost" {
   428    family = "ghost"
   429    container_definitions = <<DEFINITION
   430  [
   431    {
   432      "cpu": 128,
   433      "essential": true,
   434      "image": "ghost:latest",
   435      "memory": 128,
   436      "name": "ghost"
   437    }
   438  ]
   439  DEFINITION
   440  }
   441  resource "aws_ecs_service" "ghost" {
   442    name = "ghost"
   443    cluster = "${aws_ecs_cluster.default.id}"
   444    task_definition = "${aws_ecs_task_definition.ghost.family}:${aws_ecs_task_definition.ghost.revision}"
   445    desired_count = 1
   446  }
   447  `
   448  
   449  var testAccAWSEcsServiceWithRenamedClusterModified = `
   450  resource "aws_ecs_cluster" "default" {
   451  	name = "terraformecstest3modified"
   452  }
   453  resource "aws_ecs_task_definition" "ghost" {
   454    family = "ghost"
   455    container_definitions = <<DEFINITION
   456  [
   457    {
   458      "cpu": 128,
   459      "essential": true,
   460      "image": "ghost:latest",
   461      "memory": 128,
   462      "name": "ghost"
   463    }
   464  ]
   465  DEFINITION
   466  }
   467  resource "aws_ecs_service" "ghost" {
   468    name = "ghost"
   469    cluster = "${aws_ecs_cluster.default.id}"
   470    task_definition = "${aws_ecs_task_definition.ghost.family}:${aws_ecs_task_definition.ghost.revision}"
   471    desired_count = 1
   472  }
   473  `