testutil: Add a new match function for JSON values
This commit adds a new function which compares JSON value at a JSON
path. For e.g.
```
spec:
resources:
osd: {"requests":{"cpu":"5","memory":"5Gi"},"limits":{"cpu":"5","memory":"5Gi"}}
```
package testutil
import (
+ "encoding/json"
"fmt"
"reflect"
"strings"
@@ -103,6 +104,29 @@ func MatchJSONPathInt64Value(t *testing.T, yamlConfig string, jsonPath string, e
}
}
+// MatchJSONPathJSONValue is a helper function for component unit tests. It compares the JSON values
+// at a JSON path in a YAML config to the expected JSON string given by the user.
+// e.g.
+//
+// spec:
+// resources:
+// osd: {"requests":{"cpu":"5","memory":"5Gi"},"limits":{"cpu":"5","memory":"5Gi"}}
+func MatchJSONPathJSONValue(t *testing.T, yamlConfig string, jsonPath string, expected string) {
+ obj, err := jsonPathValue(yamlConfig, jsonPath)
+ if err != nil {
+ t.Fatalf("Extracting JSON path value: %v", err)
+ }
+
+ got, err := json.Marshal(obj)
+ if err != nil {
+ t.Fatalf("Marshalling JSON object: %v", err)
+ }
+
+ if string(got) != expected {
+ t.Fatalf("Expected: %s, Got: %s", expected, got)
+ }
+}
+
// JSONPathExists checks if the given YAML config has an object at the given JSON path, also provide
// what error to expect.
func JSONPathExists(t *testing.T, yamlConfig string, jsonPath string, errExp string) {
