[libvirt] [PATCH v2 08/11] tests: qemu: Add helper to load full monitor conversation from file

Peter Krempa pkrempa at redhat.com
Wed Jan 11 09:48:18 UTC 2017


Similar to the existing qemuMonitorTestNewFromFile the *Full version
will allow to check both commands and supply responses for a better
monitor testing.
---
 tests/qemumonitortestutils.c | 119 +++++++++++++++++++++++++++++++++++++++++++
 tests/qemumonitortestutils.h |   3 ++
 2 files changed, 122 insertions(+)

diff --git a/tests/qemumonitortestutils.c b/tests/qemumonitortestutils.c
index 50042f960..80136dc14 100644
--- a/tests/qemumonitortestutils.c
+++ b/tests/qemumonitortestutils.c
@@ -1278,6 +1278,125 @@ qemuMonitorTestNewFromFile(const char *fileName,
 }


+static int
+qemuMonitorTestFullAddItem(qemuMonitorTestPtr test,
+                           const char *filename,
+                           const char *command,
+                           const char *response,
+                           size_t line)
+{
+    char *cmderr;
+    int ret;
+
+    if (virAsprintf(&cmderr, "wrong expected command in %s:%zu: ",
+                    filename, line) < 0)
+        return -1;
+
+    ret = qemuMonitorTestAddItemVerbatim(test, command, cmderr, response);
+
+    VIR_FREE(cmderr);
+    return ret;
+}
+
+
+/**
+ * qemuMonitorTestNewFromFileFull:
+ * @fileName: File name to load monitor replies from
+ * @driver: qemu driver object
+ * @vm: domain object (may be null if it's not needed by the test)
+ *
+ * Create a JSON test monitor simulator object and fill it with expected command
+ * sequence and replies specified in @fileName.
+ *
+ * The file contains a sequence of JSON commands and reply objects separated by
+ * empty lines. A command is followed by a reply. The QMP greeting is added
+ * automatically.
+ *
+ * Returns the monitor object on success; NULL on error.
+ */
+qemuMonitorTestPtr
+qemuMonitorTestNewFromFileFull(const char *fileName,
+                               virQEMUDriverPtr driver,
+                               virDomainObjPtr vm)
+{
+    qemuMonitorTestPtr ret = NULL;
+    char *jsonstr = NULL;
+    char *tmp;
+    size_t line = 0;
+
+    char *command = NULL;
+    char *response = NULL;
+    size_t commandln = 0;
+    char *cmderr = NULL;
+
+    if (virTestLoadFile(fileName, &jsonstr) < 0)
+        return NULL;
+
+    if (!(ret = qemuMonitorTestNew(true, driver->xmlopt, vm, driver, NULL)))
+        goto cleanup;
+
+    tmp = jsonstr;
+    command = tmp;
+    while ((tmp = strchr(tmp, '\n'))) {
+        bool eof = !tmp[1];
+        line++;
+
+        if (*(tmp + 1) != '\n') {
+            *tmp = ' ';
+            tmp++;
+        } else {
+            /* Cut off a single reply. */
+            *(tmp + 1) = '\0';
+
+            if (response) {
+                if (qemuMonitorTestFullAddItem(ret, fileName, command,
+                                               response, commandln) < 0)
+                    goto error;
+                command = NULL;
+                response = NULL;
+            }
+
+            if (!eof) {
+                /* Move the @tmp and @singleReply. */
+                tmp += 2;
+
+                if (!command) {
+                    commandln = line;
+                    command = tmp;
+                } else {
+                    response = tmp;
+                }
+            }
+        }
+
+        if (eof)
+            break;
+    }
+
+    if (command) {
+        if (!response) {
+            virReportError(VIR_ERR_INTERNAL_ERROR, "missing response for command "
+                           "on line '%zu' in '%s'", commandln, fileName);
+            goto error;
+        }
+
+        if (qemuMonitorTestFullAddItem(ret, fileName, command,
+                                       response, commandln) < 0)
+            goto error;
+    }
+
+ cleanup:
+    VIR_FREE(cmderr);
+    VIR_FREE(jsonstr);
+    return ret;
+
+ error:
+    qemuMonitorTestFree(ret);
+    ret = NULL;
+    goto cleanup;
+}
+
+
 qemuMonitorTestPtr
 qemuMonitorTestNewAgent(virDomainXMLOptionPtr xmlopt)
 {
diff --git a/tests/qemumonitortestutils.h b/tests/qemumonitortestutils.h
index 147996a08..8b19b37e7 100644
--- a/tests/qemumonitortestutils.h
+++ b/tests/qemumonitortestutils.h
@@ -85,6 +85,9 @@ qemuMonitorTestPtr qemuMonitorTestNew(bool json,
 qemuMonitorTestPtr qemuMonitorTestNewFromFile(const char *fileName,
                                               virDomainXMLOptionPtr xmlopt,
                                               bool simple);
+qemuMonitorTestPtr qemuMonitorTestNewFromFileFull(const char *fileName,
+                                                  virQEMUDriverPtr driver,
+                                                  virDomainObjPtr vm);

 qemuMonitorTestPtr qemuMonitorTestNewAgent(virDomainXMLOptionPtr xmlopt);

-- 
2.11.0




More information about the libvir-list mailing list