Blame SOURCES/libvirt-qemublocktest-Add-JSON-JSON-test-cases-for-block-device-backends.patch

fbe740
From b385c22388eb9810f654936decc970bc882d6c8b Mon Sep 17 00:00:00 2001
fbe740
Message-Id: <b385c22388eb9810f654936decc970bc882d6c8b@dist-git>
fbe740
From: Peter Krempa <pkrempa@redhat.com>
fbe740
Date: Mon, 16 Mar 2020 22:12:12 +0100
fbe740
Subject: [PATCH] qemublocktest: Add JSON->JSON test cases for block device
fbe740
 backends
fbe740
MIME-Version: 1.0
fbe740
Content-Type: text/plain; charset=UTF-8
fbe740
Content-Transfer-Encoding: 8bit
fbe740
fbe740
Add testing of the interpretation of the JSON pseudo-protocol backing
fbe740
store into JSON structs for blockdev. This will be used to test JSON
fbe740
pseudo-URIs used by libguestfs while actually also validating the output
fbe740
against the QMP schema. Since libguestfs uses obsolete/undocumented
fbe740
values the outputs will differ and a benefit is that modern output is
fbe740
used now.
fbe740
fbe740
The example test case covers the fields and values used by libguestfs
fbe740
when using the https driver.
fbe740
fbe740
Signed-off-by: Peter Krempa <pkrempa@redhat.com>
fbe740
Reviewed-by: Ján Tomko <jtomko@redhat.com>
fbe740
(cherry picked from commit d089234110282069e9a6dfe879ca257d114bb5bd)
fbe740
https://bugzilla.redhat.com/show_bug.cgi?id=1804750
fbe740
Message-Id: <f63c18cbdf69b27bd91fe44250737cf89b3cb090.1584391727.git.pkrempa@redhat.com>
fbe740
Reviewed-by: Ján Tomko <jtomko@redhat.com>
fbe740
---
fbe740
 tests/qemublocktest.c                         | 65 +++++++++++++++++++
fbe740
 .../jsontojson/curl-libguestfs-in.json        |  1 +
fbe740
 .../jsontojson/curl-libguestfs-out.json       |  9 +++
fbe740
 3 files changed, 75 insertions(+)
fbe740
 create mode 100644 tests/qemublocktestdata/jsontojson/curl-libguestfs-in.json
fbe740
 create mode 100644 tests/qemublocktestdata/jsontojson/curl-libguestfs-out.json
fbe740
fbe740
diff --git a/tests/qemublocktest.c b/tests/qemublocktest.c
fbe740
index d2ba85c5e5..d8bd811b4d 100644
fbe740
--- a/tests/qemublocktest.c
fbe740
+++ b/tests/qemublocktest.c
fbe740
@@ -128,6 +128,57 @@ testBackingXMLjsonXML(const void *args)
fbe740
     return 0;
fbe740
 }
fbe740
 
fbe740
+static const char *testJSONtoJSONPath = abs_srcdir "/qemublocktestdata/jsontojson/";
fbe740
+
fbe740
+struct testJSONtoJSONData {
fbe740
+    const char *name;
fbe740
+    virHashTablePtr schema;
fbe740
+    virJSONValuePtr schemaroot;
fbe740
+};
fbe740
+
fbe740
+static int
fbe740
+testJSONtoJSON(const void *args)
fbe740
+{
fbe740
+    const struct testJSONtoJSONData *data = args;
fbe740
+    g_auto(virBuffer) debug = VIR_BUFFER_INITIALIZER;
fbe740
+    g_autoptr(virJSONValue) jsonsrcout = NULL;
fbe740
+    g_autoptr(virStorageSource) src = NULL;
fbe740
+    g_autofree char *actual = NULL;
fbe740
+    g_autofree char *in = NULL;
fbe740
+    g_autofree char *infile = g_strdup_printf("%s%s-in.json", testJSONtoJSONPath,
fbe740
+                                              data->name);
fbe740
+    g_autofree char *outfile = g_strdup_printf("%s%s-out.json", testJSONtoJSONPath,
fbe740
+                                              data->name);
fbe740
+
fbe740
+    if (virTestLoadFile(infile, &in) < 0)
fbe740
+        return -1;
fbe740
+
fbe740
+    if (virStorageSourceNewFromBackingAbsolute(in, &src) < 0) {
fbe740
+        fprintf(stderr, "failed to parse disk json\n");
fbe740
+        return -1;
fbe740
+    }
fbe740
+
fbe740
+    if (!(jsonsrcout = qemuBlockStorageSourceGetBackendProps(src, false, false, true))) {
fbe740
+        fprintf(stderr, "failed to format disk source json\n");
fbe740
+        return -1;
fbe740
+    }
fbe740
+
fbe740
+    if (!(actual = virJSONValueToString(jsonsrcout, true)))
fbe740
+        return -1;
fbe740
+
fbe740
+    if (testQEMUSchemaValidate(jsonsrcout, data->schemaroot,
fbe740
+                               data->schema, &debug) < 0) {
fbe740
+        g_autofree char *debugmsg = virBufferContentAndReset(&debug);
fbe740
+
fbe740
+        VIR_TEST_VERBOSE("json does not conform to QAPI schema");
fbe740
+        VIR_TEST_DEBUG("json:\n%s\ndoes not match schema. Debug output:\n %s",
fbe740
+                       actual, NULLSTR(debugmsg));
fbe740
+        return -1;
fbe740
+    }
fbe740
+
fbe740
+    return virTestCompareToFile(actual, outfile);
fbe740
+}
fbe740
+
fbe740
 
fbe740
 struct testQemuDiskXMLToJSONData {
fbe740
     virQEMUDriverPtr driver;
fbe740
@@ -875,6 +926,7 @@ mymain(void)
fbe740
     virQEMUDriver driver;
fbe740
     struct testBackingXMLjsonXMLdata xmljsonxmldata;
fbe740
     struct testQemuDiskXMLToJSONData diskxmljsondata;
fbe740
+    struct testJSONtoJSONData jsontojsondata;
fbe740
     struct testQemuImageCreateData imagecreatedata;
fbe740
     struct testQemuBackupIncrementalBitmapCalculateData backupbitmapcalcdata;
fbe740
     struct testQemuCheckpointDeleteMergeData checkpointdeletedata;
fbe740
@@ -1068,6 +1120,19 @@ mymain(void)
fbe740
     TEST_DISK_TO_JSON("block-raw-noopts");
fbe740
     TEST_DISK_TO_JSON("block-raw-reservations");
fbe740
 
fbe740
+#define TEST_JSON_TO_JSON(nme) \
fbe740
+    do { \
fbe740
+        jsontojsondata.name = nme; \
fbe740
+        if (virTestRun("JSON to JSON " nme, testJSONtoJSON, \
fbe740
+                       &jsontojsondata) < 0) \
fbe740
+            ret = -1; \
fbe740
+    } while (0)
fbe740
+
fbe740
+    jsontojsondata.schema = qmp_schema_x86_64;
fbe740
+    jsontojsondata.schemaroot = qmp_schemaroot_x86_64_blockdev_add;
fbe740
+
fbe740
+    TEST_JSON_TO_JSON("curl-libguestfs");
fbe740
+
fbe740
 #define TEST_IMAGE_CREATE(testname, testbacking) \
fbe740
     do { \
fbe740
         imagecreatedata.name = testname; \
fbe740
diff --git a/tests/qemublocktestdata/jsontojson/curl-libguestfs-in.json b/tests/qemublocktestdata/jsontojson/curl-libguestfs-in.json
fbe740
new file mode 100644
fbe740
index 0000000000..0b92dabc6d
fbe740
--- /dev/null
fbe740
+++ b/tests/qemublocktestdata/jsontojson/curl-libguestfs-in.json
fbe740
@@ -0,0 +1 @@
fbe740
+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\""}
fbe740
diff --git a/tests/qemublocktestdata/jsontojson/curl-libguestfs-out.json b/tests/qemublocktestdata/jsontojson/curl-libguestfs-out.json
fbe740
new file mode 100644
fbe740
index 0000000000..e130c7bd3c
fbe740
--- /dev/null
fbe740
+++ b/tests/qemublocktestdata/jsontojson/curl-libguestfs-out.json
fbe740
@@ -0,0 +1,9 @@
fbe740
+{
fbe740
+  "driver": "https",
fbe740
+  "url": "https://test.host:443/whatever.img",
fbe740
+  "sslverify": false,
fbe740
+  "timeout": 2000,
fbe740
+  "readahead": 65536,
fbe740
+  "auto-read-only": true,
fbe740
+  "discard": "unmap"
fbe740
+}
fbe740
-- 
fbe740
2.25.1
fbe740