From ce2085594e08cb4a4be7cff356a47256b0ea0709 Mon Sep 17 00:00:00 2001 From: xilabao Date: Wed, 30 Nov 2016 12:05:06 +0800 Subject: [PATCH] improve the result of checking role name --- pkg/api/validation/path/name.go | 10 ++++---- pkg/api/validation/path/name_test.go | 36 ++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/pkg/api/validation/path/name.go b/pkg/api/validation/path/name.go index 981d9bb465..a50cd089df 100644 --- a/pkg/api/validation/path/name.go +++ b/pkg/api/validation/path/name.go @@ -35,25 +35,27 @@ func IsValidPathSegmentName(name string) []string { } } + var errors []string for _, illegalContent := range NameMayNotContain { 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 // It does not check for exact matches with disallowed names, since an arbitrary suffix might make the name valid func IsValidPathSegmentPrefix(name string) []string { + var errors []string for _, illegalContent := range NameMayNotContain { 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 diff --git a/pkg/api/validation/path/name_test.go b/pkg/api/validation/path/name_test.go index 779412f823..4289052738 100644 --- a/pkg/api/validation/path/name_test.go +++ b/pkg/api/validation/path/name_test.go @@ -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]) + } + } + } + } +}