[PATCH v2 04/12] Use virHashForEachSorted in tested code

Peter Krempa pkrempa at redhat.com
Wed Nov 4 17:05:40 UTC 2020


The simplest way to write tests is to check the output against expected
output, but we must ensure that the output is stable. We can use
virHashForEachSorted as a hash iterator to ensure stable ordering.

This patch fixes 3 instances of hash iteration which is tested in
various parts, including test output changes in appropriate places.

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 src/qemu/qemu_domain.c                        |   6 +-
 src/util/virmacmap.c                          |   2 +-
 tests/qemumonitorjsontest.c                   |   2 +-
 .../blockjob-blockdev-in.xml                  | 116 +++++++++---------
 4 files changed, 63 insertions(+), 63 deletions(-)

diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index d7dbca487a..e770940aca 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -2223,9 +2223,9 @@ qemuDomainObjPrivateXMLFormatBlockjobs(virBufferPtr buf,
                       virTristateBoolTypeToString(virTristateBoolFromBool(bj)));

     if (virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_BLOCKDEV) &&
-        virHashForEach(priv->blockjobs,
-                       qemuDomainObjPrivateXMLFormatBlockjobIterator,
-                       &iterdata) < 0)
+        virHashForEachSorted(priv->blockjobs,
+                             qemuDomainObjPrivateXMLFormatBlockjobIterator,
+                             &iterdata) < 0)
         return -1;

     virXMLFormatElement(buf, "blockjobs", &attrBuf, &childBuf);
diff --git a/src/util/virmacmap.c b/src/util/virmacmap.c
index 2d203e72af..fe71b06dd7 100644
--- a/src/util/virmacmap.c
+++ b/src/util/virmacmap.c
@@ -244,7 +244,7 @@ virMacMapDumpStrLocked(virMacMapPtr mgr,

     arr = virJSONValueNewArray();

-    if (virHashForEach(mgr->macs, virMACMapHashDumper, arr) < 0)
+    if (virHashForEachSorted(mgr->macs, virMACMapHashDumper, arr) < 0)
         goto cleanup;

     if (!(*str = virJSONValueToString(arr, true)))
diff --git a/tests/qemumonitorjsontest.c b/tests/qemumonitorjsontest.c
index 2a0377bae3..9231fa835a 100644
--- a/tests/qemumonitorjsontest.c
+++ b/tests/qemumonitorjsontest.c
@@ -2792,7 +2792,7 @@ testBlockNodeNameDetect(const void *opaque)
                                                       blockstatsJson)))
         goto cleanup;

-    virHashForEach(nodedata, testBlockNodeNameDetectFormat, &buf);
+    virHashForEachSorted(nodedata, testBlockNodeNameDetectFormat, &buf);

     virBufferTrim(&buf, "\n");

diff --git a/tests/qemustatusxml2xmldata/blockjob-blockdev-in.xml b/tests/qemustatusxml2xmldata/blockjob-blockdev-in.xml
index 8ffc91bdcb..c70742418b 100644
--- a/tests/qemustatusxml2xmldata/blockjob-blockdev-in.xml
+++ b/tests/qemustatusxml2xmldata/blockjob-blockdev-in.xml
@@ -234,9 +234,12 @@
   <allowReboot value='yes'/>
   <nodename index='0'/>
   <blockjobs active='yes'>
-    <blockjob name='pull-vdb-libvirt-3-format' type='pull' state='running'>
-      <disk dst='vdb'/>
-      <base node='libvirt-14-format'/>
+    <blockjob name='broken-test' type='broken' state='ready' brokentype='commit'/>
+    <blockjob name='commit-vdc-libvirt-9-format' type='commit' state='running' jobflags='0x0'>
+      <disk dst='vdc'/>
+      <base node='libvirt-11-format'/>
+      <top node='libvirt-9-format'/>
+      <topparent node='libvirt-2-format'/>
     </blockjob>
     <blockjob name='commit-vde-libvirt-17-format' type='active-commit' state='ready'>
       <disk dst='vde'/>
@@ -244,9 +247,61 @@
       <top node='libvirt-17-format'/>
       <deleteCommittedImages/>
     </blockjob>
+    <blockjob name='copy-vdd-libvirt-4321-format' type='copy' state='ready' jobflags='0x0' shallownew='yes'>
+      <disk dst='vdd'/>
+    </blockjob>
+    <blockjob name='create-libvirt-1337-storage' type='create' state='running'>
+      <create mode='storage'/>
+      <src type='network' format='qcow2'>
+        <source protocol='rbd' name='pool/volname.qcow2' tlsFromConfig='0' index='1337'>
+          <host name='example.org'/>
+          <privateData>
+            <nodenames>
+              <nodename type='storage' name='libvirt-1337-storage'/>
+              <nodename type='format' name='libvirt-1337-format'/>
+            </nodenames>
+            <objects>
+              <secret type='auth' alias='libvirt-1337-storage-secret0'/>
+            </objects>
+          </privateData>
+        </source>
+      </src>
+    </blockjob>
+    <blockjob name='create-libvirt-1338-format' type='create' state='running' jobflags='0xabcd'>
+      <chains>
+        <disk type='file' format='qcow2'>
+          <source file='/create/src1.qcow2' index='1339'>
+            <privateData>
+              <nodenames>
+                <nodename type='storage' name='libvirt-1339-storage'/>
+                <nodename type='format' name='libvirt-1339-format'/>
+              </nodenames>
+            </privateData>
+          </source>
+          <backingStore/>
+        </disk>
+      </chains>
+      <src type='file' format='qcow2'>
+        <source file='/tmp/create/overlay.qcow2' index='1338'>
+          <privateData>
+            <nodenames>
+              <nodename type='storage' name='libvirt-1338-storage'/>
+              <nodename type='format' name='libvirt-1338-format'/>
+            </nodenames>
+            <objects>
+              <secret type='encryption' alias='libvirt-1338-storage-secret0'/>
+            </objects>
+          </privateData>
+        </source>
+      </src>
+    </blockjob>
     <blockjob name='drive-virtio-disk0' type='copy' state='ready' jobflags='0x0'>
       <disk dst='vda' mirror='yes'/>
     </blockjob>
+    <blockjob name='pull-vdb-libvirt-3-format' type='pull' state='running'>
+      <disk dst='vdb'/>
+      <base node='libvirt-14-format'/>
+    </blockjob>
     <blockjob name='test-orphan-job0' type='copy' state='ready'>
       <chains>
         <disk type='file' format='qcow2'>
@@ -284,61 +339,6 @@
         </mirror>
       </chains>
     </blockjob>
-    <blockjob name='broken-test' type='broken' state='ready' brokentype='commit'/>
-    <blockjob name='create-libvirt-1338-format' type='create' state='running' jobflags='0xabcd'>
-      <chains>
-        <disk type='file' format='qcow2'>
-          <source file='/create/src1.qcow2' index='1339'>
-            <privateData>
-              <nodenames>
-                <nodename type='storage' name='libvirt-1339-storage'/>
-                <nodename type='format' name='libvirt-1339-format'/>
-              </nodenames>
-            </privateData>
-          </source>
-          <backingStore/>
-        </disk>
-      </chains>
-      <src type='file' format='qcow2'>
-        <source file='/tmp/create/overlay.qcow2' index='1338'>
-          <privateData>
-            <nodenames>
-              <nodename type='storage' name='libvirt-1338-storage'/>
-              <nodename type='format' name='libvirt-1338-format'/>
-            </nodenames>
-            <objects>
-              <secret type='encryption' alias='libvirt-1338-storage-secret0'/>
-            </objects>
-          </privateData>
-        </source>
-      </src>
-    </blockjob>
-    <blockjob name='commit-vdc-libvirt-9-format' type='commit' state='running' jobflags='0x0'>
-      <disk dst='vdc'/>
-      <base node='libvirt-11-format'/>
-      <top node='libvirt-9-format'/>
-      <topparent node='libvirt-2-format'/>
-    </blockjob>
-    <blockjob name='create-libvirt-1337-storage' type='create' state='running'>
-      <create mode='storage'/>
-      <src type='network' format='qcow2'>
-        <source protocol='rbd' name='pool/volname.qcow2' tlsFromConfig='0' index='1337'>
-          <host name='example.org'/>
-          <privateData>
-            <nodenames>
-              <nodename type='storage' name='libvirt-1337-storage'/>
-              <nodename type='format' name='libvirt-1337-format'/>
-            </nodenames>
-            <objects>
-              <secret type='auth' alias='libvirt-1337-storage-secret0'/>
-            </objects>
-          </privateData>
-        </source>
-      </src>
-    </blockjob>
-    <blockjob name='copy-vdd-libvirt-4321-format' type='copy' state='ready' jobflags='0x0' shallownew='yes'>
-      <disk dst='vdd'/>
-    </blockjob>
   </blockjobs>
   <agentTimeout>-2</agentTimeout>
   <domain type='kvm' id='4'>
-- 
2.26.2




More information about the libvir-list mailing list