]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
tests: qemu: monitor: Add helpers to test full command syntax
authorPeter Krempa <pkrempa@redhat.com>
Fri, 25 Nov 2016 09:43:23 +0000 (10:43 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Wed, 18 Jan 2017 09:02:09 +0000 (10:02 +0100)
Add test monitor infrastructure that will test the commands verbatim
rather than trying to do any smart handling.

tests/qemumonitortestutils.c
tests/qemumonitortestutils.h

index fb4f51c5446ae19ed221956ccb55cc61b54fb156..65d82f31a28dbac615df96ef061197918efe4baf 100644 (file)
@@ -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);
@@ -605,6 +607,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 successfully 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,
index 87c11af474b92c01b0b712083a81bfedbb3033a2..147996a088ed32aa5ee71661d6ff75fba66e0fd3 100644 (file)
@@ -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,