github.com/leeprovoost/terraform@v0.6.10-0.20160119085442-96f3f76118e7/builtin/providers/aws/resource_aws_elasticache_subnet_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/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestAccAWSElasticacheSubnetGroup_basic(t *testing.T) { 15 var csg elasticache.CacheSubnetGroup 16 config := fmt.Sprintf(testAccAWSElasticacheSubnetGroupConfig, genRandInt()) 17 18 resource.Test(t, resource.TestCase{ 19 PreCheck: func() { testAccPreCheck(t) }, 20 Providers: testAccProviders, 21 CheckDestroy: testAccCheckAWSElasticacheSubnetGroupDestroy, 22 Steps: []resource.TestStep{ 23 resource.TestStep{ 24 Config: config, 25 Check: resource.ComposeTestCheckFunc( 26 testAccCheckAWSElasticacheSubnetGroupExists("aws_elasticache_subnet_group.bar", &csg), 27 ), 28 }, 29 }, 30 }) 31 } 32 33 func TestAccAWSElasticacheSubnetGroup_update(t *testing.T) { 34 var csg elasticache.CacheSubnetGroup 35 rn := "aws_elasticache_subnet_group.bar" 36 ri := genRandInt() 37 preConfig := fmt.Sprintf(testAccAWSElasticacheSubnetGroupUpdateConfigPre, ri) 38 postConfig := fmt.Sprintf(testAccAWSElasticacheSubnetGroupUpdateConfigPost, ri) 39 40 resource.Test(t, resource.TestCase{ 41 PreCheck: func() { testAccPreCheck(t) }, 42 Providers: testAccProviders, 43 CheckDestroy: testAccCheckAWSElasticacheSubnetGroupDestroy, 44 Steps: []resource.TestStep{ 45 resource.TestStep{ 46 Config: preConfig, 47 Check: resource.ComposeTestCheckFunc( 48 testAccCheckAWSElasticacheSubnetGroupExists(rn, &csg), 49 testAccCheckAWSElastiCacheSubnetGroupAttrs(&csg, rn, 1), 50 ), 51 }, 52 53 resource.TestStep{ 54 Config: postConfig, 55 Check: resource.ComposeTestCheckFunc( 56 testAccCheckAWSElasticacheSubnetGroupExists(rn, &csg), 57 testAccCheckAWSElastiCacheSubnetGroupAttrs(&csg, rn, 2), 58 ), 59 }, 60 }, 61 }) 62 } 63 64 func testAccCheckAWSElasticacheSubnetGroupDestroy(s *terraform.State) error { 65 conn := testAccProvider.Meta().(*AWSClient).elasticacheconn 66 67 for _, rs := range s.RootModule().Resources { 68 if rs.Type != "aws_elasticache_subnet_group" { 69 continue 70 } 71 res, err := conn.DescribeCacheSubnetGroups(&elasticache.DescribeCacheSubnetGroupsInput{ 72 CacheSubnetGroupName: aws.String(rs.Primary.ID), 73 }) 74 if err != nil { 75 // Verify the error is what we want 76 if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "CacheSubnetGroupNotFoundFault" { 77 continue 78 } 79 return err 80 } 81 if len(res.CacheSubnetGroups) > 0 { 82 return fmt.Errorf("still exist.") 83 } 84 } 85 return nil 86 } 87 88 func testAccCheckAWSElasticacheSubnetGroupExists(n string, csg *elasticache.CacheSubnetGroup) resource.TestCheckFunc { 89 return func(s *terraform.State) error { 90 rs, ok := s.RootModule().Resources[n] 91 if !ok { 92 return fmt.Errorf("Not found: %s", n) 93 } 94 95 if rs.Primary.ID == "" { 96 return fmt.Errorf("No cache subnet group ID is set") 97 } 98 99 conn := testAccProvider.Meta().(*AWSClient).elasticacheconn 100 resp, err := conn.DescribeCacheSubnetGroups(&elasticache.DescribeCacheSubnetGroupsInput{ 101 CacheSubnetGroupName: aws.String(rs.Primary.ID), 102 }) 103 if err != nil { 104 return fmt.Errorf("CacheSubnetGroup error: %v", err) 105 } 106 107 for _, c := range resp.CacheSubnetGroups { 108 if rs.Primary.ID == *c.CacheSubnetGroupName { 109 *csg = *c 110 } 111 } 112 113 if csg == nil { 114 return fmt.Errorf("cache subnet group not found") 115 } 116 return nil 117 } 118 } 119 120 func testAccCheckAWSElastiCacheSubnetGroupAttrs(csg *elasticache.CacheSubnetGroup, n string, count int) resource.TestCheckFunc { 121 return func(s *terraform.State) error { 122 123 rs, ok := s.RootModule().Resources[n] 124 if !ok { 125 return fmt.Errorf("Not found: %s", n) 126 } 127 128 if len(csg.Subnets) != count { 129 return fmt.Errorf("Bad cache subnet count, expected: %d, got: %d", count, len(csg.Subnets)) 130 } 131 132 if rs.Primary.Attributes["description"] != *csg.CacheSubnetGroupDescription { 133 return fmt.Errorf("Bad cache subnet description, expected: %s, got: %s", rs.Primary.Attributes["description"], *csg.CacheSubnetGroupDescription) 134 } 135 136 return nil 137 } 138 } 139 140 var testAccAWSElasticacheSubnetGroupConfig = ` 141 resource "aws_vpc" "foo" { 142 cidr_block = "192.168.0.0/16" 143 tags { 144 Name = "tf-test" 145 } 146 } 147 148 resource "aws_subnet" "foo" { 149 vpc_id = "${aws_vpc.foo.id}" 150 cidr_block = "192.168.0.0/20" 151 availability_zone = "us-west-2a" 152 tags { 153 Name = "tf-test" 154 } 155 } 156 157 resource "aws_elasticache_subnet_group" "bar" { 158 // Including uppercase letters in this name to ensure 159 // that we correctly handle the fact that the API 160 // normalizes names to lowercase. 161 name = "tf-TEST-cache-subnet-%03d" 162 description = "tf-test-cache-subnet-group-descr" 163 subnet_ids = ["${aws_subnet.foo.id}"] 164 } 165 ` 166 var testAccAWSElasticacheSubnetGroupUpdateConfigPre = ` 167 resource "aws_vpc" "foo" { 168 cidr_block = "10.0.0.0/16" 169 tags { 170 Name = "tf-elc-sub-test" 171 } 172 } 173 174 resource "aws_subnet" "foo" { 175 vpc_id = "${aws_vpc.foo.id}" 176 cidr_block = "10.0.1.0/24" 177 availability_zone = "us-west-2a" 178 tags { 179 Name = "tf-test" 180 } 181 } 182 183 resource "aws_elasticache_subnet_group" "bar" { 184 name = "tf-test-cache-subnet-%03d" 185 description = "tf-test-cache-subnet-group-descr" 186 subnet_ids = ["${aws_subnet.foo.id}"] 187 } 188 ` 189 190 var testAccAWSElasticacheSubnetGroupUpdateConfigPost = ` 191 resource "aws_vpc" "foo" { 192 cidr_block = "10.0.0.0/16" 193 tags { 194 Name = "tf-elc-sub-test" 195 } 196 } 197 198 resource "aws_subnet" "foo" { 199 vpc_id = "${aws_vpc.foo.id}" 200 cidr_block = "10.0.1.0/24" 201 availability_zone = "us-west-2a" 202 tags { 203 Name = "tf-test" 204 } 205 } 206 207 resource "aws_subnet" "bar" { 208 vpc_id = "${aws_vpc.foo.id}" 209 cidr_block = "10.0.2.0/24" 210 availability_zone = "us-west-2a" 211 tags { 212 Name = "tf-test-foo-update" 213 } 214 } 215 216 resource "aws_elasticache_subnet_group" "bar" { 217 name = "tf-test-cache-subnet-%03d" 218 description = "tf-test-cache-subnet-group-descr-edited" 219 subnet_ids = [ 220 "${aws_subnet.foo.id}", 221 "${aws_subnet.bar.id}", 222 ] 223 } 224 `