From 63e167446b11121a0e375ad28375b6f5a44a5d95 Mon Sep 17 00:00:00 2001
From: Peter Krempa <pkrempa@redhat.com>
Date: Fri, 15 May 2020 14:33:10 +0200
Subject: [PATCH 18/18] qemuxml2argvtest: Add QAPI/QMP schema validation for
 -blockdev and -netdev

Our hotplug test cases are weak in comparison to the qemuxml2argvtest.
Use all the the input data to also validate the arguments for -netdev
and -blockdev against the appropriate commands of the QMP schema.

Note that currently it's done just for the _CAPS versions of tests but
commenting out a line in the test file allows to validate even cases
which don't use real capabilities.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
---
 tests/Makefile.am        |  2 +-
 tests/qemuxml2argvtest.c | 76 ++++++++++++++++++++++++++++++++++++++++
 tests/testutilsqemu.c    |  5 +++
 tests/testutilsqemu.h    |  1 +
 4 files changed, 83 insertions(+), 1 deletion(-)

diff --git a/tests/Makefile.am b/tests/Makefile.am
index ada5b8fc57..e5440906d1 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -569,7 +569,7 @@ qemuxml2argvtest_SOURCES = \
 	testutils.c testutils.h \
 	virfilewrapper.c virfilewrapper.h \
 	$(NULL)
-qemuxml2argvtest_LDADD = libqemutestdriver.la \
+qemuxml2argvtest_LDADD = libqemutestdriver.la libqemumonitortestutils.la \
 	$(LDADDS) $(LIBXML_LIBS)
 
 libqemuxml2argvmock_la_SOURCES = \
diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c
index 2217e2d81e..dc871d5698 100644
--- a/tests/qemuxml2argvtest.c
+++ b/tests/qemuxml2argvtest.c
@@ -18,6 +18,7 @@
 # include "qemu/qemu_migration.h"
 # include "qemu/qemu_process.h"
 # include "qemu/qemu_slirp.h"
+# include "qemu/qemu_qapi.h"
 # include "datatypes.h"
 # include "conf/storage_conf.h"
 # include "cpu/cpu_map.h"
@@ -26,6 +27,8 @@
 # include "virmock.h"
 # include "virfilewrapper.h"
 # include "configmake.h"
+# include "testutilsqemuschema.h"
+# include "qemu/qemu_monitor_json.h"
 
 # define LIBVIRT_QEMU_CAPSPRIV_H_ALLOW
 # include "qemu/qemu_capspriv.h"
@@ -481,6 +484,76 @@ testCompareXMLToArgvCreateArgs(virQEMUDriverPtr drv,
 }
 
 
+static int
+testCompareXMLToArgvValidateSchema(virQEMUDriverPtr drv,
+                                   virDomainObjPtr vm,
+                                   const char *migrateURI,
+                                   struct testQemuInfo *info,
+                                   unsigned int flags)
+{
+    VIR_AUTOSTRINGLIST args = NULL;
+    size_t nargs = 0;
+    size_t i;
+    g_autoptr(virHashTable) schema = NULL;
+    g_autoptr(virCommand) cmd = NULL;
+
+    if (info->schemafile)
+        schema = testQEMUSchemaLoad(info->schemafile);
+
+    /* comment out with line comment to enable schema checking for non _CAPS tests
+    if (!schema)
+        schema = testQEMUSchemaLoadLatest(virArchToString(info->arch));
+    // */
+
+    if (!schema)
+        return 0;
+
+    if (!(cmd = testCompareXMLToArgvCreateArgs(drv, vm, migrateURI, info, flags,
+                                               true)))
+        return -1;
+
+    if (virCommandGetArgList(cmd, &args, &nargs) < 0)
+        return -1;
+
+    for (i = 0; i < nargs; i++) {
+        g_auto(virBuffer) debug = VIR_BUFFER_INITIALIZER;
+        g_autoptr(virJSONValue) jsonargs = NULL;
+
+        if (STREQ(args[i], "-blockdev")) {
+            if (!(jsonargs = virJSONValueFromString(args[i + 1])))
+                return -1;
+
+            if (testQEMUSchemaValidateCommand("blockdev-add", jsonargs,
+                                              schema, &debug) < 0) {
+                VIR_TEST_VERBOSE("failed to validate -blockdev '%s' against QAPI schema: %s",
+                                 args[i + 1], virBufferCurrentContent(&debug));
+                return -1;
+            }
+
+            i++;
+        } else if (STREQ(args[i], "-netdev")) {
+            if (!(jsonargs = virJSONValueFromString(args[i + 1])))
+                return -1;
+
+            /* skip the validation for pre-QAPIfication cases */
+            if (virQEMUQAPISchemaPathExists("netdev_add/arg-type/type/!string", schema))
+                continue;
+
+            if (testQEMUSchemaValidateCommand("netdev_add", jsonargs,
+                                              schema, &debug) < 0) {
+                VIR_TEST_VERBOSE("failed to validate -netdev '%s' against QAPI schema: %s",
+                                 args[i + 1], virBufferCurrentContent(&debug));
+                return -1;
+            }
+
+            i++;
+        }
+    }
+
+    return 0;
+}
+
+
 static int
 testCompareXMLToArgv(const void *data)
 {
@@ -582,6 +655,9 @@ testCompareXMLToArgv(const void *data)
         goto cleanup;
     }
 
+    if (testCompareXMLToArgvValidateSchema(&driver, vm, migrateURI, info, flags) < 0)
+        goto cleanup;
+
     if (!(actualargv = virCommandToString(cmd, false)))
         goto cleanup;
 
diff --git a/tests/testutilsqemu.c b/tests/testutilsqemu.c
index f3b4e2b3b2..bae1fa828b 100644
--- a/tests/testutilsqemu.c
+++ b/tests/testutilsqemu.c
@@ -767,6 +767,10 @@ testQemuInfoSetArgs(struct testQemuInfo *info,
         if (stripmachinealiases)
             virQEMUCapsStripMachineAliases(qemuCaps);
         info->flags |= FLAG_REAL_CAPS;
+
+        /* provide path to the replies file for schema testing */
+        capsfile[strlen(capsfile) - 3] = '\0';
+        info->schemafile = g_strdup_printf("%sreplies", capsfile);
     }
 
     if (!qemuCaps) {
@@ -793,5 +797,6 @@ testQemuInfoClear(struct testQemuInfo *info)
 {
     VIR_FREE(info->infile);
     VIR_FREE(info->outfile);
+    VIR_FREE(info->schemafile);
     virObjectUnref(info->qemuCaps);
 }
diff --git a/tests/testutilsqemu.h b/tests/testutilsqemu.h
index edee6e450c..e7c5032012 100644
--- a/tests/testutilsqemu.h
+++ b/tests/testutilsqemu.h
@@ -64,6 +64,7 @@ struct testQemuInfo {
     unsigned int flags;
     unsigned int parseFlags;
     virArch arch;
+    char *schemafile;
 };
 
 virCapsPtr testQemuCapsInit(void);
-- 
2.23.0.windows.1

