]> xenbits.xensource.com Git - libvirt.git/commitdiff
tests: Introduce qemuMonitorTestNewFromFile
authorJiri Denemark <jdenemar@redhat.com>
Mon, 6 Jun 2016 11:46:41 +0000 (13:46 +0200)
committerJiri Denemark <jdenemar@redhat.com>
Thu, 9 Jun 2016 07:47:56 +0000 (09:47 +0200)
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@redhat.com>
tests/qemucapabilitiestest.c
tests/qemumonitortestutils.c
tests/qemumonitortestutils.h

index 8ee930996c78b9e1165025548ba0d4d6b201bd9f..5a27b3624e637abc9092e16107bc740dd22f8dff 100644 (file)
@@ -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 (virTestLoadFile(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);
index 3d34942b95af9c047be1e6fc3f6ae16baa4cca17..2b94cf2b76ba778ff2dfcb24782cf3608cac3aa0 100644 (file)
@@ -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 (virTestLoadFile(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)
 {
index 3f07f65575c526e11e1f91b434bccdf8fe7c0a58..ca8c14328718fb995ba58600853c4c7058d73ee5 100644 (file)
@@ -69,6 +69,9 @@ qemuMonitorTestPtr qemuMonitorTestNew(bool json,
                                       virQEMUDriverPtr driver,
                                       const char *greeting);
 
+qemuMonitorTestPtr qemuMonitorTestNewFromFile(const char *fileName,
+                                              virDomainXMLOptionPtr xmlopt);
+
 qemuMonitorTestPtr qemuMonitorTestNewAgent(virDomainXMLOptionPtr xmlopt);