def testAddRemoveDevices(): conn = utils.URIs.open_testdefault_cached() guest, outfile = _get_test_content(conn, "add-devices") # Basic removal of existing device rmdev = guest.devices.disk[2] guest.remove_device(rmdev) # Basic device add d = virtinst.DeviceWatchdog(conn) d.set_defaults(guest) guest.add_device(d) # Test adding device with child properties (address value) adddev = virtinst.DeviceInterface(conn) adddev.type = "network" adddev.source = "default" adddev.macaddr = "1A:2A:3A:4A:5A:6A" adddev.address.type = "spapr-vio" adddev.set_defaults(guest) # Test adding and removing the same device guest.add_device(adddev) guest.remove_device(adddev) guest.add_device(adddev) # Test adding device built from parsed XML guest.add_device( virtinst.DeviceSound(conn, parsexml="""<sound model='pcspk'/>""")) _alter_compare(conn, guest.get_xml(), outfile)
def testGuestXMLDeviceMatch(): """ 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 # find_device should fail here nodev = virtinst.DeviceWatchdog(conn) assert guest.find_device(nodev) is None # 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)