Add helper functions to open guest agent connections and a handler for replying to the "guest-sync" command. --- tests/qemumonitortestutils.c | 133 ++++++++++++++++++++++++++++++++++++++++++- tests/qemumonitortestutils.h | 6 ++ 2 files changed, 137 insertions(+), 2 deletions(-) diff --git a/tests/qemumonitortestutils.c b/tests/qemumonitortestutils.c index 6dc430e..96cb054 100644 --- a/tests/qemumonitortestutils.c +++ b/tests/qemumonitortestutils.c @@ -28,6 +28,7 @@ #include "virthread.h" #include "qemu/qemu_monitor.h" +#include "qemu/qemu_agent.h" #include "rpc/virnetsocket.h" #include "viralloc.h" #include "virlog.h" @@ -68,6 +69,7 @@ struct _qemuMonitorTest { virNetSocketPtr client; qemuMonitorPtr mon; + qemuAgentPtr agent; char *tmpdir; @@ -118,7 +120,7 @@ qemuMonitorTestAddReponse(qemuMonitorTestPtr test, static int qemuMonitorTestAddUnexpectedErrorResponse(qemuMonitorTestPtr test) { - if (test->json) { + if (test->agent || test->json) { return qemuMonitorTestAddReponse(test, "{ \"error\": " " { \"desc\": \"Unexpected command\", " @@ -312,6 +314,11 @@ qemuMonitorTestFree(qemuMonitorTestPtr test) qemuMonitorClose(test->mon); } + if (test->agent) { + virObjectUnlock(test->agent); + qemuAgentClose(test->agent); + } + virObjectUnref(test->vm); if (test->running) @@ -387,7 +394,7 @@ qemuMonitorTestProcessCommandDefault(qemuMonitorTestPtr test, char *tmp; int ret = -1; - if (test->json) { + if (test->agent || test->json) { if (!(val = virJSONValueFromString(cmdstr))) return -1; @@ -442,6 +449,72 @@ qemuMonitorTestAddItem(qemuMonitorTestPtr test, } +static int +qemuMonitorTestProcessGuestAgentSync(qemuMonitorTestPtr test, + qemuMonitorTestItemPtr item ATTRIBUTE_UNUSED, + const char *cmdstr) +{ + virJSONValuePtr val = NULL; + virJSONValuePtr args; + unsigned long long id; + const char *cmdname; + char *retmsg = NULL; + int ret = -1; + + if (!(val = virJSONValueFromString(cmdstr))) + return -1; + + if (!(cmdname = virJSONValueObjectGetString(val, "execute"))) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + "Missing guest-sync command name"); + goto cleanup; + } + + if (STRNEQ(cmdname, "guest-sync")) { + ret = qemuMonitorTestAddUnexpectedErrorResponse(test); + goto cleanup; + } + + if (!(args = virJSONValueObjectGet(val, "arguments"))) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + "Missing arguments for guest-sync"); + goto cleanup; + } + + if (virJSONValueObjectGetNumberUlong(args, "id", &id)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + "Missing id for guest sync"); + goto cleanup; + } + + if (virAsprintf(&retmsg, "{\"return\":%llu}", id) < 0) + goto cleanup; + + + ret = qemuMonitorTestAddReponse(test, retmsg); + +cleanup: + virJSONValueFree(val); + VIR_FREE(retmsg); + return ret; +} + + +int +qemuMonitorTestAddAgentSyncResponse(qemuMonitorTestPtr test) +{ + if (!test->agent) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + "This test is not an agent test"); + return -1; + } + + return qemuMonitorTestAddHandler(test, + qemuMonitorTestProcessGuestAgentSync, + NULL, NULL); +} + + static void qemuMonitorTestEOFNotify(qemuMonitorPtr mon ATTRIBUTE_UNUSED, virDomainObjPtr vm ATTRIBUTE_UNUSED) @@ -462,6 +535,24 @@ static qemuMonitorCallbacks qemuMonitorTestCallbacks = { }; +static void +qemuMonitorTestAgentEOFNotify(qemuAgentPtr agent ATTRIBUTE_UNUSED, + virDomainObjPtr vm ATTRIBUTE_UNUSED) +{ +} + +static void +qemuMonitorTestAgentErrorNotify(qemuAgentPtr agent ATTRIBUTE_UNUSED, + virDomainObjPtr vm ATTRIBUTE_UNUSED) +{ +} + +static qemuAgentCallbacks qemuMonitorTestAgentCallbacks = { + .eofNotify = qemuMonitorTestAgentEOFNotify, + .errorNotify = qemuMonitorTestAgentErrorNotify, +}; + + static qemuMonitorTestPtr qemuMonitorCommonTestNew(virDomainXMLOptionPtr xmlopt, virDomainChrSourceDefPtr src) @@ -594,12 +685,50 @@ qemuMonitorTestNew(bool json, virDomainXMLOptionPtr xmlopt) return test; error: + virDomainChrSourceDefClear(&src); + qemuMonitorTestFree(test); + return NULL; +} + +qemuMonitorTestPtr +qemuMonitorTestNewAgent(virDomainXMLOptionPtr xmlopt) +{ + qemuMonitorTestPtr test = NULL; + virDomainChrSourceDef src; + + if (!(test = qemuMonitorCommonTestNew(xmlopt, &src))) + goto error; + + if (!(test->agent = qemuAgentOpen(test->vm, + &src, + &qemuMonitorTestAgentCallbacks))) + goto error; + + virObjectLock(test->agent); + + if (qemuMonitorCommonTestInit(test) < 0) + goto error; + + virDomainChrSourceDefClear(&src); + + return test; + +error: + virDomainChrSourceDefClear(&src); qemuMonitorTestFree(test); return NULL; } + qemuMonitorPtr qemuMonitorTestGetMonitor(qemuMonitorTestPtr test) { return test->mon; } + + +qemuAgentPtr +qemuMonitorTestGetAgent(qemuMonitorTestPtr test) +{ + return test->agent; +} diff --git a/tests/qemumonitortestutils.h b/tests/qemumonitortestutils.h index 9df118f..4307feb 100644 --- a/tests/qemumonitortestutils.h +++ b/tests/qemumonitortestutils.h @@ -22,6 +22,7 @@ # include "domain_conf.h" # include "qemu/qemu_monitor.h" +# include "qemu/qemu_agent.h" typedef struct _qemuMonitorTest qemuMonitorTest; typedef qemuMonitorTest *qemuMonitorTestPtr; @@ -31,12 +32,17 @@ qemuMonitorTestAddItem(qemuMonitorTestPtr test, const char *command_name, const char *response); +int qemuMonitorTestAddAgentSyncResponse(qemuMonitorTestPtr test); + qemuMonitorTestPtr qemuMonitorTestNew(bool json, virDomainXMLOptionPtr xmlopt); +qemuMonitorTestPtr qemuMonitorTestNewAgent(virDomainXMLOptionPtr xmlopt); + void qemuMonitorTestFree(qemuMonitorTestPtr test); qemuMonitorPtr qemuMonitorTestGetMonitor(qemuMonitorTestPtr test); +qemuAgentPtr qemuMonitorTestGetAgent(qemuMonitorTestPtr test); #endif /* __VIR_QEMU_MONITOR_TEST_UTILS_H__ */ -- 1.8.3.2 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list