fix parse pairs

This commit is contained in:
xilabao 2017-06-01 16:48:02 +08:00
parent 91cef78f43
commit 8fe8e4f106
3 changed files with 35 additions and 3 deletions

View File

@ -156,6 +156,18 @@ func TestParseAnnotations(t *testing.T) {
scenario: "incorrect annotation input (missing =value)",
expectErr: true,
},
{
annotations: []string{"-"},
expectedErr: "invalid annotation format: -",
scenario: "incorrect annotation input (missing key)",
expectErr: true,
},
{
annotations: []string{"=bar"},
expectedErr: "invalid annotation format: =bar",
scenario: "incorrect annotation input (missing key)",
expectErr: true,
},
}
for _, test := range tests {
annotations, remove, err := parseAnnotations(test.annotations)
@ -380,6 +392,18 @@ func TestAnnotateErrors(t *testing.T) {
return strings.Contains(err.Error(), "at least one annotation update is required")
},
},
"wrong annotations": {
args: []string{"pods", "-"},
errFn: func(err error) bool {
return strings.Contains(err.Error(), "at least one annotation update is required")
},
},
"wrong annotations 2": {
args: []string{"pods", "=bar"},
errFn: func(err error) bool {
return strings.Contains(err.Error(), "at least one annotation update is required")
},
},
"no resources remove annotations": {
args: []string{"pods-"},
errFn: func(err error) bool { return strings.Contains(err.Error(), "one or more resources must be specified") },

View File

@ -289,6 +289,14 @@ func TestLabelErrors(t *testing.T) {
args: []string{"pods"},
errFn: func(err error) bool { return strings.Contains(err.Error(), "at least one label update is required") },
},
"wrong labels": {
args: []string{"pods", "-"},
errFn: func(err error) bool { return strings.Contains(err.Error(), "at least one label update is required") },
},
"wrong labels 2": {
args: []string{"pods", "=bar"},
errFn: func(err error) bool { return strings.Contains(err.Error(), "at least one label update is required") },
},
"no resources": {
args: []string{"pods-"},
errFn: func(err error) bool { return strings.Contains(err.Error(), "one or more resources must be specified") },

View File

@ -620,7 +620,7 @@ func AddInclude3rdPartyVarFlags(cmd *cobra.Command, include3rdParty *bool) {
func GetResourcesAndPairs(args []string, pairType string) (resources []string, pairArgs []string, err error) {
foundPair := false
for _, s := range args {
nonResource := strings.Contains(s, "=") || strings.HasSuffix(s, "-")
nonResource := (strings.Contains(s, "=") && s[0] != '=') || (strings.HasSuffix(s, "-") && s != "-")
switch {
case !foundPair && nonResource:
foundPair = true
@ -646,7 +646,7 @@ func ParsePairs(pairArgs []string, pairType string, supportRemove bool) (newPair
var invalidBuf bytes.Buffer
var invalidBufNonEmpty bool
for _, pairArg := range pairArgs {
if strings.Contains(pairArg, "=") {
if strings.Contains(pairArg, "=") && pairArg[0] != '=' {
parts := strings.SplitN(pairArg, "=", 2)
if len(parts) != 2 {
if invalidBufNonEmpty {
@ -657,7 +657,7 @@ func ParsePairs(pairArgs []string, pairType string, supportRemove bool) (newPair
} else {
newPairs[parts[0]] = parts[1]
}
} else if supportRemove && strings.HasSuffix(pairArg, "-") {
} else if supportRemove && strings.HasSuffix(pairArg, "-") && pairArg != "-" {
removePairs = append(removePairs, pairArg[:len(pairArg)-1])
} else {
if invalidBufNonEmpty {