Use assertXmlEqual() helper for all XML comparison tests

assertXmlEqual() is a useful helper provided in LibvirtConfigBaseTest
which gives syntactic sugar for asserting that two XML strings are
equivalent XML trees.  However XML equivalence assertions also occur
in other code outside test_config.py, so move the helper to the
TestCase base class next to assertJsonEqual(), and then take advantage
of it in all other such tests.

Change-Id: I6ca9949e8cddf29d52ff8fc8c6416e5bd9d05799
This commit is contained in:
Adam Spiers 2019-03-07 21:53:07 +00:00
parent 131f7606c1
commit 060563864d
5 changed files with 37 additions and 38 deletions

View File

@ -59,6 +59,7 @@ from nova import objects
from nova.objects import base as objects_base from nova.objects import base as objects_base
from nova.tests import fixtures as nova_fixtures from nova.tests import fixtures as nova_fixtures
from nova.tests.unit import conf_fixture from nova.tests.unit import conf_fixture
from nova.tests.unit import matchers
from nova.tests.unit import policy_fixture from nova.tests.unit import policy_fixture
from nova import utils from nova import utils
from nova.virt import images from nova.virt import images
@ -511,6 +512,9 @@ class TestCase(testtools.TestCase):
error.difference = difference error.difference = difference
raise error raise error
def assertXmlEqual(self, expected, observed):
self.assertThat(observed, matchers.XMLMatches(expected))
def assertPublicAPISignatures(self, baseinst, inst): def assertPublicAPISignatures(self, baseinst, inst):
def get_public_apis(inst): def get_public_apis(inst):
methods = {} methods = {}

View File

@ -18,13 +18,11 @@ from oslo_utils import units
from nova.objects import fields as obj_fields from nova.objects import fields as obj_fields
from nova import test from nova import test
from nova.tests.unit import matchers
from nova.virt.libvirt import config from nova.virt.libvirt import config
class LibvirtConfigBaseTest(test.NoDBTestCase): class LibvirtConfigBaseTest(test.NoDBTestCase):
def assertXmlEqual(self, expectedXmlstr, actualXmlstr): pass
self.assertThat(actualXmlstr, matchers.XMLMatches(expectedXmlstr))
class LibvirtConfigTest(LibvirtConfigBaseTest): class LibvirtConfigTest(LibvirtConfigBaseTest):

View File

@ -18,7 +18,6 @@ import six
from nova.objects import fields as obj_fields from nova.objects import fields as obj_fields
from nova import test from nova import test
from nova.tests.unit import matchers
import nova.tests.unit.virt.libvirt.fakelibvirt as libvirt import nova.tests.unit.virt.libvirt.fakelibvirt as libvirt
from nova.virt.libvirt import config as vconfig from nova.virt.libvirt import config as vconfig
@ -486,8 +485,8 @@ class FakeLibvirtTests(test.NoDBTestCase):
gen_pf = pci_info.get_device_by_name('pci_0000_81_00_0') gen_pf = pci_info.get_device_by_name('pci_0000_81_00_0')
gen_vf = pci_info.get_device_by_name('pci_0000_81_00_1') gen_vf = pci_info.get_device_by_name('pci_0000_81_00_1')
self.assertThat(gen_pf.XMLDesc(0), matchers.XMLMatches(pf_xml)) self.assertXmlEqual(gen_pf.XMLDesc(0), pf_xml)
self.assertThat(gen_vf.XMLDesc(0), matchers.XMLMatches(vf_xml)) self.assertXmlEqual(gen_vf.XMLDesc(0), vf_xml)
# parse the generated xml with a libvirt config class and compare # parse the generated xml with a libvirt config class and compare
# device address # device address

View File

@ -25,7 +25,6 @@ from nova import exception
from nova.network import model as network_model from nova.network import model as network_model
from nova import objects from nova import objects
from nova import test from nova import test
from nova.tests.unit import matchers
from nova.tests.unit.virt.libvirt import fakelibvirt from nova.tests.unit.virt.libvirt import fakelibvirt
from nova.virt.libvirt import config as vconfig from nova.virt.libvirt import config as vconfig
from nova.virt.libvirt import guest as libvirt_guest from nova.virt.libvirt import guest as libvirt_guest
@ -132,7 +131,7 @@ class UtilityMigrationTestCase(test.NoDBTestCase):
encoding='unicode') encoding='unicode')
new_xml = xml.replace("127.0.0.1", "127.0.0.100").replace( new_xml = xml.replace("127.0.0.1", "127.0.0.100").replace(
"2000", "2001") "2000", "2001")
self.assertThat(res, matchers.XMLMatches(new_xml)) self.assertXmlEqual(res, new_xml)
def test_update_serial_xml_console(self): def test_update_serial_xml_console(self):
data = objects.LibvirtLiveMigrateData( data = objects.LibvirtLiveMigrateData(
@ -156,7 +155,7 @@ class UtilityMigrationTestCase(test.NoDBTestCase):
new_xml = xml.replace("127.0.0.1", "127.0.0.100").replace( new_xml = xml.replace("127.0.0.1", "127.0.0.100").replace(
"2001", "299").replace("2002", "300") "2001", "299").replace("2002", "300")
self.assertThat(res, matchers.XMLMatches(new_xml)) self.assertXmlEqual(res, new_xml)
def test_update_serial_xml_without_ports(self): def test_update_serial_xml_without_ports(self):
# This test is for backwards compatibility when we don't # This test is for backwards compatibility when we don't
@ -180,7 +179,7 @@ class UtilityMigrationTestCase(test.NoDBTestCase):
res = etree.tostring(migration._update_serial_xml(doc, data), res = etree.tostring(migration._update_serial_xml(doc, data),
encoding='unicode') encoding='unicode')
new_xml = xml.replace("127.0.0.1", "127.0.0.100") new_xml = xml.replace("127.0.0.1", "127.0.0.100")
self.assertThat(res, matchers.XMLMatches(new_xml)) self.assertXmlEqual(res, new_xml)
def test_update_graphics(self): def test_update_graphics(self):
data = objects.LibvirtLiveMigrateData( data = objects.LibvirtLiveMigrateData(
@ -201,7 +200,7 @@ class UtilityMigrationTestCase(test.NoDBTestCase):
encoding='unicode') encoding='unicode')
new_xml = xml.replace("127.0.0.1", "127.0.0.100") new_xml = xml.replace("127.0.0.1", "127.0.0.100")
new_xml = new_xml.replace("127.0.0.2", "127.0.0.200") new_xml = new_xml.replace("127.0.0.2", "127.0.0.200")
self.assertThat(res, matchers.XMLMatches(new_xml)) self.assertXmlEqual(res, new_xml)
def test_update_volume_xml(self): def test_update_volume_xml(self):
connection_info = { connection_info = {
@ -250,7 +249,7 @@ class UtilityMigrationTestCase(test.NoDBTestCase):
doc, data, get_volume_config), encoding='unicode') doc, data, get_volume_config), encoding='unicode')
new_xml = xml.replace('ip-1.2.3.4:3260-iqn.abc.12345.opst-lun-X', new_xml = xml.replace('ip-1.2.3.4:3260-iqn.abc.12345.opst-lun-X',
'ip-1.2.3.4:3260-iqn.cde.67890.opst-lun-Z') 'ip-1.2.3.4:3260-iqn.cde.67890.opst-lun-Z')
self.assertThat(res, matchers.XMLMatches(new_xml)) self.assertXmlEqual(res, new_xml)
def test_update_volume_xml_keeps_address(self): def test_update_volume_xml_keeps_address(self):
# Now test to make sure address isn't altered for virtio-scsi and rbd # Now test to make sure address isn't altered for virtio-scsi and rbd
@ -317,7 +316,7 @@ class UtilityMigrationTestCase(test.NoDBTestCase):
doc, data, get_volume_config), encoding='unicode') doc, data, get_volume_config), encoding='unicode')
new_xml = xml.replace('sdb', new_xml = xml.replace('sdb',
'sdc') 'sdc')
self.assertThat(res, matchers.XMLMatches(new_xml)) self.assertXmlEqual(res, new_xml)
def test_update_volume_xml_add_encryption(self): def test_update_volume_xml_add_encryption(self):
connection_info = { connection_info = {
@ -404,7 +403,7 @@ class UtilityMigrationTestCase(test.NoDBTestCase):
doc = etree.fromstring(xml) doc = etree.fromstring(xml)
res = etree.tostring(migration._update_volume_xml( res = etree.tostring(migration._update_volume_xml(
doc, data, get_volume_config), encoding='unicode') doc, data, get_volume_config), encoding='unicode')
self.assertThat(res, matchers.XMLMatches(new_xml)) self.assertXmlEqual(res, new_xml)
def test_update_volume_xml_update_encryption(self): def test_update_volume_xml_update_encryption(self):
connection_info = { connection_info = {
@ -474,7 +473,7 @@ class UtilityMigrationTestCase(test.NoDBTestCase):
doc, data, get_volume_config), encoding='unicode') doc, data, get_volume_config), encoding='unicode')
new_xml = xml.replace(uuids.encryption_secret_uuid_old, new_xml = xml.replace(uuids.encryption_secret_uuid_old,
uuids.encryption_secret_uuid_new) uuids.encryption_secret_uuid_new)
self.assertThat(res, matchers.XMLMatches(new_xml)) self.assertXmlEqual(res, new_xml)
def test_update_perf_events_xml(self): def test_update_perf_events_xml(self):
data = objects.LibvirtLiveMigrateData( data = objects.LibvirtLiveMigrateData(
@ -489,10 +488,10 @@ class UtilityMigrationTestCase(test.NoDBTestCase):
res = etree.tostring(migration._update_perf_events_xml(doc, data), res = etree.tostring(migration._update_perf_events_xml(doc, data),
encoding='unicode') encoding='unicode')
self.assertThat(res, matchers.XMLMatches("""<domain> self.assertXmlEqual(res, """<domain>
<perf> <perf>
<event enabled="yes" name="cmt"/></perf> <event enabled="yes" name="cmt"/></perf>
</domain>""")) </domain>""")
def test_update_perf_events_xml_add_new_events(self): def test_update_perf_events_xml_add_new_events(self):
data = objects.LibvirtLiveMigrateData( data = objects.LibvirtLiveMigrateData(
@ -503,8 +502,8 @@ class UtilityMigrationTestCase(test.NoDBTestCase):
res = etree.tostring(migration._update_perf_events_xml(doc, data), res = etree.tostring(migration._update_perf_events_xml(doc, data),
encoding='unicode') encoding='unicode')
self.assertThat(res, matchers.XMLMatches("""<domain> self.assertXmlEqual(res, """<domain>
<perf><event enabled="yes" name="cmt"/></perf></domain>""")) <perf><event enabled="yes" name="cmt"/></perf></domain>""")
def test_update_perf_events_xml_add_new_events1(self): def test_update_perf_events_xml_add_new_events1(self):
data = objects.LibvirtLiveMigrateData( data = objects.LibvirtLiveMigrateData(
@ -518,10 +517,10 @@ class UtilityMigrationTestCase(test.NoDBTestCase):
res = etree.tostring(migration._update_perf_events_xml(doc, data), res = etree.tostring(migration._update_perf_events_xml(doc, data),
encoding='unicode') encoding='unicode')
self.assertThat(res, matchers.XMLMatches("""<domain> self.assertXmlEqual(res, """<domain>
<perf> <perf>
<event enabled="yes" name="cmt"/><event enabled="yes" name="mbml"/></perf> <event enabled="yes" name="cmt"/><event enabled="yes" name="mbml"/></perf>
</domain>""")) </domain>""")
def test_update_perf_events_xml_remove_all_events(self): def test_update_perf_events_xml_remove_all_events(self):
data = objects.LibvirtLiveMigrateData( data = objects.LibvirtLiveMigrateData(
@ -535,10 +534,10 @@ class UtilityMigrationTestCase(test.NoDBTestCase):
res = etree.tostring(migration._update_perf_events_xml(doc, data), res = etree.tostring(migration._update_perf_events_xml(doc, data),
encoding='unicode') encoding='unicode')
self.assertThat(res, matchers.XMLMatches("""<domain> self.assertXmlEqual(res, """<domain>
<perf> <perf>
</perf> </perf>
</domain>""")) </domain>""")
def test_update_memory_backing_xml_remove(self): def test_update_memory_backing_xml_remove(self):
data = objects.LibvirtLiveMigrateData( data = objects.LibvirtLiveMigrateData(
@ -554,9 +553,9 @@ class UtilityMigrationTestCase(test.NoDBTestCase):
res = etree.tostring(migration._update_memory_backing_xml(doc, data), res = etree.tostring(migration._update_memory_backing_xml(doc, data),
encoding='unicode') encoding='unicode')
self.assertThat(res, matchers.XMLMatches("""<domain> self.assertXmlEqual(res, """<domain>
<memoryBacking/> <memoryBacking/>
</domain>""")) </domain>""")
def test_update_memory_backing_xml_add(self): def test_update_memory_backing_xml_add(self):
data = objects.LibvirtLiveMigrateData( data = objects.LibvirtLiveMigrateData(
@ -566,13 +565,13 @@ class UtilityMigrationTestCase(test.NoDBTestCase):
res = etree.tostring(migration._update_memory_backing_xml(doc, data), res = etree.tostring(migration._update_memory_backing_xml(doc, data),
encoding='unicode') encoding='unicode')
self.assertThat(res, matchers.XMLMatches("""<domain> self.assertXmlEqual(res, """<domain>
<memoryBacking> <memoryBacking>
<source type="file"/> <source type="file"/>
<access mode="shared"/> <access mode="shared"/>
<allocation mode="immediate"/> <allocation mode="immediate"/>
</memoryBacking> </memoryBacking>
</domain>""")) </domain>""")
def test_update_memory_backing_xml_keep(self): def test_update_memory_backing_xml_keep(self):
data = objects.LibvirtLiveMigrateData( data = objects.LibvirtLiveMigrateData(
@ -589,13 +588,13 @@ class UtilityMigrationTestCase(test.NoDBTestCase):
res = etree.tostring(migration._update_memory_backing_xml(doc, data), res = etree.tostring(migration._update_memory_backing_xml(doc, data),
encoding='unicode') encoding='unicode')
self.assertThat(res, matchers.XMLMatches("""<domain> self.assertXmlEqual(res, """<domain>
<memoryBacking> <memoryBacking>
<source type="file"/> <source type="file"/>
<access mode="shared"/> <access mode="shared"/>
<allocation mode="immediate"/> <allocation mode="immediate"/>
</memoryBacking> </memoryBacking>
</domain>""")) </domain>""")
def test_update_memory_backing_discard_add(self): def test_update_memory_backing_discard_add(self):
data = objects.LibvirtLiveMigrateData( data = objects.LibvirtLiveMigrateData(
@ -612,14 +611,14 @@ class UtilityMigrationTestCase(test.NoDBTestCase):
res = etree.tostring(migration._update_memory_backing_xml(doc, data), res = etree.tostring(migration._update_memory_backing_xml(doc, data),
encoding='unicode') encoding='unicode')
self.assertThat(res, matchers.XMLMatches("""<domain> self.assertXmlEqual(res, """<domain>
<memoryBacking> <memoryBacking>
<source type="file"/> <source type="file"/>
<access mode="shared"/> <access mode="shared"/>
<allocation mode="immediate"/> <allocation mode="immediate"/>
<discard /> <discard />
</memoryBacking> </memoryBacking>
</domain>""")) </domain>""")
def test_update_memory_backing_discard_remove(self): def test_update_memory_backing_discard_remove(self):
data = objects.LibvirtLiveMigrateData( data = objects.LibvirtLiveMigrateData(
@ -638,13 +637,13 @@ class UtilityMigrationTestCase(test.NoDBTestCase):
res = etree.tostring(migration._update_memory_backing_xml(doc, data), res = etree.tostring(migration._update_memory_backing_xml(doc, data),
encoding='unicode') encoding='unicode')
self.assertThat(res, matchers.XMLMatches("""<domain> self.assertXmlEqual(res, """<domain>
<memoryBacking> <memoryBacking>
<source type="file"/> <source type="file"/>
<access mode="shared"/> <access mode="shared"/>
<allocation mode="immediate"/> <allocation mode="immediate"/>
</memoryBacking> </memoryBacking>
</domain>""")) </domain>""")
def test_update_memory_backing_discard_keep(self): def test_update_memory_backing_discard_keep(self):
data = objects.LibvirtLiveMigrateData( data = objects.LibvirtLiveMigrateData(
@ -662,14 +661,14 @@ class UtilityMigrationTestCase(test.NoDBTestCase):
res = etree.tostring(migration._update_memory_backing_xml(doc, data), res = etree.tostring(migration._update_memory_backing_xml(doc, data),
encoding='unicode') encoding='unicode')
self.assertThat(res, matchers.XMLMatches("""<domain> self.assertXmlEqual(res, """<domain>
<memoryBacking> <memoryBacking>
<source type="file"/> <source type="file"/>
<access mode="shared"/> <access mode="shared"/>
<allocation mode="immediate"/> <allocation mode="immediate"/>
<discard /> <discard />
</memoryBacking> </memoryBacking>
</domain>""")) </domain>""")
def _test_update_vif_xml(self, conf, original_xml, expected_xml): def _test_update_vif_xml(self, conf, original_xml, expected_xml):
"""Simulates updating the guest xml for live migrating from a host """Simulates updating the guest xml for live migrating from a host
@ -700,7 +699,7 @@ class UtilityMigrationTestCase(test.NoDBTestCase):
updated_xml = etree.tostring( updated_xml = etree.tostring(
migration._update_vif_xml(doc, data, get_vif_config), migration._update_vif_xml(doc, data, get_vif_config),
encoding='unicode') encoding='unicode')
self.assertThat(updated_xml, matchers.XMLMatches(expected_xml)) self.assertXmlEqual(updated_xml, expected_xml)
def test_update_vif_xml_to_vhostuser(self): def test_update_vif_xml_to_vhostuser(self):
conf = vconfig.LibvirtConfigGuestInterface() conf = vconfig.LibvirtConfigGuestInterface()

View File

@ -30,7 +30,6 @@ from nova.network import model as network_model
from nova import objects from nova import objects
from nova.pci import utils as pci_utils from nova.pci import utils as pci_utils
from nova import test from nova import test
from nova.tests.unit import matchers
from nova.tests.unit.virt import fakelibosinfo from nova.tests.unit.virt import fakelibosinfo
from nova.tests.unit.virt.libvirt import fakelibvirt from nova.tests.unit.virt.libvirt import fakelibvirt
from nova.virt.libvirt import config as vconfig from nova.virt.libvirt import config as vconfig
@ -577,7 +576,7 @@ class LibvirtVifTestCase(test.NoDBTestCase):
def _assertXmlEqual(self, expectedXmlstr, actualXmlstr): def _assertXmlEqual(self, expectedXmlstr, actualXmlstr):
if not isinstance(actualXmlstr, six.string_types): if not isinstance(actualXmlstr, six.string_types):
actualXmlstr = etree.tostring(actualXmlstr, pretty_print=True) actualXmlstr = etree.tostring(actualXmlstr, pretty_print=True)
self.assertThat(actualXmlstr, matchers.XMLMatches(expectedXmlstr)) self.assertXmlEqual(actualXmlstr, expectedXmlstr)
def _get_conf(self): def _get_conf(self):
conf = vconfig.LibvirtConfigGuest() conf = vconfig.LibvirtConfigGuest()