From cc56431b978af010747bbf93d9e32c185f8c9f9a Mon Sep 17 00:00:00 2001 From: stack72 Date: Thu, 8 Oct 2015 09:39:11 +0100 Subject: [PATCH 1/5] Added a set of tests for the AWS DBParamGroup Name --- .../aws/resource_aws_db_parameter_group.go | 57 ++++++++++--------- .../resource_aws_db_parameter_group_test.go | 45 +++++++++++++++ 2 files changed, 75 insertions(+), 27 deletions(-) diff --git a/builtin/providers/aws/resource_aws_db_parameter_group.go b/builtin/providers/aws/resource_aws_db_parameter_group.go index 72101ac90..b4f07e43d 100644 --- a/builtin/providers/aws/resource_aws_db_parameter_group.go +++ b/builtin/providers/aws/resource_aws_db_parameter_group.go @@ -25,33 +25,10 @@ func resourceAwsDbParameterGroup() *schema.Resource { Delete: resourceAwsDbParameterGroupDelete, Schema: map[string]*schema.Schema{ "name": &schema.Schema{ - Type: schema.TypeString, - ForceNew: true, - Required: true, - ValidateFunc: func(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 - }, + Type: schema.TypeString, + ForceNew: true, + Required: true, + ValidateFunc: validateDbParamGroupName, }, "family": &schema.Schema{ Type: schema.TypeString, @@ -252,3 +229,29 @@ func resourceAwsDbParameterHash(v interface{}) int { 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 + +} diff --git a/builtin/providers/aws/resource_aws_db_parameter_group_test.go b/builtin/providers/aws/resource_aws_db_parameter_group_test.go index 93e74bb74..354c16db9 100644 --- a/builtin/providers/aws/resource_aws_db_parameter_group_test.go +++ b/builtin/providers/aws/resource_aws_db_parameter_group_test.go @@ -106,6 +106,51 @@ func TestAccAWSDBParameterGroupOnly(t *testing.T) { }) } +func TestResourceAWSDBParameterGroupName_uppercaseCharacter(t *testing.T) { + var dbParamGroupName = "tEsting123" + _, errors := validateDbParamGroupName(dbParamGroupName, "aws_db_parameter_group_name") + + if len(errors) != 1 { + t.Fatalf("Expected the DB Parameter Group Name to trigger a validation error") + } +} + +func TestResourceAWSDBParameterGroupName_specialCharacters(t *testing.T) { + var dbParamGroupName = "testing123!" + _, errors := validateDbParamGroupName(dbParamGroupName, "SampleKey") + + if len(errors) != 1 { + t.Fatalf("Expected the DB Parameter Group Name to trigger a validation error") + } +} + +func TestResourceAWSDBParameterGroupName_firstCharacterNumber(t *testing.T) { + var dbParamGroupName = "1testing123" + _, errors := validateDbParamGroupName(dbParamGroupName, "SampleKey") + + if len(errors) != 1 { + t.Fatalf("Expected the DB Parameter Group Name to trigger a validation error") + } +} + +func TestResourceAWSDBParameterGroupName_doubleHyphen(t *testing.T) { + var dbParamGroupName = "testing--123" + _, errors := validateDbParamGroupName(dbParamGroupName, "SampleKey") + + if len(errors) != 1 { + t.Fatalf("Expected the DB Parameter Group Name to trigger a validation error") + } +} + +func TestResourceAWSDBParameterGroupName_trailingHyphen(t *testing.T) { + var dbParamGroupName = "testing123-" + _, errors := validateDbParamGroupName(dbParamGroupName, "SampleKey") + + if len(errors) != 1 { + t.Fatalf("Expected the DB Parameter Group Name to trigger a validation error") + } +} + func testAccCheckAWSDBParameterGroupDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).rdsconn From 97188d65834ab38a7b8d63daa05bbff33a8de453 Mon Sep 17 00:00:00 2001 From: stack72 Date: Thu, 8 Oct 2015 10:05:50 +0100 Subject: [PATCH 2/5] Adding a RandomString generator to test for db_param_group_name being too long --- .../resource_aws_db_parameter_group_test.go | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/builtin/providers/aws/resource_aws_db_parameter_group_test.go b/builtin/providers/aws/resource_aws_db_parameter_group_test.go index 354c16db9..2df4ce3c4 100644 --- a/builtin/providers/aws/resource_aws_db_parameter_group_test.go +++ b/builtin/providers/aws/resource_aws_db_parameter_group_test.go @@ -2,7 +2,9 @@ package aws import ( "fmt" + "math/rand" "testing" + "time" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" @@ -151,6 +153,15 @@ func TestResourceAWSDBParameterGroupName_trailingHyphen(t *testing.T) { } } +func TestResourceAWSDBParameterGroupName_tooManyCharacters(t *testing.T) { + dbParamGroupName := RandomString(256) + _, errors := validateDbParamGroupName(dbParamGroupName, "SampleKey") + + if len(errors) != 1 { + t.Fatalf("Expected the DB Parameter Group Name to trigger a validation error") + } +} + func testAccCheckAWSDBParameterGroupDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).rdsconn @@ -238,6 +249,16 @@ func testAccCheckAWSDBParameterGroupExists(n string, v *rds.DBParameterGroup) re } } +func RandomString(strlen int) string { + rand.Seed(time.Now().UTC().UnixNano()) + const chars = "abcdefghijklmnopqrstuvwxyz0123456789" + result := make([]byte, strlen) + for i := 0; i < strlen; i++ { + result[i] = chars[rand.Intn(len(chars))] + } + return string(result) +} + const testAccAWSDBParameterGroupConfig = ` resource "aws_db_parameter_group" "bar" { name = "parameter-group-test-terraform" From bd78dfd8859125fb8c3f98fec3c394ed1a11a0f8 Mon Sep 17 00:00:00 2001 From: stack72 Date: Thu, 8 Oct 2015 12:15:59 +0100 Subject: [PATCH 3/5] Refactoring the multiple tests into a simple test case with multiple inputs as per feedback --- .../resource_aws_db_parameter_group_test.go | 85 ++++++++----------- 1 file changed, 35 insertions(+), 50 deletions(-) diff --git a/builtin/providers/aws/resource_aws_db_parameter_group_test.go b/builtin/providers/aws/resource_aws_db_parameter_group_test.go index 2df4ce3c4..8a49eb031 100644 --- a/builtin/providers/aws/resource_aws_db_parameter_group_test.go +++ b/builtin/providers/aws/resource_aws_db_parameter_group_test.go @@ -108,57 +108,42 @@ func TestAccAWSDBParameterGroupOnly(t *testing.T) { }) } -func TestResourceAWSDBParameterGroupName_uppercaseCharacter(t *testing.T) { - var dbParamGroupName = "tEsting123" - _, errors := validateDbParamGroupName(dbParamGroupName, "aws_db_parameter_group_name") - - if len(errors) != 1 { - t.Fatalf("Expected the DB Parameter Group Name to trigger a validation error") +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, + }, } -} -func TestResourceAWSDBParameterGroupName_specialCharacters(t *testing.T) { - var dbParamGroupName = "testing123!" - _, errors := validateDbParamGroupName(dbParamGroupName, "SampleKey") - - if len(errors) != 1 { - t.Fatalf("Expected the DB Parameter Group Name to trigger a validation error") - } -} - -func TestResourceAWSDBParameterGroupName_firstCharacterNumber(t *testing.T) { - var dbParamGroupName = "1testing123" - _, errors := validateDbParamGroupName(dbParamGroupName, "SampleKey") - - if len(errors) != 1 { - t.Fatalf("Expected the DB Parameter Group Name to trigger a validation error") - } -} - -func TestResourceAWSDBParameterGroupName_doubleHyphen(t *testing.T) { - var dbParamGroupName = "testing--123" - _, errors := validateDbParamGroupName(dbParamGroupName, "SampleKey") - - if len(errors) != 1 { - t.Fatalf("Expected the DB Parameter Group Name to trigger a validation error") - } -} - -func TestResourceAWSDBParameterGroupName_trailingHyphen(t *testing.T) { - var dbParamGroupName = "testing123-" - _, errors := validateDbParamGroupName(dbParamGroupName, "SampleKey") - - if len(errors) != 1 { - t.Fatalf("Expected the DB Parameter Group Name to trigger a validation error") - } -} - -func TestResourceAWSDBParameterGroupName_tooManyCharacters(t *testing.T) { - dbParamGroupName := RandomString(256) - _, errors := validateDbParamGroupName(dbParamGroupName, "SampleKey") - - if len(errors) != 1 { - t.Fatalf("Expected the DB Parameter Group Name to trigger a validation error") + 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") + } } } @@ -249,7 +234,7 @@ func testAccCheckAWSDBParameterGroupExists(n string, v *rds.DBParameterGroup) re } } -func RandomString(strlen int) string { +func randomString(strlen int) string { rand.Seed(time.Now().UTC().UnixNano()) const chars = "abcdefghijklmnopqrstuvwxyz0123456789" result := make([]byte, strlen) From 29630547f466e6deec271c729626c8f8129275f2 Mon Sep 17 00:00:00 2001 From: stack72 Date: Thu, 8 Oct 2015 12:41:07 +0100 Subject: [PATCH 4/5] Fixing the broken build in the aws_db_parameter_group tests --- builtin/providers/aws/resource_aws_db_parameter_group_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/builtin/providers/aws/resource_aws_db_parameter_group_test.go b/builtin/providers/aws/resource_aws_db_parameter_group_test.go index 8a49eb031..397f20901 100644 --- a/builtin/providers/aws/resource_aws_db_parameter_group_test.go +++ b/builtin/providers/aws/resource_aws_db_parameter_group_test.go @@ -141,6 +141,7 @@ func TestResourceAWSDBParameterGroupName_validation(t *testing.T) { 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") } From 6ac07e970aa060819d44f604921e18135018ed37 Mon Sep 17 00:00:00 2001 From: stack72 Date: Thu, 8 Oct 2015 12:50:17 +0100 Subject: [PATCH 5/5] Removing the numbers from the dbParamGroup name random string. There is an edge case that this could actually trigger a failure due to not allowing to start with a number --- builtin/providers/aws/resource_aws_db_parameter_group_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builtin/providers/aws/resource_aws_db_parameter_group_test.go b/builtin/providers/aws/resource_aws_db_parameter_group_test.go index 397f20901..d0042df23 100644 --- a/builtin/providers/aws/resource_aws_db_parameter_group_test.go +++ b/builtin/providers/aws/resource_aws_db_parameter_group_test.go @@ -237,7 +237,7 @@ func testAccCheckAWSDBParameterGroupExists(n string, v *rds.DBParameterGroup) re func randomString(strlen int) string { rand.Seed(time.Now().UTC().UnixNano()) - const chars = "abcdefghijklmnopqrstuvwxyz0123456789" + const chars = "abcdefghijklmnopqrstuvwxyz" result := make([]byte, strlen) for i := 0; i < strlen; i++ { result[i] = chars[rand.Intn(len(chars))]