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