tests: Cover Guest.find_device and Device.compare_device

Signed-off-by: Cole Robinson <crobinso@redhat.com>
This commit is contained in:
Cole Robinson 2020-01-27 07:38:53 -05:00
parent 3e5d530fce
commit ec580f82a2
2 changed files with 48 additions and 3 deletions

View File

@ -1504,3 +1504,36 @@ class XMLParseTest(unittest.TestCase):
with self.assertRaises(ValueError):
disk.validate()
def testGuestXMLDeviceMatch(self):
"""
Test Guest.find_device and Device.compare_device
"""
uri = utils.URIs.test_suite
conn = utils.URIs.openconn(uri)
dom = conn.lookupByName("test-for-virtxml")
xml = dom.XMLDesc(0)
guest = virtinst.Guest(conn, xml)
guest2 = virtinst.Guest(conn, xml)
# Assert id matching works
diskdev = guest.devices.disk[0]
assert guest.find_device(diskdev) == diskdev
# Assert type checking correct returns False
ifacedev = guest.devices.interface[0]
assert ifacedev.compare_device(diskdev, 0) is False
# Ensure parsed XML devices match correctly
for srcdev in guest.devices.get_all():
devxml = srcdev.get_xml()
newdev = srcdev.__class__(conn, devxml)
if srcdev != guest.find_device(newdev):
raise AssertionError("guest.find_device failed for dev=%s" %
newdev)
# Ensure devices from another parsed XML doc compare correctly
for srcdev in guest.devices.get_all():
if not guest2.find_device(srcdev):
raise AssertionError("guest.find_device failed for dev=%s" %
srcdev)

View File

@ -137,7 +137,7 @@ class Device(XMLBuilder):
"input": ["bus", "type", "xmlindex"],
"sound": ["model", "xmlindex"],
"video": ["model", "xmlindex"],
"watchdog": ["xmlindex"],
"watchdog": ["model", "xmlindex"],
"hostdev": ["type", "managed", "xmlindex",
"product", "vendor",
"function", "domain", "slot"],
@ -152,8 +152,9 @@ class Device(XMLBuilder):
"redirdev": ["bus", "type", "xmlindex"],
"tpm": ["type", "xmlindex"],
"rng": ["backend_model", "xmlindex"],
"panic": ["type", "xmlindex"],
"vsock": ["xmlindex"],
"panic": ["model", "xmlindex"],
"vsock": ["model", "xmlindex"],
"memballoon": ["model", "xmlindex"],
}
if id(self) == id(newdev):
@ -162,8 +163,19 @@ class Device(XMLBuilder):
if not isinstance(self, type(newdev)):
return False
if self.DEVICE_TYPE not in devprops: # pragma: no cover
return False
# Only compare against XML ID values, if both devices were
# taken from inside a complete guest hierarchy, otherwise
# things won't line up.
can_check_xml = ("devices" in newdev.get_xml_id() and
"devices" in self.get_xml_id())
for devprop in devprops[self.DEVICE_TYPE]:
if devprop == "xmlindex":
if not can_check_xml:
continue
origval = self.get_xml_idx()
newval = idx
else: