github.com/hobbeswalsh/terraform@v0.3.7-0.20150619183303-ad17cf55a0fa/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 name = "tf-test-cache-subnet-%03d" 154 description = "tf-test-cache-subnet-group-descr" 155 subnet_ids = ["${aws_subnet.foo.id}"] 156 } 157 ` 158 var testAccAWSElasticacheSubnetGroupUpdateConfigPre = ` 159 resource "aws_vpc" "foo" { 160 cidr_block = "10.0.0.0/16" 161 tags { 162 Name = "tf-elc-sub-test" 163 } 164 } 165 166 resource "aws_subnet" "foo" { 167 vpc_id = "${aws_vpc.foo.id}" 168 cidr_block = "10.0.1.0/24" 169 availability_zone = "us-west-2a" 170 tags { 171 Name = "tf-test" 172 } 173 } 174 175 resource "aws_elasticache_subnet_group" "bar" { 176 name = "tf-test-cache-subnet-%03d" 177 description = "tf-test-cache-subnet-group-descr" 178 subnet_ids = ["${aws_subnet.foo.id}"] 179 } 180 ` 181 182 var testAccAWSElasticacheSubnetGroupUpdateConfigPost = ` 183 resource "aws_vpc" "foo" { 184 cidr_block = "10.0.0.0/16" 185 tags { 186 Name = "tf-elc-sub-test" 187 } 188 } 189 190 resource "aws_subnet" "foo" { 191 vpc_id = "${aws_vpc.foo.id}" 192 cidr_block = "10.0.1.0/24" 193 availability_zone = "us-west-2a" 194 tags { 195 Name = "tf-test" 196 } 197 } 198 199 resource "aws_subnet" "bar" { 200 vpc_id = "${aws_vpc.foo.id}" 201 cidr_block = "10.0.2.0/24" 202 availability_zone = "us-west-2a" 203 tags { 204 Name = "tf-test-foo-update" 205 } 206 } 207 208 resource "aws_elasticache_subnet_group" "bar" { 209 name = "tf-test-cache-subnet-%03d" 210 description = "tf-test-cache-subnet-group-descr-edited" 211 subnet_ids = [ 212 "${aws_subnet.foo.id}", 213 "${aws_subnet.bar.id}", 214 ] 215 } 216 `