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