Change in vdsm[master]: tests: Make it easy to debug assertXMLEqual failures

nsoffer at redhat.com nsoffer at redhat.com
Wed Jan 14 23:06:21 UTC 2015


Nir Soffer has uploaded a new change for review.

Change subject: tests: Make it easy to debug assertXMLEqual failures
......................................................................

tests: Make it easy to debug assertXMLEqual failures

The previous was normalizing xmls by replacing indentation with single
space, and compared normalized xml using assertEqual. This made it very
hard to debug failures:

    AssertionError: '<disk device="lun" sgio="unfiltered" snapshot="no"
    type="block"> <source dev="/dev/mapper/lun1-xxx"/> <target
    bus="scsi" dev="sda"/> <serial>54-a672-23e5b495a9ea</serial> <driver
    cache="none" error_policy="stop" io="native" name="qemu"
    type="raw"/> </disk>' != '<disk device="lun" sgio="unfiltered"
    snapshot="no" type="block"> <source dev="/dev/mapper/lun1"/> <target
    bus="scsi" dev="sda"/> <serial>54-a672-23e5b495a9ea</serial> <driver
    cache="none" error_policy="stop" io="native" name="qemu"
    type="raw"/> </disk>'

Now we normalize xmls by reindenting them, and when xmls do not match,
we display the indented xmls to make it easy to find the differences:

    AssertionError: XMLs are different:
    Actual:
    <disk device="lun" sgio="unfiltered" snapshot="no" type="block">
        <source dev="/dev/mapper/lun1-xxx" />
        <target bus="scsi" dev="sda" />
        <serial>54-a672-23e5b495a9ea</serial>
        <driver cache="none" error_policy="stop" io="native" name="qemu" type="raw" />
    </disk>

    Expected:
    <disk device="lun" sgio="unfiltered" snapshot="no" type="block">
        <source dev="/dev/mapper/lun1" />
        <target bus="scsi" dev="sda" />
        <serial>54-a672-23e5b495a9ea</serial>
        <driver cache="none" error_policy="stop" io="native" name="qemu" type="raw" />
    </disk>

Change-Id: I5eeaadcbb973fc30d41810fd5cfbc0ea67abe571
Signed-off-by: Nir Soffer <nsoffer at redhat.com>
---
M tests/vmTests.py
1 file changed, 44 insertions(+), 8 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/30/36930/1

diff --git a/tests/vmTests.py b/tests/vmTests.py
index 1c44ae1..f763cb2 100644
--- a/tests/vmTests.py
+++ b/tests/vmTests.py
@@ -55,6 +55,27 @@
 from testValidation import slowtest
 
 
+def indent(elem, level=0, s="    "):
+    """
+    Modify elem indentation in-place.
+
+    Based on http://effbot.org/zone/element-lib.htm#prettyprint
+    """
+    i = "\n" + level * s
+    if len(elem):
+        if not elem.text or not elem.text.strip():
+            elem.text = i + s
+        if not elem.tail or not elem.tail.strip():
+            elem.tail = i
+        for elem in elem:
+            indent(elem, level + 1, s)
+        if not elem.tail or not elem.tail.strip():
+            elem.tail = i
+    else:
+        if level and (not elem.tail or not elem.tail.strip()):
+            elem.tail = i
+
+
 class TestVm(TestCaseBase):
 
     PCI_ADDR = \
@@ -104,14 +125,29 @@
                      'memSize': '1024', 'memGuaranteedSize': '512'}
 
     def assertXMLEqual(self, element, expectedXML, path=None):
-        if path is None:
-            converted = element.toprettyxml()
-        else:
-            elem = ET.fromstring(element.toprettyxml())
-            converted = re.sub(' />', '/>',
-                               ET.tostring(elem.find("./%s" % path)))
-        self.assertEqual(re.sub('\n\s*', ' ', converted).strip(' '),
-                         re.sub('\n\s*', ' ', expectedXML).strip(' '))
+        """
+        Assert that serializing element generates expected xml, ignoring
+        whitespace differences.
+
+        If path is not None, find and compare the element at path instead of
+        element.
+
+        In case of a mismatch, display normalized xmls to make it easier to
+        find the differences.
+        """
+        actual = ET.fromstring(element.toxml())
+        if path:
+            actual = actual.find("./" + path)
+        indent(actual)
+        actualXML = ET.tostring(actual)
+
+        expected = ET.fromstring(expectedXML)
+        indent(expected)
+        expectedXML = ET.tostring(expected)
+
+        self.assertEqual(actualXML, expectedXML,
+                         "XMLs are different:\nActual:\n%s\nExpected:\n%s\n" %
+                         (actualXML, expectedXML))
 
     def assertXMLNone(self, element, path):
         elem = ET.fromstring(element.toprettyxml())


-- 
To view, visit http://gerrit.ovirt.org/36930
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I5eeaadcbb973fc30d41810fd5cfbc0ea67abe571
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Nir Soffer <nsoffer at redhat.com>


More information about the vdsm-patches mailing list