diff --git a/staging/src/k8s.io/apimachinery/pkg/util/validation/validation.go b/staging/src/k8s.io/apimachinery/pkg/util/validation/validation.go index b6be7af1b16..bc4521c392b 100644 --- a/staging/src/k8s.io/apimachinery/pkg/util/validation/validation.go +++ b/staging/src/k8s.io/apimachinery/pkg/util/validation/validation.go @@ -235,7 +235,7 @@ func IsDNS1123SubdomainWithUnderscore(value string) []string { errs = append(errs, MaxLenError(DNS1123SubdomainMaxLength)) } if !dns1123SubdomainRegexpWithUnderscore.MatchString(value) { - errs = append(errs, RegexError(dns1123SubdomainErrorMsgFG, dns1123SubdomainFmt, "example.com")) + errs = append(errs, RegexError(dns1123SubdomainErrorMsgFG, dns1123SubdomainFmtWithUnderscore, "example.com")) } return errs } diff --git a/staging/src/k8s.io/apimachinery/pkg/util/validation/validation_test.go b/staging/src/k8s.io/apimachinery/pkg/util/validation/validation_test.go index 848fb60edb9..acfe2cb7687 100644 --- a/staging/src/k8s.io/apimachinery/pkg/util/validation/validation_test.go +++ b/staging/src/k8s.io/apimachinery/pkg/util/validation/validation_test.go @@ -17,6 +17,7 @@ limitations under the License. package validation import ( + "reflect" "strings" "testing" @@ -635,3 +636,86 @@ func TestIsRelaxedEnvVarName(t *testing.T) { } } } + +func TestIsDNS1123SubdomainWithUnderscore(t *testing.T) { + tests := []struct { + name string + value string + expected []string + }{ + { + name: "valid subdomain", + value: "example.com", + expected: nil, + }, + { + name: "valid subdomain with underscore", + value: "_example.com", + expected: nil, + }, + { + name: "valid subdomain with multiple segments", + value: "sub._example.com", + expected: nil, + }, + { + name: "valid subdomain with hyphen", + value: "sub-domain.example.com", + expected: nil, + }, + { + name: "valid subdomain with underscore in middle", + value: "sub_domain.example.com", + expected: nil, + }, + { + name: "empty string", + value: "", + expected: []string{RegexError(dns1123SubdomainErrorMsgFG, dns1123SubdomainFmtWithUnderscore, "example.com")}, + }, + { + name: "invalid uppercase characters", + value: "Example.com", + expected: []string{RegexError(dns1123SubdomainErrorMsgFG, dns1123SubdomainFmtWithUnderscore, "example.com")}, + }, + { + name: "invalid starting character", + value: "-example.com", + expected: []string{RegexError(dns1123SubdomainErrorMsgFG, dns1123SubdomainFmtWithUnderscore, "example.com")}, + }, + { + name: "invalid ending character", + value: "example.com-", + expected: []string{RegexError(dns1123SubdomainErrorMsgFG, dns1123SubdomainFmtWithUnderscore, "example.com")}, + }, + { + name: "invalid characters", + value: "example@.com", + expected: []string{RegexError(dns1123SubdomainErrorMsgFG, dns1123SubdomainFmtWithUnderscore, "example.com")}, + }, + { + name: "too long subdomain", + value: "a." + string(make([]byte, 251)) + ".com", + expected: []string{MaxLenError(DNS1123SubdomainMaxLength), RegexError(dns1123SubdomainErrorMsgFG, dns1123SubdomainFmtWithUnderscore, "example.com")}, + }, + { + name: "multiple dots", + value: "example..com", + expected: []string{RegexError(dns1123SubdomainErrorMsgFG, dns1123SubdomainFmtWithUnderscore, "example.com")}, + }, + { + name: "underscore only", + value: "_", + expected: []string{RegexError(dns1123SubdomainErrorMsgFG, dns1123SubdomainFmtWithUnderscore, "example.com")}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := IsDNS1123SubdomainWithUnderscore(tt.value) + if !reflect.DeepEqual(result, tt.expected) { + t.Errorf("IsDNS1123SubdomainWithUnderscore(%q) = %v; want %v", tt.value, result, tt.expected) + } + }) + } +}