mirror of https://github.com/k3s-io/k3s
Merge pull request #37934 from xilabao/improve-the-result-of-checking-role-name
Automatic merge from submit-queue (batch tested with PRs 38527, 37934) improve the result of checking role name Get all error messages before return.pull/6/head
commit
b7559ad4c5
|
@ -35,25 +35,27 @@ func IsValidPathSegmentName(name string) []string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var errors []string
|
||||||
for _, illegalContent := range NameMayNotContain {
|
for _, illegalContent := range NameMayNotContain {
|
||||||
if strings.Contains(name, illegalContent) {
|
if strings.Contains(name, illegalContent) {
|
||||||
return []string{fmt.Sprintf(`may not contain '%s'`, illegalContent)}
|
errors = append(errors, fmt.Sprintf(`may not contain '%s'`, illegalContent))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return errors
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsValidPathSegmentPrefix validates the name can be used as a prefix for a name which will be encoded as a path segment
|
// IsValidPathSegmentPrefix validates the name can be used as a prefix for a name which will be encoded as a path segment
|
||||||
// It does not check for exact matches with disallowed names, since an arbitrary suffix might make the name valid
|
// It does not check for exact matches with disallowed names, since an arbitrary suffix might make the name valid
|
||||||
func IsValidPathSegmentPrefix(name string) []string {
|
func IsValidPathSegmentPrefix(name string) []string {
|
||||||
|
var errors []string
|
||||||
for _, illegalContent := range NameMayNotContain {
|
for _, illegalContent := range NameMayNotContain {
|
||||||
if strings.Contains(name, illegalContent) {
|
if strings.Contains(name, illegalContent) {
|
||||||
return []string{fmt.Sprintf(`may not contain '%s'`, illegalContent)}
|
errors = append(errors, fmt.Sprintf(`may not contain '%s'`, illegalContent))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return errors
|
||||||
}
|
}
|
||||||
|
|
||||||
// ValidatePathSegmentName validates the name can be safely encoded as a path segment
|
// ValidatePathSegmentName validates the name can be safely encoded as a path segment
|
||||||
|
|
|
@ -130,3 +130,39 @@ func TestValidatePathSegmentName(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestValidateWithMultiErrors(t *testing.T) {
|
||||||
|
testcases := map[string]struct {
|
||||||
|
Name string
|
||||||
|
Prefix bool
|
||||||
|
ExpectedMsg []string
|
||||||
|
}{
|
||||||
|
"slash,percent": {
|
||||||
|
Name: "foo//bar%",
|
||||||
|
Prefix: false,
|
||||||
|
ExpectedMsg: []string{"may not contain '/'", "may not contain '%'"},
|
||||||
|
},
|
||||||
|
"slash,percent,prefix": {
|
||||||
|
Name: "foo//bar%",
|
||||||
|
Prefix: true,
|
||||||
|
ExpectedMsg: []string{"may not contain '/'", "may not contain '%'"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, tc := range testcases {
|
||||||
|
msgs := ValidatePathSegmentName(tc.Name, tc.Prefix)
|
||||||
|
if len(tc.ExpectedMsg) == 0 && len(msgs) > 0 {
|
||||||
|
t.Errorf("%s: expected no message, got %v", k, msgs)
|
||||||
|
}
|
||||||
|
if len(tc.ExpectedMsg) > 0 && len(msgs) == 0 {
|
||||||
|
t.Errorf("%s: expected error message, got none", k)
|
||||||
|
}
|
||||||
|
if len(tc.ExpectedMsg) > 0 {
|
||||||
|
for i := 0; i < len(tc.ExpectedMsg); i++ {
|
||||||
|
if msgs[i] != tc.ExpectedMsg[i] {
|
||||||
|
t.Errorf("%s: expected message containing %q, got %v", k, tc.ExpectedMsg[i], msgs[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue