From 7615917a0acec668c3e229148bda11aa3694db00 Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Fri, 1 Jul 2016 10:30:53 +0200 Subject: [PATCH] tests: qemuxml2xml: Format status XML header dynamically Status XML tests were done by prepending a constant string to an existing XML. With the planned changes the header will depend on data present in the definition rather than just on the data that was parsed. The first dynamic element in the header will be the vcpu thread list. Reuse and rename qemuXML2XMLPreFormatCallback for gathering the relevant data when checking the active XML parsing and formating and pass the bitmap to a newly crated header generator. --- tests/qemuxml2xmltest.c | 74 +++++++++++++++++++++++++++++++++++------ 1 file changed, 63 insertions(+), 11 deletions(-) diff --git a/tests/qemuxml2xmltest.c b/tests/qemuxml2xmltest.c index ae328c2aa1..eb392f4863 100644 --- a/tests/qemuxml2xmltest.c +++ b/tests/qemuxml2xmltest.c @@ -33,13 +33,21 @@ struct testInfo { char *outActiveName; char *outInactiveName; + virBitmapPtr activeVcpus; + virQEMUCapsPtr qemuCaps; }; static int -qemuXML2XMLPreFormatCallback(virDomainDefPtr def ATTRIBUTE_UNUSED, - const void *opaque ATTRIBUTE_UNUSED) +qemuXML2XMLActivePreFormatCallback(virDomainDefPtr def, + const void *opaque) { + struct testInfo *info = (struct testInfo *) opaque; + + /* store vCPU bitmap so that the status XML can be created faithfully */ + if (!info->activeVcpus) + info->activeVcpus = virDomainDefGetOnlineVcpumap(def); + return 0; } @@ -50,7 +58,8 @@ testXML2XMLActive(const void *opaque) return testCompareDomXML2XMLFiles(driver.caps, driver.xmlopt, info->inName, info->outActiveName, true, - qemuXML2XMLPreFormatCallback, opaque, 0, + qemuXML2XMLActivePreFormatCallback, + opaque, 0, TEST_COMPARE_DOM_XML2XML_RESULT_SUCCESS); } @@ -62,18 +71,17 @@ testXML2XMLInactive(const void *opaque) return testCompareDomXML2XMLFiles(driver.caps, driver.xmlopt, info->inName, info->outInactiveName, false, - qemuXML2XMLPreFormatCallback, opaque, 0, + NULL, opaque, 0, TEST_COMPARE_DOM_XML2XML_RESULT_SUCCESS); } -static const char testStatusXMLPrefix[] = +static const char testStatusXMLPrefixHeader[] = "\n" " \n" -" \n" -" \n" -" \n" -" \n" +" \n"; + +static const char testStatusXMLPrefixFooter[] = " \n" " \n" " \n" @@ -95,6 +103,40 @@ static const char testStatusXMLSuffix[] = "\n"; +static void +testGetStatuXMLPrefixVcpus(virBufferPtr buf, + const struct testInfo *data) +{ + ssize_t vcpuid = -1; + + virBufferAddLit(buf, "\n"); + virBufferAdjustIndent(buf, 2); + + while ((vcpuid = virBitmapNextSetBit(data->activeVcpus, vcpuid)) >= 0) + virBufferAsprintf(buf, "\n", vcpuid + 3803519); + + virBufferAdjustIndent(buf, -2); + virBufferAddLit(buf, "\n"); +} + + +static char * +testGetStatusXMLPrefix(const struct testInfo *data) +{ + virBuffer buf = VIR_BUFFER_INITIALIZER; + + virBufferAdd(&buf, testStatusXMLPrefixHeader, -1); + virBufferAdjustIndent(&buf, 2); + + testGetStatuXMLPrefixVcpus(&buf, data); + + virBufferAdjustIndent(&buf, -2); + virBufferAdd(&buf, testStatusXMLPrefixFooter, -1); + + return virBufferContentAndReset(&buf); +} + + static int testCompareStatusXMLToXMLFiles(const void *opaque) { @@ -105,6 +147,7 @@ testCompareStatusXMLToXMLFiles(const void *opaque) char *expect = NULL; char *actual = NULL; char *source = NULL; + char *header = NULL; char *inFile = NULL, *outActiveFile = NULL; int ret = -1; int keepBlanksDefault = xmlKeepBlanksDefault(0); @@ -114,8 +157,11 @@ testCompareStatusXMLToXMLFiles(const void *opaque) if (virTestLoadFile(data->outActiveName, &outActiveFile) < 0) goto cleanup; + if (!(header = testGetStatusXMLPrefix(data))) + goto cleanup; + /* construct faked source status XML */ - virBufferAdd(&buf, testStatusXMLPrefix, -1); + virBufferAdd(&buf, header, -1); virBufferAdjustIndent(&buf, 2); virBufferAddStr(&buf, inFile); virBufferAdjustIndent(&buf, -2); @@ -127,7 +173,7 @@ testCompareStatusXMLToXMLFiles(const void *opaque) } /* construct the expect string */ - virBufferAdd(&buf, testStatusXMLPrefix, -1); + virBufferAdd(&buf, header, -1); virBufferAdjustIndent(&buf, 2); virBufferAddStr(&buf, outActiveFile); virBufferAdjustIndent(&buf, -2); @@ -175,6 +221,7 @@ testCompareStatusXMLToXMLFiles(const void *opaque) VIR_FREE(actual); VIR_FREE(source); VIR_FREE(inFile); + VIR_FREE(header); VIR_FREE(outActiveFile); return ret; } @@ -187,6 +234,9 @@ testInfoFree(struct testInfo *info) VIR_FREE(info->outActiveName); VIR_FREE(info->outInactiveName); + virBitmapFree(info->activeVcpus); + info->activeVcpus = NULL; + virObjectUnref(info->qemuCaps); } @@ -261,6 +311,8 @@ mymain(void) struct testInfo info; virQEMUDriverConfigPtr cfg = NULL; + memset(&info, 0, sizeof(info)); + if (qemuTestDriverInit(&driver) < 0) return EXIT_FAILURE;