[libvirt] [PATCH 3/3] virjsontest: switch AddAndRemove tests to work with files

Ján Tomko jtomko at redhat.com
Tue Feb 12 15:57:31 UTC 2019


Instead of using JSON in C strings, put it in separate files
for easier manipulation.

Signed-off-by: Ján Tomko <jtomko at redhat.com>
---
 tests/virjsondata/add-remove-failure-in.json  |  1 +
 tests/virjsondata/add-remove-success-in.json  |  1 +
 tests/virjsondata/add-remove-success-out.json |  1 +
 tests/virjsontest.c                           | 38 ++++++++++++-------
 4 files changed, 27 insertions(+), 14 deletions(-)
 create mode 100644 tests/virjsondata/add-remove-failure-in.json
 create mode 100644 tests/virjsondata/add-remove-success-in.json
 create mode 100644 tests/virjsondata/add-remove-success-out.json

diff --git a/tests/virjsondata/add-remove-failure-in.json b/tests/virjsondata/add-remove-failure-in.json
new file mode 100644
index 0000000000..dd264421e9
--- /dev/null
+++ b/tests/virjsondata/add-remove-failure-in.json
@@ -0,0 +1 @@
+[ 1 ]
diff --git a/tests/virjsondata/add-remove-success-in.json b/tests/virjsondata/add-remove-success-in.json
new file mode 100644
index 0000000000..b8edd30963
--- /dev/null
+++ b/tests/virjsondata/add-remove-success-in.json
@@ -0,0 +1 @@
+{"name": "sample", "value": true}
diff --git a/tests/virjsondata/add-remove-success-out.json b/tests/virjsondata/add-remove-success-out.json
new file mode 100644
index 0000000000..9f1e44637c
--- /dev/null
+++ b/tests/virjsondata/add-remove-success-out.json
@@ -0,0 +1 @@
+{"value":true,"newname":"foo"}
diff --git a/tests/virjsontest.c b/tests/virjsontest.c
index 48cf5df5a9..5bac08d039 100644
--- a/tests/virjsontest.c
+++ b/tests/virjsontest.c
@@ -114,12 +114,24 @@ static int
 testJSONAddRemove(const void *data)
 {
     const struct testInfo *info = data;
-    virJSONValuePtr json;
+    virJSONValuePtr json = NULL;
     virJSONValuePtr name = NULL;
-    char *result = NULL;
+    char *infile = NULL;
+    char *indata = NULL;
+    char *outfile = NULL;
+    char *actual = NULL;
     int ret = -1;
 
-    json = virJSONValueFromString(info->doc);
+    if (virAsprintf(&infile, "%s/virjsondata/add-remove-%s-in.json",
+                    abs_srcdir, info->name) < 0 ||
+        virAsprintf(&outfile, "%s/virjsondata/add-remove-%s-out.json",
+                    abs_srcdir, info->name) < 0)
+        goto cleanup;
+
+    if (virTestLoadFile(infile, &indata) < 0)
+        goto cleanup;
+
+    json = virJSONValueFromString(indata);
     if (!json) {
         VIR_TEST_VERBOSE("Fail to parse %s\n", info->name);
         ret = -1;
@@ -159,20 +171,22 @@ testJSONAddRemove(const void *data)
         VIR_TEST_VERBOSE("%s", "unexpected failure adding new key\n");
         goto cleanup;
     }
-    if (!(result = virJSONValueToString(json, false))) {
+    if (!(actual = virJSONValueToString(json, false))) {
         VIR_TEST_VERBOSE("%s", "failed to stringize result\n");
         goto cleanup;
     }
-    if (STRNEQ(info->expect, result)) {
-        virTestDifference(stderr, info->expect, result);
+    if (virTestCompareToFile(actual, outfile) < 0)
         goto cleanup;
-    }
+
     ret = 0;
 
  cleanup:
     virJSONValueFree(json);
     virJSONValueFree(name);
-    VIR_FREE(result);
+    VIR_FREE(infile);
+    VIR_FREE(indata);
+    VIR_FREE(outfile);
+    VIR_FREE(actual);
     return ret;
 }
 
@@ -554,12 +568,8 @@ mymain(void)
     DO_TEST_PARSE_FILE("Harder");
     DO_TEST_PARSE_FILE("VeryHard");
 
-    DO_TEST_FULL("add and remove", AddRemove,
-                 "{\"name\": \"sample\", \"value\": true}",
-                 "{\"value\":true,\"newname\":\"foo\"}",
-                 true);
-    DO_TEST_FULL("add and remove", AddRemove,
-                 "[ 1 ]", NULL, false);
+    DO_TEST_FULL("success", AddRemove, NULL, NULL, true);
+    DO_TEST_FULL("failure", AddRemove, NULL, NULL, false);
 
     DO_TEST_FULL("copy and free", Copy,
                  "{\"return\": [{\"name\": \"quit\"}, {\"name\": \"eject\"},"
-- 
2.19.2




More information about the libvir-list mailing list