Merge pull request #3446 from stack72/aws-test-dbParamGroupName

provider/aws: Added a set of tests for the DBParamGroup Name
This commit is contained in:
Clint 2015-10-08 08:59:19 -05:00
commit 877f9f85f0
2 changed files with 82 additions and 27 deletions

View File

@ -25,33 +25,10 @@ func resourceAwsDbParameterGroup() *schema.Resource {
Delete: resourceAwsDbParameterGroupDelete, Delete: resourceAwsDbParameterGroupDelete,
Schema: map[string]*schema.Schema{ Schema: map[string]*schema.Schema{
"name": &schema.Schema{ "name": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
ForceNew: true, ForceNew: true,
Required: true, Required: true,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) { ValidateFunc: validateDbParamGroupName,
value := v.(string)
if !regexp.MustCompile(`^[0-9a-z-]+$`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"only lowercase alphanumeric characters and hyphens allowed in %q", k))
}
if !regexp.MustCompile(`^[a-z]`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"first character of %q must be a letter", k))
}
if regexp.MustCompile(`--`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q cannot contain two consecutive hyphens", k))
}
if regexp.MustCompile(`-$`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q cannot end with a hyphen", k))
}
if len(value) > 255 {
errors = append(errors, fmt.Errorf(
"%q cannot be greater than 255 characters", k))
}
return
},
}, },
"family": &schema.Schema{ "family": &schema.Schema{
Type: schema.TypeString, Type: schema.TypeString,
@ -252,3 +229,29 @@ func resourceAwsDbParameterHash(v interface{}) int {
return hashcode.String(buf.String()) return hashcode.String(buf.String())
} }
func validateDbParamGroupName(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if !regexp.MustCompile(`^[0-9a-z-]+$`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"only lowercase alphanumeric characters and hyphens allowed in %q", k))
}
if !regexp.MustCompile(`^[a-z]`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"first character of %q must be a letter", k))
}
if regexp.MustCompile(`--`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q cannot contain two consecutive hyphens", k))
}
if regexp.MustCompile(`-$`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q cannot end with a hyphen", k))
}
if len(value) > 255 {
errors = append(errors, fmt.Errorf(
"%q cannot be greater than 255 characters", k))
}
return
}

View File

@ -2,7 +2,9 @@ package aws
import ( import (
"fmt" "fmt"
"math/rand"
"testing" "testing"
"time"
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/aws/awserr"
@ -106,6 +108,46 @@ func TestAccAWSDBParameterGroupOnly(t *testing.T) {
}) })
} }
func TestResourceAWSDBParameterGroupName_validation(t *testing.T) {
cases := []struct {
Value string
ErrCount int
}{
{
Value: "tEsting123",
ErrCount: 1,
},
{
Value: "testing123!",
ErrCount: 1,
},
{
Value: "1testing123",
ErrCount: 1,
},
{
Value: "testing--123",
ErrCount: 1,
},
{
Value: "testing123-",
ErrCount: 1,
},
{
Value: randomString(256),
ErrCount: 1,
},
}
for _, tc := range cases {
_, errors := validateDbParamGroupName(tc.Value, "aws_db_parameter_group_name")
if len(errors) != tc.ErrCount {
t.Fatalf("Expected the DB Parameter Group Name to trigger a validation error")
}
}
}
func testAccCheckAWSDBParameterGroupDestroy(s *terraform.State) error { func testAccCheckAWSDBParameterGroupDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).rdsconn conn := testAccProvider.Meta().(*AWSClient).rdsconn
@ -193,6 +235,16 @@ func testAccCheckAWSDBParameterGroupExists(n string, v *rds.DBParameterGroup) re
} }
} }
func randomString(strlen int) string {
rand.Seed(time.Now().UTC().UnixNano())
const chars = "abcdefghijklmnopqrstuvwxyz"
result := make([]byte, strlen)
for i := 0; i < strlen; i++ {
result[i] = chars[rand.Intn(len(chars))]
}
return string(result)
}
const testAccAWSDBParameterGroupConfig = ` const testAccAWSDBParameterGroupConfig = `
resource "aws_db_parameter_group" "bar" { resource "aws_db_parameter_group" "bar" {
name = "parameter-group-test-terraform" name = "parameter-group-test-terraform"