Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix validation for serivce bus topic name #7083

Merged
merged 7 commits into from May 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
}
})
}
}