6ae9ed
From e4c5a6e99b4e0f7a2314a72b4aaf20362c93bf71 Mon Sep 17 00:00:00 2001
6ae9ed
Message-Id: <e4c5a6e99b4e0f7a2314a72b4aaf20362c93bf71@dist-git>
6ae9ed
From: Peter Krempa <pkrempa@redhat.com>
6ae9ed
Date: Wed, 24 Aug 2016 16:10:46 -0400
6ae9ed
Subject: [PATCH] tests: qemuxml2xml: Format status XML header dynamically
6ae9ed
6ae9ed
https://bugzilla.redhat.com/show_bug.cgi?id=1097930
6ae9ed
https://bugzilla.redhat.com/show_bug.cgi?id=1224341
6ae9ed
6ae9ed
Status XML tests were done by prepending a constant string to an
6ae9ed
existing XML. With the planned changes the header will depend on data
6ae9ed
present in the definition rather than just on the data that was parsed.
6ae9ed
6ae9ed
The first dynamic element in the header will be the vcpu thread list.
6ae9ed
Reuse and rename qemuXML2XMLPreFormatCallback for gathering the relevant
6ae9ed
data when checking the active XML parsing and formating and pass the
6ae9ed
bitmap to a newly crated header generator.
6ae9ed
6ae9ed
(cherry picked from commit 7615917a0acec668c3e229148bda11aa3694db00)
6ae9ed
---
6ae9ed
 tests/qemuxml2xmltest.c | 74 +++++++++++++++++++++++++++++++++++++++++--------
6ae9ed
 1 file changed, 63 insertions(+), 11 deletions(-)
6ae9ed
6ae9ed
diff --git a/tests/qemuxml2xmltest.c b/tests/qemuxml2xmltest.c
6ae9ed
index a757fdb..573899f 100644
6ae9ed
--- a/tests/qemuxml2xmltest.c
6ae9ed
+++ b/tests/qemuxml2xmltest.c
6ae9ed
@@ -33,13 +33,21 @@ struct testInfo {
6ae9ed
     char *outActiveName;
6ae9ed
     char *outInactiveName;
6ae9ed
 
6ae9ed
+    virBitmapPtr activeVcpus;
6ae9ed
+
6ae9ed
     virQEMUCapsPtr qemuCaps;
6ae9ed
 };
6ae9ed
 
6ae9ed
 static int
6ae9ed
-qemuXML2XMLPreFormatCallback(virDomainDefPtr def ATTRIBUTE_UNUSED,
6ae9ed
-                             const void *opaque ATTRIBUTE_UNUSED)
6ae9ed
+qemuXML2XMLActivePreFormatCallback(virDomainDefPtr def,
6ae9ed
+                                   const void *opaque)
6ae9ed
 {
6ae9ed
+    struct testInfo *info = (struct testInfo *) opaque;
6ae9ed
+
6ae9ed
+    /* store vCPU bitmap so that the status XML can be created faithfully */
6ae9ed
+    if (!info->activeVcpus)
6ae9ed
+        info->activeVcpus = virDomainDefGetOnlineVcpumap(def);
6ae9ed
+
6ae9ed
     return 0;
6ae9ed
 }
6ae9ed
 
6ae9ed
@@ -50,7 +58,8 @@ testXML2XMLActive(const void *opaque)
6ae9ed
 
6ae9ed
     return testCompareDomXML2XMLFiles(driver.caps, driver.xmlopt,
6ae9ed
                                       info->inName, info->outActiveName, true,
6ae9ed
-                                      qemuXML2XMLPreFormatCallback, opaque, 0,
6ae9ed
+                                      qemuXML2XMLActivePreFormatCallback,
6ae9ed
+                                      opaque, 0,
6ae9ed
                                       TEST_COMPARE_DOM_XML2XML_RESULT_SUCCESS);
6ae9ed
 }
6ae9ed
 
6ae9ed
@@ -62,18 +71,17 @@ testXML2XMLInactive(const void *opaque)
6ae9ed
 
6ae9ed
     return testCompareDomXML2XMLFiles(driver.caps, driver.xmlopt, info->inName,
6ae9ed
                                       info->outInactiveName, false,
6ae9ed
-                                      qemuXML2XMLPreFormatCallback, opaque, 0,
6ae9ed
+                                      NULL, opaque, 0,
6ae9ed
                                       TEST_COMPARE_DOM_XML2XML_RESULT_SUCCESS);
6ae9ed
 }
6ae9ed
 
6ae9ed
 
6ae9ed
-static const char testStatusXMLPrefix[] =
6ae9ed
+static const char testStatusXMLPrefixHeader[] =
6ae9ed
 "<domstatus state='running' reason='booted' pid='3803518'>\n"
6ae9ed
 "  <taint flag='high-privileges'/>\n"
6ae9ed
-"  <monitor path='/var/lib/libvirt/qemu/test.monitor' json='1' type='unix'/>\n"
6ae9ed
-"  <vcpus>\n"
6ae9ed
-"    <vcpu pid='3803519'/>\n"
6ae9ed
-"  </vcpus>\n"
6ae9ed
+"  <monitor path='/var/lib/libvirt/qemu/test.monitor' json='1' type='unix'/>\n";
6ae9ed
+
6ae9ed
+static const char testStatusXMLPrefixFooter[] =
6ae9ed
 "  <qemuCaps>\n"
6ae9ed
 "    <flag name='vnet-hdr'/>\n"
6ae9ed
 "    <flag name='qxl.vgamem_mb'/>\n"
6ae9ed
@@ -95,6 +103,40 @@ static const char testStatusXMLSuffix[] =
6ae9ed
 "</domstatus>\n";
6ae9ed
 
6ae9ed
 
6ae9ed
+static void
6ae9ed
+testGetStatuXMLPrefixVcpus(virBufferPtr buf,
6ae9ed
+                           const struct testInfo *data)
6ae9ed
+{
6ae9ed
+    ssize_t vcpuid = -1;
6ae9ed
+
6ae9ed
+    virBufferAddLit(buf, "<vcpus>\n");
6ae9ed
+    virBufferAdjustIndent(buf, 2);
6ae9ed
+
6ae9ed
+    while ((vcpuid = virBitmapNextSetBit(data->activeVcpus, vcpuid)) >= 0)
6ae9ed
+        virBufferAsprintf(buf, "<vcpu pid='%zd'/>\n", vcpuid + 3803519);
6ae9ed
+
6ae9ed
+    virBufferAdjustIndent(buf, -2);
6ae9ed
+    virBufferAddLit(buf, "</vcpus>\n");
6ae9ed
+}
6ae9ed
+
6ae9ed
+
6ae9ed
+static char *
6ae9ed
+testGetStatusXMLPrefix(const struct testInfo *data)
6ae9ed
+{
6ae9ed
+    virBuffer buf = VIR_BUFFER_INITIALIZER;
6ae9ed
+
6ae9ed
+    virBufferAdd(&buf, testStatusXMLPrefixHeader, -1);
6ae9ed
+    virBufferAdjustIndent(&buf, 2);
6ae9ed
+
6ae9ed
+    testGetStatuXMLPrefixVcpus(&buf, data);
6ae9ed
+
6ae9ed
+    virBufferAdjustIndent(&buf, -2);
6ae9ed
+    virBufferAdd(&buf, testStatusXMLPrefixFooter, -1);
6ae9ed
+
6ae9ed
+    return virBufferContentAndReset(&buf;;
6ae9ed
+}
6ae9ed
+
6ae9ed
+
6ae9ed
 static int
6ae9ed
 testCompareStatusXMLToXMLFiles(const void *opaque)
6ae9ed
 {
6ae9ed
@@ -105,6 +147,7 @@ testCompareStatusXMLToXMLFiles(const void *opaque)
6ae9ed
     char *expect = NULL;
6ae9ed
     char *actual = NULL;
6ae9ed
     char *source = NULL;
6ae9ed
+    char *header = NULL;
6ae9ed
     char *inFile = NULL, *outActiveFile = NULL;
6ae9ed
     int ret = -1;
6ae9ed
     int keepBlanksDefault = xmlKeepBlanksDefault(0);
6ae9ed
@@ -114,8 +157,11 @@ testCompareStatusXMLToXMLFiles(const void *opaque)
6ae9ed
     if (virTestLoadFile(data->outActiveName, &outActiveFile) < 0)
6ae9ed
         goto cleanup;
6ae9ed
 
6ae9ed
+    if (!(header = testGetStatusXMLPrefix(data)))
6ae9ed
+        goto cleanup;
6ae9ed
+
6ae9ed
     /* construct faked source status XML */
6ae9ed
-    virBufferAdd(&buf, testStatusXMLPrefix, -1);
6ae9ed
+    virBufferAdd(&buf, header, -1);
6ae9ed
     virBufferAdjustIndent(&buf, 2);
6ae9ed
     virBufferAddStr(&buf, inFile);
6ae9ed
     virBufferAdjustIndent(&buf, -2);
6ae9ed
@@ -127,7 +173,7 @@ testCompareStatusXMLToXMLFiles(const void *opaque)
6ae9ed
     }
6ae9ed
 
6ae9ed
     /* construct the expect string */
6ae9ed
-    virBufferAdd(&buf, testStatusXMLPrefix, -1);
6ae9ed
+    virBufferAdd(&buf, header, -1);
6ae9ed
     virBufferAdjustIndent(&buf, 2);
6ae9ed
     virBufferAddStr(&buf, outActiveFile);
6ae9ed
     virBufferAdjustIndent(&buf, -2);
6ae9ed
@@ -175,6 +221,7 @@ testCompareStatusXMLToXMLFiles(const void *opaque)
6ae9ed
     VIR_FREE(actual);
6ae9ed
     VIR_FREE(source);
6ae9ed
     VIR_FREE(inFile);
6ae9ed
+    VIR_FREE(header);
6ae9ed
     VIR_FREE(outActiveFile);
6ae9ed
     return ret;
6ae9ed
 }
6ae9ed
@@ -187,6 +234,9 @@ testInfoFree(struct testInfo *info)
6ae9ed
     VIR_FREE(info->outActiveName);
6ae9ed
     VIR_FREE(info->outInactiveName);
6ae9ed
 
6ae9ed
+    virBitmapFree(info->activeVcpus);
6ae9ed
+    info->activeVcpus = NULL;
6ae9ed
+
6ae9ed
     virObjectUnref(info->qemuCaps);
6ae9ed
 }
6ae9ed
 
6ae9ed
@@ -261,6 +311,8 @@ mymain(void)
6ae9ed
     struct testInfo info;
6ae9ed
     virQEMUDriverConfigPtr cfg = NULL;
6ae9ed
 
6ae9ed
+    memset(&info, 0, sizeof(info));
6ae9ed
+
6ae9ed
     if (qemuTestDriverInit(&driver) < 0)
6ae9ed
         return EXIT_FAILURE;
6ae9ed
 
6ae9ed
-- 
6ae9ed
2.10.0
6ae9ed