github.com/jsoriano/terraform@v0.6.7-0.20151026070445-8b70867fdd95/builtin/providers/aws/resource_aws_elasticache_cluster_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/aws/aws-sdk-go/aws"
    10  	"github.com/aws/aws-sdk-go/service/elasticache"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestAccAWSElasticacheCluster_basic(t *testing.T) {
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckAWSElasticacheClusterDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccAWSElasticacheClusterConfig,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckAWSElasticacheSecurityGroupExists("aws_elasticache_security_group.bar"),
    25  					testAccCheckAWSElasticacheClusterExists("aws_elasticache_cluster.bar"),
    26  					resource.TestCheckResourceAttr(
    27  						"aws_elasticache_cluster.bar", "cache_nodes.0.id", "0001"),
    28  				),
    29  			},
    30  		},
    31  	})
    32  }
    33  
    34  func TestAccAWSElasticacheCluster_vpc(t *testing.T) {
    35  	var csg elasticache.CacheSubnetGroup
    36  	resource.Test(t, resource.TestCase{
    37  		PreCheck:     func() { testAccPreCheck(t) },
    38  		Providers:    testAccProviders,
    39  		CheckDestroy: testAccCheckAWSElasticacheClusterDestroy,
    40  		Steps: []resource.TestStep{
    41  			resource.TestStep{
    42  				Config: testAccAWSElasticacheClusterInVPCConfig,
    43  				Check: resource.ComposeTestCheckFunc(
    44  					testAccCheckAWSElasticacheSubnetGroupExists("aws_elasticache_subnet_group.bar", &csg),
    45  					testAccCheckAWSElasticacheClusterExists("aws_elasticache_cluster.bar"),
    46  				),
    47  			},
    48  		},
    49  	})
    50  }
    51  
    52  func testAccCheckAWSElasticacheClusterDestroy(s *terraform.State) error {
    53  	conn := testAccProvider.Meta().(*AWSClient).elasticacheconn
    54  
    55  	for _, rs := range s.RootModule().Resources {
    56  		if rs.Type != "aws_elasticache_cluster" {
    57  			continue
    58  		}
    59  		res, err := conn.DescribeCacheClusters(&elasticache.DescribeCacheClustersInput{
    60  			CacheClusterId: aws.String(rs.Primary.ID),
    61  		})
    62  		if err != nil {
    63  			return err
    64  		}
    65  		if len(res.CacheClusters) > 0 {
    66  			return fmt.Errorf("still exist.")
    67  		}
    68  	}
    69  	return nil
    70  }
    71  
    72  func testAccCheckAWSElasticacheClusterExists(n string) resource.TestCheckFunc {
    73  	return func(s *terraform.State) error {
    74  		rs, ok := s.RootModule().Resources[n]
    75  		if !ok {
    76  			return fmt.Errorf("Not found: %s", n)
    77  		}
    78  
    79  		if rs.Primary.ID == "" {
    80  			return fmt.Errorf("No cache cluster ID is set")
    81  		}
    82  
    83  		conn := testAccProvider.Meta().(*AWSClient).elasticacheconn
    84  		_, err := conn.DescribeCacheClusters(&elasticache.DescribeCacheClustersInput{
    85  			CacheClusterId: aws.String(rs.Primary.ID),
    86  		})
    87  		if err != nil {
    88  			return fmt.Errorf("Elasticache error: %v", err)
    89  		}
    90  		return nil
    91  	}
    92  }
    93  
    94  func genRandInt() int {
    95  	return rand.New(rand.NewSource(time.Now().UnixNano())).Int() % 1000
    96  }
    97  
    98  var testAccAWSElasticacheClusterConfig = fmt.Sprintf(`
    99  provider "aws" {
   100  	region = "us-east-1"
   101  }
   102  resource "aws_security_group" "bar" {
   103      name = "tf-test-security-group-%03d"
   104      description = "tf-test-security-group-descr"
   105      ingress {
   106          from_port = -1
   107          to_port = -1
   108          protocol = "icmp"
   109          cidr_blocks = ["0.0.0.0/0"]
   110      }
   111  }
   112  
   113  resource "aws_elasticache_security_group" "bar" {
   114      name = "tf-test-security-group-%03d"
   115      description = "tf-test-security-group-descr"
   116      security_group_names = ["${aws_security_group.bar.name}"]
   117  }
   118  
   119  resource "aws_elasticache_cluster" "bar" {
   120      cluster_id = "tf-test-%03d"
   121      engine = "memcached"
   122      node_type = "cache.m1.small"
   123      num_cache_nodes = 1
   124      port = 11211
   125      parameter_group_name = "default.memcached1.4"
   126      security_group_names = ["${aws_elasticache_security_group.bar.name}"]
   127  }
   128  `, genRandInt(), genRandInt(), genRandInt())
   129  
   130  var testAccAWSElasticacheClusterInVPCConfig = fmt.Sprintf(`
   131  resource "aws_vpc" "foo" {
   132      cidr_block = "192.168.0.0/16"
   133      tags {
   134              Name = "tf-test"
   135      }
   136  }
   137  
   138  resource "aws_subnet" "foo" {
   139      vpc_id = "${aws_vpc.foo.id}"
   140      cidr_block = "192.168.0.0/20"
   141      availability_zone = "us-west-2a"
   142      tags {
   143              Name = "tf-test"
   144      }
   145  }
   146  
   147  resource "aws_elasticache_subnet_group" "bar" {
   148      name = "tf-test-cache-subnet-%03d"
   149      description = "tf-test-cache-subnet-group-descr"
   150      subnet_ids = ["${aws_subnet.foo.id}"]
   151  }
   152  
   153  resource "aws_security_group" "bar" {
   154      name = "tf-test-security-group-%03d"
   155      description = "tf-test-security-group-descr"
   156      vpc_id = "${aws_vpc.foo.id}"
   157      ingress {
   158          from_port = -1
   159          to_port = -1
   160          protocol = "icmp"
   161          cidr_blocks = ["0.0.0.0/0"]
   162      }
   163  }
   164  
   165  resource "aws_elasticache_cluster" "bar" {
   166      // Including uppercase letters in this name to ensure
   167      // that we correctly handle the fact that the API
   168      // normalizes names to lowercase.
   169      cluster_id = "tf-TEST-%03d"
   170      node_type = "cache.m1.small"
   171      num_cache_nodes = 1
   172      engine = "redis"
   173      engine_version = "2.8.19"
   174      port = 6379
   175      subnet_group_name = "${aws_elasticache_subnet_group.bar.name}"
   176      security_group_ids = ["${aws_security_group.bar.id}"]
   177      parameter_group_name = "default.redis2.8"
   178  }
   179  `, genRandInt(), genRandInt(), genRandInt())