github.com/armen/terraform@v0.5.2-0.20150529052519-caa8117a08f1/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/awslabs/aws-sdk-go/aws" 10 "github.com/awslabs/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 resource.Test(t, resource.TestCase{ 36 PreCheck: func() { testAccPreCheck(t) }, 37 Providers: testAccProviders, 38 CheckDestroy: testAccCheckAWSElasticacheClusterDestroy, 39 Steps: []resource.TestStep{ 40 resource.TestStep{ 41 Config: testAccAWSElasticacheClusterInVPCConfig, 42 Check: resource.ComposeTestCheckFunc( 43 testAccCheckAWSElasticacheSubnetGroupExists("aws_elasticache_subnet_group.bar"), 44 testAccCheckAWSElasticacheClusterExists("aws_elasticache_cluster.bar"), 45 ), 46 }, 47 }, 48 }) 49 } 50 51 func testAccCheckAWSElasticacheClusterDestroy(s *terraform.State) error { 52 conn := testAccProvider.Meta().(*AWSClient).elasticacheconn 53 54 for _, rs := range s.RootModule().Resources { 55 if rs.Type != "aws_elasticache_cluster" { 56 continue 57 } 58 res, err := conn.DescribeCacheClusters(&elasticache.DescribeCacheClustersInput{ 59 CacheClusterID: aws.String(rs.Primary.ID), 60 }) 61 if err != nil { 62 return err 63 } 64 if len(res.CacheClusters) > 0 { 65 return fmt.Errorf("still exist.") 66 } 67 } 68 return nil 69 } 70 71 func testAccCheckAWSElasticacheClusterExists(n string) resource.TestCheckFunc { 72 return func(s *terraform.State) error { 73 rs, ok := s.RootModule().Resources[n] 74 if !ok { 75 return fmt.Errorf("Not found: %s", n) 76 } 77 78 if rs.Primary.ID == "" { 79 return fmt.Errorf("No cache cluster ID is set") 80 } 81 82 conn := testAccProvider.Meta().(*AWSClient).elasticacheconn 83 _, err := conn.DescribeCacheClusters(&elasticache.DescribeCacheClustersInput{ 84 CacheClusterID: aws.String(rs.Primary.ID), 85 }) 86 if err != nil { 87 return fmt.Errorf("Elasticache error: %v", err) 88 } 89 return nil 90 } 91 } 92 93 func genRandInt() int { 94 return rand.New(rand.NewSource(time.Now().UnixNano())).Int() % 1000 95 } 96 97 var testAccAWSElasticacheClusterConfig = fmt.Sprintf(` 98 provider "aws" { 99 region = "us-east-1" 100 } 101 resource "aws_security_group" "bar" { 102 name = "tf-test-security-group-%03d" 103 description = "tf-test-security-group-descr" 104 ingress { 105 from_port = -1 106 to_port = -1 107 protocol = "icmp" 108 cidr_blocks = ["0.0.0.0/0"] 109 } 110 } 111 112 resource "aws_elasticache_security_group" "bar" { 113 name = "tf-test-security-group-%03d" 114 description = "tf-test-security-group-descr" 115 security_group_names = ["${aws_security_group.bar.name}"] 116 } 117 118 resource "aws_elasticache_cluster" "bar" { 119 cluster_id = "tf-test-%03d" 120 engine = "memcached" 121 node_type = "cache.m1.small" 122 num_cache_nodes = 1 123 parameter_group_name = "default.memcached1.4" 124 security_group_names = ["${aws_elasticache_security_group.bar.name}"] 125 } 126 `, genRandInt(), genRandInt(), genRandInt()) 127 128 var testAccAWSElasticacheClusterInVPCConfig = fmt.Sprintf(` 129 resource "aws_vpc" "foo" { 130 cidr_block = "192.168.0.0/16" 131 tags { 132 Name = "tf-test" 133 } 134 } 135 136 resource "aws_subnet" "foo" { 137 vpc_id = "${aws_vpc.foo.id}" 138 cidr_block = "192.168.0.0/20" 139 availability_zone = "us-west-2a" 140 tags { 141 Name = "tf-test" 142 } 143 } 144 145 resource "aws_elasticache_subnet_group" "bar" { 146 name = "tf-test-cache-subnet-%03d" 147 description = "tf-test-cache-subnet-group-descr" 148 subnet_ids = ["${aws_subnet.foo.id}"] 149 } 150 151 resource "aws_security_group" "bar" { 152 name = "tf-test-security-group-%03d" 153 description = "tf-test-security-group-descr" 154 vpc_id = "${aws_vpc.foo.id}" 155 ingress { 156 from_port = -1 157 to_port = -1 158 protocol = "icmp" 159 cidr_blocks = ["0.0.0.0/0"] 160 } 161 } 162 163 resource "aws_elasticache_cluster" "bar" { 164 cluster_id = "tf-test-%03d" 165 node_type = "cache.m1.small" 166 num_cache_nodes = 1 167 engine = "redis" 168 engine_version = "2.8.19" 169 port = 6379 170 subnet_group_name = "${aws_elasticache_subnet_group.bar.name}" 171 security_group_ids = ["${aws_security_group.bar.id}"] 172 parameter_group_name = "default.redis2.8" 173 } 174 `, genRandInt(), genRandInt(), genRandInt())