From 8fe8e4f1067c78c70c3a8a48a4b572cb99194e8b Mon Sep 17 00:00:00 2001 From: xilabao Date: Thu, 1 Jun 2017 16:48:02 +0800 Subject: [PATCH] fix parse pairs --- pkg/kubectl/cmd/annotate_test.go | 24 ++++++++++++++++++++++++ pkg/kubectl/cmd/label_test.go | 8 ++++++++ pkg/kubectl/cmd/util/helpers.go | 6 +++--- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/pkg/kubectl/cmd/annotate_test.go b/pkg/kubectl/cmd/annotate_test.go index 7f8a2e24538..e1b6ca7d6dd 100644 --- a/pkg/kubectl/cmd/annotate_test.go +++ b/pkg/kubectl/cmd/annotate_test.go @@ -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") }, diff --git a/pkg/kubectl/cmd/label_test.go b/pkg/kubectl/cmd/label_test.go index e92dda131bd..5140ca2bcf2 100644 --- a/pkg/kubectl/cmd/label_test.go +++ b/pkg/kubectl/cmd/label_test.go @@ -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") }, diff --git a/pkg/kubectl/cmd/util/helpers.go b/pkg/kubectl/cmd/util/helpers.go index 867d5c55cea..fa8ccfa7dc5 100644 --- a/pkg/kubectl/cmd/util/helpers.go +++ b/pkg/kubectl/cmd/util/helpers.go @@ -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 {