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

This commit is contained in:
stack72 2015-09-21 22:00:51 +01:00
parent e7f33bf523
commit 91274c9a90
2 changed files with 64 additions and 25 deletions

View File

@ -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
}

View File

@ -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