From 91274c9a90b50e32bc29a564e4178fe8b075cf6c Mon Sep 17 00:00:00 2001 From: stack72 Date: Mon, 21 Sep 2015 22:00:51 +0100 Subject: [PATCH 1/2] Test spike to extract the function that does the validation for ELB Name. This will allow me to test this in isolation to make sure that the validation rules work as expected --- builtin/providers/aws/resource_aws_elb.go | 53 ++++++++++--------- .../providers/aws/resource_aws_elb_test.go | 36 +++++++++++++ 2 files changed, 64 insertions(+), 25 deletions(-) diff --git a/builtin/providers/aws/resource_aws_elb.go b/builtin/providers/aws/resource_aws_elb.go index a57fc840aa1f..9955c7cf0a3b 100644 --- a/builtin/providers/aws/resource_aws_elb.go +++ b/builtin/providers/aws/resource_aws_elb.go @@ -24,31 +24,11 @@ func resourceAwsElb() *schema.Resource { Schema: map[string]*schema.Schema{ "name": &schema.Schema{ - Type: schema.TypeString, - Optional: true, - Computed: true, - ForceNew: true, - ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) { - value := v.(string) - if !regexp.MustCompile(`^[0-9A-Za-z-]+$`).MatchString(value) { - errors = append(errors, fmt.Errorf( - "only alphanumeric characters and hyphens allowed in %q: %q", - k, value)) - } - if len(value) > 32 { - errors = append(errors, fmt.Errorf( - "%q cannot be longer than 32 characters: %q", k, value)) - } - if regexp.MustCompile(`^-`).MatchString(value) { - errors = append(errors, fmt.Errorf( - "%q cannot begin with a hyphen: %q", k, value)) - } - if regexp.MustCompile(`-$`).MatchString(value) { - errors = append(errors, fmt.Errorf( - "%q cannot end with a hyphen: %q", k, value)) - } - return - }, + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + ValidateFunc: validateElbName, }, "internal": &schema.Schema{ @@ -591,3 +571,26 @@ func isLoadBalancerNotFound(err error) bool { elberr, ok := err.(awserr.Error) return ok && elberr.Code() == "LoadBalancerNotFound" } + +func validateElbName(v interface{}, k string) (ws []string, errors []error) { + value := v.(string) + if !regexp.MustCompile(`^[0-9A-Za-z-]+$`).MatchString(value) { + errors = append(errors, fmt.Errorf( + "only alphanumeric characters and hyphens allowed in %q: %q", + k, value)) + } + if len(value) > 32 { + errors = append(errors, fmt.Errorf( + "%q cannot be longer than 32 characters: %q", k, value)) + } + if regexp.MustCompile(`^-`).MatchString(value) { + errors = append(errors, fmt.Errorf( + "%q cannot begin with a hyphen: %q", k, value)) + } + if regexp.MustCompile(`-$`).MatchString(value) { + errors = append(errors, fmt.Errorf( + "%q cannot end with a hyphen: %q", k, value)) + } + return + +} diff --git a/builtin/providers/aws/resource_aws_elb_test.go b/builtin/providers/aws/resource_aws_elb_test.go index 941b2fdef81d..e3ace0585795 100644 --- a/builtin/providers/aws/resource_aws_elb_test.go +++ b/builtin/providers/aws/resource_aws_elb_test.go @@ -437,6 +437,42 @@ func TestResourceAwsElbListenerHash(t *testing.T) { } } +func TestAccAWSELB_validateElbNameCannotBeginWithHyphen(t *testing.T) { + var elbName = "-Testing123" + _, errors := validateElbName(elbName, "SampleKey") + + if len(errors) != 1 { + t.Fatalf("Expected the ELB Name to trigger a validation error") + } +} + +func TestAccAWSELB_validateElbNameCannotBeLongerThen32Characters(t *testing.T) { + var elbName = "Testing123dddddddddddddddddddvvvv" + _, errors := validateElbName(elbName, "SampleKey") + + if len(errors) != 1 { + t.Fatalf("Expected the ELB Name to trigger a validation error") + } +} + +func TestAccAWSELB_validateElbNameCannotHaveSpecialCharacters(t *testing.T) { + var elbName = "Testing123%%" + _, errors := validateElbName(elbName, "SampleKey") + + if len(errors) != 1 { + t.Fatalf("Expected the ELB Name to trigger a validation error") + } +} + +func TestAccAWSELB_validateElbNameCannotEndWithHyphen(t *testing.T) { + var elbName = "Testing123-" + _, errors := validateElbName(elbName, "SampleKey") + + if len(errors) != 1 { + t.Fatalf("Expected the ELB Name to trigger a validation error") + } +} + func testAccCheckAWSELBDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).elbconn From 04722c88c56e6964e257042c85a50f20c31b4b59 Mon Sep 17 00:00:00 2001 From: stack72 Date: Mon, 21 Sep 2015 22:08:33 +0100 Subject: [PATCH 2/2] Renaming the unit tests for the ELB Name validation to be TestResource to keep inline with existing conventions --- builtin/providers/aws/resource_aws_elb_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/builtin/providers/aws/resource_aws_elb_test.go b/builtin/providers/aws/resource_aws_elb_test.go index e3ace0585795..5b85f54ce83c 100644 --- a/builtin/providers/aws/resource_aws_elb_test.go +++ b/builtin/providers/aws/resource_aws_elb_test.go @@ -437,7 +437,7 @@ func TestResourceAwsElbListenerHash(t *testing.T) { } } -func TestAccAWSELB_validateElbNameCannotBeginWithHyphen(t *testing.T) { +func TestResourceAWSELB_validateElbNameCannotBeginWithHyphen(t *testing.T) { var elbName = "-Testing123" _, errors := validateElbName(elbName, "SampleKey") @@ -446,7 +446,7 @@ func TestAccAWSELB_validateElbNameCannotBeginWithHyphen(t *testing.T) { } } -func TestAccAWSELB_validateElbNameCannotBeLongerThen32Characters(t *testing.T) { +func TestResourceAWSELB_validateElbNameCannotBeLongerThen32Characters(t *testing.T) { var elbName = "Testing123dddddddddddddddddddvvvv" _, errors := validateElbName(elbName, "SampleKey") @@ -455,7 +455,7 @@ func TestAccAWSELB_validateElbNameCannotBeLongerThen32Characters(t *testing.T) { } } -func TestAccAWSELB_validateElbNameCannotHaveSpecialCharacters(t *testing.T) { +func TestResourceAWSELB_validateElbNameCannotHaveSpecialCharacters(t *testing.T) { var elbName = "Testing123%%" _, errors := validateElbName(elbName, "SampleKey") @@ -464,7 +464,7 @@ func TestAccAWSELB_validateElbNameCannotHaveSpecialCharacters(t *testing.T) { } } -func TestAccAWSELB_validateElbNameCannotEndWithHyphen(t *testing.T) { +func TestResourceAWSELB_validateElbNameCannotEndWithHyphen(t *testing.T) { var elbName = "Testing123-" _, errors := validateElbName(elbName, "SampleKey")