From 231b63e3ca4b85a010b345e4404cec73e28b6a15 Mon Sep 17 00:00:00 2001 From: Francesco Romani Date: Mon, 17 Mar 2014 16:19:46 +0100 Subject: [PATCH] qemu: add unit tests for the capabilities xml The test is loosely inspired from qemucapabilitiestest and qemuxml2xmltest. Added a new test instead of extending an existing one because the feature being tested don't really fits nicely in any existing place. --- tests/Makefile.am | 10 +- tests/qemucaps2xmldata/all_1.6.0-1.caps | 142 ++++++++++++ tests/qemucaps2xmldata/all_1.6.0-1.xml | 31 +++ .../nodisksnapshot_1.6.0-1.caps | 141 ++++++++++++ .../nodisksnapshot_1.6.0-1.xml | 31 +++ tests/qemucaps2xmltest.c | 209 ++++++++++++++++++ 6 files changed, 563 insertions(+), 1 deletion(-) create mode 100644 tests/qemucaps2xmldata/all_1.6.0-1.caps create mode 100644 tests/qemucaps2xmldata/all_1.6.0-1.xml create mode 100644 tests/qemucaps2xmldata/nodisksnapshot_1.6.0-1.caps create mode 100644 tests/qemucaps2xmldata/nodisksnapshot_1.6.0-1.xml create mode 100644 tests/qemucaps2xmltest.c diff --git a/tests/Makefile.am b/tests/Makefile.am index 8025e77b56..dc6a676513 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -94,6 +94,7 @@ EXTRA_DIST = \ nwfilterxml2xmlout \ oomtrace.pl \ qemucapabilitiesdata \ + qemucaps2xmldata \ qemuhelpdata \ qemuhotplugtestdata \ qemumonitorjsondata \ @@ -209,7 +210,7 @@ if WITH_QEMU test_programs += qemuxml2argvtest qemuxml2xmltest qemuxmlnstest \ qemuargv2xmltest qemuhelptest domainsnapshotxml2xmltest \ qemumonitortest qemumonitorjsontest qemuhotplugtest \ - qemuagenttest qemucapabilitiestest + qemuagenttest qemucapabilitiestest qemucaps2xmltest endif WITH_QEMU if WITH_LXC @@ -512,6 +513,12 @@ qemucapabilitiestest_SOURCES = \ $(NULL) qemucapabilitiestest_LDADD = libqemumonitortestutils.la $(qemu_LDADDS) +qemucaps2xmltest_SOURCES = \ + qemucaps2xmltest.c \ + testutils.c testutils.h \ + $(NULL) +qemucaps2xmltest_LDADD = $(qemu_LDADDS) + qemuagenttest_SOURCES = \ qemuagenttest.c \ testutils.c testutils.h \ @@ -536,6 +543,7 @@ EXTRA_DIST += qemuxml2argvtest.c qemuxml2xmltest.c qemuargv2xmltest.c \ qemumonitortest.c testutilsqemu.c testutilsqemu.h \ qemumonitorjsontest.c qemuhotplugtest.c \ qemuagenttest.c qemucapabilitiestest.c \ + qemucaps2xmltest.c \ $(QEMUMONITORTESTUTILS_SOURCES) endif ! WITH_QEMU diff --git a/tests/qemucaps2xmldata/all_1.6.0-1.caps b/tests/qemucaps2xmldata/all_1.6.0-1.caps new file mode 100644 index 0000000000..2d50cf98fc --- /dev/null +++ b/tests/qemucaps2xmldata/all_1.6.0-1.caps @@ -0,0 +1,142 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/qemucaps2xmldata/all_1.6.0-1.xml b/tests/qemucaps2xmldata/all_1.6.0-1.xml new file mode 100644 index 0000000000..425b22eac7 --- /dev/null +++ b/tests/qemucaps2xmldata/all_1.6.0-1.xml @@ -0,0 +1,31 @@ + + + + + i686 + + + + + + hvm + + 32 + /usr/bin/qemu-system-i386 + + + + /usr/bin/qemu-system-i386 + + + + + + + + + + + + + diff --git a/tests/qemucaps2xmldata/nodisksnapshot_1.6.0-1.caps b/tests/qemucaps2xmldata/nodisksnapshot_1.6.0-1.caps new file mode 100644 index 0000000000..5092e41964 --- /dev/null +++ b/tests/qemucaps2xmldata/nodisksnapshot_1.6.0-1.caps @@ -0,0 +1,141 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/qemucaps2xmldata/nodisksnapshot_1.6.0-1.xml b/tests/qemucaps2xmldata/nodisksnapshot_1.6.0-1.xml new file mode 100644 index 0000000000..cd19814ec4 --- /dev/null +++ b/tests/qemucaps2xmldata/nodisksnapshot_1.6.0-1.xml @@ -0,0 +1,31 @@ + + + + + i686 + + + + + + hvm + + 32 + /usr/bin/qemu-system-i386 + + + + /usr/bin/qemu-system-i386 + + + + + + + + + + + + + diff --git a/tests/qemucaps2xmltest.c b/tests/qemucaps2xmltest.c new file mode 100644 index 0000000000..8bf67385f3 --- /dev/null +++ b/tests/qemucaps2xmltest.c @@ -0,0 +1,209 @@ +/* + * Copyright (C) 2014 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + * + * Authors: + * Francesco Romani + */ + +#include + +#include "testutils.h" +#include "qemu/qemu_capabilities.h" + + +#define VIR_FROM_THIS VIR_FROM_NONE + + +static int +testCompareXMLToXML(const char *inxmldata, const char *outxmldata) +{ + int ret = 1; + + if (STRNEQ(outxmldata, inxmldata)) { + virtTestDifference(stderr, outxmldata, inxmldata); + goto cleanup; + } + + ret = 0; + cleanup: + + return ret; +} + + +typedef struct _testQemuData testQemuData; +typedef testQemuData *testQemuDataPtr; +struct _testQemuData { + const char *base; + virArch guestarch; +}; + +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 (!(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 virCapsPtr +testGetCaps(char *capsData, const testQemuData *data) +{ + virQEMUCapsPtr qemuCaps = NULL; + virCapsPtr caps = NULL; + + if ((qemuCaps = testQemuGetCaps(capsData)) == NULL) { + fprintf(stderr, "failed to parse qemu capabilities flags"); + goto error; + } + + if ((caps = virCapabilitiesNew(data->guestarch, 0, 0)) == NULL) { + fprintf(stderr, "failed to create the fake capabilities"); + goto error; + } + + if (virQEMUCapsInitGuestFromBinary(caps, + "/usr/bin/qemu-system-i386", + qemuCaps, + NULL, + NULL, + data->guestarch) < 0) { + fprintf(stderr, "failed to create the capabilities from qemu"); + goto error; + } + + return caps; + + error: + virObjectUnref(qemuCaps); + return NULL; +} + +static int +testQemuCapsXML(const void *opaque) +{ + int ret = -1; + const testQemuData *data = opaque; + char *capsFile = NULL, *xmlFile = NULL; + char *capsData = NULL, *xmlData = NULL; + char *capsXml = NULL; + virCapsPtr capsProvided = NULL; + + if (virAsprintf(&xmlFile, "%s/qemucaps2xmldata/%s.xml", + abs_srcdir, data->base) < 0) + goto cleanup; + + if (virAsprintf(&capsFile, "%s/qemucaps2xmldata/%s.caps", + abs_srcdir, data->base) < 0) + goto cleanup; + + if (virtTestLoadFile(xmlFile, &xmlData) < 0) + goto cleanup; + + if (virtTestLoadFile(capsFile, &capsData) < 0) + goto cleanup; + + if (!(capsProvided = testGetCaps(capsData, data))) + goto cleanup; + + capsXml = virCapabilitiesFormatXML(capsProvided); + if (!capsXml) + goto cleanup; + + ret = testCompareXMLToXML(capsXml, xmlData); + + cleanup: + VIR_FREE(xmlFile); + VIR_FREE(capsFile); + VIR_FREE(capsXml); + VIR_FREE(capsData); + VIR_FREE(xmlData); + virObjectUnref(capsProvided); + return ret; +} + +static int +mymain(void) +{ + int ret = 0; + + testQemuData data; + +#if !WITH_YAJL + fputs("libvirt not compiled with yajl, skipping this test\n", stderr); + return EXIT_AM_SKIP; +#endif + + if (virThreadInitialize() < 0) + return EXIT_FAILURE; + + virEventRegisterDefaultImpl(); + +#define DO_TEST_FULL(name, guest) \ + data.base = name; \ + data.guestarch = guest; \ + if (virtTestRun(name, testQemuCapsXML, &data) < 0) \ + ret = -1 + +#define DO_TEST(name) DO_TEST_FULL(name, VIR_ARCH_I686) + + DO_TEST("all_1.6.0-1"); + DO_TEST("nodisksnapshot_1.6.0-1"); + + return (ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE; +} + +VIRT_TEST_MAIN(mymain)