github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/aws/resource_aws_db_subnet_group_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/terraform" 9 10 "github.com/aws/aws-sdk-go/aws" 11 "github.com/aws/aws-sdk-go/aws/awserr" 12 "github.com/aws/aws-sdk-go/service/rds" 13 ) 14 15 func TestAccAWSDBSubnetGroup_basic(t *testing.T) { 16 var v rds.DBSubnetGroup 17 18 testCheck := func(*terraform.State) error { 19 return nil 20 } 21 22 resource.Test(t, resource.TestCase{ 23 PreCheck: func() { testAccPreCheck(t) }, 24 Providers: testAccProviders, 25 CheckDestroy: testAccCheckDBSubnetGroupDestroy, 26 Steps: []resource.TestStep{ 27 resource.TestStep{ 28 Config: testAccDBSubnetGroupConfig, 29 Check: resource.ComposeTestCheckFunc( 30 testAccCheckDBSubnetGroupExists( 31 "aws_db_subnet_group.foo", &v), 32 resource.TestCheckResourceAttr( 33 "aws_db_subnet_group.foo", "name", "foo"), 34 resource.TestCheckResourceAttr( 35 "aws_db_subnet_group.foo", "description", "Managed by Terraform"), 36 testCheck, 37 ), 38 }, 39 }, 40 }) 41 } 42 43 // Regression test for https://github.com/hashicorp/terraform/issues/2603 and 44 // https://github.com/hashicorp/terraform/issues/2664 45 func TestAccAWSDBSubnetGroup_withUndocumentedCharacters(t *testing.T) { 46 var v rds.DBSubnetGroup 47 48 testCheck := func(*terraform.State) error { 49 return nil 50 } 51 52 resource.Test(t, resource.TestCase{ 53 PreCheck: func() { testAccPreCheck(t) }, 54 Providers: testAccProviders, 55 CheckDestroy: testAccCheckDBSubnetGroupDestroy, 56 Steps: []resource.TestStep{ 57 resource.TestStep{ 58 Config: testAccDBSubnetGroupConfig_withUnderscoresAndPeriodsAndSpaces, 59 Check: resource.ComposeTestCheckFunc( 60 testAccCheckDBSubnetGroupExists( 61 "aws_db_subnet_group.underscores", &v), 62 testAccCheckDBSubnetGroupExists( 63 "aws_db_subnet_group.periods", &v), 64 testAccCheckDBSubnetGroupExists( 65 "aws_db_subnet_group.spaces", &v), 66 testCheck, 67 ), 68 }, 69 }, 70 }) 71 } 72 73 func TestAccAWSDBSubnetGroup_updateDescription(t *testing.T) { 74 var v rds.DBSubnetGroup 75 76 resource.Test(t, resource.TestCase{ 77 PreCheck: func() { testAccPreCheck(t) }, 78 Providers: testAccProviders, 79 CheckDestroy: testAccCheckDBSubnetGroupDestroy, 80 Steps: []resource.TestStep{ 81 resource.TestStep{ 82 Config: testAccDBSubnetGroupConfig, 83 Check: resource.ComposeTestCheckFunc( 84 testAccCheckDBSubnetGroupExists( 85 "aws_db_subnet_group.foo", &v), 86 resource.TestCheckResourceAttr( 87 "aws_db_subnet_group.foo", "description", "Managed by Terraform"), 88 ), 89 }, 90 91 resource.TestStep{ 92 Config: testAccDBSubnetGroupConfig_updatedDescription, 93 Check: resource.ComposeTestCheckFunc( 94 testAccCheckDBSubnetGroupExists( 95 "aws_db_subnet_group.foo", &v), 96 resource.TestCheckResourceAttr( 97 "aws_db_subnet_group.foo", "description", "foo description updated"), 98 ), 99 }, 100 }, 101 }) 102 } 103 104 func TestResourceAWSDBSubnetGroupNameValidation(t *testing.T) { 105 cases := []struct { 106 Value string 107 ErrCount int 108 }{ 109 { 110 Value: "tEsting", 111 ErrCount: 1, 112 }, 113 { 114 Value: "testing?", 115 ErrCount: 1, 116 }, 117 { 118 Value: "default", 119 ErrCount: 1, 120 }, 121 { 122 Value: randomString(300), 123 ErrCount: 1, 124 }, 125 } 126 127 for _, tc := range cases { 128 _, errors := validateSubnetGroupName(tc.Value, "aws_db_subnet_group") 129 130 if len(errors) != tc.ErrCount { 131 t.Fatalf("Expected the DB Subnet Group name to trigger a validation error") 132 } 133 } 134 } 135 136 func testAccCheckDBSubnetGroupDestroy(s *terraform.State) error { 137 conn := testAccProvider.Meta().(*AWSClient).rdsconn 138 139 for _, rs := range s.RootModule().Resources { 140 if rs.Type != "aws_db_subnet_group" { 141 continue 142 } 143 144 // Try to find the resource 145 resp, err := conn.DescribeDBSubnetGroups( 146 &rds.DescribeDBSubnetGroupsInput{DBSubnetGroupName: aws.String(rs.Primary.ID)}) 147 if err == nil { 148 if len(resp.DBSubnetGroups) > 0 { 149 return fmt.Errorf("still exist.") 150 } 151 152 return nil 153 } 154 155 // Verify the error is what we want 156 rdserr, ok := err.(awserr.Error) 157 if !ok { 158 return err 159 } 160 if rdserr.Code() != "DBSubnetGroupNotFoundFault" { 161 return err 162 } 163 } 164 165 return nil 166 } 167 168 func testAccCheckDBSubnetGroupExists(n string, v *rds.DBSubnetGroup) resource.TestCheckFunc { 169 return func(s *terraform.State) error { 170 rs, ok := s.RootModule().Resources[n] 171 if !ok { 172 return fmt.Errorf("Not found: %s", n) 173 } 174 175 if rs.Primary.ID == "" { 176 return fmt.Errorf("No ID is set") 177 } 178 179 conn := testAccProvider.Meta().(*AWSClient).rdsconn 180 resp, err := conn.DescribeDBSubnetGroups( 181 &rds.DescribeDBSubnetGroupsInput{DBSubnetGroupName: aws.String(rs.Primary.ID)}) 182 if err != nil { 183 return err 184 } 185 if len(resp.DBSubnetGroups) == 0 { 186 return fmt.Errorf("DbSubnetGroup not found") 187 } 188 189 *v = *resp.DBSubnetGroups[0] 190 191 return nil 192 } 193 } 194 195 const testAccDBSubnetGroupConfig = ` 196 resource "aws_vpc" "foo" { 197 cidr_block = "10.1.0.0/16" 198 } 199 200 resource "aws_subnet" "foo" { 201 cidr_block = "10.1.1.0/24" 202 availability_zone = "us-west-2a" 203 vpc_id = "${aws_vpc.foo.id}" 204 tags { 205 Name = "tf-dbsubnet-test-1" 206 } 207 } 208 209 resource "aws_subnet" "bar" { 210 cidr_block = "10.1.2.0/24" 211 availability_zone = "us-west-2b" 212 vpc_id = "${aws_vpc.foo.id}" 213 tags { 214 Name = "tf-dbsubnet-test-2" 215 } 216 } 217 218 resource "aws_db_subnet_group" "foo" { 219 name = "foo" 220 subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}"] 221 tags { 222 Name = "tf-dbsubnet-group-test" 223 } 224 } 225 ` 226 227 const testAccDBSubnetGroupConfig_updatedDescription = ` 228 resource "aws_vpc" "foo" { 229 cidr_block = "10.1.0.0/16" 230 } 231 232 resource "aws_subnet" "foo" { 233 cidr_block = "10.1.1.0/24" 234 availability_zone = "us-west-2a" 235 vpc_id = "${aws_vpc.foo.id}" 236 tags { 237 Name = "tf-dbsubnet-test-1" 238 } 239 } 240 241 resource "aws_subnet" "bar" { 242 cidr_block = "10.1.2.0/24" 243 availability_zone = "us-west-2b" 244 vpc_id = "${aws_vpc.foo.id}" 245 tags { 246 Name = "tf-dbsubnet-test-2" 247 } 248 } 249 250 resource "aws_db_subnet_group" "foo" { 251 name = "foo" 252 description = "foo description updated" 253 subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}"] 254 tags { 255 Name = "tf-dbsubnet-group-test" 256 } 257 } 258 ` 259 260 const testAccDBSubnetGroupConfig_withUnderscoresAndPeriodsAndSpaces = ` 261 resource "aws_vpc" "main" { 262 cidr_block = "192.168.0.0/16" 263 } 264 265 resource "aws_subnet" "frontend" { 266 vpc_id = "${aws_vpc.main.id}" 267 availability_zone = "us-west-2b" 268 cidr_block = "192.168.1.0/24" 269 } 270 271 resource "aws_subnet" "backend" { 272 vpc_id = "${aws_vpc.main.id}" 273 availability_zone = "us-west-2c" 274 cidr_block = "192.168.2.0/24" 275 } 276 277 resource "aws_db_subnet_group" "underscores" { 278 name = "with_underscores" 279 description = "Our main group of subnets" 280 subnet_ids = ["${aws_subnet.frontend.id}", "${aws_subnet.backend.id}"] 281 } 282 283 resource "aws_db_subnet_group" "periods" { 284 name = "with.periods" 285 description = "Our main group of subnets" 286 subnet_ids = ["${aws_subnet.frontend.id}", "${aws_subnet.backend.id}"] 287 } 288 289 resource "aws_db_subnet_group" "spaces" { 290 name = "with spaces" 291 description = "Our main group of subnets" 292 subnet_ids = ["${aws_subnet.frontend.id}", "${aws_subnet.backend.id}"] 293 } 294 `