Skip to content

Commit

Permalink
Merge pull request #7083 from neil-yechenwei/sbnamevalidate
Browse files Browse the repository at this point in the history
Fix validation for serivce bus topic name
  • Loading branch information
tombuildsstuff committed May 26, 2020
2 parents c4332c7 + de443e1 commit ec1f2e6
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
4 changes: 2 additions & 2 deletions azurerm/helpers/azure/servicebus.go
Expand Up @@ -28,8 +28,8 @@ func ValidateServiceBusSubscriptionName() schema.SchemaValidateFunc {

func ValidateServiceBusTopicName() schema.SchemaValidateFunc {
return validation.StringMatch(
regexp.MustCompile("^[a-zA-Z][-._~a-zA-Z0-9]{0,258}([a-zA-Z0-9])?$"),
"The topic name can contain only letters, numbers, periods, hyphens, tildas and underscores. The namespace must start with a letter, and it must end with a letter or number and be less then 260 characters long.",
regexp.MustCompile("^[a-zA-Z0-9]([-._~a-zA-Z0-9]{0,258}[a-zA-Z0-9])?$"),
"The topic name can contain only letters, numbers, periods, hyphens, tildas and underscores. The namespace must start with a letter or number, and it must end with a letter or number and be less then 260 characters long.",
)
}

Expand Down
76 changes: 76 additions & 0 deletions azurerm/helpers/azure/servicebus_test.go
@@ -0,0 +1,76 @@
package azure

import (
"strings"
"testing"
)

func TestValidateServiceBusTopicName(t *testing.T) {
tests := []struct {
name string
input string
valid bool
}{
{
name: "Empty value",
input: "",
valid: false,
},
{
name: "Invalid name with only 1 letter",
input: "a",
valid: true,
},
{
name: "Invalid name starts with underscore",
input: "_a",
valid: false,
},
{
name: "Invalid name ends with period",
input: "a.",
valid: false,
},
{
name: "Valid name with numbers",
input: "12345",
valid: true,
},
{
name: "Valid name with only 1 number",
input: "1",
valid: true,
},
{
name: "Valid name with hyphens",
input: "malcolm-in-the-middle",
valid: true,
},
{
name: "Valid name with 259 characters",
input: strings.Repeat("w", 259),
valid: true,
},
{
name: "Valid name with 260 characters",
input: strings.Repeat("w", 260),
valid: true,
},
{
name: "Invalid name with 261 characters",
input: strings.Repeat("w", 261),
valid: false,
},
}

var validationFunction = ValidateServiceBusTopicName()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := validationFunction(tt.input, "name")
valid := err == nil
if valid != tt.valid {
t.Errorf("Expected valid status %t but got %t for input %s", tt.valid, valid, tt.input)
}
})
}
}

0 comments on commit ec1f2e6

Please sign in to comment.