Merge "Use assertXmlEqual() helper for all XML comparison tests"

This commit is contained in:
Zuul 2019-03-19 20:23:04 +00:00 committed by Gerrit Code Review
commit 05a01a14b7
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.tests import fixtures as nova_fixtures
from nova.tests.unit import conf_fixture
from nova.tests.unit import matchers
from nova.tests.unit import policy_fixture
from nova import utils
from nova.virt import images
@ -511,6 +512,9 @@ class TestCase(testtools.TestCase):
error.difference = difference
raise error
def assertXmlEqual(self, expected, observed):
self.assertThat(observed, matchers.XMLMatches(expected))
def assertPublicAPISignatures(self, baseinst, inst):
def get_public_apis(inst):
methods = {}

View File

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

View File

@ -18,7 +18,6 @@ import six
from nova.objects import fields as obj_fields
from nova import test
from nova.tests.unit import matchers
import nova.tests.unit.virt.libvirt.fakelibvirt as libvirt
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_vf = pci_info.get_device_by_name('pci_0000_81_00_1')
self.assertThat(gen_pf.XMLDesc(0), matchers.XMLMatches(pf_xml))
self.assertThat(gen_vf.XMLDesc(0), matchers.XMLMatches(vf_xml))
self.assertXmlEqual(gen_pf.XMLDesc(0), pf_xml)
self.assertXmlEqual(gen_vf.XMLDesc(0), vf_xml)
# parse the generated xml with a libvirt config class and compare
# device address

View File

@ -25,7 +25,6 @@ from nova import exception
from nova.network import model as network_model
from nova import objects
from nova import test
from nova.tests.unit import matchers
from nova.tests.unit.virt.libvirt import fakelibvirt
from nova.virt.libvirt import config as vconfig
from nova.virt.libvirt import guest as libvirt_guest
@ -132,7 +131,7 @@ class UtilityMigrationTestCase(test.NoDBTestCase):
encoding='unicode')
new_xml = xml.replace("127.0.0.1", "127.0.0.100").replace(
"2000", "2001")
self.assertThat(res, matchers.XMLMatches(new_xml))
self.assertXmlEqual(res, new_xml)
def test_update_serial_xml_console(self):
data = objects.LibvirtLiveMigrateData(
@ -156,7 +155,7 @@ class UtilityMigrationTestCase(test.NoDBTestCase):
new_xml = xml.replace("127.0.0.1", "127.0.0.100").replace(
"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):
# 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),
encoding='unicode')
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):
data = objects.LibvirtLiveMigrateData(
@ -201,7 +200,7 @@ class UtilityMigrationTestCase(test.NoDBTestCase):
encoding='unicode')
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")
self.assertThat(res, matchers.XMLMatches(new_xml))
self.assertXmlEqual(res, new_xml)
def test_update_volume_xml(self):
connection_info = {
@ -250,7 +249,7 @@ class UtilityMigrationTestCase(test.NoDBTestCase):
doc, data, get_volume_config), encoding='unicode')
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')
self.assertThat(res, matchers.XMLMatches(new_xml))
self.assertXmlEqual(res, new_xml)
def test_update_volume_xml_keeps_address(self):
# 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')
new_xml = xml.replace('sdb',
'sdc')
self.assertThat(res, matchers.XMLMatches(new_xml))
self.assertXmlEqual(res, new_xml)
def test_update_volume_xml_add_encryption(self):
connection_info = {
@ -404,7 +403,7 @@ class UtilityMigrationTestCase(test.NoDBTestCase):
doc = etree.fromstring(xml)
res = etree.tostring(migration._update_volume_xml(
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):
connection_info = {
@ -474,7 +473,7 @@ class UtilityMigrationTestCase(test.NoDBTestCase):
doc, data, get_volume_config), encoding='unicode')
new_xml = xml.replace(uuids.encryption_secret_uuid_old,
uuids.encryption_secret_uuid_new)
self.assertThat(res, matchers.XMLMatches(new_xml))
self.assertXmlEqual(res, new_xml)
def test_update_perf_events_xml(self):
data = objects.LibvirtLiveMigrateData(
@ -489,10 +488,10 @@ class UtilityMigrationTestCase(test.NoDBTestCase):
res = etree.tostring(migration._update_perf_events_xml(doc, data),
encoding='unicode')
self.assertThat(res, matchers.XMLMatches("""<domain>
self.assertXmlEqual(res, """<domain>
<perf>
<event enabled="yes" name="cmt"/></perf>
</domain>"""))
</domain>""")
def test_update_perf_events_xml_add_new_events(self):
data = objects.LibvirtLiveMigrateData(
@ -503,8 +502,8 @@ class UtilityMigrationTestCase(test.NoDBTestCase):
res = etree.tostring(migration._update_perf_events_xml(doc, data),
encoding='unicode')
self.assertThat(res, matchers.XMLMatches("""<domain>
<perf><event enabled="yes" name="cmt"/></perf></domain>"""))
self.assertXmlEqual(res, """<domain>
<perf><event enabled="yes" name="cmt"/></perf></domain>""")
def test_update_perf_events_xml_add_new_events1(self):
data = objects.LibvirtLiveMigrateData(
@ -518,10 +517,10 @@ class UtilityMigrationTestCase(test.NoDBTestCase):
res = etree.tostring(migration._update_perf_events_xml(doc, data),
encoding='unicode')
self.assertThat(res, matchers.XMLMatches("""<domain>
self.assertXmlEqual(res, """<domain>
<perf>
<event enabled="yes" name="cmt"/><event enabled="yes" name="mbml"/></perf>
</domain>"""))
</domain>""")
def test_update_perf_events_xml_remove_all_events(self):
data = objects.LibvirtLiveMigrateData(
@ -535,10 +534,10 @@ class UtilityMigrationTestCase(test.NoDBTestCase):
res = etree.tostring(migration._update_perf_events_xml(doc, data),
encoding='unicode')
self.assertThat(res, matchers.XMLMatches("""<domain>
self.assertXmlEqual(res, """<domain>
<perf>
</perf>
</domain>"""))
</domain>""")
def test_update_memory_backing_xml_remove(self):
data = objects.LibvirtLiveMigrateData(
@ -554,9 +553,9 @@ class UtilityMigrationTestCase(test.NoDBTestCase):
res = etree.tostring(migration._update_memory_backing_xml(doc, data),
encoding='unicode')
self.assertThat(res, matchers.XMLMatches("""<domain>
self.assertXmlEqual(res, """<domain>
<memoryBacking/>
</domain>"""))
</domain>""")
def test_update_memory_backing_xml_add(self):
data = objects.LibvirtLiveMigrateData(
@ -566,13 +565,13 @@ class UtilityMigrationTestCase(test.NoDBTestCase):
res = etree.tostring(migration._update_memory_backing_xml(doc, data),
encoding='unicode')
self.assertThat(res, matchers.XMLMatches("""<domain>
self.assertXmlEqual(res, """<domain>
<memoryBacking>
<source type="file"/>
<access mode="shared"/>
<allocation mode="immediate"/>
</memoryBacking>
</domain>"""))
</domain>""")
def test_update_memory_backing_xml_keep(self):
data = objects.LibvirtLiveMigrateData(
@ -589,13 +588,13 @@ class UtilityMigrationTestCase(test.NoDBTestCase):
res = etree.tostring(migration._update_memory_backing_xml(doc, data),
encoding='unicode')
self.assertThat(res, matchers.XMLMatches("""<domain>
self.assertXmlEqual(res, """<domain>
<memoryBacking>
<source type="file"/>
<access mode="shared"/>
<allocation mode="immediate"/>
</memoryBacking>
</domain>"""))
</domain>""")
def test_update_memory_backing_discard_add(self):
data = objects.LibvirtLiveMigrateData(
@ -612,14 +611,14 @@ class UtilityMigrationTestCase(test.NoDBTestCase):
res = etree.tostring(migration._update_memory_backing_xml(doc, data),
encoding='unicode')
self.assertThat(res, matchers.XMLMatches("""<domain>
self.assertXmlEqual(res, """<domain>
<memoryBacking>
<source type="file"/>
<access mode="shared"/>
<allocation mode="immediate"/>
<discard />
</memoryBacking>
</domain>"""))
</domain>""")
def test_update_memory_backing_discard_remove(self):
data = objects.LibvirtLiveMigrateData(
@ -638,13 +637,13 @@ class UtilityMigrationTestCase(test.NoDBTestCase):
res = etree.tostring(migration._update_memory_backing_xml(doc, data),
encoding='unicode')
self.assertThat(res, matchers.XMLMatches("""<domain>
self.assertXmlEqual(res, """<domain>
<memoryBacking>
<source type="file"/>
<access mode="shared"/>
<allocation mode="immediate"/>
</memoryBacking>
</domain>"""))
</domain>""")
def test_update_memory_backing_discard_keep(self):
data = objects.LibvirtLiveMigrateData(
@ -662,14 +661,14 @@ class UtilityMigrationTestCase(test.NoDBTestCase):
res = etree.tostring(migration._update_memory_backing_xml(doc, data),
encoding='unicode')
self.assertThat(res, matchers.XMLMatches("""<domain>
self.assertXmlEqual(res, """<domain>
<memoryBacking>
<source type="file"/>
<access mode="shared"/>
<allocation mode="immediate"/>
<discard />
</memoryBacking>
</domain>"""))
</domain>""")
def _test_update_vif_xml(self, conf, original_xml, expected_xml):
"""Simulates updating the guest xml for live migrating from a host
@ -700,7 +699,7 @@ class UtilityMigrationTestCase(test.NoDBTestCase):
updated_xml = etree.tostring(
migration._update_vif_xml(doc, data, get_vif_config),
encoding='unicode')
self.assertThat(updated_xml, matchers.XMLMatches(expected_xml))
self.assertXmlEqual(updated_xml, expected_xml)
def test_update_vif_xml_to_vhostuser(self):
conf = vconfig.LibvirtConfigGuestInterface()

View File

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