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