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