chore(fix): test jsonpath condition parsing errors

Test parsing logic for invalid JSONPath condition formats,
excluding JSON path expression parsing.
Fix error in parsing logic
This commit is contained in:
minherz 2023-06-03 21:40:16 -07:00
parent 9d3e55ec43
commit a5c4fbe979
No known key found for this signature in database
2 changed files with 39 additions and 1 deletions

View File

@ -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])

View File

@ -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())
}
})
}
}