From fb869a0d3a28591d17a9f047eafd3b77513d6d77 Mon Sep 17 00:00:00 2001
From: Peter Krempa <pkrempa@redhat.com>
Date: Fri, 15 May 2020 14:24:21 +0200
Subject: [PATCH 17/18] qemu: Prepare for testing of 'netdev_add' props via
 qemuxml2argvtest

qemuxml2argv test suite is way more comprehensive than the hotplug
suite. Since we share the code paths for monitor and command line
hotplug we can easily test the properties of devices against the QAPI
schema.

To achieve this we'll need to skip the JSON->commandline conversion for
the test run so that we can analyze the pure properties. This patch adds
flags for the comand line generator and hook them into the
JSON->commandline convertor for -netdev. An upcoming patch will make use
of this new infrastructure.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
---
 src/qemu/qemu_command.c  | 28 +++++++++++++++++-----------
 src/qemu/qemu_command.h  |  7 ++++++-
 src/qemu/qemu_driver.c   |  2 +-
 src/qemu/qemu_process.c  | 11 +++++++++--
 src/qemu/qemu_process.h  |  1 +
 src/util/virqemu.c       |  9 ++++++++-
 src/util/virqemu.h       |  3 ++-
 tests/qemuxml2argvtest.c |  6 ++++--
 8 files changed, 48 insertions(+), 19 deletions(-)

diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index bd78bfb31b..dae6b5a7f9 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -8027,7 +8027,8 @@ qemuBuildInterfaceCommandLine(virQEMUDriverPtr driver,
                               virNetDevVPortProfileOp vmop,
                               bool standalone,
                               size_t *nnicindexes,
-                              int **nicindexes)
+                              int **nicindexes,
+                              unsigned int flags)
 {
     virDomainDefPtr def = vm->def;
     int ret = -1;
@@ -8256,7 +8257,8 @@ qemuBuildInterfaceCommandLine(virQEMUDriverPtr driver,
                                              slirpfdName)))
         goto cleanup;
 
-    if (!(host = virQEMUBuildNetdevCommandlineFromJSON(hostnetprops)))
+    if (!(host = virQEMUBuildNetdevCommandlineFromJSON(hostnetprops,
+                                                       (flags & QEMU_BUILD_COMMANDLINE_VALIDATE_KEEP_JSON))))
         goto cleanup;
 
     virCommandAddArgList(cmd, "-netdev", host, NULL);
@@ -8332,7 +8334,8 @@ qemuBuildNetCommandLine(virQEMUDriverPtr driver,
                         bool standalone,
                         size_t *nnicindexes,
                         int **nicindexes,
-                        unsigned int *bootHostdevNet)
+                        unsigned int *bootHostdevNet,
+                        unsigned int flags)
 {
     size_t i;
     int last_good_net = -1;
@@ -8356,7 +8359,7 @@ qemuBuildNetCommandLine(virQEMUDriverPtr driver,
             if (qemuBuildInterfaceCommandLine(driver, vm, logManager, secManager, cmd, net,
                                               qemuCaps, bootNet, vmop,
                                               standalone, nnicindexes,
-                                              nicindexes) < 0)
+                                              nicindexes, flags) < 0)
                 goto error;
 
             last_good_net = i;
@@ -8892,7 +8895,8 @@ qemuBuildChannelsCommandLine(virLogManagerPtr logManager,
                              virQEMUDriverConfigPtr cfg,
                              const virDomainDef *def,
                              virQEMUCapsPtr qemuCaps,
-                             bool chardevStdioLogd)
+                             bool chardevStdioLogd,
+                             unsigned int flags)
 {
     size_t i;
     unsigned int cdevflags = QEMU_BUILD_CHARDEV_TCP_NOWAIT |
@@ -8921,7 +8925,8 @@ qemuBuildChannelsCommandLine(virLogManagerPtr logManager,
             if (!(netdevprops = qemuBuildChannelGuestfwdNetdevProps(channel)))
                 return -1;
 
-            if (!(netdevstr = virQEMUBuildNetdevCommandlineFromJSON(netdevprops)))
+            if (!(netdevstr = virQEMUBuildNetdevCommandlineFromJSON(netdevprops,
+                                                                    (flags & QEMU_BUILD_COMMANDLINE_VALIDATE_KEEP_JSON))))
                 return -1;
 
             virCommandAddArgList(cmd, "-netdev", netdevstr, NULL);
@@ -9857,7 +9862,8 @@ qemuBuildCommandLine(virQEMUDriverPtr driver,
                      bool standalone,
                      bool enableFips,
                      size_t *nnicindexes,
-                     int **nicindexes)
+                     int **nicindexes,
+                     unsigned int flags)
 {
     size_t i;
     char uuid[VIR_UUID_STRING_BUFLEN];
@@ -9870,9 +9876,9 @@ qemuBuildCommandLine(virQEMUDriverPtr driver,
     bool chardevStdioLogd = priv->chardevStdioLogd;
 
     VIR_DEBUG("driver=%p def=%p mon=%p "
-              "qemuCaps=%p migrateURI=%s snapshot=%p vmop=%d",
+              "qemuCaps=%p migrateURI=%s snapshot=%p vmop=%d flags=0x%x",
               driver, def, priv->monConfig,
-              qemuCaps, migrateURI, snapshot, vmop);
+              qemuCaps, migrateURI, snapshot, vmop, flags);
 
     if (qemuBuildCommandLineValidate(driver, def) < 0)
         return NULL;
@@ -10017,7 +10023,7 @@ qemuBuildCommandLine(virQEMUDriverPtr driver,
 
     if (qemuBuildNetCommandLine(driver, vm, logManager, secManager, cmd,
                                 qemuCaps, vmop, standalone,
-                                nnicindexes, nicindexes, &bootHostdevNet) < 0)
+                                nnicindexes, nicindexes, &bootHostdevNet, flags) < 0)
         return NULL;
 
     if (qemuBuildSmartcardCommandLine(logManager, secManager, cmd, cfg, def, qemuCaps,
@@ -10033,7 +10039,7 @@ qemuBuildCommandLine(virQEMUDriverPtr driver,
         return NULL;
 
     if (qemuBuildChannelsCommandLine(logManager, secManager, cmd, cfg, def, qemuCaps,
-                                     chardevStdioLogd) < 0)
+                                     chardevStdioLogd, flags) < 0)
         return NULL;
 
     if (qemuBuildConsoleCommandLine(logManager, secManager, cmd, cfg, def, qemuCaps,
diff --git a/src/qemu/qemu_command.h b/src/qemu/qemu_command.h
index 1e6b23ba05..4b1c2103c5 100644
--- a/src/qemu/qemu_command.h
+++ b/src/qemu/qemu_command.h
@@ -43,6 +43,10 @@
 VIR_ENUM_DECL(qemuVideo);
 VIR_ENUM_DECL(qemuSoundCodec);
 
+typedef enum {
+    QEMU_BUILD_COMMANDLINE_VALIDATE_KEEP_JSON = 1 << 0,
+} qemuBuildCommandLineFlags;
+
 virCommandPtr qemuBuildCommandLine(virQEMUDriverPtr driver,
                                    virLogManagerPtr logManager,
                                    virSecurityManagerPtr secManager,
@@ -53,7 +57,8 @@ virCommandPtr qemuBuildCommandLine(virQEMUDriverPtr driver,
                                    bool standalone,
                                    bool enableFips,
                                    size_t *nnicindexes,
-                                   int **nicindexes);
+                                   int **nicindexes,
+                                   unsigned int flags);
 
 /* Generate the object properties for pr-manager */
 virJSONValuePtr qemuBuildPRManagerInfoProps(virStorageSourcePtr src);
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 2b24881f75..b3ebf3aa9b 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -7449,7 +7449,7 @@ static char *qemuConnectDomainXMLToNative(virConnectPtr conn,
     }
 
     if (!(cmd = qemuProcessCreatePretendCmd(driver, vm, NULL,
-                                            qemuCheckFips(), true,
+                                            qemuCheckFips(), true, false,
                                             VIR_QEMU_PROCESS_START_COLD)))
         goto cleanup;
 
diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
index 818a72d8f9..db564cae36 100644
--- a/src/qemu/qemu_process.c
+++ b/src/qemu/qemu_process.c
@@ -6750,7 +6750,7 @@ qemuProcessLaunch(virConnectPtr conn,
                                      snapshot, vmop,
                                      false,
                                      qemuCheckFips(),
-                                     &nnicindexes, &nicindexes)))
+                                     &nnicindexes, &nicindexes, 0)))
         goto cleanup;
 
     if (incoming && incoming->fd != -1)
@@ -7193,8 +7193,11 @@ qemuProcessCreatePretendCmd(virQEMUDriverPtr driver,
                             const char *migrateURI,
                             bool enableFips,
                             bool standalone,
+                            bool jsonPropsValidation,
                             unsigned int flags)
 {
+    unsigned int buildflags = 0;
+
     virCheckFlags(VIR_QEMU_PROCESS_START_COLD |
                   VIR_QEMU_PROCESS_START_PAUSED |
                   VIR_QEMU_PROCESS_START_AUTODESTROY, NULL);
@@ -7204,6 +7207,9 @@ qemuProcessCreatePretendCmd(virQEMUDriverPtr driver,
     if (standalone)
         flags |= VIR_QEMU_PROCESS_START_STANDALONE;
 
+    if (jsonPropsValidation)
+        buildflags = QEMU_BUILD_COMMANDLINE_VALIDATE_KEEP_JSON;
+
     if (qemuProcessInit(driver, vm, NULL, QEMU_ASYNC_JOB_NONE,
                         !!migrateURI, flags) < 0)
         return NULL;
@@ -7222,7 +7228,8 @@ qemuProcessCreatePretendCmd(virQEMUDriverPtr driver,
                                 standalone,
                                 enableFips,
                                 NULL,
-                                NULL);
+                                NULL,
+                                buildflags);
 }
 
 
diff --git a/src/qemu/qemu_process.h b/src/qemu/qemu_process.h
index 3077d3ef9e..15e67b9762 100644
--- a/src/qemu/qemu_process.h
+++ b/src/qemu/qemu_process.h
@@ -101,6 +101,7 @@ virCommandPtr qemuProcessCreatePretendCmd(virQEMUDriverPtr driver,
                                           const char *migrateURI,
                                           bool enableFips,
                                           bool standalone,
+                                          bool jsonPropsValidation,
                                           unsigned int flags);
 
 int qemuProcessInit(virQEMUDriverPtr driver,
diff --git a/src/util/virqemu.c b/src/util/virqemu.c
index 9823ebc14d..321ddeb7e3 100644
--- a/src/util/virqemu.c
+++ b/src/util/virqemu.c
@@ -292,16 +292,23 @@ virQEMUBuildCommandLineJSON(virJSONValuePtr value,
 /**
  * virQEMUBuildNetdevCommandlineFromJSON:
  * @props: JSON properties describing a netdev
+ * @rawjson: don't transform to commandline args, but just stringify json
  *
  * Converts @props into arguments for -netdev including all the quirks and
  * differences between the monitor and command line syntax.
+ *
+ * @rawjson is meant for testing of the schema in the xml2argvtest
  */
 char *
-virQEMUBuildNetdevCommandlineFromJSON(virJSONValuePtr props)
+virQEMUBuildNetdevCommandlineFromJSON(virJSONValuePtr props,
+                                      bool rawjson)
 {
     const char *type = virJSONValueObjectGetString(props, "type");
     g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
 
+    if (rawjson)
+        return virJSONValueToString(props, false);
+
     virBufferAsprintf(&buf, "%s,", type);
 
     if (virQEMUBuildCommandLineJSON(props, &buf, "type", true,
diff --git a/src/util/virqemu.h b/src/util/virqemu.h
index 22f47851df..b1296cb657 100644
--- a/src/util/virqemu.h
+++ b/src/util/virqemu.h
@@ -50,7 +50,8 @@ int virQEMUBuildCommandLineJSON(virJSONValuePtr value,
                                 virQEMUBuildCommandLineJSONArrayFormatFunc array);
 
 char *
-virQEMUBuildNetdevCommandlineFromJSON(virJSONValuePtr props);
+virQEMUBuildNetdevCommandlineFromJSON(virJSONValuePtr props,
+                                      bool rawjson);
 
 int virQEMUBuildObjectCommandlineFromJSON(virBufferPtr buf,
                                           virJSONValuePtr objprops);
diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c
index 762d65f521..2217e2d81e 100644
--- a/tests/qemuxml2argvtest.c
+++ b/tests/qemuxml2argvtest.c
@@ -400,7 +400,8 @@ testCompareXMLToArgvCreateArgs(virQEMUDriverPtr drv,
                                virDomainObjPtr vm,
                                const char *migrateURI,
                                struct testQemuInfo *info,
-                               unsigned int flags)
+                               unsigned int flags,
+                               bool jsonPropsValidation)
 {
     size_t i;
 
@@ -475,6 +476,7 @@ testCompareXMLToArgvCreateArgs(virQEMUDriverPtr drv,
 
     return qemuProcessCreatePretendCmd(drv, vm, migrateURI,
                                        (flags & FLAG_FIPS), false,
+                                       jsonPropsValidation,
                                        VIR_QEMU_PROCESS_START_COLD);
 }
 
@@ -570,7 +572,7 @@ testCompareXMLToArgv(const void *data)
     virResetLastError();
 
     if (!(cmd = testCompareXMLToArgvCreateArgs(&driver, vm, migrateURI, info,
-                                               flags))) {
+                                               flags, false))) {
         if (flags & FLAG_EXPECT_FAILURE)
             goto ok;
         goto cleanup;
-- 
2.23.0.windows.1

