From 030b1caa13b43065a23c87ddb5f2d65ffd9babe5 Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Tue, 27 Sep 2016 17:11:55 +0200 Subject: [PATCH] tests: qemu: Add support for testing aguments on monitor verbatim Add code that takes a string and matches it against the data passed as arguments from qemu. This is a simpler version of qemuMonitorTestAddItemParams. --- tests/qemumonitortestutils.c | 109 +++++++++++++++++++++++++++++++++++ tests/qemumonitortestutils.h | 6 ++ 2 files changed, 115 insertions(+) diff --git a/tests/qemumonitortestutils.c b/tests/qemumonitortestutils.c index c86a27adce..f8f23c3d72 100644 --- a/tests/qemumonitortestutils.c +++ b/tests/qemumonitortestutils.c @@ -439,6 +439,7 @@ struct qemuMonitorTestHandlerData { char *response; size_t nargs; qemuMonitorTestCommandArgsPtr args; + char *expectArgs; }; static void @@ -458,6 +459,7 @@ qemuMonitorTestHandlerDataFree(void *opaque) VIR_FREE(data->command_name); VIR_FREE(data->response); VIR_FREE(data->args); + VIR_FREE(data->expectArgs); VIR_FREE(data); } @@ -668,6 +670,7 @@ qemuMonitorTestProcessCommandWithArgs(qemuMonitorTestPtr test, } + /* this allows to add a responder that is able to check * a (shallow) structure of arguments for a command */ int @@ -721,6 +724,112 @@ qemuMonitorTestAddItemParams(qemuMonitorTestPtr test, } +static int +qemuMonitorTestProcessCommandWithArgStr(qemuMonitorTestPtr test, + qemuMonitorTestItemPtr item, + const char *cmdstr) +{ + struct qemuMonitorTestHandlerData *data = item->opaque; + virJSONValuePtr val = NULL; + virJSONValuePtr args; + char *argstr = NULL; + const char *cmdname; + int ret = -1; + + if (!(val = virJSONValueFromString(cmdstr))) + return -1; + + if (!(cmdname = virJSONValueObjectGetString(val, "execute"))) { + ret = qemuMonitorReportError(test, "Missing command name in %s", cmdstr); + goto cleanup; + } + + if (STRNEQ(data->command_name, cmdname)) { + ret = qemuMonitorTestAddUnexpectedErrorResponse(test); + goto cleanup; + } + + if (!(args = virJSONValueObjectGet(val, "arguments"))) { + ret = qemuMonitorReportError(test, + "Missing arguments section for command '%s'", + data->command_name); + goto cleanup; + } + + /* convert the arguments to string */ + if (!(argstr = virJSONValueToString(args, false))) + goto cleanup; + + /* verify that the argument value is expected */ + if (STRNEQ(argstr, data->expectArgs)) { + ret = qemuMonitorReportError(test, + "%s: expected arguments: '%s', got: '%s'", + data->command_name, + data->expectArgs, argstr); + goto cleanup; + } + + /* arguments checked out, return the response */ + ret = qemuMonitorTestAddResponse(test, data->response); + + cleanup: + VIR_FREE(argstr); + virJSONValueFree(val); + return ret; +} + + +/** + * qemuMonitorTestAddItemExpect: + * + * @test: test monitor object + * @cmdname: command name + * @cmdargs: expected arguments of the command + * @apostrophe: convert apostrophes (') in @cmdargs to quotes (") + * @response: simulated response of the command + * + * Simulates a qemu monitor command. Checks that the 'arguments' of the qmp + * command are expected. If @apostrophe is true apostrophes are converted to + * quotes for simplification of writing the strings into code. + */ +int +qemuMonitorTestAddItemExpect(qemuMonitorTestPtr test, + const char *cmdname, + const char *cmdargs, + bool apostrophe, + const char *response) +{ + struct qemuMonitorTestHandlerData *data; + + if (VIR_ALLOC(data) < 0) + goto error; + + if (VIR_STRDUP(data->command_name, cmdname) < 0 || + VIR_STRDUP(data->response, response) < 0 || + VIR_STRDUP(data->expectArgs, cmdargs) < 0) + goto error; + + if (apostrophe) { + char *tmp = data->expectArgs; + + while (*tmp != '\0') { + if (*tmp == '\'') + *tmp = '"'; + + tmp++; + } + } + + return qemuMonitorTestAddHandler(test, + qemuMonitorTestProcessCommandWithArgStr, + data, qemuMonitorTestHandlerDataFree); + + error: + qemuMonitorTestHandlerDataFree(data); + return -1; +} + + static void qemuMonitorTestEOFNotify(qemuMonitorPtr mon ATTRIBUTE_UNUSED, virDomainObjPtr vm ATTRIBUTE_UNUSED, diff --git a/tests/qemumonitortestutils.h b/tests/qemumonitortestutils.h index 8e2f371d7b..3890cd46fd 100644 --- a/tests/qemumonitortestutils.h +++ b/tests/qemumonitortestutils.h @@ -60,6 +60,12 @@ int qemuMonitorTestAddItemParams(qemuMonitorTestPtr test, ...) ATTRIBUTE_SENTINEL; +int qemuMonitorTestAddItemExpect(qemuMonitorTestPtr test, + const char *cmdname, + const char *cmdargs, + bool apostrophe, + const char *response); + # define qemuMonitorTestNewSimple(json, xmlopt) \ qemuMonitorTestNew(json, xmlopt, NULL, NULL, NULL)