chore: address review feedback

add integration test to wait for json without value
refactor JSON condition value parsing and validating
adjusting test to reflect the error message refactoring
This commit is contained in:
minherz 2023-06-29 00:36:07 -07:00
parent a5c4fbe979
commit dbdd861ea3
No known key found for this signature in database
3 changed files with 24 additions and 9 deletions

View File

@ -250,14 +250,14 @@ func processJSONPathInput(jsonPathInput []string) (string, string, error) {
if err != nil {
return "", "", err
}
if len(jsonPathInput) > 1 {
jsonPathValue := jsonPathInput[1]
if jsonPathValue == "" {
return "", "", errors.New("jsonpath wait value cannot be empty")
}
return relaxedJSONPathExp, strings.Trim(jsonPathValue, `'"`), nil
if len(jsonPathInput) == 1 {
return relaxedJSONPathExp, "", nil
}
return relaxedJSONPathExp, "", nil
jsonPathValue := strings.Trim(jsonPathInput[1], `'"`)
if jsonPathValue == "" {
return "", "", errors.New("jsonpath wait has to have a value after equal sign, like --for=jsonpath='{.status.readyReplicas}'=3")
}
return relaxedJSONPathExp, jsonPathValue, nil
}
// ResourceLocation holds the location of a resource

View File

@ -1556,13 +1556,13 @@ func TestWaitForJSONPathBadConditionParsing(t *testing.T) {
{
name: "undefined value",
condition: "jsonpath={.metadata.name}=",
expectedErr: "jsonpath wait value cannot be empty",
expectedErr: "jsonpath wait has to have a value after equal sign, like --for=jsonpath='{.status.readyReplicas}'=3",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
_, err := conditionFuncFor(test.condition, io.Discard)
if err == nil {
if err == nil && test.expectedErr != "" {
t.Fatalf("expected %q, got empty", test.expectedErr)
}
if !strings.Contains(err.Error(), test.expectedErr) {

View File

@ -94,6 +94,21 @@ EOF
# Clean deployment
kubectl delete deployment dtest
# create test data
kubectl create deployment test-3 --image=busybox
# wait with jsonpath without value to succeed
set +o errexit
# Command: Wait with jsonpath without value
output_message=$(kubectl wait --for=jsonpath='{.status.replicas}' deploy/test-3 2>&1)
set -o errexit
# Post-Condition: Wait succeed
kube::test::if_has_string "${output_message}" 'deployment.apps/test-3 condition met'
# Clean deployment
kubectl delete deployment test-3
set +o nounset
set +o errexit
}