[PATCH for 1.2.7 4/8] tests: Move qemu caps XML parsing into shared unit

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Later on, we the qemu capabilities XML parsing code may come handy so
instead of duplicating the code make the already existing one shared.
By the same time, make the function accept file name instead of XML
document stored already in memory.

Signed-off-by: Michal Privoznik <mprivozn@xxxxxxxxxx>
---
 tests/qemucapabilitiestest.c | 57 +++-----------------------------------------
 tests/testutilsqemu.c        | 49 +++++++++++++++++++++++++++++++++++++
 tests/testutilsqemu.h        |  3 +++
 3 files changed, 55 insertions(+), 54 deletions(-)

diff --git a/tests/qemucapabilitiestest.c b/tests/qemucapabilitiestest.c
index a291460..4e5f9e5 100644
--- a/tests/qemucapabilitiestest.c
+++ b/tests/qemucapabilitiestest.c
@@ -85,55 +85,6 @@ testQemuFeedMonitor(char *replies,
     return NULL;
 }
 
-static virQEMUCapsPtr
-testQemuGetCaps(char *caps)
-{
-    virQEMUCapsPtr qemuCaps = NULL;
-    xmlDocPtr xml;
-    xmlXPathContextPtr ctxt = NULL;
-    ssize_t i, n;
-    xmlNodePtr *nodes = NULL;
-
-    if (!(xml = virXMLParseStringCtxt(caps, "(test caps)", &ctxt)))
-        goto error;
-
-    if ((n = virXPathNodeSet("/qemuCaps/flag", ctxt, &nodes)) < 0) {
-        fprintf(stderr, "failed to parse qemu capabilities flags");
-        goto error;
-    }
-
-    if (n > 0) {
-        if (!(qemuCaps = virQEMUCapsNew()))
-            goto error;
-
-        for (i = 0; i < n; i++) {
-            char *str = virXMLPropString(nodes[i], "name");
-            if (str) {
-                int flag = virQEMUCapsTypeFromString(str);
-                if (flag < 0) {
-                    fprintf(stderr, "Unknown qemu capabilities flag %s", str);
-                    VIR_FREE(str);
-                    goto error;
-                }
-                VIR_FREE(str);
-                virQEMUCapsSet(qemuCaps, flag);
-            }
-        }
-    }
-
-    VIR_FREE(nodes);
-    xmlFreeDoc(xml);
-    xmlXPathFreeContext(ctxt);
-    return qemuCaps;
-
- error:
-    VIR_FREE(nodes);
-    virObjectUnref(qemuCaps);
-    xmlFreeDoc(xml);
-    xmlXPathFreeContext(ctxt);
-    return NULL;
-}
-
 static int
 testQemuCapsCompare(virQEMUCapsPtr capsProvided,
                     virQEMUCapsPtr capsComputed)
@@ -166,7 +117,7 @@ testQemuCaps(const void *opaque)
     int ret = -1;
     const testQemuData *data = opaque;
     char *repliesFile = NULL, *capsFile = NULL;
-    char *replies = NULL, *caps = NULL;
+    char *replies = NULL;
     qemuMonitorTestPtr mon = NULL;
     virQEMUCapsPtr capsProvided = NULL, capsComputed = NULL;
 
@@ -176,14 +127,13 @@ testQemuCaps(const void *opaque)
                     abs_srcdir, data->base) < 0)
         goto cleanup;
 
-    if (virtTestLoadFile(repliesFile, &replies) < 0 ||
-        virtTestLoadFile(capsFile, &caps) < 0)
+    if (virtTestLoadFile(repliesFile, &replies) < 0)
         goto cleanup;
 
     if (!(mon = testQemuFeedMonitor(replies, data->xmlopt)))
         goto cleanup;
 
-    if (!(capsProvided = testQemuGetCaps(caps)))
+    if (!(capsProvided = qemuTestParseCapabilities(capsFile)))
         goto cleanup;
 
     if (!(capsComputed = virQEMUCapsNew()))
@@ -207,7 +157,6 @@ testQemuCaps(const void *opaque)
     VIR_FREE(repliesFile);
     VIR_FREE(capsFile);
     VIR_FREE(replies);
-    VIR_FREE(caps);
     qemuMonitorTestFree(mon);
     virObjectUnref(capsProvided);
     virObjectUnref(capsComputed);
diff --git a/tests/testutilsqemu.c b/tests/testutilsqemu.c
index 7e24909..85f0356 100644
--- a/tests/testutilsqemu.c
+++ b/tests/testutilsqemu.c
@@ -373,4 +373,53 @@ testSCSIDeviceGetSgName(const char *sysfs_prefix ATTRIBUTE_UNUSED,
 qemuBuildCommandLineCallbacks testCallbacks = {
     .qemuGetSCSIDeviceSgName = testSCSIDeviceGetSgName,
 };
+
+virQEMUCapsPtr
+qemuTestParseCapabilities(const char *capsFile)
+{
+    virQEMUCapsPtr qemuCaps = NULL;
+    xmlDocPtr xml;
+    xmlXPathContextPtr ctxt = NULL;
+    ssize_t i, n;
+    xmlNodePtr *nodes = NULL;
+
+    if (!(xml = virXMLParseFileCtxt(capsFile, &ctxt)))
+        goto error;
+
+    if ((n = virXPathNodeSet("/qemuCaps/flag", ctxt, &nodes)) < 0) {
+        fprintf(stderr, "failed to parse qemu capabilities flags");
+        goto error;
+    }
+
+    if (n > 0) {
+        if (!(qemuCaps = virQEMUCapsNew()))
+            goto error;
+
+        for (i = 0; i < n; i++) {
+            char *str = virXMLPropString(nodes[i], "name");
+            if (str) {
+                int flag = virQEMUCapsTypeFromString(str);
+                if (flag < 0) {
+                    fprintf(stderr, "Unknown qemu capabilities flag %s", str);
+                    VIR_FREE(str);
+                    goto error;
+                }
+                VIR_FREE(str);
+                virQEMUCapsSet(qemuCaps, flag);
+            }
+        }
+    }
+
+    VIR_FREE(nodes);
+    xmlFreeDoc(xml);
+    xmlXPathFreeContext(ctxt);
+    return qemuCaps;
+
+ error:
+    VIR_FREE(nodes);
+    virObjectUnref(qemuCaps);
+    xmlFreeDoc(xml);
+    xmlXPathFreeContext(ctxt);
+    return NULL;
+}
 #endif
diff --git a/tests/testutilsqemu.h b/tests/testutilsqemu.h
index f01b722..79ee143 100644
--- a/tests/testutilsqemu.h
+++ b/tests/testutilsqemu.h
@@ -3,8 +3,11 @@
 # include "capabilities.h"
 # include "domain_conf.h"
 # include "qemu/qemu_command.h"
+# include "qemu/qemu_capabilities.h"
 
 virCapsPtr testQemuCapsInit(void);
 virDomainXMLOptionPtr testQemuXMLConfInit(void);
 extern qemuBuildCommandLineCallbacks testCallbacks;
+
+virQEMUCapsPtr qemuTestParseCapabilities(const char *capsFile);
 #endif
-- 
1.8.5.5

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]