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.
This commit is contained in:
Peter Krempa 2016-07-01 10:30:53 +02:00
parent 9cc931f0bb
commit 7615917a0a

View File

@ -33,13 +33,21 @@ struct testInfo {
char *outActiveName; char *outActiveName;
char *outInactiveName; char *outInactiveName;
virBitmapPtr activeVcpus;
virQEMUCapsPtr qemuCaps; virQEMUCapsPtr qemuCaps;
}; };
static int static int
qemuXML2XMLPreFormatCallback(virDomainDefPtr def ATTRIBUTE_UNUSED, qemuXML2XMLActivePreFormatCallback(virDomainDefPtr def,
const void *opaque ATTRIBUTE_UNUSED) 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; return 0;
} }
@ -50,7 +58,8 @@ testXML2XMLActive(const void *opaque)
return testCompareDomXML2XMLFiles(driver.caps, driver.xmlopt, return testCompareDomXML2XMLFiles(driver.caps, driver.xmlopt,
info->inName, info->outActiveName, true, info->inName, info->outActiveName, true,
qemuXML2XMLPreFormatCallback, opaque, 0, qemuXML2XMLActivePreFormatCallback,
opaque, 0,
TEST_COMPARE_DOM_XML2XML_RESULT_SUCCESS); TEST_COMPARE_DOM_XML2XML_RESULT_SUCCESS);
} }
@ -62,18 +71,17 @@ testXML2XMLInactive(const void *opaque)
return testCompareDomXML2XMLFiles(driver.caps, driver.xmlopt, info->inName, return testCompareDomXML2XMLFiles(driver.caps, driver.xmlopt, info->inName,
info->outInactiveName, false, info->outInactiveName, false,
qemuXML2XMLPreFormatCallback, opaque, 0, NULL, opaque, 0,
TEST_COMPARE_DOM_XML2XML_RESULT_SUCCESS); TEST_COMPARE_DOM_XML2XML_RESULT_SUCCESS);
} }
static const char testStatusXMLPrefix[] = static const char testStatusXMLPrefixHeader[] =
"<domstatus state='running' reason='booted' pid='3803518'>\n" "<domstatus state='running' reason='booted' pid='3803518'>\n"
" <taint flag='high-privileges'/>\n" " <taint flag='high-privileges'/>\n"
" <monitor path='/var/lib/libvirt/qemu/test.monitor' json='1' type='unix'/>\n" " <monitor path='/var/lib/libvirt/qemu/test.monitor' json='1' type='unix'/>\n";
" <vcpus>\n"
" <vcpu pid='3803519'/>\n" static const char testStatusXMLPrefixFooter[] =
" </vcpus>\n"
" <qemuCaps>\n" " <qemuCaps>\n"
" <flag name='vnet-hdr'/>\n" " <flag name='vnet-hdr'/>\n"
" <flag name='qxl.vgamem_mb'/>\n" " <flag name='qxl.vgamem_mb'/>\n"
@ -95,6 +103,40 @@ static const char testStatusXMLSuffix[] =
"</domstatus>\n"; "</domstatus>\n";
static void
testGetStatuXMLPrefixVcpus(virBufferPtr buf,
const struct testInfo *data)
{
ssize_t vcpuid = -1;
virBufferAddLit(buf, "<vcpus>\n");
virBufferAdjustIndent(buf, 2);
while ((vcpuid = virBitmapNextSetBit(data->activeVcpus, vcpuid)) >= 0)
virBufferAsprintf(buf, "<vcpu pid='%zd'/>\n", vcpuid + 3803519);
virBufferAdjustIndent(buf, -2);
virBufferAddLit(buf, "</vcpus>\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 static int
testCompareStatusXMLToXMLFiles(const void *opaque) testCompareStatusXMLToXMLFiles(const void *opaque)
{ {
@ -105,6 +147,7 @@ testCompareStatusXMLToXMLFiles(const void *opaque)
char *expect = NULL; char *expect = NULL;
char *actual = NULL; char *actual = NULL;
char *source = NULL; char *source = NULL;
char *header = NULL;
char *inFile = NULL, *outActiveFile = NULL; char *inFile = NULL, *outActiveFile = NULL;
int ret = -1; int ret = -1;
int keepBlanksDefault = xmlKeepBlanksDefault(0); int keepBlanksDefault = xmlKeepBlanksDefault(0);
@ -114,8 +157,11 @@ testCompareStatusXMLToXMLFiles(const void *opaque)
if (virTestLoadFile(data->outActiveName, &outActiveFile) < 0) if (virTestLoadFile(data->outActiveName, &outActiveFile) < 0)
goto cleanup; goto cleanup;
if (!(header = testGetStatusXMLPrefix(data)))
goto cleanup;
/* construct faked source status XML */ /* construct faked source status XML */
virBufferAdd(&buf, testStatusXMLPrefix, -1); virBufferAdd(&buf, header, -1);
virBufferAdjustIndent(&buf, 2); virBufferAdjustIndent(&buf, 2);
virBufferAddStr(&buf, inFile); virBufferAddStr(&buf, inFile);
virBufferAdjustIndent(&buf, -2); virBufferAdjustIndent(&buf, -2);
@ -127,7 +173,7 @@ testCompareStatusXMLToXMLFiles(const void *opaque)
} }
/* construct the expect string */ /* construct the expect string */
virBufferAdd(&buf, testStatusXMLPrefix, -1); virBufferAdd(&buf, header, -1);
virBufferAdjustIndent(&buf, 2); virBufferAdjustIndent(&buf, 2);
virBufferAddStr(&buf, outActiveFile); virBufferAddStr(&buf, outActiveFile);
virBufferAdjustIndent(&buf, -2); virBufferAdjustIndent(&buf, -2);
@ -175,6 +221,7 @@ testCompareStatusXMLToXMLFiles(const void *opaque)
VIR_FREE(actual); VIR_FREE(actual);
VIR_FREE(source); VIR_FREE(source);
VIR_FREE(inFile); VIR_FREE(inFile);
VIR_FREE(header);
VIR_FREE(outActiveFile); VIR_FREE(outActiveFile);
return ret; return ret;
} }
@ -187,6 +234,9 @@ testInfoFree(struct testInfo *info)
VIR_FREE(info->outActiveName); VIR_FREE(info->outActiveName);
VIR_FREE(info->outInactiveName); VIR_FREE(info->outInactiveName);
virBitmapFree(info->activeVcpus);
info->activeVcpus = NULL;
virObjectUnref(info->qemuCaps); virObjectUnref(info->qemuCaps);
} }
@ -261,6 +311,8 @@ mymain(void)
struct testInfo info; struct testInfo info;
virQEMUDriverConfigPtr cfg = NULL; virQEMUDriverConfigPtr cfg = NULL;
memset(&info, 0, sizeof(info));
if (qemuTestDriverInit(&driver) < 0) if (qemuTestDriverInit(&driver) < 0)
return EXIT_FAILURE; return EXIT_FAILURE;