[PATCH 015/103] qemuxml2argvtest: Refactor QAPI schema validation code

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Prevent duplication of code when extending the validator for new
commands. Add a struct describing a command to validate and make the
validation loop a bit more robust to corner cases.

Signed-off-by: Peter Krempa <pkrempa@xxxxxxxxxx>
---
 tests/qemuxml2argvtest.c | 120 +++++++++++++++++++++------------------
 1 file changed, 66 insertions(+), 54 deletions(-)

diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c
index 7c6e3d3dd7..b5fab1178c 100644
--- a/tests/qemuxml2argvtest.c
+++ b/tests/qemuxml2argvtest.c
@@ -487,6 +487,70 @@ testCompareXMLToArgvCreateArgs(virQEMUDriver *drv,
 }


+struct testValidateSchemaCommandData {
+    const char *name;
+    const char *schema;
+};
+
+
+static const struct testValidateSchemaCommandData commands[] = {
+    { "-blockdev", "blockdev-add" },
+    { "-netdev", "netdev_add" },
+    { "-object", "object-add" },
+};
+
+static int
+testCompareXMLToArgvValidateSchemaCommand(GStrv args,
+                                          GHashTable *schema)
+{
+    GStrv arg;
+
+    for (arg = args; *arg; arg++) {
+        const char *curcommand = *arg;
+        const char *curargs = *(arg + 1);
+        size_t i;
+
+        for (i = 0; i < G_N_ELEMENTS(commands); i++) {
+            const struct testValidateSchemaCommandData *command = commands + i;
+            g_auto(virBuffer) debug = VIR_BUFFER_INITIALIZER;
+            g_autoptr(virJSONValue) jsonargs = NULL;
+
+            if (STRNEQ(curcommand, command->name))
+                continue;
+
+            if (!curargs) {
+                VIR_TEST_VERBOSE("expected arguments for command '%s'",
+                                 command->name);
+                return -1;
+            }
+
+            if (*curargs != '{') {
+                /* skip the current validation */
+                VIR_TEST_DEBUG("skipping validation of '%s': argument is not JSON",
+                               command->name);
+                arg++;
+                break;
+            }
+
+            if (!(jsonargs = virJSONValueFromString(curargs)))
+                return -1;
+
+            if (testQEMUSchemaValidateCommand(command->schema, jsonargs,
+                                              schema, false, false, &debug) < 0) {
+                VIR_TEST_VERBOSE("failed to validate '%s %s' against QAPI schema: %s",
+                                 command->name, curargs, virBufferCurrentContent(&debug));
+                return -1;
+            }
+
+            /* don't check the argument twice */
+            arg++;
+        }
+    }
+
+    return 0;
+}
+
+
 static int
 testCompareXMLToArgvValidateSchema(virQEMUDriver *drv,
                                    const char *migrateURI,
@@ -497,7 +561,6 @@ testCompareXMLToArgvValidateSchema(virQEMUDriver *drv,
     g_autoptr(virDomainObj) vm = NULL;
     qemuDomainObjPrivate *priv = NULL;
     size_t nargs = 0;
-    size_t i;
     GHashTable *schema = NULL;
     g_autoptr(virCommand) cmd = NULL;
     unsigned int parseFlags = info->parseFlags;
@@ -542,59 +605,8 @@ testCompareXMLToArgvValidateSchema(virQEMUDriver *drv,
     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, false, false, &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 (*args[i + 1] != '{') {
-                i++;
-                continue;
-            }
-
-            if (!(jsonargs = virJSONValueFromString(args[i + 1])))
-                return -1;
-
-            if (testQEMUSchemaValidateCommand("netdev_add", jsonargs,
-                                              schema, false, false, &debug) < 0) {
-                VIR_TEST_VERBOSE("failed to validate -netdev '%s' against QAPI schema: %s",
-                                 args[i + 1], virBufferCurrentContent(&debug));
-                return -1;
-            }
-
-            i++;
-        } else if (STREQ(args[i], "-object")) {
-
-            if (*args[i + 1] != '{') {
-                i++;
-                continue;
-            }
-
-            if (!(jsonargs = virJSONValueFromString(args[i + 1])))
-                return -1;
-
-            if (testQEMUSchemaValidateCommand("object-add", jsonargs,
-                                              schema, false, false, &debug) < 0) {
-                VIR_TEST_VERBOSE("failed to validate -object '%s' against QAPI schema: %s",
-                                 args[i + 1], virBufferCurrentContent(&debug));
-                return -1;
-            }
-
-            i++;
-        }
-    }
+    if (testCompareXMLToArgvValidateSchemaCommand(args, schema) < 0)
+        return -1;

     return 0;
 }
-- 
2.31.1




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux