github.com/leeprovoost/terraform@v0.6.10-0.20160119085442-96f3f76118e7/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 resource.TestCheckResourceAttr( 126 "aws_elasticache_cluster.bar", "availability_zone", "us-west-2a"), 127 ), 128 }, 129 }, 130 }) 131 } 132 133 func TestAccAWSElasticacheCluster_multiAZInVpc(t *testing.T) { 134 var csg elasticache.CacheSubnetGroup 135 var ec elasticache.CacheCluster 136 resource.Test(t, resource.TestCase{ 137 PreCheck: func() { testAccPreCheck(t) }, 138 Providers: testAccProviders, 139 CheckDestroy: testAccCheckAWSElasticacheClusterDestroy, 140 Steps: []resource.TestStep{ 141 resource.TestStep{ 142 Config: testAccAWSElasticacheClusterMultiAZInVPCConfig, 143 Check: resource.ComposeTestCheckFunc( 144 testAccCheckAWSElasticacheSubnetGroupExists("aws_elasticache_subnet_group.bar", &csg), 145 testAccCheckAWSElasticacheClusterExists("aws_elasticache_cluster.bar", &ec), 146 resource.TestCheckResourceAttr( 147 "aws_elasticache_cluster.bar", "availability_zone", "Multiple"), 148 ), 149 }, 150 }, 151 }) 152 } 153 154 func testAccCheckAWSElasticacheClusterAttributes(v *elasticache.CacheCluster) resource.TestCheckFunc { 155 return func(s *terraform.State) error { 156 if v.NotificationConfiguration == nil { 157 return fmt.Errorf("Expected NotificationConfiguration for ElastiCache Cluster (%s)", *v.CacheClusterId) 158 } 159 160 if strings.ToLower(*v.NotificationConfiguration.TopicStatus) != "active" { 161 return fmt.Errorf("Expected NotificationConfiguration status to be 'active', got (%s)", *v.NotificationConfiguration.TopicStatus) 162 } 163 164 return nil 165 } 166 } 167 168 func testAccCheckAWSElasticacheClusterDestroy(s *terraform.State) error { 169 conn := testAccProvider.Meta().(*AWSClient).elasticacheconn 170 171 for _, rs := range s.RootModule().Resources { 172 if rs.Type != "aws_elasticache_cluster" { 173 continue 174 } 175 res, err := conn.DescribeCacheClusters(&elasticache.DescribeCacheClustersInput{ 176 CacheClusterId: aws.String(rs.Primary.ID), 177 }) 178 if err != nil { 179 // Verify the error is what we want 180 if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "CacheClusterNotFound" { 181 continue 182 } 183 return err 184 } 185 if len(res.CacheClusters) > 0 { 186 return fmt.Errorf("still exist.") 187 } 188 } 189 return nil 190 } 191 192 func testAccCheckAWSElasticacheClusterExists(n string, v *elasticache.CacheCluster) resource.TestCheckFunc { 193 return func(s *terraform.State) error { 194 rs, ok := s.RootModule().Resources[n] 195 if !ok { 196 return fmt.Errorf("Not found: %s", n) 197 } 198 199 if rs.Primary.ID == "" { 200 return fmt.Errorf("No cache cluster ID is set") 201 } 202 203 conn := testAccProvider.Meta().(*AWSClient).elasticacheconn 204 resp, err := conn.DescribeCacheClusters(&elasticache.DescribeCacheClustersInput{ 205 CacheClusterId: aws.String(rs.Primary.ID), 206 }) 207 if err != nil { 208 return fmt.Errorf("Elasticache error: %v", err) 209 } 210 211 for _, c := range resp.CacheClusters { 212 if *c.CacheClusterId == rs.Primary.ID { 213 *v = *c 214 } 215 } 216 217 return nil 218 } 219 } 220 221 func genRandInt() int { 222 return rand.New(rand.NewSource(time.Now().UnixNano())).Int() % 1000 223 } 224 225 var testAccAWSElasticacheClusterConfig = fmt.Sprintf(` 226 provider "aws" { 227 region = "us-east-1" 228 } 229 resource "aws_security_group" "bar" { 230 name = "tf-test-security-group-%03d" 231 description = "tf-test-security-group-descr" 232 ingress { 233 from_port = -1 234 to_port = -1 235 protocol = "icmp" 236 cidr_blocks = ["0.0.0.0/0"] 237 } 238 } 239 240 resource "aws_elasticache_security_group" "bar" { 241 name = "tf-test-security-group-%03d" 242 description = "tf-test-security-group-descr" 243 security_group_names = ["${aws_security_group.bar.name}"] 244 } 245 246 resource "aws_elasticache_cluster" "bar" { 247 cluster_id = "tf-test-%03d" 248 engine = "memcached" 249 node_type = "cache.m1.small" 250 num_cache_nodes = 1 251 port = 11211 252 parameter_group_name = "default.memcached1.4" 253 security_group_names = ["${aws_elasticache_security_group.bar.name}"] 254 } 255 `, genRandInt(), genRandInt(), genRandInt()) 256 257 var testAccAWSElasticacheClusterConfig_snapshots = ` 258 provider "aws" { 259 region = "us-east-1" 260 } 261 resource "aws_security_group" "bar" { 262 name = "tf-test-security-group-%03d" 263 description = "tf-test-security-group-descr" 264 ingress { 265 from_port = -1 266 to_port = -1 267 protocol = "icmp" 268 cidr_blocks = ["0.0.0.0/0"] 269 } 270 } 271 272 resource "aws_elasticache_security_group" "bar" { 273 name = "tf-test-security-group-%03d" 274 description = "tf-test-security-group-descr" 275 security_group_names = ["${aws_security_group.bar.name}"] 276 } 277 278 resource "aws_elasticache_cluster" "bar" { 279 cluster_id = "tf-test-%03d" 280 engine = "redis" 281 node_type = "cache.m1.small" 282 num_cache_nodes = 1 283 port = 6379 284 parameter_group_name = "default.redis2.8" 285 security_group_names = ["${aws_elasticache_security_group.bar.name}"] 286 snapshot_window = "05:00-09:00" 287 snapshot_retention_limit = 3 288 } 289 ` 290 291 var testAccAWSElasticacheClusterConfig_snapshotsUpdated = ` 292 provider "aws" { 293 region = "us-east-1" 294 } 295 resource "aws_security_group" "bar" { 296 name = "tf-test-security-group-%03d" 297 description = "tf-test-security-group-descr" 298 ingress { 299 from_port = -1 300 to_port = -1 301 protocol = "icmp" 302 cidr_blocks = ["0.0.0.0/0"] 303 } 304 } 305 306 resource "aws_elasticache_security_group" "bar" { 307 name = "tf-test-security-group-%03d" 308 description = "tf-test-security-group-descr" 309 security_group_names = ["${aws_security_group.bar.name}"] 310 } 311 312 resource "aws_elasticache_cluster" "bar" { 313 cluster_id = "tf-test-%03d" 314 engine = "redis" 315 node_type = "cache.m1.small" 316 num_cache_nodes = 1 317 port = 6379 318 parameter_group_name = "default.redis2.8" 319 security_group_names = ["${aws_elasticache_security_group.bar.name}"] 320 snapshot_window = "07:00-09:00" 321 snapshot_retention_limit = 7 322 apply_immediately = true 323 } 324 ` 325 326 var testAccAWSElasticacheClusterConfigDecreasingNodes = ` 327 provider "aws" { 328 region = "us-east-1" 329 } 330 resource "aws_security_group" "bar" { 331 name = "tf-test-security-group-%03d" 332 description = "tf-test-security-group-descr" 333 ingress { 334 from_port = -1 335 to_port = -1 336 protocol = "icmp" 337 cidr_blocks = ["0.0.0.0/0"] 338 } 339 } 340 341 resource "aws_elasticache_security_group" "bar" { 342 name = "tf-test-security-group-%03d" 343 description = "tf-test-security-group-descr" 344 security_group_names = ["${aws_security_group.bar.name}"] 345 } 346 347 resource "aws_elasticache_cluster" "bar" { 348 cluster_id = "tf-test-%03d" 349 engine = "memcached" 350 node_type = "cache.m1.small" 351 num_cache_nodes = 3 352 port = 11211 353 parameter_group_name = "default.memcached1.4" 354 security_group_names = ["${aws_elasticache_security_group.bar.name}"] 355 } 356 ` 357 358 var testAccAWSElasticacheClusterConfigDecreasingNodes_update = ` 359 provider "aws" { 360 region = "us-east-1" 361 } 362 resource "aws_security_group" "bar" { 363 name = "tf-test-security-group-%03d" 364 description = "tf-test-security-group-descr" 365 ingress { 366 from_port = -1 367 to_port = -1 368 protocol = "icmp" 369 cidr_blocks = ["0.0.0.0/0"] 370 } 371 } 372 373 resource "aws_elasticache_security_group" "bar" { 374 name = "tf-test-security-group-%03d" 375 description = "tf-test-security-group-descr" 376 security_group_names = ["${aws_security_group.bar.name}"] 377 } 378 379 resource "aws_elasticache_cluster" "bar" { 380 cluster_id = "tf-test-%03d" 381 engine = "memcached" 382 node_type = "cache.m1.small" 383 num_cache_nodes = 1 384 port = 11211 385 parameter_group_name = "default.memcached1.4" 386 security_group_names = ["${aws_elasticache_security_group.bar.name}"] 387 apply_immediately = true 388 } 389 ` 390 391 var testAccAWSElasticacheClusterInVPCConfig = fmt.Sprintf(` 392 resource "aws_vpc" "foo" { 393 cidr_block = "192.168.0.0/16" 394 tags { 395 Name = "tf-test" 396 } 397 } 398 399 resource "aws_subnet" "foo" { 400 vpc_id = "${aws_vpc.foo.id}" 401 cidr_block = "192.168.0.0/20" 402 availability_zone = "us-west-2a" 403 tags { 404 Name = "tf-test" 405 } 406 } 407 408 resource "aws_elasticache_subnet_group" "bar" { 409 name = "tf-test-cache-subnet-%03d" 410 description = "tf-test-cache-subnet-group-descr" 411 subnet_ids = ["${aws_subnet.foo.id}"] 412 } 413 414 resource "aws_security_group" "bar" { 415 name = "tf-test-security-group-%03d" 416 description = "tf-test-security-group-descr" 417 vpc_id = "${aws_vpc.foo.id}" 418 ingress { 419 from_port = -1 420 to_port = -1 421 protocol = "icmp" 422 cidr_blocks = ["0.0.0.0/0"] 423 } 424 } 425 426 resource "aws_elasticache_cluster" "bar" { 427 // Including uppercase letters in this name to ensure 428 // that we correctly handle the fact that the API 429 // normalizes names to lowercase. 430 cluster_id = "tf-TEST-%03d" 431 node_type = "cache.m1.small" 432 num_cache_nodes = 1 433 engine = "redis" 434 engine_version = "2.8.19" 435 port = 6379 436 subnet_group_name = "${aws_elasticache_subnet_group.bar.name}" 437 security_group_ids = ["${aws_security_group.bar.id}"] 438 parameter_group_name = "default.redis2.8" 439 notification_topic_arn = "${aws_sns_topic.topic_example.arn}" 440 availability_zone = "us-west-2a" 441 } 442 443 resource "aws_sns_topic" "topic_example" { 444 name = "tf-ecache-cluster-test" 445 } 446 `, genRandInt(), genRandInt(), genRandInt()) 447 448 var testAccAWSElasticacheClusterMultiAZInVPCConfig = fmt.Sprintf(` 449 resource "aws_vpc" "foo" { 450 cidr_block = "192.168.0.0/16" 451 tags { 452 Name = "tf-test" 453 } 454 } 455 456 resource "aws_subnet" "foo" { 457 vpc_id = "${aws_vpc.foo.id}" 458 cidr_block = "192.168.0.0/20" 459 availability_zone = "us-west-2a" 460 tags { 461 Name = "tf-test-%03d" 462 } 463 } 464 465 resource "aws_subnet" "bar" { 466 vpc_id = "${aws_vpc.foo.id}" 467 cidr_block = "192.168.16.0/20" 468 availability_zone = "us-west-2b" 469 tags { 470 Name = "tf-test-%03d" 471 } 472 } 473 474 resource "aws_elasticache_subnet_group" "bar" { 475 name = "tf-test-cache-subnet-%03d" 476 description = "tf-test-cache-subnet-group-descr" 477 subnet_ids = [ 478 "${aws_subnet.foo.id}", 479 "${aws_subnet.bar.id}" 480 ] 481 } 482 483 resource "aws_security_group" "bar" { 484 name = "tf-test-security-group-%03d" 485 description = "tf-test-security-group-descr" 486 vpc_id = "${aws_vpc.foo.id}" 487 ingress { 488 from_port = -1 489 to_port = -1 490 protocol = "icmp" 491 cidr_blocks = ["0.0.0.0/0"] 492 } 493 } 494 495 resource "aws_elasticache_cluster" "bar" { 496 cluster_id = "tf-test-%03d" 497 engine = "memcached" 498 node_type = "cache.m1.small" 499 num_cache_nodes = 2 500 port = 11211 501 subnet_group_name = "${aws_elasticache_subnet_group.bar.name}" 502 security_group_ids = ["${aws_security_group.bar.id}"] 503 parameter_group_name = "default.memcached1.4" 504 az_mode = "cross-az" 505 availability_zones = [ 506 "us-west-2a", 507 "us-west-2b" 508 ] 509 } 510 `, genRandInt(), genRandInt(), genRandInt(), genRandInt(), genRandInt())