tests: qemumonitor: Allow testing schema for fake monitor interactions

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 <pkrempa@redhat.com>
This commit is contained in:
Peter Krempa 2018-03-22 19:05:26 +01:00
parent a54c82e463
commit 5a96b8d775
4 changed files with 90 additions and 7 deletions

View File

@ -555,6 +555,7 @@ endif ! WITH_LIBXL
QEMUMONITORTESTUTILS_SOURCES = \ QEMUMONITORTESTUTILS_SOURCES = \
qemumonitortestutils.c \ qemumonitortestutils.c \
qemumonitortestutils.h \ qemumonitortestutils.h \
testutilsqemuschema.h testutilsqemuschema.c \
$(NULL) $(NULL)
if WITH_QEMU if WITH_QEMU
@ -616,7 +617,6 @@ qemumonitorjsontest_SOURCES = \
qemumonitorjsontest.c \ qemumonitorjsontest.c \
testutils.c testutils.h \ testutils.c testutils.h \
testutilsqemu.c testutilsqemu.h \ testutilsqemu.c testutilsqemu.h \
testutilsqemuschema.c testutilsqemuschema.h \
$(NULL) $(NULL)
qemumonitorjsontest_LDADD = libqemumonitortestutils.la \ qemumonitorjsontest_LDADD = libqemumonitortestutils.la \
$(qemu_LDADDS) $(LDADDS) $(qemu_LDADDS) $(LDADDS)

View File

@ -283,7 +283,8 @@ testQemuHotplug(const void *data)
/* Now is the best time to feed the spoofed monitor with predefined /* Now is the best time to feed the spoofed monitor with predefined
* replies. */ * replies. */
if (!(test_mon = qemuMonitorTestNew(true, driver.xmlopt, vm, &driver, NULL))) if (!(test_mon = qemuMonitorTestNew(true, driver.xmlopt, vm, &driver,
NULL, NULL)))
goto cleanup; goto cleanup;
tmp = test->mon; tmp = test->mon;

View File

@ -25,12 +25,14 @@
#include <time.h> #include <time.h>
#include "testutils.h" #include "testutils.h"
#include "testutilsqemuschema.h"
#include "qemumonitortestutils.h" #include "qemumonitortestutils.h"
#include "virthread.h" #include "virthread.h"
#include "qemu/qemu_processpriv.h" #include "qemu/qemu_processpriv.h"
#include "qemu/qemu_monitor.h" #include "qemu/qemu_monitor.h"
#include "qemu/qemu_agent.h" #include "qemu/qemu_agent.h"
#include "qemu/qemu_qapi.h"
#include "rpc/virnetsocket.h" #include "rpc/virnetsocket.h"
#include "viralloc.h" #include "viralloc.h"
#include "virlog.h" #include "virlog.h"
@ -76,6 +78,7 @@ struct _qemuMonitorTest {
qemuMonitorTestItemPtr *items; qemuMonitorTestItemPtr *items;
virDomainObjPtr vm; virDomainObjPtr vm;
virHashTablePtr qapischema;
}; };
@ -537,6 +540,67 @@ qemuMonitorTestHandlerDataFree(void *opaque)
VIR_FREE(data); 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 static int
qemuMonitorTestProcessCommandDefault(qemuMonitorTestPtr test, qemuMonitorTestProcessCommandDefault(qemuMonitorTestPtr test,
qemuMonitorTestItemPtr item, qemuMonitorTestItemPtr item,
@ -544,10 +608,12 @@ qemuMonitorTestProcessCommandDefault(qemuMonitorTestPtr test,
{ {
struct qemuMonitorTestHandlerData *data = item->opaque; struct qemuMonitorTestHandlerData *data = item->opaque;
virJSONValuePtr val = NULL; virJSONValuePtr val = NULL;
virJSONValuePtr cmdargs = NULL;
char *cmdcopy = NULL; char *cmdcopy = NULL;
const char *cmdname; const char *cmdname;
char *tmp; char *tmp;
int ret = -1; int ret = -1;
int rc;
if (test->agent || test->json) { if (test->agent || test->json) {
if (!(val = virJSONValueFromString(cmdstr))) if (!(val = virJSONValueFromString(cmdstr)))
@ -557,6 +623,8 @@ qemuMonitorTestProcessCommandDefault(qemuMonitorTestPtr test,
ret = qemuMonitorReportError(test, "Missing command name in %s", cmdstr); ret = qemuMonitorReportError(test, "Missing command name in %s", cmdstr);
goto cleanup; goto cleanup;
} }
cmdargs = virJSONValueObjectGet(val, "arguments");
} else { } else {
if (VIR_STRDUP(cmdcopy, cmdstr) < 0) if (VIR_STRDUP(cmdcopy, cmdstr) < 0)
return -1; return -1;
@ -572,6 +640,14 @@ qemuMonitorTestProcessCommandDefault(qemuMonitorTestPtr test,
*tmp = '\0'; *tmp = '\0';
} }
if ((rc = qemuMonitorTestProcessCommandDefaultValidate(test, cmdname, cmdargs)) < 0)
goto cleanup;
if (rc == 1) {
ret = 0;
goto cleanup;
}
if (data->command_name && STRNEQ(data->command_name, cmdname)) if (data->command_name && STRNEQ(data->command_name, cmdname))
ret = qemuMonitorTestAddInvalidCommandResponse(test, data->command_name, ret = qemuMonitorTestAddInvalidCommandResponse(test, data->command_name,
cmdname); cmdname);
@ -1160,7 +1236,8 @@ qemuMonitorTestNew(bool json,
virDomainXMLOptionPtr xmlopt, virDomainXMLOptionPtr xmlopt,
virDomainObjPtr vm, virDomainObjPtr vm,
virQEMUDriverPtr driver, virQEMUDriverPtr driver,
const char *greeting) const char *greeting,
virHashTablePtr schema)
{ {
qemuMonitorTestPtr test = NULL; qemuMonitorTestPtr test = NULL;
virDomainChrSourceDef src; virDomainChrSourceDef src;
@ -1171,6 +1248,7 @@ qemuMonitorTestNew(bool json,
goto error; goto error;
test->json = json; test->json = json;
test->qapischema = schema;
if (!(test->mon = qemuMonitorOpen(test->vm, if (!(test->mon = qemuMonitorOpen(test->vm,
&src, &src,
json, json,
@ -1249,7 +1327,8 @@ qemuMonitorTestNewFromFile(const char *fileName,
goto error; goto error;
} else { } else {
/* Create new mocked monitor with our greeting */ /* Create new mocked monitor with our greeting */
if (!(test = qemuMonitorTestNew(true, xmlopt, NULL, NULL, singleReply))) if (!(test = qemuMonitorTestNew(true, xmlopt, NULL, NULL,
singleReply, NULL)))
goto error; goto error;
} }
@ -1331,7 +1410,7 @@ qemuMonitorTestNewFromFileFull(const char *fileName,
if (virTestLoadFile(fileName, &jsonstr) < 0) if (virTestLoadFile(fileName, &jsonstr) < 0)
return NULL; return NULL;
if (!(ret = qemuMonitorTestNew(true, driver->xmlopt, vm, driver, NULL))) if (!(ret = qemuMonitorTestNew(true, driver->xmlopt, vm, driver, NULL, NULL)))
goto cleanup; goto cleanup;
tmp = jsonstr; tmp = jsonstr;

View File

@ -74,13 +74,16 @@ int qemuMonitorTestAddItemExpect(qemuMonitorTestPtr test,
const char *response); const char *response);
# define qemuMonitorTestNewSimple(json, xmlopt) \ # define qemuMonitorTestNewSimple(json, xmlopt) \
qemuMonitorTestNew(json, xmlopt, NULL, NULL, NULL) qemuMonitorTestNew(json, xmlopt, NULL, NULL, NULL, NULL)
# define qemuMonitorTestNewSchema(xmlopt, schema) \
qemuMonitorTestNew(true, xmlopt, NULL, NULL, NULL, schema)
qemuMonitorTestPtr qemuMonitorTestNew(bool json, qemuMonitorTestPtr qemuMonitorTestNew(bool json,
virDomainXMLOptionPtr xmlopt, virDomainXMLOptionPtr xmlopt,
virDomainObjPtr vm, virDomainObjPtr vm,
virQEMUDriverPtr driver, virQEMUDriverPtr driver,
const char *greeting); const char *greeting,
virHashTablePtr schema);
qemuMonitorTestPtr qemuMonitorTestNewFromFile(const char *fileName, qemuMonitorTestPtr qemuMonitorTestNewFromFile(const char *fileName,
virDomainXMLOptionPtr xmlopt, virDomainXMLOptionPtr xmlopt,