[libvirt] [PATCH 07/11] tests: qemu: monitor: Add helpers to test full command syntax

Peter Krempa pkrempa at redhat.com
Fri Dec 9 16:08:11 UTC 2016


Add test monitor infrastructure that will test the commands verbatim
rather than trying to do any smart handling.
---
 tests/qemumonitortestutils.c | 89 ++++++++++++++++++++++++++++++++++++++++++++
 tests/qemumonitortestutils.h |  5 +++
 2 files changed, 94 insertions(+)

diff --git a/tests/qemumonitortestutils.c b/tests/qemumonitortestutils.c
index fb4f51c..50042f9 100644
--- a/tests/qemumonitortestutils.c
+++ b/tests/qemumonitortestutils.c
@@ -508,6 +508,7 @@ struct _qemuMonitorTestCommandArgs {

 struct qemuMonitorTestHandlerData {
     char *command_name;
+    char *cmderr;
     char *response;
     size_t nargs;
     qemuMonitorTestCommandArgsPtr args;
@@ -529,6 +530,7 @@ qemuMonitorTestHandlerDataFree(void *opaque)
     }

     VIR_FREE(data->command_name);
+    VIR_FREE(data->cmderr);
     VIR_FREE(data->response);
     VIR_FREE(data->args);
     VIR_FREE(data->expectArgs);
@@ -606,6 +608,93 @@ qemuMonitorTestAddItem(qemuMonitorTestPtr test,


 static int
+qemuMonitorTestProcessCommandVerbatim(qemuMonitorTestPtr test,
+                                      qemuMonitorTestItemPtr item,
+                                      const char *cmdstr)
+{
+    struct qemuMonitorTestHandlerData *data = item->opaque;
+    char *reformatted = NULL;
+    char *errmsg = NULL;
+    int ret = -1;
+
+    /* JSON strings will be reformatted to simplify checking */
+    if (test->json || test->agent) {
+        if (!(reformatted = virJSONStringReformat(cmdstr, false)))
+            return -1;
+
+        cmdstr = reformatted;
+    }
+
+    if (STREQ(data->command_name, cmdstr)) {
+        ret = qemuMonitorTestAddResponse(test, data->response);
+    } else {
+        if (data->cmderr) {
+            if (virAsprintf(&errmsg, "%s: %s", data->cmderr, cmdstr) < 0)
+                goto cleanup;
+
+            ret = qemuMonitorTestAddErrorResponse(test, errmsg);
+        } else {
+            ret = qemuMonitorTestAddInvalidCommandResponse(test,
+                                                           data->command_name,
+                                                           cmdstr);
+        }
+    }
+
+ cleanup:
+    VIR_FREE(errmsg);
+    VIR_FREE(reformatted);
+    return ret;
+}
+
+
+/**
+ * qemuMonitorTestAddItemVerbatim:
+ * @test: monitor test object
+ * @command: full expected command syntax
+ * @cmderr: possible explanation of expected command (may be NULL)
+ * @response: full reply of @command
+ *
+ * Adds a test command for the simulated monitor. The full syntax is checked
+ * as specified in @command. For JSON monitor tests formatting/whitespace is
+ * ignored. If the command on the monitor is not as expected an error containing
+ * @cmderr is returned. Otherwise @response is put as-is on the monitor.
+ *
+ * Returns 0 when command was succesfully added, -1 on error.
+ */
+int
+qemuMonitorTestAddItemVerbatim(qemuMonitorTestPtr test,
+                               const char *command,
+                               const char *cmderr,
+                               const char *response)
+{
+    struct qemuMonitorTestHandlerData *data;
+
+    if (VIR_ALLOC(data) < 0)
+        return -1;
+
+    if (VIR_STRDUP(data->response, response) < 0 ||
+        VIR_STRDUP(data->cmderr, cmderr) < 0)
+        goto error;
+
+    if (test->json || test->agent)
+        data->command_name = virJSONStringReformat(command, false);
+    else
+        ignore_value(VIR_STRDUP(data->command_name, command));
+
+    if (!data->command_name)
+        goto error;
+
+    return qemuMonitorTestAddHandler(test,
+                                     qemuMonitorTestProcessCommandVerbatim,
+                                     data, qemuMonitorTestHandlerDataFree);
+
+ error:
+    qemuMonitorTestHandlerDataFree(data);
+    return -1;
+}
+
+
+static int
 qemuMonitorTestProcessGuestAgentSync(qemuMonitorTestPtr test,
                                      qemuMonitorTestItemPtr item ATTRIBUTE_UNUSED,
                                      const char *cmdstr)
diff --git a/tests/qemumonitortestutils.h b/tests/qemumonitortestutils.h
index 87c11af..147996a 100644
--- a/tests/qemumonitortestutils.h
+++ b/tests/qemumonitortestutils.h
@@ -54,6 +54,11 @@ int qemuMonitorTestAddItem(qemuMonitorTestPtr test,
                            const char *command_name,
                            const char *response);

+int qemuMonitorTestAddItemVerbatim(qemuMonitorTestPtr test,
+                                   const char *command,
+                                   const char *cmderr,
+                                   const char *response);
+
 int qemuMonitorTestAddAgentSyncResponse(qemuMonitorTestPtr test);

 int qemuMonitorTestAddItemParams(qemuMonitorTestPtr test,
-- 
2.10.2




More information about the libvir-list mailing list