def test_q_pci_bus(self): """ PCI bus tests """ bus = qemu_devices.QPCIBus('pci.0', 'pci', 'my_pci') qdevice = qemu_devices.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_q_pci_bus_strict(self): """ PCI bus tests in strict_mode (enforce additional options) """ bus = qemu_devices.QPCIBus('pci.0', 'pci', 'my_pci') qdevice = qemu_devices.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: ---------------< 0x0 >--------------- device aid = None aobject = None parent_bus = {'type': 'pci'} child_bus = [] params: driver = dev1 bus = pci.0 addr = 0x0 ---------------< 0x1 >--------------- device aid = None aobject = None parent_bus = {'type': 'pci'} child_bus = [] params: driver = dev2 bus = pci.0 addr = 0x1 ---------------< 0x2 >--------------- device aid = None aobject = None parent_bus = {'type': 'pci'} child_bus = [] params: driver = dev3 bus = pci.0 addr = 0x2 ---------------< 0x3 >--------------- None ---------------< 0x4 >--------------- None ---------------< 0x5 >--------------- None ---------------< 0x6 >--------------- None ---------------< 0x7 >--------------- None ---------------< 0x8 >--------------- None ---------------< 0x9 >--------------- None ---------------< 0xa >--------------- None ---------------< 0xb >--------------- None ---------------< 0xc >--------------- device aid = None aobject = None parent_bus = {'type': 'pci'} child_bus = [] params: addr = 0xc driver = dev1 bus = pci.0 ---------------< 0xd >--------------- None ---------------< 0xe >--------------- None ---------------< 0xf >--------------- None ---------------< 0x10 >--------------- None ---------------< 0x11 >--------------- None ---------------< 0x12 >--------------- None ---------------< 0x13 >--------------- None ---------------< 0x14 >--------------- None ---------------< 0x15 >--------------- None ---------------< 0x16 >--------------- None ---------------< 0x17 >--------------- None ---------------< 0x18 >--------------- None ---------------< 0x19 >--------------- None ---------------< 0x1a >--------------- None ---------------< 0x1b >--------------- None ---------------< 0x1c >--------------- None ---------------< 0x1d >--------------- None ---------------< 0x1e >--------------- device aid = None aobject = None parent_bus = {'type': 'pci'} child_bus = [] params: addr = 0x1e driver = dev1 bus = pci.0 ---------------< 0x1f >--------------- device aid = None aobject = None parent_bus = {'type': 'pci'} child_bus = [] params: addr = 0x1f driver = dev1 bus = pci.0 """ out = str(bus.str_long()) self.assertEqual( out, exp, "Long representation corrupted:\n%s\n%s" % (repr(out), exp))
def test_q_pci_bus(self): """ PCI bus tests """ bus = qemu_devices.QPCIBus('pci.0', 'pci', 'my_pci') qdevice = qemu_devices.QDevice # Good devices params = {'addr': '0'} dev = qdevice('dev1', params, parent_bus={'type': 'pci'}) exp = True out = bus.insert(dev, False, 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 = True out = bus.insert(dev, False, 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 = True out = bus.insert(dev, False, 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): [a'dev1',None,None,None,None,None,None,None,None," "None,a'dev2',None,None,None,None,None,None,None,None,None," "None,None,None,None,None,None,None,None,None,None,None," "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 = None out = bus.insert(dev, False, 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 = False out = bus.insert(dev, False, 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): [a'dev1',None,None,None,None,None,None,None,None," "None,a'dev2',None,None,None,None,None,None,None,None,None," "None,None,None,None,None,None,None,None,None,None,None," "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())) # Forced records # Used address params = {'addr': '0x0'} dev = qdevice('devB1', params, parent_bus={'type': 'pci'}) exp = "(errors: UsedSlot)" out = bus.insert(dev, False, True) self.assertEqual( exp in out, True, "%s not in %s\n%s\n\n%s" % (exp, out, dev.str_long(), bus.str_long())) # Out of range address params = {'addr': '0xffff'} dev = qdevice('devB2', params, parent_bus={'type': 'pci'}) exp = "(errors: BadAddr([65535]))" out = bus.insert(dev, False, True) self.assertEqual( exp in out, True, "%s not in %s\n%s\n\n%s" % (exp, out, dev.str_long(), bus.str_long())) # Incorrect bus name params = {'bus': 'other_bus'} dev = qdevice('devB3', params, parent_bus={'type': 'pci'}) exp = "(errors: BusId)" out = bus.insert(dev, False, True) self.assertEqual( exp in out, True, "%s not in %s\n%s\n\n%s" % (exp, out, dev.str_long(), bus.str_long())) # Compare short repr exp = ("pci.0(pci): [a'dev1',a'devB3',None,None,None,None,None,None," "None,None,a'dev2',None,None,None,None,None,None,None,None," "None,None,None,None,None,None,None,None,None,None,None,None," "a'dev3'] {0x0(2x):a'devB1',o0xffff:a'devB2'}") 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 = 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_q_pci_bus_strict(self): """ PCI bus tests in strict_mode (enforce additional options) """ bus = qemu_devices.QPCIBus('pci.0', 'pci', 'my_pci') qdevice = qemu_devices.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))