github.com/ggiamarchi/terraform@v0.3.7-0.20150607194748-ed2a66a46a71/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      parameter_group_name = "default.memcached1.4"
   125      security_group_names = ["${aws_elasticache_security_group.bar.name}"]
   126  }
   127  `, genRandInt(), genRandInt(), genRandInt())
   128  
   129  var testAccAWSElasticacheClusterInVPCConfig = fmt.Sprintf(`
   130  resource "aws_vpc" "foo" {
   131      cidr_block = "192.168.0.0/16"
   132      tags {
   133              Name = "tf-test"
   134      }
   135  }
   136  
   137  resource "aws_subnet" "foo" {
   138      vpc_id = "${aws_vpc.foo.id}"
   139      cidr_block = "192.168.0.0/20"
   140      availability_zone = "us-west-2a"
   141      tags {
   142              Name = "tf-test"
   143      }
   144  }
   145  
   146  resource "aws_elasticache_subnet_group" "bar" {
   147      name = "tf-test-cache-subnet-%03d"
   148      description = "tf-test-cache-subnet-group-descr"
   149      subnet_ids = ["${aws_subnet.foo.id}"]
   150  }
   151  
   152  resource "aws_security_group" "bar" {
   153      name = "tf-test-security-group-%03d"
   154      description = "tf-test-security-group-descr"
   155      vpc_id = "${aws_vpc.foo.id}"
   156      ingress {
   157          from_port = -1
   158          to_port = -1
   159          protocol = "icmp"
   160          cidr_blocks = ["0.0.0.0/0"]
   161      }
   162  }
   163  
   164  resource "aws_elasticache_cluster" "bar" {
   165      cluster_id = "tf-test-%03d"
   166      node_type = "cache.m1.small"
   167      num_cache_nodes = 1
   168      engine = "redis"
   169      engine_version = "2.8.19"
   170      port = 6379
   171      subnet_group_name = "${aws_elasticache_subnet_group.bar.name}"
   172      security_group_ids = ["${aws_security_group.bar.id}"]
   173      parameter_group_name = "default.redis2.8"
   174  }
   175  `, genRandInt(), genRandInt(), genRandInt())