mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-02-22 17:57:43 +03:00
qemumonitortestutils: Improve error reporting from mock qemu monitor
Use the JSON error messages to report errors back to the caller in addition to erroring out. The error reported from the event loop from the mock function of the monitor was later overwritten by the call to the monitor/agent interaction API. This will also allow testing of error reporting.
This commit is contained in:
parent
659fe6d2db
commit
69441df4d0
@ -97,6 +97,8 @@ qemuMonitorTestAddReponse(qemuMonitorTestPtr test,
|
|||||||
size_t want = strlen(response) + 2;
|
size_t want = strlen(response) + 2;
|
||||||
size_t have = test->outgoingCapacity - test->outgoingLength;
|
size_t have = test->outgoingCapacity - test->outgoingLength;
|
||||||
|
|
||||||
|
VIR_DEBUG("Adding response to monitor command: '%s", response);
|
||||||
|
|
||||||
if (have < want) {
|
if (have < want) {
|
||||||
size_t need = want - have;
|
size_t need = want - have;
|
||||||
if (VIR_EXPAND_N(test->outgoing, test->outgoingCapacity, need) < 0)
|
if (VIR_EXPAND_N(test->outgoing, test->outgoingCapacity, need) < 0)
|
||||||
@ -111,7 +113,7 @@ qemuMonitorTestAddReponse(qemuMonitorTestPtr test,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int
|
int
|
||||||
qemuMonitorTestAddUnexpectedErrorResponse(qemuMonitorTestPtr test)
|
qemuMonitorTestAddUnexpectedErrorResponse(qemuMonitorTestPtr test)
|
||||||
{
|
{
|
||||||
if (test->agent || test->json) {
|
if (test->agent || test->json) {
|
||||||
@ -125,12 +127,48 @@ qemuMonitorTestAddUnexpectedErrorResponse(qemuMonitorTestPtr test)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int ATTRIBUTE_FMT_PRINTF(2, 3)
|
||||||
|
qemuMonitorReportError(qemuMonitorTestPtr test, const char *errmsg, ...)
|
||||||
|
{
|
||||||
|
va_list msgargs;
|
||||||
|
char *msg = NULL;
|
||||||
|
char *jsonmsg = NULL;
|
||||||
|
int ret = -1;
|
||||||
|
|
||||||
|
va_start(msgargs, errmsg);
|
||||||
|
|
||||||
|
if (virVasprintf(&msg, errmsg, msgargs) < 0)
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
if (test->agent || test->json) {
|
||||||
|
if (virAsprintf(&jsonmsg, "{ \"error\": "
|
||||||
|
" { \"desc\": \"%s\", "
|
||||||
|
" \"class\": \"UnexpectedCommand\" } }",
|
||||||
|
msg) < 0)
|
||||||
|
goto cleanup;
|
||||||
|
} else {
|
||||||
|
if (virAsprintf(&jsonmsg, "error: '%s'", msg) < 0)
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = qemuMonitorTestAddReponse(test, jsonmsg);
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
va_end(msgargs);
|
||||||
|
VIR_FREE(msg);
|
||||||
|
VIR_FREE(jsonmsg);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
qemuMonitorTestProcessCommand(qemuMonitorTestPtr test,
|
qemuMonitorTestProcessCommand(qemuMonitorTestPtr test,
|
||||||
const char *cmdstr)
|
const char *cmdstr)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
|
VIR_DEBUG("Processing string from monitor handler: '%s", cmdstr);
|
||||||
|
|
||||||
if (test->nitems == 0) {
|
if (test->nitems == 0) {
|
||||||
return qemuMonitorTestAddUnexpectedErrorResponse(test);
|
return qemuMonitorTestAddUnexpectedErrorResponse(test);
|
||||||
} else {
|
} else {
|
||||||
@ -399,8 +437,7 @@ qemuMonitorTestProcessCommandDefault(qemuMonitorTestPtr test,
|
|||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if (!(cmdname = virJSONValueObjectGetString(val, "execute"))) {
|
if (!(cmdname = virJSONValueObjectGetString(val, "execute"))) {
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR,
|
ret = qemuMonitorReportError(test, "Missing command name in %s", cmdstr);
|
||||||
"Missing command name in %s", cmdstr);
|
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -410,8 +447,9 @@ qemuMonitorTestProcessCommandDefault(qemuMonitorTestPtr test,
|
|||||||
cmdname = cmdcopy;
|
cmdname = cmdcopy;
|
||||||
|
|
||||||
if (!(tmp = strchr(cmdcopy, ' '))) {
|
if (!(tmp = strchr(cmdcopy, ' '))) {
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR,
|
ret = qemuMonitorReportError(test,
|
||||||
"Cannot find command name in '%s'", cmdstr);
|
"Cannot find command name in '%s'",
|
||||||
|
cmdstr);
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
*tmp = '\0';
|
*tmp = '\0';
|
||||||
@ -465,8 +503,7 @@ qemuMonitorTestProcessGuestAgentSync(qemuMonitorTestPtr test,
|
|||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if (!(cmdname = virJSONValueObjectGetString(val, "execute"))) {
|
if (!(cmdname = virJSONValueObjectGetString(val, "execute"))) {
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
ret = qemuMonitorReportError(test, "Missing guest-sync command name");
|
||||||
"Missing guest-sync command name");
|
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -476,14 +513,12 @@ qemuMonitorTestProcessGuestAgentSync(qemuMonitorTestPtr test,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!(args = virJSONValueObjectGet(val, "arguments"))) {
|
if (!(args = virJSONValueObjectGet(val, "arguments"))) {
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
ret = qemuMonitorReportError(test, "Missing arguments for guest-sync");
|
||||||
"Missing arguments for guest-sync");
|
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (virJSONValueObjectGetNumberUlong(args, "id", &id)) {
|
if (virJSONValueObjectGetNumberUlong(args, "id", &id)) {
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
ret = qemuMonitorReportError(test, "Missing id for guest sync");
|
||||||
"Missing id for guest sync");
|
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,8 +41,11 @@ int qemuMonitorTestAddHandler(qemuMonitorTestPtr test,
|
|||||||
int qemuMonitorTestAddReponse(qemuMonitorTestPtr test,
|
int qemuMonitorTestAddReponse(qemuMonitorTestPtr test,
|
||||||
const char *response);
|
const char *response);
|
||||||
|
|
||||||
|
int qemuMonitorTestAddUnexpectedErrorResponse(qemuMonitorTestPtr test);
|
||||||
|
|
||||||
void *qemuMonitorTestItemGetPrivateData(qemuMonitorTestItemPtr item);
|
void *qemuMonitorTestItemGetPrivateData(qemuMonitorTestItemPtr item);
|
||||||
|
|
||||||
|
int qemuMonitorReportError(qemuMonitorTestPtr test, const char *errmsg, ...);
|
||||||
|
|
||||||
int qemuMonitorTestAddItem(qemuMonitorTestPtr test,
|
int qemuMonitorTestAddItem(qemuMonitorTestPtr test,
|
||||||
const char *command_name,
|
const char *command_name,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user