github.com/gwilym/terraform@v0.3.8-0.20151231151641-c7573de75b19/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/aws/awserr" 12 "github.com/aws/aws-sdk-go/service/elasticache" 13 "github.com/hashicorp/terraform/helper/resource" 14 "github.com/hashicorp/terraform/terraform" 15 ) 16 17 func TestAccAWSElasticacheCluster_basic(t *testing.T) { 18 var ec elasticache.CacheCluster 19 resource.Test(t, resource.TestCase{ 20 PreCheck: func() { testAccPreCheck(t) }, 21 Providers: testAccProviders, 22 CheckDestroy: testAccCheckAWSElasticacheClusterDestroy, 23 Steps: []resource.TestStep{ 24 resource.TestStep{ 25 Config: testAccAWSElasticacheClusterConfig, 26 Check: resource.ComposeTestCheckFunc( 27 testAccCheckAWSElasticacheSecurityGroupExists("aws_elasticache_security_group.bar"), 28 testAccCheckAWSElasticacheClusterExists("aws_elasticache_cluster.bar", &ec), 29 resource.TestCheckResourceAttr( 30 "aws_elasticache_cluster.bar", "cache_nodes.0.id", "0001"), 31 ), 32 }, 33 }, 34 }) 35 } 36 37 func TestAccAWSElasticacheCluster_snapshotsWithUpdates(t *testing.T) { 38 var ec elasticache.CacheCluster 39 40 ri := genRandInt() 41 preConfig := fmt.Sprintf(testAccAWSElasticacheClusterConfig_snapshots, ri, ri, ri) 42 postConfig := fmt.Sprintf(testAccAWSElasticacheClusterConfig_snapshotsUpdated, ri, ri, ri) 43 44 resource.Test(t, resource.TestCase{ 45 PreCheck: func() { testAccPreCheck(t) }, 46 Providers: testAccProviders, 47 CheckDestroy: testAccCheckAWSElasticacheClusterDestroy, 48 Steps: []resource.TestStep{ 49 resource.TestStep{ 50 Config: preConfig, 51 Check: resource.ComposeTestCheckFunc( 52 testAccCheckAWSElasticacheSecurityGroupExists("aws_elasticache_security_group.bar"), 53 testAccCheckAWSElasticacheClusterExists("aws_elasticache_cluster.bar", &ec), 54 resource.TestCheckResourceAttr( 55 "aws_elasticache_cluster.bar", "snapshot_window", "05:00-09:00"), 56 resource.TestCheckResourceAttr( 57 "aws_elasticache_cluster.bar", "snapshot_retention_limit", "3"), 58 ), 59 }, 60 61 resource.TestStep{ 62 Config: postConfig, 63 Check: resource.ComposeTestCheckFunc( 64 testAccCheckAWSElasticacheSecurityGroupExists("aws_elasticache_security_group.bar"), 65 testAccCheckAWSElasticacheClusterExists("aws_elasticache_cluster.bar", &ec), 66 resource.TestCheckResourceAttr( 67 "aws_elasticache_cluster.bar", "snapshot_window", "07:00-09:00"), 68 resource.TestCheckResourceAttr( 69 "aws_elasticache_cluster.bar", "snapshot_retention_limit", "7"), 70 ), 71 }, 72 }, 73 }) 74 } 75 76 func TestAccAWSElasticacheCluster_decreasingCacheNodes(t *testing.T) { 77 var ec elasticache.CacheCluster 78 79 ri := genRandInt() 80 preConfig := fmt.Sprintf(testAccAWSElasticacheClusterConfigDecreasingNodes, ri, ri, ri) 81 postConfig := fmt.Sprintf(testAccAWSElasticacheClusterConfigDecreasingNodes_update, ri, ri, ri) 82 83 resource.Test(t, resource.TestCase{ 84 PreCheck: func() { testAccPreCheck(t) }, 85 Providers: testAccProviders, 86 CheckDestroy: testAccCheckAWSElasticacheClusterDestroy, 87 Steps: []resource.TestStep{ 88 resource.TestStep{ 89 Config: preConfig, 90 Check: resource.ComposeTestCheckFunc( 91 testAccCheckAWSElasticacheSecurityGroupExists("aws_elasticache_security_group.bar"), 92 testAccCheckAWSElasticacheClusterExists("aws_elasticache_cluster.bar", &ec), 93 resource.TestCheckResourceAttr( 94 "aws_elasticache_cluster.bar", "num_cache_nodes", "3"), 95 ), 96 }, 97 98 resource.TestStep{ 99 Config: postConfig, 100 Check: resource.ComposeTestCheckFunc( 101 testAccCheckAWSElasticacheSecurityGroupExists("aws_elasticache_security_group.bar"), 102 testAccCheckAWSElasticacheClusterExists("aws_elasticache_cluster.bar", &ec), 103 resource.TestCheckResourceAttr( 104 "aws_elasticache_cluster.bar", "num_cache_nodes", "1"), 105 ), 106 }, 107 }, 108 }) 109 } 110 111 func TestAccAWSElasticacheCluster_vpc(t *testing.T) { 112 var csg elasticache.CacheSubnetGroup 113 var ec elasticache.CacheCluster 114 resource.Test(t, resource.TestCase{ 115 PreCheck: func() { testAccPreCheck(t) }, 116 Providers: testAccProviders, 117 CheckDestroy: testAccCheckAWSElasticacheClusterDestroy, 118 Steps: []resource.TestStep{ 119 resource.TestStep{ 120 Config: testAccAWSElasticacheClusterInVPCConfig, 121 Check: resource.ComposeTestCheckFunc( 122 testAccCheckAWSElasticacheSubnetGroupExists("aws_elasticache_subnet_group.bar", &csg), 123 testAccCheckAWSElasticacheClusterExists("aws_elasticache_cluster.bar", &ec), 124 testAccCheckAWSElasticacheClusterAttributes(&ec), 125 ), 126 }, 127 }, 128 }) 129 } 130 131 func testAccCheckAWSElasticacheClusterAttributes(v *elasticache.CacheCluster) resource.TestCheckFunc { 132 return func(s *terraform.State) error { 133 if v.NotificationConfiguration == nil { 134 return fmt.Errorf("Expected NotificationConfiguration for ElastiCache Cluster (%s)", *v.CacheClusterId) 135 } 136 137 if strings.ToLower(*v.NotificationConfiguration.TopicStatus) != "active" { 138 return fmt.Errorf("Expected NotificationConfiguration status to be 'active', got (%s)", *v.NotificationConfiguration.TopicStatus) 139 } 140 141 return nil 142 } 143 } 144 145 func testAccCheckAWSElasticacheClusterDestroy(s *terraform.State) error { 146 conn := testAccProvider.Meta().(*AWSClient).elasticacheconn 147 148 for _, rs := range s.RootModule().Resources { 149 if rs.Type != "aws_elasticache_cluster" { 150 continue 151 } 152 res, err := conn.DescribeCacheClusters(&elasticache.DescribeCacheClustersInput{ 153 CacheClusterId: aws.String(rs.Primary.ID), 154 }) 155 if err != nil { 156 // Verify the error is what we want 157 if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "CacheClusterNotFound" { 158 continue 159 } 160 return err 161 } 162 if len(res.CacheClusters) > 0 { 163 return fmt.Errorf("still exist.") 164 } 165 } 166 return nil 167 } 168 169 func testAccCheckAWSElasticacheClusterExists(n string, v *elasticache.CacheCluster) resource.TestCheckFunc { 170 return func(s *terraform.State) error { 171 rs, ok := s.RootModule().Resources[n] 172 if !ok { 173 return fmt.Errorf("Not found: %s", n) 174 } 175 176 if rs.Primary.ID == "" { 177 return fmt.Errorf("No cache cluster ID is set") 178 } 179 180 conn := testAccProvider.Meta().(*AWSClient).elasticacheconn 181 resp, err := conn.DescribeCacheClusters(&elasticache.DescribeCacheClustersInput{ 182 CacheClusterId: aws.String(rs.Primary.ID), 183 }) 184 if err != nil { 185 return fmt.Errorf("Elasticache error: %v", err) 186 } 187 188 for _, c := range resp.CacheClusters { 189 if *c.CacheClusterId == rs.Primary.ID { 190 *v = *c 191 } 192 } 193 194 return nil 195 } 196 } 197 198 func genRandInt() int { 199 return rand.New(rand.NewSource(time.Now().UnixNano())).Int() % 1000 200 } 201 202 var testAccAWSElasticacheClusterConfig = fmt.Sprintf(` 203 provider "aws" { 204 region = "us-east-1" 205 } 206 resource "aws_security_group" "bar" { 207 name = "tf-test-security-group-%03d" 208 description = "tf-test-security-group-descr" 209 ingress { 210 from_port = -1 211 to_port = -1 212 protocol = "icmp" 213 cidr_blocks = ["0.0.0.0/0"] 214 } 215 } 216 217 resource "aws_elasticache_security_group" "bar" { 218 name = "tf-test-security-group-%03d" 219 description = "tf-test-security-group-descr" 220 security_group_names = ["${aws_security_group.bar.name}"] 221 } 222 223 resource "aws_elasticache_cluster" "bar" { 224 cluster_id = "tf-test-%03d" 225 engine = "memcached" 226 node_type = "cache.m1.small" 227 num_cache_nodes = 1 228 port = 11211 229 parameter_group_name = "default.memcached1.4" 230 security_group_names = ["${aws_elasticache_security_group.bar.name}"] 231 } 232 `, genRandInt(), genRandInt(), genRandInt()) 233 234 var testAccAWSElasticacheClusterConfig_snapshots = ` 235 provider "aws" { 236 region = "us-east-1" 237 } 238 resource "aws_security_group" "bar" { 239 name = "tf-test-security-group-%03d" 240 description = "tf-test-security-group-descr" 241 ingress { 242 from_port = -1 243 to_port = -1 244 protocol = "icmp" 245 cidr_blocks = ["0.0.0.0/0"] 246 } 247 } 248 249 resource "aws_elasticache_security_group" "bar" { 250 name = "tf-test-security-group-%03d" 251 description = "tf-test-security-group-descr" 252 security_group_names = ["${aws_security_group.bar.name}"] 253 } 254 255 resource "aws_elasticache_cluster" "bar" { 256 cluster_id = "tf-test-%03d" 257 engine = "redis" 258 node_type = "cache.m1.small" 259 num_cache_nodes = 1 260 port = 6379 261 parameter_group_name = "default.redis2.8" 262 security_group_names = ["${aws_elasticache_security_group.bar.name}"] 263 snapshot_window = "05:00-09:00" 264 snapshot_retention_limit = 3 265 } 266 ` 267 268 var testAccAWSElasticacheClusterConfig_snapshotsUpdated = ` 269 provider "aws" { 270 region = "us-east-1" 271 } 272 resource "aws_security_group" "bar" { 273 name = "tf-test-security-group-%03d" 274 description = "tf-test-security-group-descr" 275 ingress { 276 from_port = -1 277 to_port = -1 278 protocol = "icmp" 279 cidr_blocks = ["0.0.0.0/0"] 280 } 281 } 282 283 resource "aws_elasticache_security_group" "bar" { 284 name = "tf-test-security-group-%03d" 285 description = "tf-test-security-group-descr" 286 security_group_names = ["${aws_security_group.bar.name}"] 287 } 288 289 resource "aws_elasticache_cluster" "bar" { 290 cluster_id = "tf-test-%03d" 291 engine = "redis" 292 node_type = "cache.m1.small" 293 num_cache_nodes = 1 294 port = 6379 295 parameter_group_name = "default.redis2.8" 296 security_group_names = ["${aws_elasticache_security_group.bar.name}"] 297 snapshot_window = "07:00-09:00" 298 snapshot_retention_limit = 7 299 apply_immediately = true 300 } 301 ` 302 303 var testAccAWSElasticacheClusterConfigDecreasingNodes = ` 304 provider "aws" { 305 region = "us-east-1" 306 } 307 resource "aws_security_group" "bar" { 308 name = "tf-test-security-group-%03d" 309 description = "tf-test-security-group-descr" 310 ingress { 311 from_port = -1 312 to_port = -1 313 protocol = "icmp" 314 cidr_blocks = ["0.0.0.0/0"] 315 } 316 } 317 318 resource "aws_elasticache_security_group" "bar" { 319 name = "tf-test-security-group-%03d" 320 description = "tf-test-security-group-descr" 321 security_group_names = ["${aws_security_group.bar.name}"] 322 } 323 324 resource "aws_elasticache_cluster" "bar" { 325 cluster_id = "tf-test-%03d" 326 engine = "memcached" 327 node_type = "cache.m1.small" 328 num_cache_nodes = 3 329 port = 11211 330 parameter_group_name = "default.memcached1.4" 331 security_group_names = ["${aws_elasticache_security_group.bar.name}"] 332 } 333 ` 334 335 var testAccAWSElasticacheClusterConfigDecreasingNodes_update = ` 336 provider "aws" { 337 region = "us-east-1" 338 } 339 resource "aws_security_group" "bar" { 340 name = "tf-test-security-group-%03d" 341 description = "tf-test-security-group-descr" 342 ingress { 343 from_port = -1 344 to_port = -1 345 protocol = "icmp" 346 cidr_blocks = ["0.0.0.0/0"] 347 } 348 } 349 350 resource "aws_elasticache_security_group" "bar" { 351 name = "tf-test-security-group-%03d" 352 description = "tf-test-security-group-descr" 353 security_group_names = ["${aws_security_group.bar.name}"] 354 } 355 356 resource "aws_elasticache_cluster" "bar" { 357 cluster_id = "tf-test-%03d" 358 engine = "memcached" 359 node_type = "cache.m1.small" 360 num_cache_nodes = 1 361 port = 11211 362 parameter_group_name = "default.memcached1.4" 363 security_group_names = ["${aws_elasticache_security_group.bar.name}"] 364 apply_immediately = true 365 } 366 ` 367 368 var testAccAWSElasticacheClusterInVPCConfig = fmt.Sprintf(` 369 resource "aws_vpc" "foo" { 370 cidr_block = "192.168.0.0/16" 371 tags { 372 Name = "tf-test" 373 } 374 } 375 376 resource "aws_subnet" "foo" { 377 vpc_id = "${aws_vpc.foo.id}" 378 cidr_block = "192.168.0.0/20" 379 availability_zone = "us-west-2a" 380 tags { 381 Name = "tf-test" 382 } 383 } 384 385 resource "aws_elasticache_subnet_group" "bar" { 386 name = "tf-test-cache-subnet-%03d" 387 description = "tf-test-cache-subnet-group-descr" 388 subnet_ids = ["${aws_subnet.foo.id}"] 389 } 390 391 resource "aws_security_group" "bar" { 392 name = "tf-test-security-group-%03d" 393 description = "tf-test-security-group-descr" 394 vpc_id = "${aws_vpc.foo.id}" 395 ingress { 396 from_port = -1 397 to_port = -1 398 protocol = "icmp" 399 cidr_blocks = ["0.0.0.0/0"] 400 } 401 } 402 403 resource "aws_elasticache_cluster" "bar" { 404 // Including uppercase letters in this name to ensure 405 // that we correctly handle the fact that the API 406 // normalizes names to lowercase. 407 cluster_id = "tf-TEST-%03d" 408 node_type = "cache.m1.small" 409 num_cache_nodes = 1 410 engine = "redis" 411 engine_version = "2.8.19" 412 port = 6379 413 subnet_group_name = "${aws_elasticache_subnet_group.bar.name}" 414 security_group_ids = ["${aws_security_group.bar.id}"] 415 parameter_group_name = "default.redis2.8" 416 notification_topic_arn = "${aws_sns_topic.topic_example.arn}" 417 } 418 419 resource "aws_sns_topic" "topic_example" { 420 name = "tf-ecache-cluster-test" 421 } 422 `, genRandInt(), genRandInt(), genRandInt())