github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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 func TestAccAWSElasticacheReplicationGroup_vpc(t *testing.T) { 102 var rg elasticache.ReplicationGroup 103 resource.Test(t, resource.TestCase{ 104 PreCheck: func() { testAccPreCheck(t) }, 105 Providers: testAccProviders, 106 CheckDestroy: testAccCheckAWSElasticacheReplicationDestroy, 107 Steps: []resource.TestStep{ 108 resource.TestStep{ 109 Config: testAccAWSElasticacheReplicationGroupInVPCConfig, 110 Check: resource.ComposeTestCheckFunc( 111 testAccCheckAWSElasticacheReplicationGroupExists("aws_elasticache_replication_group.bar", &rg), 112 resource.TestCheckResourceAttr( 113 "aws_elasticache_replication_group.bar", "number_cache_clusters", "1"), 114 ), 115 }, 116 }, 117 }) 118 } 119 120 func TestAccAWSElasticacheReplicationGroup_multiAzInVpc(t *testing.T) { 121 var rg elasticache.ReplicationGroup 122 resource.Test(t, resource.TestCase{ 123 PreCheck: func() { testAccPreCheck(t) }, 124 Providers: testAccProviders, 125 CheckDestroy: testAccCheckAWSElasticacheReplicationDestroy, 126 Steps: []resource.TestStep{ 127 resource.TestStep{ 128 Config: testAccAWSElasticacheReplicationGroupMultiAZInVPCConfig, 129 Check: resource.ComposeTestCheckFunc( 130 testAccCheckAWSElasticacheReplicationGroupExists("aws_elasticache_replication_group.bar", &rg), 131 resource.TestCheckResourceAttr( 132 "aws_elasticache_replication_group.bar", "number_cache_clusters", "2"), 133 resource.TestCheckResourceAttr( 134 "aws_elasticache_replication_group.bar", "automatic_failover_enabled", "true"), 135 ), 136 }, 137 }, 138 }) 139 } 140 141 func TestResourceAWSElastiCacheReplicationGroupIdValidation(t *testing.T) { 142 cases := []struct { 143 Value string 144 ErrCount int 145 }{ 146 { 147 Value: "tEsting", 148 ErrCount: 0, 149 }, 150 { 151 Value: "t.sting", 152 ErrCount: 1, 153 }, 154 { 155 Value: "t--sting", 156 ErrCount: 1, 157 }, 158 { 159 Value: "1testing", 160 ErrCount: 1, 161 }, 162 { 163 Value: "testing-", 164 ErrCount: 1, 165 }, 166 { 167 Value: randomString(17), 168 ErrCount: 1, 169 }, 170 } 171 172 for _, tc := range cases { 173 _, errors := validateAwsElastiCacheReplicationGroupId(tc.Value, "aws_elasticache_replication_group_replication_group_id") 174 175 if len(errors) != tc.ErrCount { 176 t.Fatalf("Expected the ElastiCache Replication Group Id to trigger a validation error") 177 } 178 } 179 } 180 181 func TestResourceAWSElastiCacheReplicationGroupEngineValidation(t *testing.T) { 182 cases := []struct { 183 Value string 184 ErrCount int 185 }{ 186 { 187 Value: "Redis", 188 ErrCount: 0, 189 }, 190 { 191 Value: "REDIS", 192 ErrCount: 0, 193 }, 194 { 195 Value: "memcached", 196 ErrCount: 1, 197 }, 198 } 199 200 for _, tc := range cases { 201 _, errors := validateAwsElastiCacheReplicationGroupEngine(tc.Value, "aws_elasticache_replication_group_engine") 202 203 if len(errors) != tc.ErrCount { 204 t.Fatalf("Expected the ElastiCache Replication Group Engine to trigger a validation error") 205 } 206 } 207 } 208 209 func testAccCheckAWSElasticacheReplicationGroupExists(n string, v *elasticache.ReplicationGroup) resource.TestCheckFunc { 210 return func(s *terraform.State) error { 211 rs, ok := s.RootModule().Resources[n] 212 if !ok { 213 return fmt.Errorf("Not found: %s", n) 214 } 215 216 if rs.Primary.ID == "" { 217 return fmt.Errorf("No replication group ID is set") 218 } 219 220 conn := testAccProvider.Meta().(*AWSClient).elasticacheconn 221 res, err := conn.DescribeReplicationGroups(&elasticache.DescribeReplicationGroupsInput{ 222 ReplicationGroupId: aws.String(rs.Primary.ID), 223 }) 224 if err != nil { 225 return fmt.Errorf("Elasticache error: %v", err) 226 } 227 228 for _, rg := range res.ReplicationGroups { 229 if *rg.ReplicationGroupId == rs.Primary.ID { 230 *v = *rg 231 } 232 } 233 234 return nil 235 } 236 } 237 238 func testAccCheckAWSElasticacheReplicationDestroy(s *terraform.State) error { 239 conn := testAccProvider.Meta().(*AWSClient).elasticacheconn 240 241 for _, rs := range s.RootModule().Resources { 242 if rs.Type != "aws_elasticache_replication_group" { 243 continue 244 } 245 res, err := conn.DescribeReplicationGroups(&elasticache.DescribeReplicationGroupsInput{ 246 ReplicationGroupId: aws.String(rs.Primary.ID), 247 }) 248 if err != nil { 249 // Verify the error is what we want 250 if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "ReplicationGroupNotFoundFault" { 251 continue 252 } 253 return err 254 } 255 if len(res.ReplicationGroups) > 0 { 256 return fmt.Errorf("still exist.") 257 } 258 } 259 return nil 260 } 261 262 func testAccAWSElasticacheReplicationGroupConfig(rName string) string { 263 return fmt.Sprintf(` 264 provider "aws" { 265 region = "us-east-1" 266 } 267 resource "aws_security_group" "bar" { 268 name = "tf-test-security-group-%s" 269 description = "tf-test-security-group-descr" 270 ingress { 271 from_port = -1 272 to_port = -1 273 protocol = "icmp" 274 cidr_blocks = ["0.0.0.0/0"] 275 } 276 } 277 278 resource "aws_elasticache_security_group" "bar" { 279 name = "tf-test-security-group-%s" 280 description = "tf-test-security-group-descr" 281 security_group_names = ["${aws_security_group.bar.name}"] 282 } 283 284 resource "aws_elasticache_replication_group" "bar" { 285 replication_group_id = "tf-%s" 286 replication_group_description = "test description" 287 node_type = "cache.m1.small" 288 number_cache_clusters = 2 289 port = 6379 290 parameter_group_name = "default.redis2.8" 291 security_group_names = ["${aws_elasticache_security_group.bar.name}"] 292 apply_immediately = true 293 }`, rName, rName, rName) 294 } 295 296 func testAccAWSElasticacheReplicationGroupConfigUpdatedDescription(rName string) string { 297 return fmt.Sprintf(` 298 provider "aws" { 299 region = "us-east-1" 300 } 301 resource "aws_security_group" "bar" { 302 name = "tf-test-security-group-%s" 303 description = "tf-test-security-group-descr" 304 ingress { 305 from_port = -1 306 to_port = -1 307 protocol = "icmp" 308 cidr_blocks = ["0.0.0.0/0"] 309 } 310 } 311 312 resource "aws_elasticache_security_group" "bar" { 313 name = "tf-test-security-group-%s" 314 description = "tf-test-security-group-descr" 315 security_group_names = ["${aws_security_group.bar.name}"] 316 } 317 318 resource "aws_elasticache_replication_group" "bar" { 319 replication_group_id = "tf-%s" 320 replication_group_description = "updated description" 321 node_type = "cache.m1.small" 322 number_cache_clusters = 2 323 port = 6379 324 parameter_group_name = "default.redis2.8" 325 security_group_names = ["${aws_elasticache_security_group.bar.name}"] 326 apply_immediately = true 327 }`, rName, rName, rName) 328 } 329 330 func testAccAWSElasticacheReplicationGroupConfigUpdatedNodeSize(rName string) string { 331 return fmt.Sprintf(` 332 provider "aws" { 333 region = "us-east-1" 334 } 335 resource "aws_security_group" "bar" { 336 name = "tf-test-security-group-%s" 337 description = "tf-test-security-group-descr" 338 ingress { 339 from_port = -1 340 to_port = -1 341 protocol = "icmp" 342 cidr_blocks = ["0.0.0.0/0"] 343 } 344 } 345 346 resource "aws_elasticache_security_group" "bar" { 347 name = "tf-test-security-group-%s" 348 description = "tf-test-security-group-descr" 349 security_group_names = ["${aws_security_group.bar.name}"] 350 } 351 352 resource "aws_elasticache_replication_group" "bar" { 353 replication_group_id = "tf-%s" 354 replication_group_description = "updated description" 355 node_type = "cache.m1.medium" 356 number_cache_clusters = 2 357 port = 6379 358 parameter_group_name = "default.redis2.8" 359 security_group_names = ["${aws_elasticache_security_group.bar.name}"] 360 apply_immediately = true 361 }`, rName, rName, rName) 362 } 363 364 var testAccAWSElasticacheReplicationGroupInVPCConfig = fmt.Sprintf(` 365 resource "aws_vpc" "foo" { 366 cidr_block = "192.168.0.0/16" 367 tags { 368 Name = "tf-test" 369 } 370 } 371 372 resource "aws_subnet" "foo" { 373 vpc_id = "${aws_vpc.foo.id}" 374 cidr_block = "192.168.0.0/20" 375 availability_zone = "us-west-2a" 376 tags { 377 Name = "tf-test" 378 } 379 } 380 381 resource "aws_elasticache_subnet_group" "bar" { 382 name = "tf-test-cache-subnet-%03d" 383 description = "tf-test-cache-subnet-group-descr" 384 subnet_ids = ["${aws_subnet.foo.id}"] 385 } 386 387 resource "aws_security_group" "bar" { 388 name = "tf-test-security-group-%03d" 389 description = "tf-test-security-group-descr" 390 vpc_id = "${aws_vpc.foo.id}" 391 ingress { 392 from_port = -1 393 to_port = -1 394 protocol = "icmp" 395 cidr_blocks = ["0.0.0.0/0"] 396 } 397 } 398 399 resource "aws_elasticache_replication_group" "bar" { 400 replication_group_id = "tf-%s" 401 replication_group_description = "test description" 402 node_type = "cache.m1.small" 403 number_cache_clusters = 1 404 port = 6379 405 subnet_group_name = "${aws_elasticache_subnet_group.bar.name}" 406 security_group_ids = ["${aws_security_group.bar.id}"] 407 parameter_group_name = "default.redis2.8" 408 availability_zones = ["us-west-2a"] 409 } 410 411 `, acctest.RandInt(), acctest.RandInt(), acctest.RandString(10)) 412 413 var testAccAWSElasticacheReplicationGroupMultiAZInVPCConfig = fmt.Sprintf(` 414 resource "aws_vpc" "foo" { 415 cidr_block = "192.168.0.0/16" 416 tags { 417 Name = "tf-test" 418 } 419 } 420 421 resource "aws_subnet" "foo" { 422 vpc_id = "${aws_vpc.foo.id}" 423 cidr_block = "192.168.0.0/20" 424 availability_zone = "us-west-2a" 425 tags { 426 Name = "tf-test-%03d" 427 } 428 } 429 430 resource "aws_subnet" "bar" { 431 vpc_id = "${aws_vpc.foo.id}" 432 cidr_block = "192.168.16.0/20" 433 availability_zone = "us-west-2b" 434 tags { 435 Name = "tf-test-%03d" 436 } 437 } 438 439 resource "aws_elasticache_subnet_group" "bar" { 440 name = "tf-test-cache-subnet-%03d" 441 description = "tf-test-cache-subnet-group-descr" 442 subnet_ids = [ 443 "${aws_subnet.foo.id}", 444 "${aws_subnet.bar.id}" 445 ] 446 } 447 448 resource "aws_security_group" "bar" { 449 name = "tf-test-security-group-%03d" 450 description = "tf-test-security-group-descr" 451 vpc_id = "${aws_vpc.foo.id}" 452 ingress { 453 from_port = -1 454 to_port = -1 455 protocol = "icmp" 456 cidr_blocks = ["0.0.0.0/0"] 457 } 458 } 459 460 resource "aws_elasticache_replication_group" "bar" { 461 replication_group_id = "tf-%s" 462 replication_group_description = "test description" 463 node_type = "cache.m1.small" 464 number_cache_clusters = 2 465 port = 6379 466 subnet_group_name = "${aws_elasticache_subnet_group.bar.name}" 467 security_group_ids = ["${aws_security_group.bar.id}"] 468 parameter_group_name = "default.redis2.8" 469 availability_zones = ["us-west-2a","us-west-2b"] 470 automatic_failover_enabled = true 471 } 472 `, acctest.RandInt(), acctest.RandInt(), acctest.RandInt(), acctest.RandInt(), acctest.RandString(10))