From ec580f82a25b49fdca22b8df2d3e1457bc3418bd Mon Sep 17 00:00:00 2001 From: Cole Robinson Date: Mon, 27 Jan 2020 07:38:53 -0500 Subject: [PATCH] tests: Cover Guest.find_device and Device.compare_device Signed-off-by: Cole Robinson --- tests/test_xmlparse.py | 33 +++++++++++++++++++++++++++++++++ virtinst/devices/device.py | 18 +++++++++++++++--- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/tests/test_xmlparse.py b/tests/test_xmlparse.py index ca97f0c0b..f2c462ab7 100644 --- a/tests/test_xmlparse.py +++ b/tests/test_xmlparse.py @@ -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) diff --git a/virtinst/devices/device.py b/virtinst/devices/device.py index 1946572d1..891411ae7 100644 --- a/virtinst/devices/device.py +++ b/virtinst/devices/device.py @@ -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: