def test_qdev_equal(self): qdev1 = self.create_qdev('vm1', allow_hotplugged_vm='no') qdev2 = self.create_qdev('vm1', allow_hotplugged_vm='no') qdev3 = self.create_qdev('vm1', allow_hotplugged_vm='yes') monitor = MockHMPMonitor() assert qdev1 == qdev2, ("Init qdevs are not alike\n%s\n%s" % (qdev1.str_long(), qdev2.str_long())) # Insert a device to qdev1 dev = qemu_devices.QDevice('dev1', {'id': 'dev1'}) qdev1.insert(dev) assert qdev1 != qdev2, ("Different qdevs match:\n%s\n%s" % (qdev1.str_long(), qdev2.str_long())) # Insert similar device to qdev2 dev = qemu_devices.QDevice('dev1', {'id': 'dev1'}) qdev2.insert(dev) assert qdev1 == qdev2, ("Similar qdevs are not alike\n%s\n%s" % (qdev1.str_long(), qdev2.str_long())) # Hotplug similar device to qdev3 dev = qemu_devices.QDevice('dev1', {'id': 'dev1'}) dev.hotplug = lambda _monitor: "" # override the hotplug method qdev3.hotplug(dev, monitor, False, False) assert qdev1 != qdev3, ("Similar hotplugged qdevs match even thought " "qdev3 has different state\n%s\n%s" % (qdev1.str_long(), qdev2.str_long())) qdev3.hotplug_verified() assert qdev1 == qdev3, ("Similar hotplugged qdevs are not alike\n%s\n" "%s" % (qdev1.str_long(), qdev2.str_long())) # Eq. is not symmetrical, qdev1 doesn't allow hotplugged VMs. assert qdev3 != qdev1, ("Similar hotplugged qdevs match even thought " "qdev1 doesn't allow hotplugged VM\n%s\n%s" % (qdev1.str_long(), qdev2.str_long())) qdev2.__qemu_help = "I support only this :-)" # pylint: disable=W0212 assert qdev1 == qdev2, ("qdevs of different qemu versions match:\n%s\n" "%s" % (qdev1.str_long(), qdev2.str_long()))
def test_pci(self): qdev = self.create_qdev('vm1') devs = qdev.machine_by_params(ParamsDict({'machine_type': 'pc'})) for dev in devs: qdev.insert(dev) # machine creates main pci (pci.0) # buses root.1 pci_switch pci_bridge # root.1: ioh3420(pci.0) # pci_switch: x3130(root.1) # pci_bridge: pci-bridge(root.1) devs = qdev.pcic_by_params('root.1', {'pci_bus': 'pci.0', 'type': 'ioh3420'}) qdev.insert(devs) devs = qdev.pcic_by_params('pci_switch', {'pci_bus': 'root.1', 'type': 'x3130'}) qdev.insert(devs) devs = qdev.pcic_by_params('pci_bridge', {'pci_bus': 'root.1', 'type': 'pci-bridge'}) qdev.insert(devs) qdev.insert(qemu_devices.QDevice("ahci", {'id': 'in_bridge'}, parent_bus={'type': ('PCI', 'PCIE'), 'aobject': 'pci_bridge'})) qdev.insert(qemu_devices.QDevice("ahci", {'id': 'in_switch1'}, parent_bus={'type': ('PCI', 'PCIE'), 'aobject': 'pci_switch'})) qdev.insert(qemu_devices.QDevice("ahci", {'id': 'in_switch2'}, parent_bus={'type': ('PCI', 'PCIE'), 'aobject': 'pci_switch'})) qdev.insert(qemu_devices.QDevice("ahci", {'id': 'in_switch3'}, parent_bus={'type': ('PCI', 'PCIE'), 'aobject': 'pci_switch'})) qdev.insert(qemu_devices.QDevice("ahci", {'id': 'in_root1'}, parent_bus={'type': ('PCI', 'PCIE'), 'aobject': 'root.1'})) qdev.insert(qemu_devices.QDevice("ahci", {'id': 'in_pci.0'}, parent_bus={'type': ('PCI', 'PCIE'), 'aobject': 'pci.0'})) exp = ("-M pc -device ioh3420,id=root.1,bus=pci.0,addr=02 " "-device x3130-upstream,id=pci_switch,bus=root.1,addr=00 " "-device pci-bridge,id=pci_bridge,bus=root.1,addr=01," "chassis_nr=1 -device ahci,id=in_bridge,bus=pci_bridge,addr=01" " -device xio3130-downstream,bus=pci_switch,id=pci_switch.0," "addr=00,chassis=1 -device ahci,id=in_switch1,bus=pci_switch.0" ",addr=00 " "-device xio3130-downstream,bus=pci_switch,id=pci_switch.1," "addr=01,chassis=2 -device ahci,id=in_switch2,bus=pci_switch.1" ",addr=00 " "-device xio3130-downstream,bus=pci_switch,id=pci_switch.2," "addr=02,chassis=3 -device ahci,id=in_switch3,bus=pci_switch.2" ",addr=00 " "-device ahci,id=in_root1,bus=root.1,addr=02 " "-device ahci,id=in_pci.0,bus=pci.0,addr=03") out = qdev.cmdline() assert out == exp, (out, exp)
def test_q_device(self): """ QDevice tests """ qdevice = qemu_devices.QDevice('ahci', {'addr': '0x7'}) self.assertEqual(str(qdevice), "a'ahci'", "Alternative name error %s " "!= %s" % (str(qdevice), "a'ahci'")) qdevice['id'] = 'ahci1' self.assertEqual(str(qdevice), "q'ahci1'", "Id name error %s " "!= %s" % (str(qdevice), "q'ahci1'")) exp = "device_add ahci,addr=0x7,id=ahci1" out = qdevice.hotplug_hmp() self.assertEqual(out, exp, "HMP command corrupted:\n%s\n%s" % (out, exp)) exp = ("('device_add', OrderedDict([('addr', '0x7'), " "('driver', 'ahci'), ('id', 'ahci1')]))") out = str(qdevice.hotplug_qmp()) self.assertEqual(out, exp, "QMP command corrupted:\n%s\n%s" % (out, exp))
def test_usb_bus(self): """ Tests the specific handlings of QUSBBus """ usbc1 = qemu_devices.QUSBBus(2, 'usb1.0', 'uhci') # Insert device into usb controller, default port self.assertTrue( usbc1.insert( qemu_devices.QDevice('usb-kbd', parent_bus={'type': 'uhci'}))) # Insert usb-hub into usb controller, default port dev = qemu_devices.QDevice('usb-hub', parent_bus={'type': 'uhci'}) self.assertTrue(usbc1.insert(dev)) hub1 = dev.child_bus[-1] # Insert usb-hub into usb-hub, exact port dev = qemu_devices.QDevice('usb-hub', {'port': '2.4'}, parent_bus={'type': 'uhci'}) self.assertTrue(hub1.insert(dev)) hub2 = dev.child_bus[-1] # Insert usb-hub into usb-hub in usb-hub, exact port dev = qemu_devices.QDevice('usb-hub', {'port': '2.4.3'}, parent_bus={'type': 'uhci'}) self.assertTrue(hub2.insert(dev)) hub3 = dev.child_bus[-1] # verify that port is updated correctly self.assertEqual("2.4.3", dev.get_param("port")) # Insert usb-device into usb-hub in usb-hub in usb-hub, exact port self.assertTrue( hub3.insert( qemu_devices.QDevice('usb-kbd', {'port': '2.4.3.1'}, parent_bus={'type': 'uhci'}))) # Insert usb-device into usb-hub in usb-hub in usb-hub, default port self.assertTrue( hub3.insert( qemu_devices.QDevice('usb-kbd', parent_bus={'type': 'uhci'}))) # Try to insert device into specific port which belongs to inferior bus self.assertFalse( hub2.insert( qemu_devices.QDevice('usb-kbd', {'port': '2.4.3.3'}, parent_bus={'type': 'uhci'}))) # Try to insert device into specific port which belongs to superior bus self.assertFalse( hub2.insert( qemu_devices.QDevice('usb-kbd', {'port': '2.4'}, parent_bus={'type': 'uhci'}))) # Try to insert device into specific port which belongs to same level # but different port self.assertFalse( hub2.insert( qemu_devices.QDevice('usb-kbd', {'port': '2.3.4'}, parent_bus={'type': 'uhci'}))) # Force insert device with port which belongs to other hub dev = qemu_devices.QDevice('usb-hub', {'port': '2.4.3.4'}, parent_bus={'type': 'uhci'}) out = hub2.insert(dev, force=True) res = bool("BusId" in out and "BasicAddress" in out) self.assertTrue( res, "Incorrect output of force insert:\n%s\nOutput" "have to contain BusId and BasicAddress (err)" % (out)) # verify that port is updated correctly self.assertEqual("2.4.1", dev.get_param("port")) # Check the overall buses correctness self.assertEqual("usb1.0(uhci): {1:a'usb-kbd',2:a'usb-hub'} {}", usbc1.str_short()) self.assertEqual("usb1.0(uhci): {4:a'usb-hub'} {}", hub1.str_short()) self.assertEqual("usb1.0(uhci): {1:a'usb-hub',3:a'usb-hub'} {}", hub2.str_short()) self.assertEqual("usb1.0(uhci): {1:a'usb-kbd',2:a'usb-kbd'} {}", hub3.str_short())
def test_qdev_low_level(self): """ Test low level functions """ qdev = self.create_qdev('vm1') # Representation state (used for hotplug or other nasty things) out = qdev.get_state() assert out == -1, "qdev state is incorrect %s != %s" % (out, 1) qdev.set_dirty() out = qdev.get_state() self.assertEqual(out, 1, "qdev state is incorrect %s != %s" % (out, 1)) qdev.set_dirty() out = qdev.get_state() self.assertEqual(out, 2, "qdev state is incorrect %s != %s" % (out, 1)) qdev.set_clean() out = qdev.get_state() self.assertEqual(out, 1, "qdev state is incorrect %s != %s" % (out, 1)) qdev.set_clean() out = qdev.get_state() self.assertEqual(out, 0, "qdev state is incorrect %s != %s" % (out, 1)) qdev.reset_state() out = qdev.get_state() assert out == -1, "qdev state is incorrect %s != %s" % (out, 1) # __create_unique_aid dev = qemu_devices.QDevice() qdev.insert(dev) out = dev.get_aid() self.assertEqual(out, '__0', "incorrect aid %s != %s" % (out, '__0')) dev = qemu_devices.QDevice(None, {'id': 'qid'}) qdev.insert(dev) out = dev.get_aid() self.assertEqual(out, 'qid', "incorrect aid %s != %s" % (out, 'qid')) dev = qemu_devices.QDevice(None, {'id': 'qid'}) qdev.insert(dev, True) out = dev.get_aid() self.assertEqual(out, 'qid__0', "incorrect aid %s != %s" % (out, 'qid__0')) dev = qemu_devices.QDevice(None, {'id': 'qid__1'}) qdev.insert(dev, True) out = dev.get_aid() self.assertEqual(out, 'qid__1', "incorrect aid %s != %s" % (out, 'qid__1')) dev = qemu_devices.QDevice(None, {'id': 'qid'}) qdev.insert(dev, True) out = dev.get_aid() self.assertEqual(out, 'qid__2', "incorrect aid %s != %s" % (out, 'qid__2')) # has_option out = qdev.has_option('device') self.assertEqual(out, True) out = qdev.has_option('missing_option') self.assertEqual(out, False) # has_device out = qdev.has_device('ide-drive') self.assertEqual(out, True) out = qdev.has_device('missing_device') self.assertEqual(out, False) # get_help_text out = qdev.get_help_text() self.assertEqual(out, QEMU_HELP) # has_hmp_cmd self.assertTrue(qdev.has_hmp_cmd('pcie_aer_inject_error')) self.assertTrue(qdev.has_hmp_cmd('c')) self.assertTrue(qdev.has_hmp_cmd('cont')) self.assertFalse(qdev.has_hmp_cmd('off')) self.assertFalse(qdev.has_hmp_cmd('\ndump-guest-memory')) self.assertFalse(qdev.has_hmp_cmd('The')) # has_qmp_cmd self.assertTrue(qdev.has_qmp_cmd('device_add')) self.assertFalse(qdev.has_qmp_cmd('RAND91')) # Add some buses bus1 = qemu_devices.QPCIBus('pci.0', 'pci', 'a_pci0') qdev.insert(qemu_devices.QDevice(params={'id': 'pci0'}, child_bus=bus1)) bus2 = qemu_devices.QPCIBus('pci.1', 'pci', 'a_pci1') qdev.insert(qemu_devices.QDevice(child_bus=bus2)) bus3 = qemu_devices.QPCIBus('pci.2', 'pci', 'a_pci2') qdev.insert(qemu_devices.QDevice(child_bus=bus3)) bus4 = qemu_devices.QPCIBus('pcie.0', 'pcie', 'a_pcie0') qdev.insert(qemu_devices.QDevice(child_bus=bus4)) # get_buses (all buses of this type) out = qdev.get_buses({'type': 'pci'}) self.assertEqual( len(out), 3, 'get_buses should return 3 buses but ' 'returned %s instead:\n%s' % (len(out), out)) # get_first_free_bus (last added bus of this type) out = qdev.get_first_free_bus({'type': 'pci'}, [None]) self.assertEqual(bus3, out) # fill the first pci bus for _ in xrange(32): qdev.insert(qemu_devices.QDevice(parent_bus={'type': 'pci'})) # get_first_free_bus (last one is full, return the previous one) out = qdev.get_first_free_bus({'type': 'pci'}, [None]) self.assertEqual(bus2, out) # list_named_buses out = qdev.list_missing_named_buses('pci.', 'pci', 5) self.assertEqual( len(out), 2, 'Number of missing named buses is ' 'incorrect: %s != %s\n%s' % (len(out), 2, out)) out = qdev.list_missing_named_buses('pci.', 'abc', 5) self.assertEqual( len(out), 5, 'Number of missing named buses is ' 'incorrect: %s != %s\n%s' % (len(out), 2, out)) # idx_of_next_named_bus out = qdev.idx_of_next_named_bus('pci.') self.assertEqual( out, 3, 'Incorrect idx of next named bus: %s !=' ' %s' % (out, 3)) # get_children dev = qemu_devices.QDevice(parent_bus={'aobject': 'a_pci0'}) bus = qemu_devices.QPCIBus('test1', 'test', 'a_test1') dev.add_child_bus(bus) bus = qemu_devices.QPCIBus('test2', 'test', 'a_test2') dev.add_child_bus(bus) qdev.insert(dev) qdev.insert(qemu_devices.QDevice(parent_bus={'aobject': 'a_test1'})) qdev.insert(qemu_devices.QDevice(parent_bus={'aobject': 'a_test2'})) out = dev.get_children() assert len(out) == 2, ("Not all children were listed %d != 2:\n%s" % (len(out), out)) out = bus.get_device() assert out == dev, ("bus.get_device() returned different device " "than the one in which it was plugged:\n" "%s\n%s\n%s" % (out.str_long(), dev.str_long(), qdev.str_long()))
def test_usb_bus(self): """ Tests the specific handlings of QUSBBus """ usbc1 = qemu_devices.QUSBBus(2, 'usb1.0', 'uhci') # Insert device into usb controller, default port dev = qemu_devices.QDevice('usb-kbd', parent_bus={'type': 'uhci'}) assert usbc1.insert(dev) == [] # Insert usb-hub into usb controller, default port dev = qemu_devices.QDevice('usb-hub', parent_bus={'type': 'uhci'}) assert usbc1.insert(dev) == [] hub1 = dev.child_bus[-1] # Insert usb-hub into usb-hub, exact port dev = qemu_devices.QDevice('usb-hub', {'port': '2.4'}, parent_bus={'type': 'uhci'}) assert hub1.insert(dev) == [] hub2 = dev.child_bus[-1] # Insert usb-hub into usb-hub in usb-hub, exact port dev = qemu_devices.QDevice('usb-hub', {'port': '2.4.3'}, parent_bus={'type': 'uhci'}) assert hub2.insert(dev) == [] hub3 = dev.child_bus[-1] # verify that port is updated correctly self.assertEqual("2.4.3", dev.get_param("port")) # Insert usb-device into usb-hub in usb-hub in usb-hub, exact port dev = qemu_devices.QDevice('usb-kbd', {'port': '2.4.3.1'}, parent_bus={'type': 'uhci'}) assert hub3.insert(dev) == [] # Insert usb-device into usb-hub in usb-hub in usb-hub, default port dev = qemu_devices.QDevice('usb-kbd', parent_bus={'type': 'uhci'}) assert hub3.insert(dev) == [] # Try to insert device into specific port which belongs to inferior bus out = hub2.insert(qemu_devices.QDevice('usb-kbd', {'port': '2.4.3.3'}, parent_bus={'type': 'uhci'})) assert out == "BusId" # Try to insert device into specific port which belongs to superior bus out = hub2.insert(qemu_devices.QDevice('usb-kbd', {'port': '2.4'}, parent_bus={'type': 'uhci'})) assert out == "BusId" # Try to insert device into specific port which belongs to same level # but different port out = hub2.insert(qemu_devices.QDevice('usb-kbd', {'port': '2.3.4'}, parent_bus={'type': 'uhci'})) assert out == "BusId" # Force insert device with port which belongs to other hub dev = qemu_devices.QDevice('usb-hub', {'port': '2.4.3.4'}, parent_bus={'type': 'uhci'}) # Check the overall buses correctness self.assertEqual("usb1.0(uhci): {1:a'usb-kbd',2:a'usb-hub'}", usbc1.str_short()) self.assertEqual("usb1.0(uhci): {4:a'usb-hub'}", hub1.str_short()) self.assertEqual("usb1.0(uhci): {3:a'usb-hub'}", hub2.str_short()) self.assertEqual("usb1.0(uhci): {1:a'usb-kbd',2:a'usb-kbd'}", hub3.str_short())