github.com/inge4pres/terraform@v0.7.5-0.20160930053151-bd083f84f376/builtin/providers/aws/resource_aws_elasticache_replication_group_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/aws/aws-sdk-go/aws/awserr" 9 "github.com/aws/aws-sdk-go/service/elasticache" 10 "github.com/hashicorp/terraform/helper/acctest" 11 "github.com/hashicorp/terraform/helper/resource" 12 "github.com/hashicorp/terraform/terraform" 13 ) 14 15 func TestAccAWSElasticacheReplicationGroup_basic(t *testing.T) { 16 var rg elasticache.ReplicationGroup 17 18 resource.Test(t, resource.TestCase{ 19 PreCheck: func() { testAccPreCheck(t) }, 20 Providers: testAccProviders, 21 CheckDestroy: testAccCheckAWSElasticacheReplicationDestroy, 22 Steps: []resource.TestStep{ 23 resource.TestStep{ 24 Config: testAccAWSElasticacheReplicationGroupConfig(acctest.RandString(10)), 25 Check: resource.ComposeTestCheckFunc( 26 testAccCheckAWSElasticacheReplicationGroupExists("aws_elasticache_replication_group.bar", &rg), 27 resource.TestCheckResourceAttr( 28 "aws_elasticache_replication_group.bar", "number_cache_clusters", "2"), 29 ), 30 }, 31 }, 32 }) 33 } 34 35 func TestAccAWSElasticacheReplicationGroup_updateDescription(t *testing.T) { 36 var rg elasticache.ReplicationGroup 37 rName := acctest.RandString(10) 38 resource.Test(t, resource.TestCase{ 39 PreCheck: func() { testAccPreCheck(t) }, 40 Providers: testAccProviders, 41 CheckDestroy: testAccCheckAWSElasticacheReplicationDestroy, 42 Steps: []resource.TestStep{ 43 resource.TestStep{ 44 Config: testAccAWSElasticacheReplicationGroupConfig(rName), 45 Check: resource.ComposeTestCheckFunc( 46 testAccCheckAWSElasticacheReplicationGroupExists("aws_elasticache_replication_group.bar", &rg), 47 resource.TestCheckResourceAttr( 48 "aws_elasticache_replication_group.bar", "number_cache_clusters", "2"), 49 resource.TestCheckResourceAttr( 50 "aws_elasticache_replication_group.bar", "replication_group_description", "test description"), 51 ), 52 }, 53 54 resource.TestStep{ 55 Config: testAccAWSElasticacheReplicationGroupConfigUpdatedDescription(rName), 56 Check: resource.ComposeTestCheckFunc( 57 testAccCheckAWSElasticacheReplicationGroupExists("aws_elasticache_replication_group.bar", &rg), 58 resource.TestCheckResourceAttr( 59 "aws_elasticache_replication_group.bar", "number_cache_clusters", "2"), 60 resource.TestCheckResourceAttr( 61 "aws_elasticache_replication_group.bar", "replication_group_description", "updated description"), 62 ), 63 }, 64 }, 65 }) 66 } 67 68 func TestAccAWSElasticacheReplicationGroup_updateNodeSize(t *testing.T) { 69 var rg elasticache.ReplicationGroup 70 rName := acctest.RandString(10) 71 resource.Test(t, resource.TestCase{ 72 PreCheck: func() { testAccPreCheck(t) }, 73 Providers: testAccProviders, 74 CheckDestroy: testAccCheckAWSElasticacheReplicationDestroy, 75 Steps: []resource.TestStep{ 76 resource.TestStep{ 77 Config: testAccAWSElasticacheReplicationGroupConfig(rName), 78 Check: resource.ComposeTestCheckFunc( 79 testAccCheckAWSElasticacheReplicationGroupExists("aws_elasticache_replication_group.bar", &rg), 80 resource.TestCheckResourceAttr( 81 "aws_elasticache_replication_group.bar", "number_cache_clusters", "2"), 82 resource.TestCheckResourceAttr( 83 "aws_elasticache_replication_group.bar", "node_type", "cache.m1.small"), 84 ), 85 }, 86 87 resource.TestStep{ 88 Config: testAccAWSElasticacheReplicationGroupConfigUpdatedNodeSize(rName), 89 Check: resource.ComposeTestCheckFunc( 90 testAccCheckAWSElasticacheReplicationGroupExists("aws_elasticache_replication_group.bar", &rg), 91 resource.TestCheckResourceAttr( 92 "aws_elasticache_replication_group.bar", "number_cache_clusters", "2"), 93 resource.TestCheckResourceAttr( 94 "aws_elasticache_replication_group.bar", "node_type", "cache.m1.medium"), 95 ), 96 }, 97 }, 98 }) 99 } 100 101 //This is a test to prove that we panic we get in https://github.com/hashicorp/terraform/issues/9097 102 func TestAccAWSElasticacheReplicationGroup_updateParameterGroup(t *testing.T) { 103 var rg elasticache.ReplicationGroup 104 rName := acctest.RandString(10) 105 resource.Test(t, resource.TestCase{ 106 PreCheck: func() { testAccPreCheck(t) }, 107 Providers: testAccProviders, 108 CheckDestroy: testAccCheckAWSElasticacheReplicationDestroy, 109 Steps: []resource.TestStep{ 110 resource.TestStep{ 111 Config: testAccAWSElasticacheReplicationGroupConfig(rName), 112 Check: resource.ComposeTestCheckFunc( 113 testAccCheckAWSElasticacheReplicationGroupExists("aws_elasticache_replication_group.bar", &rg), 114 resource.TestCheckResourceAttr( 115 "aws_elasticache_replication_group.bar", "parameter_group_name", "default.redis2.8"), 116 ), 117 }, 118 119 resource.TestStep{ 120 Config: testAccAWSElasticacheReplicationGroupConfigUpdatedParameterGroup(rName), 121 Check: resource.ComposeTestCheckFunc( 122 testAccCheckAWSElasticacheReplicationGroupExists("aws_elasticache_replication_group.bar", &rg), 123 resource.TestCheckResourceAttr( 124 "aws_elasticache_replication_group.bar", "parameter_group_name", "allkeys-lru"), 125 ), 126 }, 127 }, 128 }) 129 } 130 131 func TestAccAWSElasticacheReplicationGroup_vpc(t *testing.T) { 132 var rg elasticache.ReplicationGroup 133 resource.Test(t, resource.TestCase{ 134 PreCheck: func() { testAccPreCheck(t) }, 135 Providers: testAccProviders, 136 CheckDestroy: testAccCheckAWSElasticacheReplicationDestroy, 137 Steps: []resource.TestStep{ 138 resource.TestStep{ 139 Config: testAccAWSElasticacheReplicationGroupInVPCConfig, 140 Check: resource.ComposeTestCheckFunc( 141 testAccCheckAWSElasticacheReplicationGroupExists("aws_elasticache_replication_group.bar", &rg), 142 resource.TestCheckResourceAttr( 143 "aws_elasticache_replication_group.bar", "number_cache_clusters", "1"), 144 ), 145 }, 146 }, 147 }) 148 } 149 150 func TestAccAWSElasticacheReplicationGroup_multiAzInVpc(t *testing.T) { 151 var rg elasticache.ReplicationGroup 152 resource.Test(t, resource.TestCase{ 153 PreCheck: func() { testAccPreCheck(t) }, 154 Providers: testAccProviders, 155 CheckDestroy: testAccCheckAWSElasticacheReplicationDestroy, 156 Steps: []resource.TestStep{ 157 resource.TestStep{ 158 Config: testAccAWSElasticacheReplicationGroupMultiAZInVPCConfig, 159 Check: resource.ComposeTestCheckFunc( 160 testAccCheckAWSElasticacheReplicationGroupExists("aws_elasticache_replication_group.bar", &rg), 161 resource.TestCheckResourceAttr( 162 "aws_elasticache_replication_group.bar", "number_cache_clusters", "2"), 163 resource.TestCheckResourceAttr( 164 "aws_elasticache_replication_group.bar", "automatic_failover_enabled", "true"), 165 ), 166 }, 167 }, 168 }) 169 } 170 171 func TestResourceAWSElastiCacheReplicationGroupIdValidation(t *testing.T) { 172 cases := []struct { 173 Value string 174 ErrCount int 175 }{ 176 { 177 Value: "tEsting", 178 ErrCount: 0, 179 }, 180 { 181 Value: "t.sting", 182 ErrCount: 1, 183 }, 184 { 185 Value: "t--sting", 186 ErrCount: 1, 187 }, 188 { 189 Value: "1testing", 190 ErrCount: 1, 191 }, 192 { 193 Value: "testing-", 194 ErrCount: 1, 195 }, 196 { 197 Value: randomString(17), 198 ErrCount: 1, 199 }, 200 } 201 202 for _, tc := range cases { 203 _, errors := validateAwsElastiCacheReplicationGroupId(tc.Value, "aws_elasticache_replication_group_replication_group_id") 204 205 if len(errors) != tc.ErrCount { 206 t.Fatalf("Expected the ElastiCache Replication Group Id to trigger a validation error") 207 } 208 } 209 } 210 211 func TestResourceAWSElastiCacheReplicationGroupEngineValidation(t *testing.T) { 212 cases := []struct { 213 Value string 214 ErrCount int 215 }{ 216 { 217 Value: "Redis", 218 ErrCount: 0, 219 }, 220 { 221 Value: "REDIS", 222 ErrCount: 0, 223 }, 224 { 225 Value: "memcached", 226 ErrCount: 1, 227 }, 228 } 229 230 for _, tc := range cases { 231 _, errors := validateAwsElastiCacheReplicationGroupEngine(tc.Value, "aws_elasticache_replication_group_engine") 232 233 if len(errors) != tc.ErrCount { 234 t.Fatalf("Expected the ElastiCache Replication Group Engine to trigger a validation error") 235 } 236 } 237 } 238 239 func testAccCheckAWSElasticacheReplicationGroupExists(n string, v *elasticache.ReplicationGroup) resource.TestCheckFunc { 240 return func(s *terraform.State) error { 241 rs, ok := s.RootModule().Resources[n] 242 if !ok { 243 return fmt.Errorf("Not found: %s", n) 244 } 245 246 if rs.Primary.ID == "" { 247 return fmt.Errorf("No replication group ID is set") 248 } 249 250 conn := testAccProvider.Meta().(*AWSClient).elasticacheconn 251 res, err := conn.DescribeReplicationGroups(&elasticache.DescribeReplicationGroupsInput{ 252 ReplicationGroupId: aws.String(rs.Primary.ID), 253 }) 254 if err != nil { 255 return fmt.Errorf("Elasticache error: %v", err) 256 } 257 258 for _, rg := range res.ReplicationGroups { 259 if *rg.ReplicationGroupId == rs.Primary.ID { 260 *v = *rg 261 } 262 } 263 264 return nil 265 } 266 } 267 268 func testAccCheckAWSElasticacheReplicationDestroy(s *terraform.State) error { 269 conn := testAccProvider.Meta().(*AWSClient).elasticacheconn 270 271 for _, rs := range s.RootModule().Resources { 272 if rs.Type != "aws_elasticache_replication_group" { 273 continue 274 } 275 res, err := conn.DescribeReplicationGroups(&elasticache.DescribeReplicationGroupsInput{ 276 ReplicationGroupId: aws.String(rs.Primary.ID), 277 }) 278 if err != nil { 279 // Verify the error is what we want 280 if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "ReplicationGroupNotFoundFault" { 281 continue 282 } 283 return err 284 } 285 if len(res.ReplicationGroups) > 0 { 286 return fmt.Errorf("still exist.") 287 } 288 } 289 return nil 290 } 291 292 func testAccAWSElasticacheReplicationGroupConfig(rName string) string { 293 return fmt.Sprintf(` 294 provider "aws" { 295 region = "us-east-1" 296 } 297 resource "aws_security_group" "bar" { 298 name = "tf-test-security-group-%s" 299 description = "tf-test-security-group-descr" 300 ingress { 301 from_port = -1 302 to_port = -1 303 protocol = "icmp" 304 cidr_blocks = ["0.0.0.0/0"] 305 } 306 } 307 308 resource "aws_elasticache_security_group" "bar" { 309 name = "tf-test-security-group-%s" 310 description = "tf-test-security-group-descr" 311 security_group_names = ["${aws_security_group.bar.name}"] 312 } 313 314 resource "aws_elasticache_replication_group" "bar" { 315 replication_group_id = "tf-%s" 316 replication_group_description = "test description" 317 node_type = "cache.m1.small" 318 number_cache_clusters = 2 319 port = 6379 320 parameter_group_name = "default.redis2.8" 321 security_group_names = ["${aws_elasticache_security_group.bar.name}"] 322 apply_immediately = true 323 }`, rName, rName, rName) 324 } 325 326 func testAccAWSElasticacheReplicationGroupConfigUpdatedParameterGroup(rName string) string { 327 return fmt.Sprintf(` 328 provider "aws" { 329 region = "us-east-1" 330 } 331 resource "aws_security_group" "bar" { 332 name = "tf-test-security-group-%s" 333 description = "tf-test-security-group-descr" 334 ingress { 335 from_port = -1 336 to_port = -1 337 protocol = "icmp" 338 cidr_blocks = ["0.0.0.0/0"] 339 } 340 } 341 342 resource "aws_elasticache_security_group" "bar" { 343 name = "tf-test-security-group-%s" 344 description = "tf-test-security-group-descr" 345 security_group_names = ["${aws_security_group.bar.name}"] 346 } 347 348 resource "aws_elasticache_parameter_group" "bar" { 349 name = "allkeys-lru" 350 family = "redis2.8" 351 352 parameter { 353 name = "maxmemory-policy" 354 value = "allkeys-lru" 355 } 356 } 357 358 resource "aws_elasticache_replication_group" "bar" { 359 replication_group_id = "tf-%s" 360 replication_group_description = "test description" 361 node_type = "cache.m1.small" 362 number_cache_clusters = 2 363 port = 6379 364 parameter_group_name = "${aws_elasticache_parameter_group.bar.name}" 365 security_group_names = ["${aws_elasticache_security_group.bar.name}"] 366 apply_immediately = true 367 }`, rName, rName, rName) 368 } 369 370 func testAccAWSElasticacheReplicationGroupConfigUpdatedDescription(rName string) string { 371 return fmt.Sprintf(` 372 provider "aws" { 373 region = "us-east-1" 374 } 375 resource "aws_security_group" "bar" { 376 name = "tf-test-security-group-%s" 377 description = "tf-test-security-group-descr" 378 ingress { 379 from_port = -1 380 to_port = -1 381 protocol = "icmp" 382 cidr_blocks = ["0.0.0.0/0"] 383 } 384 } 385 386 resource "aws_elasticache_security_group" "bar" { 387 name = "tf-test-security-group-%s" 388 description = "tf-test-security-group-descr" 389 security_group_names = ["${aws_security_group.bar.name}"] 390 } 391 392 resource "aws_elasticache_replication_group" "bar" { 393 replication_group_id = "tf-%s" 394 replication_group_description = "updated description" 395 node_type = "cache.m1.small" 396 number_cache_clusters = 2 397 port = 6379 398 parameter_group_name = "default.redis2.8" 399 security_group_names = ["${aws_elasticache_security_group.bar.name}"] 400 apply_immediately = true 401 }`, rName, rName, rName) 402 } 403 404 func testAccAWSElasticacheReplicationGroupConfigUpdatedNodeSize(rName string) string { 405 return fmt.Sprintf(` 406 provider "aws" { 407 region = "us-east-1" 408 } 409 resource "aws_security_group" "bar" { 410 name = "tf-test-security-group-%s" 411 description = "tf-test-security-group-descr" 412 ingress { 413 from_port = -1 414 to_port = -1 415 protocol = "icmp" 416 cidr_blocks = ["0.0.0.0/0"] 417 } 418 } 419 420 resource "aws_elasticache_security_group" "bar" { 421 name = "tf-test-security-group-%s" 422 description = "tf-test-security-group-descr" 423 security_group_names = ["${aws_security_group.bar.name}"] 424 } 425 426 resource "aws_elasticache_replication_group" "bar" { 427 replication_group_id = "tf-%s" 428 replication_group_description = "updated description" 429 node_type = "cache.m1.medium" 430 number_cache_clusters = 2 431 port = 6379 432 parameter_group_name = "default.redis2.8" 433 security_group_names = ["${aws_elasticache_security_group.bar.name}"] 434 apply_immediately = true 435 }`, rName, rName, rName) 436 } 437 438 var testAccAWSElasticacheReplicationGroupInVPCConfig = fmt.Sprintf(` 439 resource "aws_vpc" "foo" { 440 cidr_block = "192.168.0.0/16" 441 tags { 442 Name = "tf-test" 443 } 444 } 445 446 resource "aws_subnet" "foo" { 447 vpc_id = "${aws_vpc.foo.id}" 448 cidr_block = "192.168.0.0/20" 449 availability_zone = "us-west-2a" 450 tags { 451 Name = "tf-test" 452 } 453 } 454 455 resource "aws_elasticache_subnet_group" "bar" { 456 name = "tf-test-cache-subnet-%03d" 457 description = "tf-test-cache-subnet-group-descr" 458 subnet_ids = ["${aws_subnet.foo.id}"] 459 } 460 461 resource "aws_security_group" "bar" { 462 name = "tf-test-security-group-%03d" 463 description = "tf-test-security-group-descr" 464 vpc_id = "${aws_vpc.foo.id}" 465 ingress { 466 from_port = -1 467 to_port = -1 468 protocol = "icmp" 469 cidr_blocks = ["0.0.0.0/0"] 470 } 471 } 472 473 resource "aws_elasticache_replication_group" "bar" { 474 replication_group_id = "tf-%s" 475 replication_group_description = "test description" 476 node_type = "cache.m1.small" 477 number_cache_clusters = 1 478 port = 6379 479 subnet_group_name = "${aws_elasticache_subnet_group.bar.name}" 480 security_group_ids = ["${aws_security_group.bar.id}"] 481 parameter_group_name = "default.redis2.8" 482 availability_zones = ["us-west-2a"] 483 } 484 485 `, acctest.RandInt(), acctest.RandInt(), acctest.RandString(10)) 486 487 var testAccAWSElasticacheReplicationGroupMultiAZInVPCConfig = fmt.Sprintf(` 488 resource "aws_vpc" "foo" { 489 cidr_block = "192.168.0.0/16" 490 tags { 491 Name = "tf-test" 492 } 493 } 494 495 resource "aws_subnet" "foo" { 496 vpc_id = "${aws_vpc.foo.id}" 497 cidr_block = "192.168.0.0/20" 498 availability_zone = "us-west-2a" 499 tags { 500 Name = "tf-test-%03d" 501 } 502 } 503 504 resource "aws_subnet" "bar" { 505 vpc_id = "${aws_vpc.foo.id}" 506 cidr_block = "192.168.16.0/20" 507 availability_zone = "us-west-2b" 508 tags { 509 Name = "tf-test-%03d" 510 } 511 } 512 513 resource "aws_elasticache_subnet_group" "bar" { 514 name = "tf-test-cache-subnet-%03d" 515 description = "tf-test-cache-subnet-group-descr" 516 subnet_ids = [ 517 "${aws_subnet.foo.id}", 518 "${aws_subnet.bar.id}" 519 ] 520 } 521 522 resource "aws_security_group" "bar" { 523 name = "tf-test-security-group-%03d" 524 description = "tf-test-security-group-descr" 525 vpc_id = "${aws_vpc.foo.id}" 526 ingress { 527 from_port = -1 528 to_port = -1 529 protocol = "icmp" 530 cidr_blocks = ["0.0.0.0/0"] 531 } 532 } 533 534 resource "aws_elasticache_replication_group" "bar" { 535 replication_group_id = "tf-%s" 536 replication_group_description = "test description" 537 node_type = "cache.m1.small" 538 number_cache_clusters = 2 539 port = 6379 540 subnet_group_name = "${aws_elasticache_subnet_group.bar.name}" 541 security_group_ids = ["${aws_security_group.bar.id}"] 542 parameter_group_name = "default.redis2.8" 543 availability_zones = ["us-west-2a","us-west-2b"] 544 automatic_failover_enabled = true 545 } 546 `, acctest.RandInt(), acctest.RandInt(), acctest.RandInt(), acctest.RandInt(), acctest.RandString(10))