github.com/gwilym/terraform@v0.3.8-0.20151231151641-c7573de75b19/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/aws/awserr"
    10  	"github.com/aws/aws-sdk-go/service/ecs"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestParseTaskDefinition(t *testing.T) {
    16  	cases := map[string]map[string]interface{}{
    17  		"invalid": map[string]interface{}{
    18  			"family":   "",
    19  			"revision": "",
    20  			"isValid":  false,
    21  		},
    22  		"invalidWithColon:": map[string]interface{}{
    23  			"family":   "",
    24  			"revision": "",
    25  			"isValid":  false,
    26  		},
    27  		"1234": map[string]interface{}{
    28  			"family":   "",
    29  			"revision": "",
    30  			"isValid":  false,
    31  		},
    32  		"invalid:aaa": map[string]interface{}{
    33  			"family":   "",
    34  			"revision": "",
    35  			"isValid":  false,
    36  		},
    37  		"invalid=family:1": map[string]interface{}{
    38  			"family":   "",
    39  			"revision": "",
    40  			"isValid":  false,
    41  		},
    42  		"invalid:name:1": map[string]interface{}{
    43  			"family":   "",
    44  			"revision": "",
    45  			"isValid":  false,
    46  		},
    47  		"valid:1": map[string]interface{}{
    48  			"family":   "valid",
    49  			"revision": "1",
    50  			"isValid":  true,
    51  		},
    52  		"abc12-def:54": map[string]interface{}{
    53  			"family":   "abc12-def",
    54  			"revision": "54",
    55  			"isValid":  true,
    56  		},
    57  		"lorem_ip-sum:123": map[string]interface{}{
    58  			"family":   "lorem_ip-sum",
    59  			"revision": "123",
    60  			"isValid":  true,
    61  		},
    62  		"lorem-ipsum:1": map[string]interface{}{
    63  			"family":   "lorem-ipsum",
    64  			"revision": "1",
    65  			"isValid":  true,
    66  		},
    67  	}
    68  
    69  	for input, expectedOutput := range cases {
    70  		family, revision, err := parseTaskDefinition(input)
    71  		isValid := expectedOutput["isValid"].(bool)
    72  		if !isValid && err == nil {
    73  			t.Fatalf("Task definition %s should fail", input)
    74  		}
    75  
    76  		expectedFamily := expectedOutput["family"].(string)
    77  		if family != expectedFamily {
    78  			t.Fatalf("Unexpected family (%#v) for task definition %s\n%#v", family, input, err)
    79  		}
    80  		expectedRevision := expectedOutput["revision"].(string)
    81  		if revision != expectedRevision {
    82  			t.Fatalf("Unexpected revision (%#v) for task definition %s\n%#v", revision, input, err)
    83  		}
    84  	}
    85  }
    86  
    87  func TestAccAWSEcsServiceWithARN(t *testing.T) {
    88  	resource.Test(t, resource.TestCase{
    89  		PreCheck:     func() { testAccPreCheck(t) },
    90  		Providers:    testAccProviders,
    91  		CheckDestroy: testAccCheckAWSEcsServiceDestroy,
    92  		Steps: []resource.TestStep{
    93  			resource.TestStep{
    94  				Config: testAccAWSEcsService,
    95  				Check: resource.ComposeTestCheckFunc(
    96  					testAccCheckAWSEcsServiceExists("aws_ecs_service.mongo"),
    97  				),
    98  			},
    99  
   100  			resource.TestStep{
   101  				Config: testAccAWSEcsServiceModified,
   102  				Check: resource.ComposeTestCheckFunc(
   103  					testAccCheckAWSEcsServiceExists("aws_ecs_service.mongo"),
   104  				),
   105  			},
   106  		},
   107  	})
   108  }
   109  
   110  func TestAccAWSEcsServiceWithFamilyAndRevision(t *testing.T) {
   111  	resource.Test(t, resource.TestCase{
   112  		PreCheck:     func() { testAccPreCheck(t) },
   113  		Providers:    testAccProviders,
   114  		CheckDestroy: testAccCheckAWSEcsServiceDestroy,
   115  		Steps: []resource.TestStep{
   116  			resource.TestStep{
   117  				Config: testAccAWSEcsServiceWithFamilyAndRevision,
   118  				Check: resource.ComposeTestCheckFunc(
   119  					testAccCheckAWSEcsServiceExists("aws_ecs_service.jenkins"),
   120  				),
   121  			},
   122  
   123  			resource.TestStep{
   124  				Config: testAccAWSEcsServiceWithFamilyAndRevisionModified,
   125  				Check: resource.ComposeTestCheckFunc(
   126  					testAccCheckAWSEcsServiceExists("aws_ecs_service.jenkins"),
   127  				),
   128  			},
   129  		},
   130  	})
   131  }
   132  
   133  // Regression for https://github.com/hashicorp/terraform/issues/2427
   134  func TestAccAWSEcsServiceWithRenamedCluster(t *testing.T) {
   135  	originalRegexp := regexp.MustCompile(
   136  		"^arn:aws:ecs:[^:]+:[0-9]+:cluster/terraformecstest3$")
   137  	modifiedRegexp := regexp.MustCompile(
   138  		"^arn:aws:ecs:[^:]+:[0-9]+:cluster/terraformecstest3modified$")
   139  
   140  	resource.Test(t, resource.TestCase{
   141  		PreCheck:     func() { testAccPreCheck(t) },
   142  		Providers:    testAccProviders,
   143  		CheckDestroy: testAccCheckAWSEcsServiceDestroy,
   144  		Steps: []resource.TestStep{
   145  			resource.TestStep{
   146  				Config: testAccAWSEcsServiceWithRenamedCluster,
   147  				Check: resource.ComposeTestCheckFunc(
   148  					testAccCheckAWSEcsServiceExists("aws_ecs_service.ghost"),
   149  					resource.TestMatchResourceAttr(
   150  						"aws_ecs_service.ghost", "cluster", originalRegexp),
   151  				),
   152  			},
   153  
   154  			resource.TestStep{
   155  				Config: testAccAWSEcsServiceWithRenamedClusterModified,
   156  				Check: resource.ComposeTestCheckFunc(
   157  					testAccCheckAWSEcsServiceExists("aws_ecs_service.ghost"),
   158  					resource.TestMatchResourceAttr(
   159  						"aws_ecs_service.ghost", "cluster", modifiedRegexp),
   160  				),
   161  			},
   162  		},
   163  	})
   164  }
   165  
   166  func TestAccAWSEcsService_withIamRole(t *testing.T) {
   167  	resource.Test(t, resource.TestCase{
   168  		PreCheck:     func() { testAccPreCheck(t) },
   169  		Providers:    testAccProviders,
   170  		CheckDestroy: testAccCheckAWSEcsServiceDestroy,
   171  		Steps: []resource.TestStep{
   172  			resource.TestStep{
   173  				Config: testAccAWSEcsService_withIamRole,
   174  				Check: resource.ComposeTestCheckFunc(
   175  					testAccCheckAWSEcsServiceExists("aws_ecs_service.ghost"),
   176  				),
   177  			},
   178  		},
   179  	})
   180  }
   181  
   182  // Regression for https://github.com/hashicorp/terraform/issues/3361
   183  func TestAccAWSEcsService_withEcsClusterName(t *testing.T) {
   184  	clusterName := regexp.MustCompile("^terraformecstestcluster$")
   185  	resource.Test(t, resource.TestCase{
   186  		PreCheck:     func() { testAccPreCheck(t) },
   187  		Providers:    testAccProviders,
   188  		CheckDestroy: testAccCheckAWSEcsServiceDestroy,
   189  		Steps: []resource.TestStep{
   190  			resource.TestStep{
   191  				Config: testAccAWSEcsServiceWithEcsClusterName,
   192  				Check: resource.ComposeTestCheckFunc(
   193  					testAccCheckAWSEcsServiceExists("aws_ecs_service.jenkins"),
   194  					resource.TestMatchResourceAttr(
   195  						"aws_ecs_service.jenkins", "cluster", clusterName),
   196  				),
   197  			},
   198  		},
   199  	})
   200  }
   201  
   202  func testAccCheckAWSEcsServiceDestroy(s *terraform.State) error {
   203  	conn := testAccProvider.Meta().(*AWSClient).ecsconn
   204  
   205  	for _, rs := range s.RootModule().Resources {
   206  		if rs.Type != "aws_ecs_service" {
   207  			continue
   208  		}
   209  
   210  		out, err := conn.DescribeServices(&ecs.DescribeServicesInput{
   211  			Services: []*string{aws.String(rs.Primary.ID)},
   212  		})
   213  
   214  		if awserr, ok := err.(awserr.Error); ok && awserr.Code() == "ClusterNotFoundException" {
   215  			continue
   216  		}
   217  
   218  		if err == nil {
   219  			if len(out.Services) > 0 {
   220  				return fmt.Errorf("ECS service still exists:\n%#v", out.Services)
   221  			}
   222  		}
   223  
   224  		return err
   225  	}
   226  
   227  	return nil
   228  }
   229  
   230  func testAccCheckAWSEcsServiceExists(name string) resource.TestCheckFunc {
   231  	return func(s *terraform.State) error {
   232  		_, ok := s.RootModule().Resources[name]
   233  		if !ok {
   234  			return fmt.Errorf("Not found: %s", name)
   235  		}
   236  
   237  		return nil
   238  	}
   239  }
   240  
   241  var testAccAWSEcsService = `
   242  resource "aws_ecs_cluster" "default" {
   243  	name = "terraformecstest1"
   244  }
   245  
   246  resource "aws_ecs_task_definition" "mongo" {
   247    family = "mongodb"
   248    container_definitions = <<DEFINITION
   249  [
   250    {
   251      "cpu": 128,
   252      "essential": true,
   253      "image": "mongo:latest",
   254      "memory": 128,
   255      "name": "mongodb"
   256    }
   257  ]
   258  DEFINITION
   259  }
   260  
   261  resource "aws_ecs_service" "mongo" {
   262    name = "mongodb"
   263    cluster = "${aws_ecs_cluster.default.id}"
   264    task_definition = "${aws_ecs_task_definition.mongo.arn}"
   265    desired_count = 1
   266  }
   267  `
   268  
   269  var testAccAWSEcsServiceModified = `
   270  resource "aws_ecs_cluster" "default" {
   271  	name = "terraformecstest1"
   272  }
   273  
   274  resource "aws_ecs_task_definition" "mongo" {
   275    family = "mongodb"
   276    container_definitions = <<DEFINITION
   277  [
   278    {
   279      "cpu": 128,
   280      "essential": true,
   281      "image": "mongo:latest",
   282      "memory": 128,
   283      "name": "mongodb"
   284    }
   285  ]
   286  DEFINITION
   287  }
   288  
   289  resource "aws_ecs_service" "mongo" {
   290    name = "mongodb"
   291    cluster = "${aws_ecs_cluster.default.id}"
   292    task_definition = "${aws_ecs_task_definition.mongo.arn}"
   293    desired_count = 2
   294  }
   295  `
   296  
   297  var testAccAWSEcsService_withIamRole = `
   298  resource "aws_ecs_cluster" "main" {
   299  	name = "terraformecstest11"
   300  }
   301  
   302  resource "aws_ecs_task_definition" "ghost" {
   303    family = "ghost_service"
   304    container_definitions = <<DEFINITION
   305  [
   306    {
   307      "cpu": 128,
   308      "essential": true,
   309      "image": "ghost:latest",
   310      "memory": 128,
   311      "name": "ghost",
   312      "portMappings": [
   313        {
   314          "containerPort": 2368,
   315          "hostPort": 8080
   316        }
   317      ]
   318    }
   319  ]
   320  DEFINITION
   321  }
   322  
   323  resource "aws_iam_role" "ecs_service" {
   324      name = "EcsService"
   325      assume_role_policy = <<EOF
   326  {
   327      "Version": "2012-10-17",
   328      "Statement": [
   329          {
   330              "Action": "sts:AssumeRole",
   331              "Principal": {"AWS": "*"},
   332              "Effect": "Allow",
   333              "Sid": ""
   334          }
   335      ]
   336  }
   337  EOF
   338  }
   339  
   340  resource "aws_iam_role_policy" "ecs_service" {
   341      name = "EcsService"
   342      role = "${aws_iam_role.ecs_service.name}"
   343      policy = <<EOF
   344  {
   345    "Version": "2012-10-17",
   346    "Statement": [
   347      {
   348        "Effect": "Allow",
   349        "Action": [
   350          "elasticloadbalancing:*",
   351          "ec2:*",
   352          "ecs:*"
   353        ],
   354        "Resource": [
   355          "*"
   356        ]
   357      }
   358    ]
   359  }
   360  EOF
   361  }
   362  
   363  resource "aws_elb" "main" {
   364    availability_zones = ["us-west-2a"]
   365  
   366    listener {
   367      instance_port = 8080
   368      instance_protocol = "http"
   369      lb_port = 80
   370      lb_protocol = "http"
   371    }
   372  }
   373  
   374  resource "aws_ecs_service" "ghost" {
   375    name = "ghost"
   376    cluster = "${aws_ecs_cluster.main.id}"
   377    task_definition = "${aws_ecs_task_definition.ghost.arn}"
   378    desired_count = 1
   379    iam_role = "${aws_iam_role.ecs_service.name}"
   380  
   381    load_balancer {
   382      elb_name = "${aws_elb.main.id}"
   383      container_name = "ghost"
   384      container_port = "2368"
   385    }
   386  
   387    depends_on = ["aws_iam_role_policy.ecs_service"]
   388  }
   389  `
   390  
   391  var testAccAWSEcsServiceWithFamilyAndRevision = `
   392  resource "aws_ecs_cluster" "default" {
   393  	name = "terraformecstest2"
   394  }
   395  
   396  resource "aws_ecs_task_definition" "jenkins" {
   397    family = "jenkins"
   398    container_definitions = <<DEFINITION
   399  [
   400    {
   401      "cpu": 128,
   402      "essential": true,
   403      "image": "jenkins:latest",
   404      "memory": 128,
   405      "name": "jenkins"
   406    }
   407  ]
   408  DEFINITION
   409  }
   410  
   411  resource "aws_ecs_service" "jenkins" {
   412    name = "jenkins"
   413    cluster = "${aws_ecs_cluster.default.id}"
   414    task_definition = "${aws_ecs_task_definition.jenkins.family}:${aws_ecs_task_definition.jenkins.revision}"
   415    desired_count = 1
   416  }
   417  `
   418  
   419  var testAccAWSEcsServiceWithFamilyAndRevisionModified = `
   420  resource "aws_ecs_cluster" "default" {
   421  	name = "terraformecstest2"
   422  }
   423  
   424  resource "aws_ecs_task_definition" "jenkins" {
   425    family = "jenkins"
   426    container_definitions = <<DEFINITION
   427  [
   428    {
   429      "cpu": 128,
   430      "essential": true,
   431      "image": "jenkins:latest",
   432      "memory": 128,
   433      "name": "jenkins"
   434    }
   435  ]
   436  DEFINITION
   437  }
   438  
   439  resource "aws_ecs_service" "jenkins" {
   440    name = "jenkins"
   441    cluster = "${aws_ecs_cluster.default.id}"
   442    task_definition = "${aws_ecs_task_definition.jenkins.family}:${aws_ecs_task_definition.jenkins.revision}"
   443    desired_count = 1
   444  }
   445  `
   446  
   447  var testAccAWSEcsServiceWithRenamedCluster = `
   448  resource "aws_ecs_cluster" "default" {
   449  	name = "terraformecstest3"
   450  }
   451  resource "aws_ecs_task_definition" "ghost" {
   452    family = "ghost"
   453    container_definitions = <<DEFINITION
   454  [
   455    {
   456      "cpu": 128,
   457      "essential": true,
   458      "image": "ghost:latest",
   459      "memory": 128,
   460      "name": "ghost"
   461    }
   462  ]
   463  DEFINITION
   464  }
   465  resource "aws_ecs_service" "ghost" {
   466    name = "ghost"
   467    cluster = "${aws_ecs_cluster.default.id}"
   468    task_definition = "${aws_ecs_task_definition.ghost.family}:${aws_ecs_task_definition.ghost.revision}"
   469    desired_count = 1
   470  }
   471  `
   472  
   473  var testAccAWSEcsServiceWithRenamedClusterModified = `
   474  resource "aws_ecs_cluster" "default" {
   475  	name = "terraformecstest3modified"
   476  }
   477  resource "aws_ecs_task_definition" "ghost" {
   478    family = "ghost"
   479    container_definitions = <<DEFINITION
   480  [
   481    {
   482      "cpu": 128,
   483      "essential": true,
   484      "image": "ghost:latest",
   485      "memory": 128,
   486      "name": "ghost"
   487    }
   488  ]
   489  DEFINITION
   490  }
   491  resource "aws_ecs_service" "ghost" {
   492    name = "ghost"
   493    cluster = "${aws_ecs_cluster.default.id}"
   494    task_definition = "${aws_ecs_task_definition.ghost.family}:${aws_ecs_task_definition.ghost.revision}"
   495    desired_count = 1
   496  }
   497  `
   498  
   499  var testAccAWSEcsServiceWithEcsClusterName = `
   500  resource "aws_ecs_cluster" "default" {
   501  	name = "terraformecstestcluster"
   502  }
   503  
   504  resource "aws_ecs_task_definition" "jenkins" {
   505    family = "jenkins"
   506    container_definitions = <<DEFINITION
   507  [
   508    {
   509      "cpu": 128,
   510      "essential": true,
   511      "image": "jenkins:latest",
   512      "memory": 128,
   513      "name": "jenkins"
   514    }
   515  ]
   516  DEFINITION
   517  }
   518  
   519  resource "aws_ecs_service" "jenkins" {
   520    name = "jenkins"
   521    cluster = "${aws_ecs_cluster.default.name}"
   522    task_definition = "${aws_ecs_task_definition.jenkins.arn}"
   523    desired_count = 1
   524  }
   525  `