github.com/jsoriano/terraform@v0.6.7-0.20151026070445-8b70867fdd95/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: testAccAWSEcsTaskDefinitionModifier,
    27  				Check: resource.ComposeTestCheckFunc(
    28  					testAccCheckAWSEcsTaskDefinitionExists("aws_ecs_task_definition.jenkins"),
    29  				),
    30  			},
    31  		},
    32  	})
    33  }
    34  
    35  func testAccCheckAWSEcsTaskDefinitionDestroy(s *terraform.State) error {
    36  	conn := testAccProvider.Meta().(*AWSClient).ecsconn
    37  
    38  	for _, rs := range s.RootModule().Resources {
    39  		if rs.Type != "aws_ecs_task_definition" {
    40  			continue
    41  		}
    42  
    43  		out, err := conn.DescribeTaskDefinition(&ecs.DescribeTaskDefinitionInput{
    44  			TaskDefinition: aws.String(rs.Primary.ID),
    45  		})
    46  
    47  		if err == nil {
    48  			if out.TaskDefinition != nil {
    49  				return fmt.Errorf("ECS task definition still exists:\n%#v", *out.TaskDefinition)
    50  			}
    51  		}
    52  
    53  		return err
    54  	}
    55  
    56  	return nil
    57  }
    58  
    59  func testAccCheckAWSEcsTaskDefinitionExists(name string) resource.TestCheckFunc {
    60  	return func(s *terraform.State) error {
    61  		_, ok := s.RootModule().Resources[name]
    62  		if !ok {
    63  			return fmt.Errorf("Not found: %s", name)
    64  		}
    65  
    66  		return nil
    67  	}
    68  }
    69  
    70  var testAccAWSEcsTaskDefinition = `
    71  resource "aws_ecs_task_definition" "jenkins" {
    72    family = "terraform-acc-test"
    73    container_definitions = <<TASK_DEFINITION
    74  [
    75  	{
    76  		"cpu": 10,
    77  		"command": ["sleep", "10"],
    78  		"entryPoint": ["/"],
    79  		"environment": [
    80  			{"name": "VARNAME", "value": "VARVAL"}
    81  		],
    82  		"essential": true,
    83  		"image": "jenkins",
    84  		"links": ["mongodb"],
    85  		"memory": 128,
    86  		"name": "jenkins",
    87  		"portMappings": [
    88  			{
    89  				"containerPort": 80,
    90  				"hostPort": 8080
    91  			}
    92  		]
    93  	},
    94  	{
    95  		"cpu": 10,
    96  		"command": ["sleep", "10"],
    97  		"entryPoint": ["/"],
    98  		"essential": true,
    99  		"image": "mongodb",
   100  		"memory": 128,
   101  		"name": "mongodb",
   102  		"portMappings": [
   103  			{
   104  				"containerPort": 28017,
   105  				"hostPort": 28017
   106  			}
   107  		]
   108  	}
   109  ]
   110  TASK_DEFINITION
   111  
   112    volume {
   113      name = "jenkins-home"
   114      host_path = "/ecs/jenkins-home"
   115    }
   116  }
   117  `
   118  
   119  var testAccAWSEcsTaskDefinitionModifier = `
   120  resource "aws_ecs_task_definition" "jenkins" {
   121    family = "terraform-acc-test"
   122    container_definitions = <<TASK_DEFINITION
   123  [
   124  	{
   125  		"cpu": 10,
   126  		"command": ["sleep", "10"],
   127  		"entryPoint": ["/"],
   128  		"environment": [
   129  			{"name": "VARNAME", "value": "VARVAL"}
   130  		],
   131  		"essential": true,
   132  		"image": "jenkins",
   133  		"links": ["mongodb"],
   134  		"memory": 128,
   135  		"name": "jenkins",
   136  		"portMappings": [
   137  			{
   138  				"containerPort": 80,
   139  				"hostPort": 8080
   140  			}
   141  		]
   142  	},
   143  	{
   144  		"cpu": 20,
   145  		"command": ["sleep", "10"],
   146  		"entryPoint": ["/"],
   147  		"essential": true,
   148  		"image": "mongodb",
   149  		"memory": 128,
   150  		"name": "mongodb",
   151  		"portMappings": [
   152  			{
   153  				"containerPort": 28017,
   154  				"hostPort": 28017
   155  			}
   156  		]
   157  	}
   158  ]
   159  TASK_DEFINITION
   160  
   161    volume {
   162      name = "jenkins-home"
   163      host_path = "/ecs/jenkins-home"
   164    }
   165  }
   166  `