[libvirt] [PATCH 0/4] tests: qemu: Allow QMP schema testing with qemuMonitorTestProcessCommandVerbatim

Allow more tests to be run against QMP schema checker. Peter Krempa (4): tests: Refactor cleanup in qemuMonitorTestProcessCommandVerbatim tests: qemu: Add QMP schema checking in qemuMonitorTestProcessCommandVerbatim tests: Allow QMP schema testing in qemuMonitorTestNewFromFileFull tests: qemuhotplug: Use schema testing with qemuMonitorTestNewFromFileFull tests/qemucapabilitiestest.c | 3 ++- tests/qemuhotplugtest.c | 21 +++++++++++++++++---- tests/qemumonitortestutils.c | 34 +++++++++++++++++++++++++--------- tests/qemumonitortestutils.h | 3 ++- 4 files changed, 46 insertions(+), 15 deletions(-) -- 2.21.0

Use VIR_AUTOFREE and get rid of the cleanup section. Signed-off-by: Peter Krempa <pkrempa@redhat.com> --- tests/qemumonitortestutils.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/tests/qemumonitortestutils.c b/tests/qemumonitortestutils.c index 4a108c382d..1cd35830af 100644 --- a/tests/qemumonitortestutils.c +++ b/tests/qemumonitortestutils.c @@ -688,8 +688,8 @@ qemuMonitorTestProcessCommandVerbatim(qemuMonitorTestPtr test, const char *cmdstr) { struct qemuMonitorTestHandlerData *data = item->opaque; - char *reformatted = NULL; - char *errmsg = NULL; + VIR_AUTOFREE(char *) reformatted = NULL; + VIR_AUTOFREE(char *) errmsg = NULL; int ret = -1; /* JSON strings will be reformatted to simplify checking */ @@ -705,7 +705,7 @@ qemuMonitorTestProcessCommandVerbatim(qemuMonitorTestPtr test, } else { if (data->cmderr) { if (virAsprintf(&errmsg, "%s: %s", data->cmderr, cmdstr) < 0) - goto cleanup; + return -1; ret = qemuMonitorTestAddErrorResponse(test, errmsg); } else { @@ -715,9 +715,6 @@ qemuMonitorTestProcessCommandVerbatim(qemuMonitorTestPtr test, } } - cleanup: - VIR_FREE(errmsg); - VIR_FREE(reformatted); return ret; } -- 2.21.0

In case when we are testing a QMP command we can try to schema check it so that we catch inconsistencies. Signed-off-by: Peter Krempa <pkrempa@redhat.com> --- tests/qemumonitortestutils.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/tests/qemumonitortestutils.c b/tests/qemumonitortestutils.c index 1cd35830af..0de9048243 100644 --- a/tests/qemumonitortestutils.c +++ b/tests/qemumonitortestutils.c @@ -690,14 +690,30 @@ qemuMonitorTestProcessCommandVerbatim(qemuMonitorTestPtr test, struct qemuMonitorTestHandlerData *data = item->opaque; VIR_AUTOFREE(char *) reformatted = NULL; VIR_AUTOFREE(char *) errmsg = NULL; + VIR_AUTOPTR(virJSONValue) json = NULL; + virJSONValuePtr cmdargs; + const char *cmdname; int ret = -1; + int rc; /* JSON strings will be reformatted to simplify checking */ if (test->json || test->agent) { - if (!(reformatted = virJSONStringReformat(cmdstr, false))) + if (!(json = virJSONValueFromString(cmdstr)) || + !(reformatted = virJSONValueToString(json, false))) return -1; cmdstr = reformatted; + + /* in this case we do a best-effort schema check if we can find the command */ + if ((cmdname = virJSONValueObjectGetString(json, "execute"))) { + cmdargs = virJSONValueObjectGet(json, "arguments"); + + if ((rc = qemuMonitorTestProcessCommandDefaultValidate(test, cmdname, cmdargs)) < 0) + return -1; + + if (rc == 1) + return 0; + } } if (STREQ(data->command_name, cmdstr)) { -- 2.21.0

Pass in the schema data from the caller if QMP schema testing is desired. Signed-off-by: Peter Krempa <pkrempa@redhat.com> --- tests/qemucapabilitiestest.c | 3 ++- tests/qemuhotplugtest.c | 2 +- tests/qemumonitortestutils.c | 7 +++++-- tests/qemumonitortestutils.h | 3 ++- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/tests/qemucapabilitiestest.c b/tests/qemucapabilitiestest.c index ac9ab6bfce..67f57a4fdc 100644 --- a/tests/qemucapabilitiestest.c +++ b/tests/qemucapabilitiestest.c @@ -82,7 +82,8 @@ testQemuCaps(const void *opaque) data->dataDir, data->base, data->archName) < 0) goto cleanup; - if (!(mon = qemuMonitorTestNewFromFileFull(repliesFile, &data->driver, NULL))) + if (!(mon = qemuMonitorTestNewFromFileFull(repliesFile, &data->driver, NULL, + NULL))) goto cleanup; if (qemuProcessQMPInitMonitor(qemuMonitorTestGetMonitor(mon)) < 0) diff --git a/tests/qemuhotplugtest.c b/tests/qemuhotplugtest.c index fbf488c54c..08f00b9c72 100644 --- a/tests/qemuhotplugtest.c +++ b/tests/qemuhotplugtest.c @@ -435,7 +435,7 @@ testQemuHotplugCpuPrepare(const char *test, virQEMUCapsSet(priv->qemuCaps, QEMU_CAPS_QUERY_HOTPLUGGABLE_CPUS); if (!(data->mon = qemuMonitorTestNewFromFileFull(data->file_json_monitor, - &driver, data->vm))) + &driver, data->vm, NULL))) goto error; priv->mon = qemuMonitorTestGetMonitor(data->mon); diff --git a/tests/qemumonitortestutils.c b/tests/qemumonitortestutils.c index 0de9048243..8d7c503c6e 100644 --- a/tests/qemumonitortestutils.c +++ b/tests/qemumonitortestutils.c @@ -1396,6 +1396,7 @@ qemuMonitorTestFullAddItem(qemuMonitorTestPtr test, * @fileName: File name to load monitor replies from * @driver: qemu driver object * @vm: domain object (may be null if it's not needed by the test) + * @qmpschema: QMP schema data hash table if QMP checking is required * * Create a JSON test monitor simulator object and fill it with expected command * sequence and replies specified in @fileName. @@ -1409,7 +1410,8 @@ qemuMonitorTestFullAddItem(qemuMonitorTestPtr test, qemuMonitorTestPtr qemuMonitorTestNewFromFileFull(const char *fileName, virQEMUDriverPtr driver, - virDomainObjPtr vm) + virDomainObjPtr vm, + virHashTablePtr qmpschema) { qemuMonitorTestPtr ret = NULL; char *jsonstr = NULL; @@ -1423,7 +1425,8 @@ qemuMonitorTestNewFromFileFull(const char *fileName, if (virTestLoadFile(fileName, &jsonstr) < 0) return NULL; - if (!(ret = qemuMonitorTestNew(true, driver->xmlopt, vm, driver, NULL, NULL))) + if (!(ret = qemuMonitorTestNew(true, driver->xmlopt, vm, driver, NULL, + qmpschema))) goto cleanup; tmp = jsonstr; diff --git a/tests/qemumonitortestutils.h b/tests/qemumonitortestutils.h index d2520e08a4..8461c80caa 100644 --- a/tests/qemumonitortestutils.h +++ b/tests/qemumonitortestutils.h @@ -90,7 +90,8 @@ qemuMonitorTestPtr qemuMonitorTestNewFromFile(const char *fileName, bool simple); qemuMonitorTestPtr qemuMonitorTestNewFromFileFull(const char *fileName, virQEMUDriverPtr driver, - virDomainObjPtr vm); + virDomainObjPtr vm, + virHashTablePtr qmpschema); qemuMonitorTestPtr qemuMonitorTestNewAgent(virDomainXMLOptionPtr xmlopt); -- 2.21.0

Pass in the schema since it works with the 'file' test now. Signed-off-by: Peter Krempa <pkrempa@redhat.com> --- tests/qemuhotplugtest.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/tests/qemuhotplugtest.c b/tests/qemuhotplugtest.c index 08f00b9c72..bfbf32baa4 100644 --- a/tests/qemuhotplugtest.c +++ b/tests/qemuhotplugtest.c @@ -27,6 +27,7 @@ #include "qemumonitortestutils.h" #include "testutils.h" #include "testutilsqemu.h" +#include "testutilsqemuschema.h" #include "virerror.h" #include "virstring.h" #include "virthread.h" @@ -394,7 +395,8 @@ testQemuHotplugCpuDataFree(struct testQemuHotplugCpuData *data) static struct testQemuHotplugCpuData * testQemuHotplugCpuPrepare(const char *test, - bool modern) + bool modern, + virHashTablePtr qmpschema) { qemuDomainObjPrivatePtr priv = NULL; virCapsPtr caps = NULL; @@ -435,7 +437,7 @@ testQemuHotplugCpuPrepare(const char *test, virQEMUCapsSet(priv->qemuCaps, QEMU_CAPS_QUERY_HOTPLUGGABLE_CPUS); if (!(data->mon = qemuMonitorTestNewFromFileFull(data->file_json_monitor, - &driver, data->vm, NULL))) + &driver, data->vm, qmpschema))) goto error; priv->mon = qemuMonitorTestGetMonitor(data->mon); @@ -499,6 +501,7 @@ struct testQemuHotplugCpuParams { bool state; bool modern; bool fail; + virHashTablePtr schema; }; @@ -510,7 +513,8 @@ testQemuHotplugCpuGroup(const void *opaque) int ret = -1; int rc; - if (!(data = testQemuHotplugCpuPrepare(params->test, params->modern))) + if (!(data = testQemuHotplugCpuPrepare(params->test, params->modern, + params->schema))) return -1; rc = qemuDomainSetVcpusInternal(&driver, data->vm, data->vm->def, @@ -546,7 +550,8 @@ testQemuHotplugCpuIndividual(const void *opaque) int ret = -1; int rc; - if (!(data = testQemuHotplugCpuPrepare(params->test, params->modern))) + if (!(data = testQemuHotplugCpuPrepare(params->test, params->modern, + params->schema))) return -1; if (virBitmapParse(params->cpumap, &map, 128) < 0) @@ -580,6 +585,7 @@ testQemuHotplugCpuIndividual(const void *opaque) static int mymain(void) { + VIR_AUTOPTR(virHashTable) qmpschema = NULL; int ret = 0; struct qemuHotplugTestData data = {0}; struct testQemuHotplugCpuParams cpudata; @@ -604,6 +610,13 @@ mymain(void) if (!(driver.domainEventState = virObjectEventStateNew())) return EXIT_FAILURE; + if (!(qmpschema = testQEMUSchemaLoad())) { + VIR_TEST_VERBOSE("failed to load qapi schema\n"); + return EXIT_FAILURE; + } + + cpudata.schema = qmpschema; + driver.lockManager = virLockManagerPluginNew("nop", "qemu", driver.config->configBaseDir, 0); -- 2.21.0

On 6/7/19 7:01 AM, Peter Krempa wrote:
Allow more tests to be run against QMP schema checker.
Peter Krempa (4): tests: Refactor cleanup in qemuMonitorTestProcessCommandVerbatim tests: qemu: Add QMP schema checking in qemuMonitorTestProcessCommandVerbatim tests: Allow QMP schema testing in qemuMonitorTestNewFromFileFull tests: qemuhotplug: Use schema testing with qemuMonitorTestNewFromFileFull
Series: Reviewed-by: Eric Blake <eblake@redhat.com>
tests/qemucapabilitiestest.c | 3 ++- tests/qemuhotplugtest.c | 21 +++++++++++++++++---- tests/qemumonitortestutils.c | 34 +++++++++++++++++++++++++--------- tests/qemumonitortestutils.h | 3 ++- 4 files changed, 46 insertions(+), 15 deletions(-)
-- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org
participants (2)
-
Eric Blake
-
Peter Krempa