diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/wait/wait.go b/staging/src/k8s.io/kubectl/pkg/cmd/wait/wait.go index 561007a4263..7f2359f55cb 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/wait/wait.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/wait/wait.go @@ -243,7 +243,7 @@ func newJSONPathParser(jsonPathExpression string) (*jsonpath.JSONPath, error) { // processJSONPathInput will parses the user's JSONPath input containing JSON expression and, optionally, JSON value for matching condition and process it func processJSONPathInput(jsonPathInput []string) (string, string, error) { - if numOfArgs := len(jsonPathInput); 1 < numOfArgs || numOfArgs > 2 { + if numOfArgs := len(jsonPathInput); numOfArgs < 1 || numOfArgs > 2 { return "", "", fmt.Errorf("jsonpath wait format must be --for=jsonpath='{.status.readyReplicas}'=3 or --for=jsonpath='{.status.readyReplicas}'") } relaxedJSONPathExp, err := cmdget.RelaxedJSONPathExpression(jsonPathInput[0]) diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/wait/wait_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/wait/wait_test.go index 50d2b0a0989..2923b7bc3e9 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/wait/wait_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/wait/wait_test.go @@ -1533,3 +1533,41 @@ func TestWaitForJSONPathCondition(t *testing.T) { }) } } + +// TestWaitForJSONPathBadConditionParsing will test errors in parsing JSONPath bad condition expressions +// except for parsing JSONPath expression itself (i.e. call to cmdget.RelaxedJSONPathExpression()) +func TestWaitForJSONPathBadConditionParsing(t *testing.T) { + tests := []struct { + name string + condition string + expectedResult JSONPathWait + expectedErr string + }{ + { + name: "missing JSONPath expression", + condition: "jsonpath=", + expectedErr: "jsonpath expression cannot be empty", + }, + { + name: "value in JSONPath expression has equal sign", + condition: "jsonpath={.metadata.name}='test=wrong'", + expectedErr: "jsonpath wait format must be --for=jsonpath='{.status.readyReplicas}'=3 or --for=jsonpath='{.status.readyReplicas}'", + }, + { + name: "undefined value", + condition: "jsonpath={.metadata.name}=", + expectedErr: "jsonpath wait value cannot be empty", + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + _, err := conditionFuncFor(test.condition, io.Discard) + if err == nil { + t.Fatalf("expected %q, got empty", test.expectedErr) + } + if !strings.Contains(err.Error(), test.expectedErr) { + t.Fatalf("expected %q, got %q", test.expectedErr, err.Error()) + } + }) + } +}