Blob Blame History Raw
From b385c22388eb9810f654936decc970bc882d6c8b Mon Sep 17 00:00:00 2001
Message-Id: <b385c22388eb9810f654936decc970bc882d6c8b@dist-git>
From: Peter Krempa <pkrempa@redhat.com>
Date: Mon, 16 Mar 2020 22:12:12 +0100
Subject: [PATCH] qemublocktest: Add JSON->JSON test cases for block device
 backends
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Add testing of the interpretation of the JSON pseudo-protocol backing
store into JSON structs for blockdev. This will be used to test JSON
pseudo-URIs used by libguestfs while actually also validating the output
against the QMP schema. Since libguestfs uses obsolete/undocumented
values the outputs will differ and a benefit is that modern output is
used now.

The example test case covers the fields and values used by libguestfs
when using the https driver.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
(cherry picked from commit d089234110282069e9a6dfe879ca257d114bb5bd)
https://bugzilla.redhat.com/show_bug.cgi?id=1804750
Message-Id: <f63c18cbdf69b27bd91fe44250737cf89b3cb090.1584391727.git.pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
---
 tests/qemublocktest.c                         | 65 +++++++++++++++++++
 .../jsontojson/curl-libguestfs-in.json        |  1 +
 .../jsontojson/curl-libguestfs-out.json       |  9 +++
 3 files changed, 75 insertions(+)
 create mode 100644 tests/qemublocktestdata/jsontojson/curl-libguestfs-in.json
 create mode 100644 tests/qemublocktestdata/jsontojson/curl-libguestfs-out.json

diff --git a/tests/qemublocktest.c b/tests/qemublocktest.c
index d2ba85c5e5..d8bd811b4d 100644
--- a/tests/qemublocktest.c
+++ b/tests/qemublocktest.c
@@ -128,6 +128,57 @@ testBackingXMLjsonXML(const void *args)
     return 0;
 }
 
+static const char *testJSONtoJSONPath = abs_srcdir "/qemublocktestdata/jsontojson/";
+
+struct testJSONtoJSONData {
+    const char *name;
+    virHashTablePtr schema;
+    virJSONValuePtr schemaroot;
+};
+
+static int
+testJSONtoJSON(const void *args)
+{
+    const struct testJSONtoJSONData *data = args;
+    g_auto(virBuffer) debug = VIR_BUFFER_INITIALIZER;
+    g_autoptr(virJSONValue) jsonsrcout = NULL;
+    g_autoptr(virStorageSource) src = NULL;
+    g_autofree char *actual = NULL;
+    g_autofree char *in = NULL;
+    g_autofree char *infile = g_strdup_printf("%s%s-in.json", testJSONtoJSONPath,
+                                              data->name);
+    g_autofree char *outfile = g_strdup_printf("%s%s-out.json", testJSONtoJSONPath,
+                                              data->name);
+
+    if (virTestLoadFile(infile, &in) < 0)
+        return -1;
+
+    if (virStorageSourceNewFromBackingAbsolute(in, &src) < 0) {
+        fprintf(stderr, "failed to parse disk json\n");
+        return -1;
+    }
+
+    if (!(jsonsrcout = qemuBlockStorageSourceGetBackendProps(src, false, false, true))) {
+        fprintf(stderr, "failed to format disk source json\n");
+        return -1;
+    }
+
+    if (!(actual = virJSONValueToString(jsonsrcout, true)))
+        return -1;
+
+    if (testQEMUSchemaValidate(jsonsrcout, data->schemaroot,
+                               data->schema, &debug) < 0) {
+        g_autofree char *debugmsg = virBufferContentAndReset(&debug);
+
+        VIR_TEST_VERBOSE("json does not conform to QAPI schema");
+        VIR_TEST_DEBUG("json:\n%s\ndoes not match schema. Debug output:\n %s",
+                       actual, NULLSTR(debugmsg));
+        return -1;
+    }
+
+    return virTestCompareToFile(actual, outfile);
+}
+
 
 struct testQemuDiskXMLToJSONData {
     virQEMUDriverPtr driver;
@@ -875,6 +926,7 @@ mymain(void)
     virQEMUDriver driver;
     struct testBackingXMLjsonXMLdata xmljsonxmldata;
     struct testQemuDiskXMLToJSONData diskxmljsondata;
+    struct testJSONtoJSONData jsontojsondata;
     struct testQemuImageCreateData imagecreatedata;
     struct testQemuBackupIncrementalBitmapCalculateData backupbitmapcalcdata;
     struct testQemuCheckpointDeleteMergeData checkpointdeletedata;
@@ -1068,6 +1120,19 @@ mymain(void)
     TEST_DISK_TO_JSON("block-raw-noopts");
     TEST_DISK_TO_JSON("block-raw-reservations");
 
+#define TEST_JSON_TO_JSON(nme) \
+    do { \
+        jsontojsondata.name = nme; \
+        if (virTestRun("JSON to JSON " nme, testJSONtoJSON, \
+                       &jsontojsondata) < 0) \
+            ret = -1; \
+    } while (0)
+
+    jsontojsondata.schema = qmp_schema_x86_64;
+    jsontojsondata.schemaroot = qmp_schemaroot_x86_64_blockdev_add;
+
+    TEST_JSON_TO_JSON("curl-libguestfs");
+
 #define TEST_IMAGE_CREATE(testname, testbacking) \
     do { \
         imagecreatedata.name = testname; \
diff --git a/tests/qemublocktestdata/jsontojson/curl-libguestfs-in.json b/tests/qemublocktestdata/jsontojson/curl-libguestfs-in.json
new file mode 100644
index 0000000000..0b92dabc6d
--- /dev/null
+++ b/tests/qemublocktestdata/jsontojson/curl-libguestfs-in.json
@@ -0,0 +1 @@
+json:{"file.driver":"https","file.url":"https://test.host/whatever.img","file.timeout":2000,"file.readahead":65536,"file.sslverify":"off","file.cookie":"some_cookie=\"some_value_or_whatever\""}
diff --git a/tests/qemublocktestdata/jsontojson/curl-libguestfs-out.json b/tests/qemublocktestdata/jsontojson/curl-libguestfs-out.json
new file mode 100644
index 0000000000..e130c7bd3c
--- /dev/null
+++ b/tests/qemublocktestdata/jsontojson/curl-libguestfs-out.json
@@ -0,0 +1,9 @@
+{
+  "driver": "https",
+  "url": "https://test.host:443/whatever.img",
+  "sslverify": false,
+  "timeout": 2000,
+  "readahead": 65536,
+  "auto-read-only": true,
+  "discard": "unmap"
+}
-- 
2.25.1