github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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 TestAccAWSEcsTaskDefinition_withNetworkMode(t *testing.T) {
    94  	resource.Test(t, resource.TestCase{
    95  		PreCheck:     func() { testAccPreCheck(t) },
    96  		Providers:    testAccProviders,
    97  		CheckDestroy: testAccCheckAWSEcsTaskDefinitionDestroy,
    98  		Steps: []resource.TestStep{
    99  			resource.TestStep{
   100  				Config: testAccAWSEcsTaskDefinitionWithNetworkMode,
   101  				Check: resource.ComposeTestCheckFunc(
   102  					testAccCheckAWSEcsTaskDefinitionExists("aws_ecs_task_definition.sleep"),
   103  					resource.TestCheckResourceAttr(
   104  						"aws_ecs_task_definition.sleep", "network_mode", "bridge"),
   105  				),
   106  			},
   107  		},
   108  	})
   109  }
   110  
   111  func TestValidateAwsEcsTaskDefinitionNetworkMode(t *testing.T) {
   112  	validNames := []string{
   113  		"bridge",
   114  		"host",
   115  		"none",
   116  	}
   117  	for _, v := range validNames {
   118  		_, errors := validateAwsEcsTaskDefinitionNetworkMode(v, "network_mode")
   119  		if len(errors) != 0 {
   120  			t.Fatalf("%q should be a valid AWS ECS Task Definition Network Mode: %q", v, errors)
   121  		}
   122  	}
   123  
   124  	invalidNames := []string{
   125  		"bridged",
   126  		"-docker",
   127  	}
   128  	for _, v := range invalidNames {
   129  		_, errors := validateAwsEcsTaskDefinitionNetworkMode(v, "network_mode")
   130  		if len(errors) == 0 {
   131  			t.Fatalf("%q should be an invalid AWS ECS Task Definition Network Mode", v)
   132  		}
   133  	}
   134  }
   135  
   136  func testAccCheckAWSEcsTaskDefinitionDestroy(s *terraform.State) error {
   137  	conn := testAccProvider.Meta().(*AWSClient).ecsconn
   138  
   139  	for _, rs := range s.RootModule().Resources {
   140  		if rs.Type != "aws_ecs_task_definition" {
   141  			continue
   142  		}
   143  
   144  		input := ecs.DescribeTaskDefinitionInput{
   145  			TaskDefinition: aws.String(rs.Primary.Attributes["arn"]),
   146  		}
   147  
   148  		out, err := conn.DescribeTaskDefinition(&input)
   149  
   150  		if err != nil {
   151  			return err
   152  		}
   153  
   154  		if out.TaskDefinition != nil && *out.TaskDefinition.Status != "INACTIVE" {
   155  			return fmt.Errorf("ECS task definition still exists:\n%#v", *out.TaskDefinition)
   156  		}
   157  	}
   158  
   159  	return nil
   160  }
   161  
   162  func testAccCheckAWSEcsTaskDefinitionExists(name string) resource.TestCheckFunc {
   163  	return func(s *terraform.State) error {
   164  		_, ok := s.RootModule().Resources[name]
   165  		if !ok {
   166  			return fmt.Errorf("Not found: %s", name)
   167  		}
   168  
   169  		return nil
   170  	}
   171  }
   172  
   173  var testAccAWSEcsTaskDefinition = `
   174  resource "aws_ecs_task_definition" "jenkins" {
   175    family = "terraform-acc-test"
   176    container_definitions = <<TASK_DEFINITION
   177  [
   178  	{
   179  		"cpu": 10,
   180  		"command": ["sleep", "10"],
   181  		"entryPoint": ["/"],
   182  		"environment": [
   183  			{"name": "VARNAME", "value": "VARVAL"}
   184  		],
   185  		"essential": true,
   186  		"image": "jenkins",
   187  		"links": ["mongodb"],
   188  		"memory": 128,
   189  		"name": "jenkins",
   190  		"portMappings": [
   191  			{
   192  				"containerPort": 80,
   193  				"hostPort": 8080
   194  			}
   195  		]
   196  	},
   197  	{
   198  		"cpu": 10,
   199  		"command": ["sleep", "10"],
   200  		"entryPoint": ["/"],
   201  		"essential": true,
   202  		"image": "mongodb",
   203  		"memory": 128,
   204  		"name": "mongodb",
   205  		"portMappings": [
   206  			{
   207  				"containerPort": 28017,
   208  				"hostPort": 28017
   209  			}
   210  		]
   211  	}
   212  ]
   213  TASK_DEFINITION
   214  
   215    volume {
   216      name = "jenkins-home"
   217      host_path = "/ecs/jenkins-home"
   218    }
   219  }
   220  `
   221  
   222  var testAccAWSEcsTaskDefinitionWithScratchVolume = `
   223  resource "aws_ecs_task_definition" "sleep" {
   224    family = "terraform-acc-sc-volume-test"
   225    container_definitions = <<TASK_DEFINITION
   226  [
   227    {
   228      "name": "sleep",
   229      "image": "busybox",
   230      "cpu": 10,
   231      "command": ["sleep","360"],
   232      "memory": 10,
   233      "essential": true
   234    }
   235  ]
   236  TASK_DEFINITION
   237  
   238    volume {
   239      name = "database_scratch"
   240    }
   241  }
   242  `
   243  
   244  var testAccAWSEcsTaskDefinitionWithTaskRoleArn = `
   245  resource "aws_iam_role" "role_test" {
   246    name = "tf_old_name"
   247    path = "/test/"
   248    assume_role_policy = <<EOF
   249  {
   250    "Version": "2012-10-17",
   251    "Statement": [
   252      {
   253        "Action": "sts:AssumeRole",
   254        "Principal": {
   255          "Service": "ec2.amazonaws.com"
   256        },
   257        "Effect": "Allow",
   258        "Sid": ""
   259      }
   260    ]
   261  }
   262  EOF
   263  }
   264  
   265  resource "aws_iam_role_policy" "role_test" {
   266    name = "role_update_test"
   267    role = "${aws_iam_role.role_test.id}"
   268    policy = <<EOF
   269  {
   270    "Version": "2012-10-17",
   271    "Statement": [
   272      {
   273        "Effect": "Allow",
   274        "Action": [
   275          "s3:GetBucketLocation",
   276          "s3:ListAllMyBuckets"
   277        ],
   278        "Resource": "arn:aws:s3:::*"
   279      }
   280    ]
   281  }
   282  EOF
   283  }
   284  
   285  resource "aws_ecs_task_definition" "sleep" {
   286    family = "terraform-acc-sc-volume-test"
   287    task_role_arn = "${aws_iam_role.role_test.arn}"
   288    container_definitions = <<TASK_DEFINITION
   289  [
   290    {
   291      "name": "sleep",
   292      "image": "busybox",
   293      "cpu": 10,
   294      "command": ["sleep","360"],
   295      "memory": 10,
   296      "essential": true
   297    }
   298  ]
   299  TASK_DEFINITION
   300  
   301    volume {
   302      name = "database_scratch"
   303    }
   304  }
   305  `
   306  
   307  var testAccAWSEcsTaskDefinitionWithNetworkMode = `
   308  resource "aws_iam_role" "role_test" {
   309    name = "tf_old_name"
   310    path = "/test/"
   311    assume_role_policy = <<EOF
   312  {
   313    "Version": "2012-10-17",
   314    "Statement": [
   315      {
   316        "Action": "sts:AssumeRole",
   317        "Principal": {
   318          "Service": "ec2.amazonaws.com"
   319        },
   320        "Effect": "Allow",
   321        "Sid": ""
   322      }
   323    ]
   324  }
   325  EOF
   326  }
   327  
   328  resource "aws_iam_role_policy" "role_test" {
   329    name = "role_update_test"
   330    role = "${aws_iam_role.role_test.id}"
   331    policy = <<EOF
   332  {
   333    "Version": "2012-10-17",
   334    "Statement": [
   335      {
   336        "Effect": "Allow",
   337        "Action": [
   338          "s3:GetBucketLocation",
   339          "s3:ListAllMyBuckets"
   340        ],
   341        "Resource": "arn:aws:s3:::*"
   342      }
   343    ]
   344  }
   345  EOF
   346  }
   347  
   348  resource "aws_ecs_task_definition" "sleep" {
   349    family = "terraform-acc-sc-volume-test-network-mode"
   350    task_role_arn = "${aws_iam_role.role_test.arn}"
   351    network_mode = "bridge"
   352    container_definitions = <<TASK_DEFINITION
   353  [
   354    {
   355      "name": "sleep",
   356      "image": "busybox",
   357      "cpu": 10,
   358      "command": ["sleep","360"],
   359      "memory": 10,
   360      "essential": true
   361    }
   362  ]
   363  TASK_DEFINITION
   364  
   365    volume {
   366      name = "database_scratch"
   367    }
   368  }
   369  `
   370  
   371  var testAccAWSEcsTaskDefinitionWithEcsService = `
   372  resource "aws_ecs_cluster" "default" {
   373    name = "terraform-acc-test"
   374  }
   375  
   376  resource "aws_ecs_service" "sleep-svc" {
   377    name = "tf-acc-ecs-svc"
   378    cluster = "${aws_ecs_cluster.default.id}"
   379    task_definition = "${aws_ecs_task_definition.sleep.arn}"
   380    desired_count = 1
   381  }
   382  
   383  resource "aws_ecs_task_definition" "sleep" {
   384    family = "terraform-acc-sc-volume-test"
   385    container_definitions = <<TASK_DEFINITION
   386  [
   387    {
   388      "name": "sleep",
   389      "image": "busybox",
   390      "cpu": 10,
   391      "command": ["sleep","360"],
   392      "memory": 10,
   393      "essential": true
   394    }
   395  ]
   396  TASK_DEFINITION
   397  
   398    volume {
   399      name = "database_scratch"
   400    }
   401  }
   402  `
   403  var testAccAWSEcsTaskDefinitionWithEcsServiceModified = `
   404  resource "aws_ecs_cluster" "default" {
   405    name = "terraform-acc-test"
   406  }
   407  
   408  resource "aws_ecs_service" "sleep-svc" {
   409    name = "tf-acc-ecs-svc"
   410    cluster = "${aws_ecs_cluster.default.id}"
   411    task_definition = "${aws_ecs_task_definition.sleep.arn}"
   412    desired_count = 1
   413  }
   414  
   415  resource "aws_ecs_task_definition" "sleep" {
   416    family = "terraform-acc-sc-volume-test"
   417    container_definitions = <<TASK_DEFINITION
   418  [
   419    {
   420      "name": "sleep",
   421      "image": "busybox",
   422      "cpu": 20,
   423      "command": ["sleep","360"],
   424      "memory": 50,
   425      "essential": true
   426    }
   427  ]
   428  TASK_DEFINITION
   429  
   430    volume {
   431      name = "database_scratch"
   432    }
   433  }
   434  `
   435  
   436  var testAccAWSEcsTaskDefinitionModified = `
   437  resource "aws_ecs_task_definition" "jenkins" {
   438    family = "terraform-acc-test"
   439    container_definitions = <<TASK_DEFINITION
   440  [
   441  	{
   442  		"cpu": 10,
   443  		"command": ["sleep", "10"],
   444  		"entryPoint": ["/"],
   445  		"environment": [
   446  			{"name": "VARNAME", "value": "VARVAL"}
   447  		],
   448  		"essential": true,
   449  		"image": "jenkins",
   450  		"links": ["mongodb"],
   451  		"memory": 128,
   452  		"name": "jenkins",
   453  		"portMappings": [
   454  			{
   455  				"containerPort": 80,
   456  				"hostPort": 8080
   457  			}
   458  		]
   459  	},
   460  	{
   461  		"cpu": 20,
   462  		"command": ["sleep", "10"],
   463  		"entryPoint": ["/"],
   464  		"essential": true,
   465  		"image": "mongodb",
   466  		"memory": 128,
   467  		"name": "mongodb",
   468  		"portMappings": [
   469  			{
   470  				"containerPort": 28017,
   471  				"hostPort": 28017
   472  			}
   473  		]
   474  	}
   475  ]
   476  TASK_DEFINITION
   477  
   478    volume {
   479      name = "jenkins-home"
   480      host_path = "/ecs/jenkins-home"
   481    }
   482  }
   483  `