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