On 09/27/2016 12:39 PM, Peter Krempa wrote:
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 | 112 +++++++++++++++++++++++++++++++++++++++++++
tests/qemumonitortestutils.h | 6 +++
2 files changed, 118 insertions(+)
diff --git a/tests/qemumonitortestutils.c b/tests/qemumonitortestutils.c
index c86a27a..50bf174 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,
}
+
Technically extra line, but I'll assume it's making possible for the 2
lines between functions...
/* this allows to add a responder that is able to check
* a (shallow) structure of arguments for a command */
int
@@ -721,6 +724,115 @@ 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;
+ }
+
+
+ VIR_FREE(argstr);
This one's duplicitous since we fall through to 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++;
+ }
Would there ever be a time when apostrophe would be false?
ACK with at least the duplicitous free resolved. Your call on the above
loop.
John
+ }
+
+ 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 8e2f371..3890cd4 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)