github.com/gabrielperezs/terraform@v0.7.0-rc2.0.20160715084931-f7da2612946f/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 testAccCheckAWSEcsTaskDefinitionDestroy(s *terraform.State) error {
    78  	conn := testAccProvider.Meta().(*AWSClient).ecsconn
    79  
    80  	for _, rs := range s.RootModule().Resources {
    81  		if rs.Type != "aws_ecs_task_definition" {
    82  			continue
    83  		}
    84  
    85  		input := ecs.DescribeTaskDefinitionInput{
    86  			TaskDefinition: aws.String(rs.Primary.Attributes["arn"]),
    87  		}
    88  
    89  		out, err := conn.DescribeTaskDefinition(&input)
    90  
    91  		if err != nil {
    92  			return err
    93  		}
    94  
    95  		if out.TaskDefinition != nil && *out.TaskDefinition.Status != "INACTIVE" {
    96  			return fmt.Errorf("ECS task definition still exists:\n%#v", *out.TaskDefinition)
    97  		}
    98  	}
    99  
   100  	return nil
   101  }
   102  
   103  func testAccCheckAWSEcsTaskDefinitionExists(name string) resource.TestCheckFunc {
   104  	return func(s *terraform.State) error {
   105  		_, ok := s.RootModule().Resources[name]
   106  		if !ok {
   107  			return fmt.Errorf("Not found: %s", name)
   108  		}
   109  
   110  		return nil
   111  	}
   112  }
   113  
   114  var testAccAWSEcsTaskDefinition = `
   115  resource "aws_ecs_task_definition" "jenkins" {
   116    family = "terraform-acc-test"
   117    container_definitions = <<TASK_DEFINITION
   118  [
   119  	{
   120  		"cpu": 10,
   121  		"command": ["sleep", "10"],
   122  		"entryPoint": ["/"],
   123  		"environment": [
   124  			{"name": "VARNAME", "value": "VARVAL"}
   125  		],
   126  		"essential": true,
   127  		"image": "jenkins",
   128  		"links": ["mongodb"],
   129  		"memory": 128,
   130  		"name": "jenkins",
   131  		"portMappings": [
   132  			{
   133  				"containerPort": 80,
   134  				"hostPort": 8080
   135  			}
   136  		]
   137  	},
   138  	{
   139  		"cpu": 10,
   140  		"command": ["sleep", "10"],
   141  		"entryPoint": ["/"],
   142  		"essential": true,
   143  		"image": "mongodb",
   144  		"memory": 128,
   145  		"name": "mongodb",
   146  		"portMappings": [
   147  			{
   148  				"containerPort": 28017,
   149  				"hostPort": 28017
   150  			}
   151  		]
   152  	}
   153  ]
   154  TASK_DEFINITION
   155  
   156    volume {
   157      name = "jenkins-home"
   158      host_path = "/ecs/jenkins-home"
   159    }
   160  }
   161  `
   162  
   163  var testAccAWSEcsTaskDefinitionWithScratchVolume = `
   164  resource "aws_ecs_task_definition" "sleep" {
   165    family = "terraform-acc-sc-volume-test"
   166    container_definitions = <<TASK_DEFINITION
   167  [
   168    {
   169      "name": "sleep",
   170      "image": "busybox",
   171      "cpu": 10,
   172      "command": ["sleep","360"],
   173      "memory": 10,
   174      "essential": true
   175    }
   176  ]
   177  TASK_DEFINITION
   178  
   179    volume {
   180      name = "database_scratch"
   181    }
   182  }
   183  `
   184  
   185  var testAccAWSEcsTaskDefinitionWithEcsService = `
   186  resource "aws_ecs_cluster" "default" {
   187    name = "terraform-acc-test"
   188  }
   189  
   190  resource "aws_ecs_service" "sleep-svc" {
   191    name = "tf-acc-ecs-svc"
   192    cluster = "${aws_ecs_cluster.default.id}"
   193    task_definition = "${aws_ecs_task_definition.sleep.arn}"
   194    desired_count = 1
   195  }
   196  
   197  resource "aws_ecs_task_definition" "sleep" {
   198    family = "terraform-acc-sc-volume-test"
   199    container_definitions = <<TASK_DEFINITION
   200  [
   201    {
   202      "name": "sleep",
   203      "image": "busybox",
   204      "cpu": 10,
   205      "command": ["sleep","360"],
   206      "memory": 10,
   207      "essential": true
   208    }
   209  ]
   210  TASK_DEFINITION
   211  
   212    volume {
   213      name = "database_scratch"
   214    }
   215  }
   216  `
   217  var testAccAWSEcsTaskDefinitionWithEcsServiceModified = `
   218  resource "aws_ecs_cluster" "default" {
   219    name = "terraform-acc-test"
   220  }
   221  
   222  resource "aws_ecs_service" "sleep-svc" {
   223    name = "tf-acc-ecs-svc"
   224    cluster = "${aws_ecs_cluster.default.id}"
   225    task_definition = "${aws_ecs_task_definition.sleep.arn}"
   226    desired_count = 1
   227  }
   228  
   229  resource "aws_ecs_task_definition" "sleep" {
   230    family = "terraform-acc-sc-volume-test"
   231    container_definitions = <<TASK_DEFINITION
   232  [
   233    {
   234      "name": "sleep",
   235      "image": "busybox",
   236      "cpu": 20,
   237      "command": ["sleep","360"],
   238      "memory": 50,
   239      "essential": true
   240    }
   241  ]
   242  TASK_DEFINITION
   243  
   244    volume {
   245      name = "database_scratch"
   246    }
   247  }
   248  `
   249  
   250  var testAccAWSEcsTaskDefinitionModified = `
   251  resource "aws_ecs_task_definition" "jenkins" {
   252    family = "terraform-acc-test"
   253    container_definitions = <<TASK_DEFINITION
   254  [
   255  	{
   256  		"cpu": 10,
   257  		"command": ["sleep", "10"],
   258  		"entryPoint": ["/"],
   259  		"environment": [
   260  			{"name": "VARNAME", "value": "VARVAL"}
   261  		],
   262  		"essential": true,
   263  		"image": "jenkins",
   264  		"links": ["mongodb"],
   265  		"memory": 128,
   266  		"name": "jenkins",
   267  		"portMappings": [
   268  			{
   269  				"containerPort": 80,
   270  				"hostPort": 8080
   271  			}
   272  		]
   273  	},
   274  	{
   275  		"cpu": 20,
   276  		"command": ["sleep", "10"],
   277  		"entryPoint": ["/"],
   278  		"essential": true,
   279  		"image": "mongodb",
   280  		"memory": 128,
   281  		"name": "mongodb",
   282  		"portMappings": [
   283  			{
   284  				"containerPort": 28017,
   285  				"hostPort": 28017
   286  			}
   287  		]
   288  	}
   289  ]
   290  TASK_DEFINITION
   291  
   292    volume {
   293      name = "jenkins-home"
   294      host_path = "/ecs/jenkins-home"
   295    }
   296  }
   297  `