From 3ecb9e7631f9f6a82e91e13abe6f0f700ccc4238 Mon Sep 17 00:00:00 2001 From: Michal Privoznik Date: Fri, 27 Jun 2014 16:39:27 +0200 Subject: [PATCH] tests: Move qemu caps XML parsing into shared unit 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 --- 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 a2914606f7..4e5f9e5296 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 7e24909bee..85f03560a9 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 f01b722d7c..79ee1434c2 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