github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_db_parameter_group_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "math/rand" 6 "testing" 7 "time" 8 9 "github.com/aws/aws-sdk-go/aws" 10 "github.com/aws/aws-sdk-go/aws/awserr" 11 "github.com/aws/aws-sdk-go/service/rds" 12 "github.com/hashicorp/terraform/helper/resource" 13 "github.com/hashicorp/terraform/terraform" 14 ) 15 16 func TestAccAWSDBParameterGroup_basic(t *testing.T) { 17 var v rds.DBParameterGroup 18 19 resource.Test(t, resource.TestCase{ 20 PreCheck: func() { testAccPreCheck(t) }, 21 Providers: testAccProviders, 22 CheckDestroy: testAccCheckAWSDBParameterGroupDestroy, 23 Steps: []resource.TestStep{ 24 resource.TestStep{ 25 Config: testAccAWSDBParameterGroupConfig, 26 Check: resource.ComposeTestCheckFunc( 27 testAccCheckAWSDBParameterGroupExists("aws_db_parameter_group.bar", &v), 28 testAccCheckAWSDBParameterGroupAttributes(&v), 29 resource.TestCheckResourceAttr( 30 "aws_db_parameter_group.bar", "name", "parameter-group-test-terraform"), 31 resource.TestCheckResourceAttr( 32 "aws_db_parameter_group.bar", "family", "mysql5.6"), 33 resource.TestCheckResourceAttr( 34 "aws_db_parameter_group.bar", "description", "Test parameter group for terraform"), 35 resource.TestCheckResourceAttr( 36 "aws_db_parameter_group.bar", "parameter.1708034931.name", "character_set_results"), 37 resource.TestCheckResourceAttr( 38 "aws_db_parameter_group.bar", "parameter.1708034931.value", "utf8"), 39 resource.TestCheckResourceAttr( 40 "aws_db_parameter_group.bar", "parameter.2421266705.name", "character_set_server"), 41 resource.TestCheckResourceAttr( 42 "aws_db_parameter_group.bar", "parameter.2421266705.value", "utf8"), 43 resource.TestCheckResourceAttr( 44 "aws_db_parameter_group.bar", "parameter.2478663599.name", "character_set_client"), 45 resource.TestCheckResourceAttr( 46 "aws_db_parameter_group.bar", "parameter.2478663599.value", "utf8"), 47 ), 48 }, 49 resource.TestStep{ 50 Config: testAccAWSDBParameterGroupAddParametersConfig, 51 Check: resource.ComposeTestCheckFunc( 52 testAccCheckAWSDBParameterGroupExists("aws_db_parameter_group.bar", &v), 53 testAccCheckAWSDBParameterGroupAttributes(&v), 54 resource.TestCheckResourceAttr( 55 "aws_db_parameter_group.bar", "name", "parameter-group-test-terraform"), 56 resource.TestCheckResourceAttr( 57 "aws_db_parameter_group.bar", "family", "mysql5.6"), 58 resource.TestCheckResourceAttr( 59 "aws_db_parameter_group.bar", "description", "Test parameter group for terraform"), 60 resource.TestCheckResourceAttr( 61 "aws_db_parameter_group.bar", "parameter.1706463059.name", "collation_connection"), 62 resource.TestCheckResourceAttr( 63 "aws_db_parameter_group.bar", "parameter.1706463059.value", "utf8_unicode_ci"), 64 resource.TestCheckResourceAttr( 65 "aws_db_parameter_group.bar", "parameter.1708034931.name", "character_set_results"), 66 resource.TestCheckResourceAttr( 67 "aws_db_parameter_group.bar", "parameter.1708034931.value", "utf8"), 68 resource.TestCheckResourceAttr( 69 "aws_db_parameter_group.bar", "parameter.2421266705.name", "character_set_server"), 70 resource.TestCheckResourceAttr( 71 "aws_db_parameter_group.bar", "parameter.2421266705.value", "utf8"), 72 resource.TestCheckResourceAttr( 73 "aws_db_parameter_group.bar", "parameter.2475805061.name", "collation_server"), 74 resource.TestCheckResourceAttr( 75 "aws_db_parameter_group.bar", "parameter.2475805061.value", "utf8_unicode_ci"), 76 resource.TestCheckResourceAttr( 77 "aws_db_parameter_group.bar", "parameter.2478663599.name", "character_set_client"), 78 resource.TestCheckResourceAttr( 79 "aws_db_parameter_group.bar", "parameter.2478663599.value", "utf8"), 80 ), 81 }, 82 }, 83 }) 84 } 85 86 func TestAccAWSDBParameterGroupOnly(t *testing.T) { 87 var v rds.DBParameterGroup 88 89 resource.Test(t, resource.TestCase{ 90 PreCheck: func() { testAccPreCheck(t) }, 91 Providers: testAccProviders, 92 CheckDestroy: testAccCheckAWSDBParameterGroupDestroy, 93 Steps: []resource.TestStep{ 94 resource.TestStep{ 95 Config: testAccAWSDBParameterGroupOnlyConfig, 96 Check: resource.ComposeTestCheckFunc( 97 testAccCheckAWSDBParameterGroupExists("aws_db_parameter_group.bar", &v), 98 testAccCheckAWSDBParameterGroupAttributes(&v), 99 resource.TestCheckResourceAttr( 100 "aws_db_parameter_group.bar", "name", "parameter-group-test-terraform"), 101 resource.TestCheckResourceAttr( 102 "aws_db_parameter_group.bar", "family", "mysql5.6"), 103 resource.TestCheckResourceAttr( 104 "aws_db_parameter_group.bar", "description", "Test parameter group for terraform"), 105 ), 106 }, 107 }, 108 }) 109 } 110 111 func TestResourceAWSDBParameterGroupName_validation(t *testing.T) { 112 cases := []struct { 113 Value string 114 ErrCount int 115 }{ 116 { 117 Value: "tEsting123", 118 ErrCount: 1, 119 }, 120 { 121 Value: "testing123!", 122 ErrCount: 1, 123 }, 124 { 125 Value: "1testing123", 126 ErrCount: 1, 127 }, 128 { 129 Value: "testing--123", 130 ErrCount: 1, 131 }, 132 { 133 Value: "testing123-", 134 ErrCount: 1, 135 }, 136 { 137 Value: randomString(256), 138 ErrCount: 1, 139 }, 140 } 141 142 for _, tc := range cases { 143 _, errors := validateDbParamGroupName(tc.Value, "aws_db_parameter_group_name") 144 145 if len(errors) != tc.ErrCount { 146 t.Fatalf("Expected the DB Parameter Group Name to trigger a validation error") 147 } 148 } 149 } 150 151 func testAccCheckAWSDBParameterGroupDestroy(s *terraform.State) error { 152 conn := testAccProvider.Meta().(*AWSClient).rdsconn 153 154 for _, rs := range s.RootModule().Resources { 155 if rs.Type != "aws_db_parameter_group" { 156 continue 157 } 158 159 // Try to find the Group 160 resp, err := conn.DescribeDBParameterGroups( 161 &rds.DescribeDBParameterGroupsInput{ 162 DBParameterGroupName: aws.String(rs.Primary.ID), 163 }) 164 165 if err == nil { 166 if len(resp.DBParameterGroups) != 0 && 167 *resp.DBParameterGroups[0].DBParameterGroupName == rs.Primary.ID { 168 return fmt.Errorf("DB Parameter Group still exists") 169 } 170 } 171 172 // Verify the error 173 newerr, ok := err.(awserr.Error) 174 if !ok { 175 return err 176 } 177 if newerr.Code() != "InvalidDBParameterGroup.NotFound" { 178 return err 179 } 180 } 181 182 return nil 183 } 184 185 func testAccCheckAWSDBParameterGroupAttributes(v *rds.DBParameterGroup) resource.TestCheckFunc { 186 return func(s *terraform.State) error { 187 188 if *v.DBParameterGroupName != "parameter-group-test-terraform" { 189 return fmt.Errorf("bad name: %#v", v.DBParameterGroupName) 190 } 191 192 if *v.DBParameterGroupFamily != "mysql5.6" { 193 return fmt.Errorf("bad family: %#v", v.DBParameterGroupFamily) 194 } 195 196 if *v.Description != "Test parameter group for terraform" { 197 return fmt.Errorf("bad description: %#v", v.Description) 198 } 199 200 return nil 201 } 202 } 203 204 func testAccCheckAWSDBParameterGroupExists(n string, v *rds.DBParameterGroup) resource.TestCheckFunc { 205 return func(s *terraform.State) error { 206 rs, ok := s.RootModule().Resources[n] 207 if !ok { 208 return fmt.Errorf("Not found: %s", n) 209 } 210 211 if rs.Primary.ID == "" { 212 return fmt.Errorf("No DB Parameter Group ID is set") 213 } 214 215 conn := testAccProvider.Meta().(*AWSClient).rdsconn 216 217 opts := rds.DescribeDBParameterGroupsInput{ 218 DBParameterGroupName: aws.String(rs.Primary.ID), 219 } 220 221 resp, err := conn.DescribeDBParameterGroups(&opts) 222 223 if err != nil { 224 return err 225 } 226 227 if len(resp.DBParameterGroups) != 1 || 228 *resp.DBParameterGroups[0].DBParameterGroupName != rs.Primary.ID { 229 return fmt.Errorf("DB Parameter Group not found") 230 } 231 232 *v = *resp.DBParameterGroups[0] 233 234 return nil 235 } 236 } 237 238 func randomString(strlen int) string { 239 rand.Seed(time.Now().UTC().UnixNano()) 240 const chars = "abcdefghijklmnopqrstuvwxyz" 241 result := make([]byte, strlen) 242 for i := 0; i < strlen; i++ { 243 result[i] = chars[rand.Intn(len(chars))] 244 } 245 return string(result) 246 } 247 248 const testAccAWSDBParameterGroupConfig = ` 249 resource "aws_db_parameter_group" "bar" { 250 name = "parameter-group-test-terraform" 251 family = "mysql5.6" 252 description = "Test parameter group for terraform" 253 parameter { 254 name = "character_set_server" 255 value = "utf8" 256 } 257 parameter { 258 name = "character_set_client" 259 value = "utf8" 260 } 261 parameter{ 262 name = "character_set_results" 263 value = "utf8" 264 } 265 } 266 ` 267 268 const testAccAWSDBParameterGroupAddParametersConfig = ` 269 resource "aws_db_parameter_group" "bar" { 270 name = "parameter-group-test-terraform" 271 family = "mysql5.6" 272 description = "Test parameter group for terraform" 273 parameter { 274 name = "character_set_server" 275 value = "utf8" 276 } 277 parameter { 278 name = "character_set_client" 279 value = "utf8" 280 } 281 parameter{ 282 name = "character_set_results" 283 value = "utf8" 284 } 285 parameter { 286 name = "collation_server" 287 value = "utf8_unicode_ci" 288 } 289 parameter { 290 name = "collation_connection" 291 value = "utf8_unicode_ci" 292 } 293 } 294 ` 295 296 const testAccAWSDBParameterGroupOnlyConfig = ` 297 resource "aws_db_parameter_group" "bar" { 298 name = "parameter-group-test-terraform" 299 family = "mysql5.6" 300 description = "Test parameter group for terraform" 301 } 302 `