github.com/blacked/terraform@v0.6.2-0.20150806163846-669c4ad71586/builtin/providers/aws/resource_aws_elasticache_replication_group_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 "log" 7 8 "github.com/aws/aws-sdk-go/aws" 9 "github.com/aws/aws-sdk-go/service/elasticache" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestAccAWSEcacheReplicationGroup(t *testing.T) { 15 resource.Test(t, resource.TestCase{ 16 PreCheck: func() { testAccPreCheck(t) }, 17 Providers: testAccProviders, 18 CheckDestroy: testAccCheckAWSEcacheReplicationGroupDestroy, 19 Steps: []resource.TestStep{ 20 resource.TestStep{ 21 Config: testAccAWSEcacheReplicationGroupConfig, 22 Check: resource.ComposeTestCheckFunc( 23 testAccCheckAWSEcacheReplicationGroupExists("aws_elasticache_replication_group.bar"), 24 ), 25 }, 26 }, 27 }) 28 } 29 30 func testAccCheckAWSEcacheReplicationGroupDestroy(s *terraform.State) error { 31 conn := testAccProvider.Meta().(*AWSClient).elasticacheconn 32 33 for _, rs := range s.RootModule().Resources { 34 if rs.Type != "aws_elasticache_replication_group" { 35 continue 36 } 37 res, err := conn.DescribeReplicationGroups(&elasticache.DescribeReplicationGroupsInput{ 38 ReplicationGroupID: aws.String(rs.Primary.ID), 39 }) 40 if err != nil { 41 return err 42 } 43 if len(res.ReplicationGroups) > 0 { 44 return fmt.Errorf("still exist.") 45 } 46 } 47 return nil 48 } 49 50 func testAccCheckAWSEcacheReplicationGroupExists(n string) resource.TestCheckFunc { 51 return func(s *terraform.State) error { 52 rs, ok := s.RootModule().Resources[n] 53 if !ok { 54 return fmt.Errorf("Not found: %s", n) 55 } 56 57 conn := testAccProvider.Meta().(*AWSClient).elasticacheconn 58 res, err := conn.DescribeReplicationGroups(&elasticache.DescribeReplicationGroupsInput{ 59 ReplicationGroupID: aws.String(rs.Primary.ID), 60 }) 61 62 if err != nil { 63 return fmt.Errorf("CacheReplicationGroup error: %v", err) 64 } 65 66 if len(res.ReplicationGroups) != 1 || 67 *res.ReplicationGroups[0].ReplicationGroupID != rs.Primary.ID { 68 return fmt.Errorf("Replication group not found") 69 } 70 log.Printf("[DEBUG] Rep group found") 71 return nil 72 } 73 } 74 75 var testAccAWSEcacheReplicationGroupConfig = fmt.Sprintf(` 76 resource "aws_elasticache_replication_group" "bar" { 77 replication_group_id = "tf-repgrp-%03d" 78 cache_node_type = "cache.m1.small" 79 num_cache_clusters = 2 80 description = "tf-test-replication-group-descr" 81 } 82 `, genRandInt())