Skip to content

Commit

Permalink
Adding unit tests for json_util
Browse files Browse the repository at this point in the history
Signed-off-by: Annaraya Narasagond <[email protected]>
Signed-off-by: Annaraya Narasagond <[email protected]> Annaraya Narasagond <[email protected]>
  • Loading branch information
asn1809 authored and ShyamsundarR committed Nov 14, 2024
1 parent 5c4ef6e commit d217f4b
Show file tree
Hide file tree
Showing 2 changed files with 211 additions and 7 deletions.
14 changes: 7 additions & 7 deletions internal/controller/util/json_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func EvaluateCheckHook(client client.Client, hook *kubeobjects.HookSpec, log log
return false, err
}

return evaluateCheckHookExp(hook.Chk.Condition, resource)
return EvaluateCheckHookExp(hook.Chk.Condition, resource)
case "deployment":
// handle deployment type
resource := &appsv1.Deployment{}
Expand All @@ -49,7 +49,7 @@ func EvaluateCheckHook(client client.Client, hook *kubeobjects.HookSpec, log log
return false, err
}

return evaluateCheckHookExp(hook.Chk.Condition, resource)
return EvaluateCheckHookExp(hook.Chk.Condition, resource)
case "statefulset":
// handle statefulset type
resource := &appsv1.StatefulSet{}
Expand All @@ -59,7 +59,7 @@ func EvaluateCheckHook(client client.Client, hook *kubeobjects.HookSpec, log log
return false, err
}

return evaluateCheckHookExp(hook.Chk.Condition, resource)
return EvaluateCheckHookExp(hook.Chk.Condition, resource)
}

return false, nil
Expand Down Expand Up @@ -104,7 +104,7 @@ func getTimeoutValue(hook *kubeobjects.HookSpec) int {
return defaultTimeoutValue
}

func evaluateCheckHookExp(booleanExpression string, jsonData interface{}) (bool, error) {
func EvaluateCheckHookExp(booleanExpression string, jsonData interface{}) (bool, error) {
op, jsonPaths, err := parseBooleanExpression(booleanExpression)
if err != nil {
return false, fmt.Errorf("failed to parse boolean expression: %w", err)
Expand Down Expand Up @@ -288,16 +288,16 @@ func parseBooleanExpression(booleanExpression string) (op string, jsonPaths []st
jsonPaths = trimLeadingTrailingWhiteSpace(exprs)

if len(exprs) == 2 &&
isValidJSONPathExpression(jsonPaths[0]) &&
isValidJSONPathExpression(jsonPaths[1]) {
IsValidJSONPathExpression(jsonPaths[0]) &&
IsValidJSONPathExpression(jsonPaths[1]) {
return op, jsonPaths, nil
}
}

return "", []string{}, fmt.Errorf("unable to parse boolean expression %v", booleanExpression)
}

func isValidJSONPathExpression(expr string) bool {
func IsValidJSONPathExpression(expr string) bool {
jp := jsonpath.New("validator").AllowMissingKeys(true)

err := jp.Parse(expr)
Expand Down
204 changes: 204 additions & 0 deletions internal/controller/util/json_util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
package util_test

import (
"encoding/json"
"strconv"
"testing"

"github.com/ramendr/ramen/internal/controller/util"
)

type testCases struct {
jsonPathExprs string
result bool
}

var jsonText1 = []byte(`{
"kind": "Deployment",
"spec": {
"progressDeadlineSeconds": 600,
"replicas": 1,
"revisionHistoryLimit": 10
},
"status": {
"replicas": 1,
"conditions": [
{
"status": "True",
"type": "Progressing"
},
{
"status": "True",
"type": "Available"
}
]
}
}`)

var testCasesData = []testCases{
{
jsonPathExprs: "{$.status.conditions[0].status} == True",
result: true,
},
{
jsonPathExprs: "{$.spec.replicas} == 1",
result: true,
},
{
jsonPathExprs: "{$.status.conditions[0].status} == {True}",
result: false,
},
}

func TestXYZ(t *testing.T) {
for i, tt := range testCasesData {
test := tt

t.Run(strconv.Itoa(i), func(t *testing.T) {
var jsonData map[string]interface{}
err := json.Unmarshal(jsonText1, &jsonData)
if err != nil {
t.Error(err)
}
_, err = util.EvaluateCheckHookExp(test.jsonPathExprs, jsonData)
if (err == nil) != test.result {
t.Errorf("EvaluateCheckHookExp() = %v, want %v", err, test.result)
}
})
}
}

func Test_isValidJsonPathExpression(t *testing.T) {
type args struct {
expr string
}

tests := []struct {
name string
args args
want bool
}{
{
name: "Simple expression",
args: args{
expr: "$.spec.replicas",
},
want: true,
},
{
name: "no $ at the start",
args: args{
expr: "{.spec.replicas}",
},
want: true,
},
{
name: "element in array",
args: args{
expr: "{$.status.conditions[0].status}",
},
want: true,
},
{
name: "spec 1",
args: args{
expr: "{$.status.readyReplicas}",
},
want: true,
},
{
name: "spec 2",
args: args{
expr: "{$.status.containerStatuses[0].ready}",
},
want: true,
},
{
name: "spec 3a",
args: args{
expr: "{True}",
},
want: false,
},
{
name: "spec 3b",
args: args{
expr: "{False}",
},
want: false,
},
{
name: "spec 3c",
args: args{
expr: "{true}",
},
want: true,
},
{
name: "spec 3d",
args: args{
expr: "{false}",
},
want: true,
},
{
name: "Spec 4",
args: args{
expr: "{$.spec.replicas}",
},
want: true,
},
{
name: "expression with == operator",
args: args{
expr: "$.store.book[?(@.price > 10)].title==$.store.book[0].title",
},
want: true,
},
{
name: "expression with > operator",
args: args{
expr: "$.store.book[?(@.author CONTAINS 'Smith')].price>20",
},
want: true,
},
{
name: "expression with >= operator",
args: args{
expr: "$.user.age>=$.minimum.age",
},
want: true,
},
{
name: "expression with < operator",
args: args{
expr: "$.user.age<$.maximum.age",
},
want: true,
},
{
name: "expression with <= operator",
args: args{
expr: "$.user.age<=$.maximum.age",
},
want: true,
},
{
name: "expression with != operator",
args: args{
expr: "$.user.age!=$.maximum.age",
},
want: true,
},
}

for _, tt := range tests {
test := tt

t.Run(test.name, func(t *testing.T) {
if got := util.IsValidJSONPathExpression(test.args.expr); got != test.want {
t.Errorf("IsValidJSONPathExpression() = %v, want %v", got, test.want)
}
})
}
}

0 comments on commit d217f4b

Please sign in to comment.