github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/resource_aws_rds_cluster_parameter_group_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 "time" 7 8 "github.com/aws/aws-sdk-go/aws" 9 "github.com/aws/aws-sdk-go/aws/awserr" 10 "github.com/aws/aws-sdk-go/service/rds" 11 "github.com/hashicorp/terraform/helper/resource" 12 "github.com/hashicorp/terraform/terraform" 13 ) 14 15 func TestAccAWSDBClusterParameterGroup_basic(t *testing.T) { 16 var v rds.DBClusterParameterGroup 17 18 resource.Test(t, resource.TestCase{ 19 PreCheck: func() { testAccPreCheck(t) }, 20 Providers: testAccProviders, 21 CheckDestroy: testAccCheckAWSDBClusterParameterGroupDestroy, 22 Steps: []resource.TestStep{ 23 resource.TestStep{ 24 Config: testAccAWSDBClusterParameterGroupConfig, 25 Check: resource.ComposeTestCheckFunc( 26 testAccCheckAWSDBClusterParameterGroupExists("aws_rds_cluster_parameter_group.bar", &v), 27 testAccCheckAWSDBClusterParameterGroupAttributes(&v), 28 resource.TestCheckResourceAttr( 29 "aws_rds_cluster_parameter_group.bar", "name", "cluster-parameter-group-test-terraform"), 30 resource.TestCheckResourceAttr( 31 "aws_rds_cluster_parameter_group.bar", "family", "aurora5.6"), 32 resource.TestCheckResourceAttr( 33 "aws_rds_cluster_parameter_group.bar", "description", "Test cluster parameter group for terraform"), 34 resource.TestCheckResourceAttr( 35 "aws_rds_cluster_parameter_group.bar", "parameter.1708034931.name", "character_set_results"), 36 resource.TestCheckResourceAttr( 37 "aws_rds_cluster_parameter_group.bar", "parameter.1708034931.value", "utf8"), 38 resource.TestCheckResourceAttr( 39 "aws_rds_cluster_parameter_group.bar", "parameter.2421266705.name", "character_set_server"), 40 resource.TestCheckResourceAttr( 41 "aws_rds_cluster_parameter_group.bar", "parameter.2421266705.value", "utf8"), 42 resource.TestCheckResourceAttr( 43 "aws_rds_cluster_parameter_group.bar", "parameter.2478663599.name", "character_set_client"), 44 resource.TestCheckResourceAttr( 45 "aws_rds_cluster_parameter_group.bar", "parameter.2478663599.value", "utf8"), 46 resource.TestCheckResourceAttr( 47 "aws_rds_cluster_parameter_group.bar", "tags.%", "1"), 48 ), 49 }, 50 resource.TestStep{ 51 Config: testAccAWSDBClusterParameterGroupAddParametersConfig, 52 Check: resource.ComposeTestCheckFunc( 53 testAccCheckAWSDBClusterParameterGroupExists("aws_rds_cluster_parameter_group.bar", &v), 54 testAccCheckAWSDBClusterParameterGroupAttributes(&v), 55 resource.TestCheckResourceAttr( 56 "aws_rds_cluster_parameter_group.bar", "name", "cluster-parameter-group-test-terraform"), 57 resource.TestCheckResourceAttr( 58 "aws_rds_cluster_parameter_group.bar", "family", "aurora5.6"), 59 resource.TestCheckResourceAttr( 60 "aws_rds_cluster_parameter_group.bar", "description", "Test cluster parameter group for terraform"), 61 resource.TestCheckResourceAttr( 62 "aws_rds_cluster_parameter_group.bar", "parameter.1706463059.name", "collation_connection"), 63 resource.TestCheckResourceAttr( 64 "aws_rds_cluster_parameter_group.bar", "parameter.1706463059.value", "utf8_unicode_ci"), 65 resource.TestCheckResourceAttr( 66 "aws_rds_cluster_parameter_group.bar", "parameter.1708034931.name", "character_set_results"), 67 resource.TestCheckResourceAttr( 68 "aws_rds_cluster_parameter_group.bar", "parameter.1708034931.value", "utf8"), 69 resource.TestCheckResourceAttr( 70 "aws_rds_cluster_parameter_group.bar", "parameter.2421266705.name", "character_set_server"), 71 resource.TestCheckResourceAttr( 72 "aws_rds_cluster_parameter_group.bar", "parameter.2421266705.value", "utf8"), 73 resource.TestCheckResourceAttr( 74 "aws_rds_cluster_parameter_group.bar", "parameter.2475805061.name", "collation_server"), 75 resource.TestCheckResourceAttr( 76 "aws_rds_cluster_parameter_group.bar", "parameter.2475805061.value", "utf8_unicode_ci"), 77 resource.TestCheckResourceAttr( 78 "aws_rds_cluster_parameter_group.bar", "parameter.2478663599.name", "character_set_client"), 79 resource.TestCheckResourceAttr( 80 "aws_rds_cluster_parameter_group.bar", "parameter.2478663599.value", "utf8"), 81 resource.TestCheckResourceAttr( 82 "aws_rds_cluster_parameter_group.bar", "tags.%", "2"), 83 ), 84 }, 85 }, 86 }) 87 } 88 89 func TestAccAWSDBClusterParameterGroup_disappears(t *testing.T) { 90 var v rds.DBClusterParameterGroup 91 92 resource.Test(t, resource.TestCase{ 93 PreCheck: func() { testAccPreCheck(t) }, 94 Providers: testAccProviders, 95 CheckDestroy: testAccCheckAWSDBClusterParameterGroupDestroy, 96 Steps: []resource.TestStep{ 97 resource.TestStep{ 98 Config: testAccAWSDBClusterParameterGroupConfig, 99 Check: resource.ComposeTestCheckFunc( 100 testAccCheckAWSDBClusterParameterGroupExists("aws_rds_cluster_parameter_group.bar", &v), 101 testAccAWSDBClusterParameterGroupDisappears(&v), 102 ), 103 ExpectNonEmptyPlan: true, 104 }, 105 }, 106 }) 107 } 108 109 func TestAccAWSDBClusterParameterGroupOnly(t *testing.T) { 110 var v rds.DBClusterParameterGroup 111 112 resource.Test(t, resource.TestCase{ 113 PreCheck: func() { testAccPreCheck(t) }, 114 Providers: testAccProviders, 115 CheckDestroy: testAccCheckAWSDBClusterParameterGroupDestroy, 116 Steps: []resource.TestStep{ 117 resource.TestStep{ 118 Config: testAccAWSDBClusterParameterGroupOnlyConfig, 119 Check: resource.ComposeTestCheckFunc( 120 testAccCheckAWSDBClusterParameterGroupExists("aws_rds_cluster_parameter_group.bar", &v), 121 testAccCheckAWSDBClusterParameterGroupAttributes(&v), 122 resource.TestCheckResourceAttr( 123 "aws_rds_cluster_parameter_group.bar", "name", "cluster-parameter-group-test-terraform"), 124 resource.TestCheckResourceAttr( 125 "aws_rds_cluster_parameter_group.bar", "family", "aurora5.6"), 126 resource.TestCheckResourceAttr( 127 "aws_rds_cluster_parameter_group.bar", "description", "Managed by Terraform"), 128 ), 129 }, 130 }, 131 }) 132 } 133 134 func TestResourceAWSDBClusterParameterGroupName_validation(t *testing.T) { 135 cases := []struct { 136 Value string 137 ErrCount int 138 }{ 139 { 140 Value: "tEsting123", 141 ErrCount: 1, 142 }, 143 { 144 Value: "testing123!", 145 ErrCount: 1, 146 }, 147 { 148 Value: "1testing123", 149 ErrCount: 1, 150 }, 151 { 152 Value: "testing--123", 153 ErrCount: 1, 154 }, 155 { 156 Value: "testing123-", 157 ErrCount: 1, 158 }, 159 { 160 Value: randomString(256), 161 ErrCount: 1, 162 }, 163 } 164 165 for _, tc := range cases { 166 _, errors := validateDbParamGroupName(tc.Value, "aws_rds_cluster_parameter_group_name") 167 168 if len(errors) != tc.ErrCount { 169 t.Fatalf("Expected the DB Cluster Parameter Group Name to trigger a validation error") 170 } 171 } 172 } 173 174 func testAccCheckAWSDBClusterParameterGroupDestroy(s *terraform.State) error { 175 conn := testAccProvider.Meta().(*AWSClient).rdsconn 176 177 for _, rs := range s.RootModule().Resources { 178 if rs.Type != "aws_rds_cluster_parameter_group" { 179 continue 180 } 181 182 // Try to find the Group 183 resp, err := conn.DescribeDBClusterParameterGroups( 184 &rds.DescribeDBClusterParameterGroupsInput{ 185 DBClusterParameterGroupName: aws.String(rs.Primary.ID), 186 }) 187 188 if err == nil { 189 if len(resp.DBClusterParameterGroups) != 0 && 190 *resp.DBClusterParameterGroups[0].DBClusterParameterGroupName == rs.Primary.ID { 191 return fmt.Errorf("DB Cluster Parameter Group still exists") 192 } 193 } 194 195 // Verify the error 196 newerr, ok := err.(awserr.Error) 197 if !ok { 198 return err 199 } 200 if newerr.Code() != "DBParameterGroupNotFound" { 201 return err 202 } 203 } 204 205 return nil 206 } 207 208 func testAccCheckAWSDBClusterParameterGroupAttributes(v *rds.DBClusterParameterGroup) resource.TestCheckFunc { 209 return func(s *terraform.State) error { 210 211 if *v.DBClusterParameterGroupName != "cluster-parameter-group-test-terraform" { 212 return fmt.Errorf("bad name: %#v", v.DBClusterParameterGroupName) 213 } 214 215 if *v.DBParameterGroupFamily != "aurora5.6" { 216 return fmt.Errorf("bad family: %#v", v.DBParameterGroupFamily) 217 } 218 219 return nil 220 } 221 } 222 223 func testAccAWSDBClusterParameterGroupDisappears(v *rds.DBClusterParameterGroup) resource.TestCheckFunc { 224 return func(s *terraform.State) error { 225 conn := testAccProvider.Meta().(*AWSClient).rdsconn 226 opts := &rds.DeleteDBClusterParameterGroupInput{ 227 DBClusterParameterGroupName: v.DBClusterParameterGroupName, 228 } 229 if _, err := conn.DeleteDBClusterParameterGroup(opts); err != nil { 230 return err 231 } 232 return resource.Retry(40*time.Minute, func() *resource.RetryError { 233 opts := &rds.DescribeDBClusterParameterGroupsInput{ 234 DBClusterParameterGroupName: v.DBClusterParameterGroupName, 235 } 236 _, err := conn.DescribeDBClusterParameterGroups(opts) 237 if err != nil { 238 dbparamgrouperr, ok := err.(awserr.Error) 239 if ok && dbparamgrouperr.Code() == "DBParameterGroupNotFound" { 240 return nil 241 } 242 return resource.NonRetryableError( 243 fmt.Errorf("Error retrieving DB Cluster Parameter Groups: %s", err)) 244 } 245 return resource.RetryableError(fmt.Errorf( 246 "Waiting for cluster parameter group to be deleted: %v", v.DBClusterParameterGroupName)) 247 }) 248 } 249 } 250 251 func testAccCheckAWSDBClusterParameterGroupExists(n string, v *rds.DBClusterParameterGroup) resource.TestCheckFunc { 252 return func(s *terraform.State) error { 253 rs, ok := s.RootModule().Resources[n] 254 if !ok { 255 return fmt.Errorf("Not found: %s", n) 256 } 257 258 if rs.Primary.ID == "" { 259 return fmt.Errorf("No DB Cluster Parameter Group ID is set") 260 } 261 262 conn := testAccProvider.Meta().(*AWSClient).rdsconn 263 264 opts := rds.DescribeDBClusterParameterGroupsInput{ 265 DBClusterParameterGroupName: aws.String(rs.Primary.ID), 266 } 267 268 resp, err := conn.DescribeDBClusterParameterGroups(&opts) 269 270 if err != nil { 271 return err 272 } 273 274 if len(resp.DBClusterParameterGroups) != 1 || 275 *resp.DBClusterParameterGroups[0].DBClusterParameterGroupName != rs.Primary.ID { 276 return fmt.Errorf("DB Cluster Parameter Group not found") 277 } 278 279 *v = *resp.DBClusterParameterGroups[0] 280 281 return nil 282 } 283 } 284 285 const testAccAWSDBClusterParameterGroupConfig = ` 286 resource "aws_rds_cluster_parameter_group" "bar" { 287 name = "cluster-parameter-group-test-terraform" 288 family = "aurora5.6" 289 description = "Test cluster parameter group for terraform" 290 291 parameter { 292 name = "character_set_server" 293 value = "utf8" 294 } 295 296 parameter { 297 name = "character_set_client" 298 value = "utf8" 299 } 300 301 parameter { 302 name = "character_set_results" 303 value = "utf8" 304 } 305 306 tags { 307 foo = "bar" 308 } 309 } 310 ` 311 312 const testAccAWSDBClusterParameterGroupAddParametersConfig = ` 313 resource "aws_rds_cluster_parameter_group" "bar" { 314 name = "cluster-parameter-group-test-terraform" 315 family = "aurora5.6" 316 description = "Test cluster parameter group for terraform" 317 318 parameter { 319 name = "character_set_server" 320 value = "utf8" 321 } 322 323 parameter { 324 name = "character_set_client" 325 value = "utf8" 326 } 327 328 parameter { 329 name = "character_set_results" 330 value = "utf8" 331 } 332 333 parameter { 334 name = "collation_server" 335 value = "utf8_unicode_ci" 336 } 337 338 parameter { 339 name = "collation_connection" 340 value = "utf8_unicode_ci" 341 } 342 343 tags { 344 foo = "bar" 345 baz = "foo" 346 } 347 } 348 ` 349 350 const testAccAWSDBClusterParameterGroupOnlyConfig = ` 351 resource "aws_rds_cluster_parameter_group" "bar" { 352 name = "cluster-parameter-group-test-terraform" 353 family = "aurora5.6" 354 } 355 `