def test_q_pci_bus(self): """ PCI bus tests """ bus = qbuses.QPCIBus('pci.0', 'pci', 'my_pci') qdevice = qdevices.QDevice # Good devices params = {'addr': '0'} dev = qdevice('dev1', params, parent_bus={'type': 'pci'}) exp = [] out = bus.insert(dev, False) self.assertEqual(out, exp, "Failed to add device; %s != %s\n%s\n\n%s" % (out, exp, dev.str_long(), bus.str_long())) params = {'addr': 10, 'bus': 'pci.0'} dev = qdevice('dev2', params, parent_bus={'type': 'pci'}) exp = [] out = bus.insert(dev, False) self.assertEqual(out, exp, "Failed to add device; %s != %s\n%s\n\n%s" % (out, exp, dev.str_long(), bus.str_long())) params = {'addr': '0x1f'} dev = qdevice('dev3', params, parent_bus={'type': 'pci'}) exp = [] out = bus.insert(dev, False) self.assertEqual(out, exp, "Failed to add device; %s != %s\n%s\n\n%s" % (out, exp, dev.str_long(), bus.str_long())) # Compare short repr exp = ("pci.0(pci): {00-00:a'dev1',0a-00:a'dev2',1f-00:a'dev3'}") out = str(bus.str_short()) self.assertEqual(out, exp, "Short representation corrupted:\n%s\n%s" "\n\n%s" % (out, exp, bus.str_long())) # Incorrect records # Used address params = {'addr': 0} dev = qdevice('devI1', params, parent_bus={'type': 'pci'}) exp = "UsedSlot" out = bus.insert(dev, False) self.assertEqual(out, exp, "Added bad device; %s != %s\n%s\n\n%s" % (out, exp, dev.str_long(), bus.str_long())) # Out of range address params = {'addr': '0xffff'} dev = qdevice('devI2', params, parent_bus={'type': 'pci'}) exp = "BadAddr(False)" out = bus.insert(dev, False) self.assertEqual(out, exp, "Added bad device; %s != %s\n%s\n\n%s" % (out, exp, dev.str_long(), bus.str_long())) # Compare short repr exp = ("pci.0(pci): {00-00:a'dev1',0a-00:a'dev2',1f-00:a'dev3'}") out = str(bus.str_short()) self.assertEqual(out, exp, "Short representation corrupted:\n%s\n%s" "\n\n%s" % (out, exp, bus.str_long()))
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 = qdevices.QDevice() qdev.insert(dev) out = dev.get_aid() self.assertEqual(out, '__0', "incorrect aid %s != %s" % (out, '__0')) dev = qdevices.QDevice(None, {'id': 'qid'}) qdev.insert(dev) out = dev.get_aid() self.assertEqual(out, 'qid', "incorrect aid %s != %s" % (out, 'qid')) # 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 = qbuses.QPCIBus('pci.0', 'pci', 'a_pci0') qdev.insert(qdevices.QDevice(params={'id': 'pci0'}, child_bus=bus1)) bus2 = qbuses.QPCIBus('pci.1', 'pci', 'a_pci1') qdev.insert(qdevices.QDevice(child_bus=bus2)) bus3 = qbuses.QPCIBus('pci.2', 'pci', 'a_pci2') qdev.insert(qdevices.QDevice(child_bus=bus3)) bus4 = qbuses.QPCIBus('pcie.0', 'pcie', 'a_pcie0') qdev.insert(qdevices.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, None]) self.assertEqual(bus3, out) # fill the first pci bus for _ in xrange(32): qdev.insert(qdevices.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, 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 = qdevices.QDevice(parent_bus={'aobject': 'a_pci0'}) bus = qbuses.QPCIBus('test1', 'test', 'a_test1') dev.add_child_bus(bus) bus = qbuses.QPCIBus('test2', 'test', 'a_test2') dev.add_child_bus(bus) qdev.insert(dev) qdev.insert(qdevices.QDevice(parent_bus={'aobject': 'a_test1'})) qdev.insert(qdevices.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_q_pci_bus_strict(self): """ PCI bus tests in strict_mode (enforce additional options) """ bus = qbuses.QPCIBus('pci.0', 'pci', 'my_pci') qdevice = qdevices.QDevice params = {} bus.insert(qdevice('dev1', params, parent_bus={'type': 'pci'}), True) bus.insert(qdevice('dev2', params, parent_bus={'type': 'pci'}), True) bus.insert(qdevice('dev3', params, parent_bus={'type': 'pci'}), True) params = {'addr': '0x1f'} bus.insert(qdevice('dev1', params, parent_bus={'type': 'pci'}), True) params = {'addr': 30} bus.insert(qdevice('dev1', params, parent_bus={'type': 'pci'}), True) params = {'addr': 12} bus.insert(qdevice('dev1', params, parent_bus={'type': 'pci'}), True) # All devices will have 'addr' set as we are in the strict mode exp = """Bus pci.0, type=pci Slots: ---------------< 1e-00 >--------------- device aid = None aobject = None parent_bus = {'type': 'pci'} child_bus = [] params: addr = 1e driver = dev1 bus = pci.0 ---------------< 02-00 >--------------- device aid = None aobject = None parent_bus = {'type': 'pci'} child_bus = [] params: driver = dev3 bus = pci.0 addr = 02 ---------------< 1f-00 >--------------- device aid = None aobject = None parent_bus = {'type': 'pci'} child_bus = [] params: addr = 1f driver = dev1 bus = pci.0 ---------------< 00-00 >--------------- device aid = None aobject = None parent_bus = {'type': 'pci'} child_bus = [] params: driver = dev1 bus = pci.0 addr = 00 ---------------< 0c-00 >--------------- device aid = None aobject = None parent_bus = {'type': 'pci'} child_bus = [] params: addr = 0c driver = dev1 bus = pci.0 ---------------< 01-00 >--------------- device aid = None aobject = None parent_bus = {'type': 'pci'} child_bus = [] params: driver = dev2 bus = pci.0 addr = 01 """ out = str(bus.str_long()) self.assertEqual(out, exp, "Long representation corrupted:\n%s\n%s" % (out, exp))