Add infrastructure that will allow testing schema of the commands we
pass to the fake monitor object, so that we can make sure that it
actually does something.
Signed-off-by: Peter Krempa
---
tests/Makefile.am| 2 +-
tests/qemuhotplugtest.c | 3 +-
tests/qemumonitortestutils.c | 85 ++--
tests/qemumonitortestutils.h | 7 ++--
4 files changed, 90 insertions(+), 7 deletions(-)
diff --git a/tests/Makefile.am b/tests/Makefile.am
index cf254f65c3..289ef35bdd 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -555,6 +555,7 @@ endif ! WITH_LIBXL
QEMUMONITORTESTUTILS_SOURCES = \
qemumonitortestutils.c \
qemumonitortestutils.h \
+ testutilsqemuschema.h testutilsqemuschema.c \
$(NULL)
if WITH_QEMU
@@ -616,7 +617,6 @@ qemumonitorjsontest_SOURCES = \
qemumonitorjsontest.c \
testutils.c testutils.h \
testutilsqemu.c testutilsqemu.h \
- testutilsqemuschema.c testutilsqemuschema.h \
$(NULL)
qemumonitorjsontest_LDADD = libqemumonitortestutils.la \
$(qemu_LDADDS) $(LDADDS)
diff --git a/tests/qemuhotplugtest.c b/tests/qemuhotplugtest.c
index d42f8e12cb..85e53653e1 100644
--- a/tests/qemuhotplugtest.c
+++ b/tests/qemuhotplugtest.c
@@ -283,7 +283,8 @@ testQemuHotplug(const void *data)
/* Now is the best time to feed the spoofed monitor with predefined
* replies. */
-if (!(test_mon = qemuMonitorTestNew(true, driver.xmlopt, vm, &driver,
NULL)))
+if (!(test_mon = qemuMonitorTestNew(true, driver.xmlopt, vm, &driver,
+NULL, NULL)))
goto cleanup;
tmp = test->mon;
diff --git a/tests/qemumonitortestutils.c b/tests/qemumonitortestutils.c
index 5e30fb067c..1232b8ebe3 100644
--- a/tests/qemumonitortestutils.c
+++ b/tests/qemumonitortestutils.c
@@ -25,12 +25,14 @@
#include
#include "testutils.h"
+#include "testutilsqemuschema.h"
#include "qemumonitortestutils.h"
#include "virthread.h"
#include "qemu/qemu_processpriv.h"
#include "qemu/qemu_monitor.h"
#include "qemu/qemu_agent.h"
+#include "qemu/qemu_qapi.h"
#include "rpc/virnetsocket.h"
#include "viralloc.h"
#include "virlog.h"
@@ -76,6 +78,7 @@ struct _qemuMonitorTest {
qemuMonitorTestItemPtr *items;
virDomainObjPtr vm;
+virHashTablePtr qapischema;
};
@@ -537,6 +540,67 @@ qemuMonitorTestHandlerDataFree(void *opaque)
VIR_FREE(data);
}
+
+/* Returns -1 on error, 0 if validation was successful/not necessary, 1 if
+ * the validation has failed, and the reply was properly constructed */
+static int
+qemuMonitorTestProcessCommandDefaultValidate(qemuMonitorTestPtr test,
+ const char *cmdname,
+ virJSONValuePtr args)
+{
+virBuffer debug = VIR_BUFFER_INITIALIZER;
+virJSONValuePtr schemaroot;
+virJSONValuePtr emptyargs = NULL;
+char *schemapath = NULL;
+int ret = -1;
+
+if (!test->qapischema || !test->json || test->agent)
+return 0;
+
+/* 'device_add' needs to be skipped as it does not have fully defined
schema */
+if (STREQ(cmdname, "device_add"))
+return 0;
+
+if (virAsprintf(&schemapath, "%s/arg-type", cmdname) < 0)
+goto cleanup;
+
+if (virQEMUQapiSchemaPathGet(schemapath, test->qapischema, &schemaroot) <
0) {
+if (qemuMonitorReportError(test,
+ "command '%s' not found in QAPI schema",
+ cmdname) == 0)
+ret = 1;
+goto cleanup;
+}
+
+if (!args) {
+if (!(emptyargs = virJSONValueNewObject()))
+goto cleanup;
+
+args = emptyargs;
+}
+
+if (testQEMUSchemaValidate(args, schemaroot, test->qapischema, &debug) <
0) {
+char *debugmsg = virBufferContentAndReset(&debug);
+if (qemuMonitorReportError(test,
+ "failed to validate arguments of '%s' "
+ "against QAPI schema: %s",
+ cmdname, debugmsg) == 0)
+ret = 1;
+
+VIR_FREE(debugmsg);
+goto cleanup;
+}
+
+ret = 0;
+
+ cleanup:
+virBufferFreeAndReset(&debug);
+virJSONValueFree(emptyargs);
+VIR_FREE(schemapath);
+return ret;
+}
+
+
static int
qemuMonitorTestProcessCommandDefault(qemuMonitorTestPtr test,
qemuMonitorTestItemPtr item,
@@ -544,10 +608,12 @@ qemuMonitorTestProcessCommandDefault(qemuMonitorTestPtr
test,
{
struct qemuMonitorTestHandlerData *data = item->opaque;
virJSONValuePtr val = NULL;
+virJSONValuePtr cmdargs = NULL;
char *cmdcopy = NULL;
const char *cmdname;
char *tmp;
int ret = -1;
+int rc;
if (test->agent || test->json) {
if (!(val = virJSONValueFromString(cmdstr)))
@@ -557,6 +623,8 @@ qemuMonitorTestProcessCommandDefault(qemuMonitorT