From debc33ef84d348d08d526c7891b5cd93a7517408 Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Fri, 24 Sep 2021 13:28:57 +0200 Subject: [PATCH] qemuxml2argvtest: Refactor QAPI schema validation code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-by: Ján Tomko --- tests/qemuxml2argvtest.c | 118 +++++++++++++++++++++------------------ 1 file changed, 64 insertions(+), 54 deletions(-) diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c index 12ce6d9559..b5d4e6aa13 100644 --- a/tests/qemuxml2argvtest.c +++ b/tests/qemuxml2argvtest.c @@ -487,6 +487,68 @@ 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 != '{') { + 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; + } + + arg++; + } + } + + return 0; +} + + static int testCompareXMLToArgvValidateSchema(virQEMUDriver *drv, const char *migrateURI, @@ -497,7 +559,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 +603,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; }