[libvirt] [PATCH 06/41] tests: Introduce qemuMonitorTestNewFromFile

Jiri Denemark jdenemar at redhat.com
Wed Jun 8 08:22:20 UTC 2016


It's a convenient wrapper around qemuMonitorTestNew which feeds the test
monitor with QMP replies from a specified file.

Signed-off-by: Jiri Denemark <jdenemar at redhat.com>
---
 tests/qemucapabilitiestest.c | 57 +---------------------------------------
 tests/qemumonitortestutils.c | 62 ++++++++++++++++++++++++++++++++++++++++++++
 tests/qemumonitortestutils.h |  3 +++
 3 files changed, 66 insertions(+), 56 deletions(-)

diff --git a/tests/qemucapabilitiestest.c b/tests/qemucapabilitiestest.c
index 3d55006..3b0463a 100644
--- a/tests/qemucapabilitiestest.c
+++ b/tests/qemucapabilitiestest.c
@@ -35,56 +35,6 @@ struct _testQemuData {
     const char *base;
 };
 
-static qemuMonitorTestPtr
-testQemuFeedMonitor(char *replies,
-                    virDomainXMLOptionPtr xmlopt)
-{
-    qemuMonitorTestPtr test = NULL;
-    char *tmp = replies;
-    char *singleReply = tmp;
-
-    /* Our JSON parser expects replies to be separated by a newline character.
-     * Hence we must preprocess the file a bit. */
-    while ((tmp = strchr(tmp, '\n'))) {
-        /* It is safe to touch tmp[1] since all strings ends with '\0'. */
-        bool eof = !tmp[1];
-
-        if (*(tmp + 1) != '\n') {
-            *tmp = ' ';
-            tmp++;
-        } else {
-            /* Cut off a single reply. */
-            *(tmp + 1) = '\0';
-
-            if (test) {
-                if (qemuMonitorTestAddItem(test, NULL, singleReply) < 0)
-                    goto error;
-            } else {
-                /* Create new mocked monitor with our greeting */
-                if (!(test = qemuMonitorTestNew(true, xmlopt, NULL, NULL, singleReply)))
-                    goto error;
-            }
-
-            if (!eof) {
-                /* Move the @tmp and @singleReply. */
-                tmp += 2;
-                singleReply = tmp;
-            }
-        }
-
-        if (eof)
-            break;
-    }
-
-    if (test && qemuMonitorTestAddItem(test, NULL, singleReply) < 0)
-        goto error;
-
-    return test;
-
- error:
-    qemuMonitorTestFree(test);
-    return NULL;
-}
 
 static int
 testQemuCaps(const void *opaque)
@@ -93,7 +43,6 @@ testQemuCaps(const void *opaque)
     const testQemuData *data = opaque;
     char *repliesFile = NULL;
     char *capsFile = NULL;
-    char *replies = NULL;
     qemuMonitorTestPtr mon = NULL;
     virQEMUCapsPtr capsActual = NULL;
     char *actual = NULL;
@@ -104,10 +53,7 @@ testQemuCaps(const void *opaque)
                     abs_srcdir, data->base, data->archName) < 0)
         goto cleanup;
 
-    if (virtTestLoadFile(repliesFile, &replies) < 0)
-        goto cleanup;
-
-    if (!(mon = testQemuFeedMonitor(replies, data->xmlopt)))
+    if (!(mon = qemuMonitorTestNewFromFile(repliesFile, data->xmlopt)))
         goto cleanup;
 
     if (!(capsActual = virQEMUCapsNew()) ||
@@ -125,7 +71,6 @@ testQemuCaps(const void *opaque)
  cleanup:
     VIR_FREE(repliesFile);
     VIR_FREE(capsFile);
-    VIR_FREE(replies);
     VIR_FREE(actual);
     qemuMonitorTestFree(mon);
     virObjectUnref(capsActual);
diff --git a/tests/qemumonitortestutils.c b/tests/qemumonitortestutils.c
index 3d34942..7b276bf 100644
--- a/tests/qemumonitortestutils.c
+++ b/tests/qemumonitortestutils.c
@@ -24,6 +24,7 @@
 #include <string.h>
 #include <time.h>
 
+#include "testutils.h"
 #include "qemumonitortestutils.h"
 
 #include "virthread.h"
@@ -926,6 +927,67 @@ qemuMonitorTestNew(bool json,
     return NULL;
 }
 
+
+qemuMonitorTestPtr
+qemuMonitorTestNewFromFile(const char *fileName,
+                           virDomainXMLOptionPtr xmlopt)
+{
+    qemuMonitorTestPtr test = NULL;
+    char *json = NULL;
+    char *tmp;
+    char *singleReply;
+
+    if (virtTestLoadFile(fileName, &json) < 0)
+        goto cleanup;
+
+    /* Our JSON parser expects replies to be separated by a newline character.
+     * Hence we must preprocess the file a bit. */
+    tmp = singleReply = json;
+    while ((tmp = strchr(tmp, '\n'))) {
+        /* It is safe to touch tmp[1] since all strings ends with '\0'. */
+        bool eof = !tmp[1];
+
+        if (*(tmp + 1) != '\n') {
+            *tmp = ' ';
+            tmp++;
+        } else {
+            /* Cut off a single reply. */
+            *(tmp + 1) = '\0';
+
+            if (test) {
+                if (qemuMonitorTestAddItem(test, NULL, singleReply) < 0)
+                    goto error;
+            } else {
+                /* Create new mocked monitor with our greeting */
+                if (!(test = qemuMonitorTestNew(true, xmlopt, NULL, NULL, singleReply)))
+                    goto error;
+            }
+
+            if (!eof) {
+                /* Move the @tmp and @singleReply. */
+                tmp += 2;
+                singleReply = tmp;
+            }
+        }
+
+        if (eof)
+            break;
+    }
+
+    if (test && qemuMonitorTestAddItem(test, NULL, singleReply) < 0)
+        goto error;
+
+ cleanup:
+    VIR_FREE(json);
+    return test;
+
+ error:
+    qemuMonitorTestFree(test);
+    test = NULL;
+    goto cleanup;
+}
+
+
 qemuMonitorTestPtr
 qemuMonitorTestNewAgent(virDomainXMLOptionPtr xmlopt)
 {
diff --git a/tests/qemumonitortestutils.h b/tests/qemumonitortestutils.h
index 3f07f65..ca8c143 100644
--- a/tests/qemumonitortestutils.h
+++ b/tests/qemumonitortestutils.h
@@ -69,6 +69,9 @@ qemuMonitorTestPtr qemuMonitorTestNew(bool json,
                                       virQEMUDriverPtr driver,
                                       const char *greeting);
 
+qemuMonitorTestPtr qemuMonitorTestNewFromFile(const char *fileName,
+                                              virDomainXMLOptionPtr xmlopt);
+
 qemuMonitorTestPtr qemuMonitorTestNewAgent(virDomainXMLOptionPtr xmlopt);
 
 
-- 
2.8.3




More information about the libvir-list mailing list