diff --git a/tests/unit/test_xapidb_filter.py b/tests/unit/test_xapidb_filter.py index 1d9c5364..133d1e14 100644 --- a/tests/unit/test_xapidb_filter.py +++ b/tests/unit/test_xapidb_filter.py @@ -1,8 +1,8 @@ """tests/unit/test_xapidb_filter.py: Ensure that the xen-bugtool.DBFilter() filters the XAPI DB properly""" -# This uses the deprecated imp module because it has to run with Python2.7 for now: import os import sys import xml.dom.minidom +import xml.etree.ElementTree as ET import pytest @@ -55,9 +55,33 @@ """ +def assert_xml_element_trees_equiv(a, b): + """Assert that the contents of two XML ElementTrees are equivalent (recursive)""" + # Check the XML tag + assert a.tag == b.tag + # Check the XML text + assert (a.text or "").strip() == (b.text or "").strip() + # Check the XML tail + assert (a.tail or "").strip() == (b.tail or "").strip() + # Check the XML attributes + assert a.attrib.items() == b.attrib.items() + # Compare the number of child nodes + assert len(list(a)) == len(list(b)) + # Recursively repeat for all child nodes (expect same order of child nodes): + for achild, bchild in zip(list(a), list(b)): + assert_xml_element_trees_equiv(achild, bchild) + + @pytest.mark.skipif(sys.version_info >= (3, 0), reason="requires python2") def test_xapi_database_filter(bugtool): """Assert that bugtool.DBFilter().output() filters the xAPI database as expected""" filtered = bugtool.DBFilter(original).output() - assert xml.dom.minidom.parseString(filtered).toprettyxml(indent=" ") == expected + + # Works for Python2 equally, so we can use it to check against Python2/3 regressions: + assert_xml_element_trees_equiv(ET.fromstring(filtered), ET.fromstring(expected)) + + # Double-check with parseString(): Its output will differ between Py2/Py3 + # though, so we will use it for one language version at a time: + if sys.version_info < (3, 0): + assert xml.dom.minidom.parseString(filtered).toprettyxml(indent=" ") == expected