]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
qemublocktest: Add JSON->JSON test cases for block device backends
authorPeter Krempa <pkrempa@redhat.com>
Mon, 9 Mar 2020 16:06:10 +0000 (17:06 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Mon, 16 Mar 2020 14:51:44 +0000 (15:51 +0100)
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>
tests/qemublocktest.c
tests/qemublocktestdata/jsontojson/curl-libguestfs-in.json [new file with mode: 0644]
tests/qemublocktestdata/jsontojson/curl-libguestfs-out.json [new file with mode: 0644]

index 77484cc8e7c26c3f8ed0d2ae595f7a42409cd2db..ec32d28188d7f772507ff7d4b4e1d26832629de9 100644 (file)
@@ -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;
@@ -879,6 +930,7 @@ mymain(void)
     virQEMUDriver driver;
     struct testBackingXMLjsonXMLdata xmljsonxmldata;
     struct testQemuDiskXMLToJSONData diskxmljsondata;
+    struct testJSONtoJSONData jsontojsondata;
     struct testQemuImageCreateData imagecreatedata;
     struct testQemuBackupIncrementalBitmapCalculateData backupbitmapcalcdata;
     struct testQemuCheckpointDeleteMergeData checkpointdeletedata;
@@ -1072,6 +1124,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 (file)
index 0000000..0b92dab
--- /dev/null
@@ -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 (file)
index 0000000..e130c7b
--- /dev/null
@@ -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"
+}