mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-20 09:05:26 +00:00
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:
parent
9d3e55ec43
commit
a5c4fbe979
@ -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
|
// 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) {
|
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}'")
|
return "", "", fmt.Errorf("jsonpath wait format must be --for=jsonpath='{.status.readyReplicas}'=3 or --for=jsonpath='{.status.readyReplicas}'")
|
||||||
}
|
}
|
||||||
relaxedJSONPathExp, err := cmdget.RelaxedJSONPathExpression(jsonPathInput[0])
|
relaxedJSONPathExp, err := cmdget.RelaxedJSONPathExpression(jsonPathInput[0])
|
||||||
|
@ -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())
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user