Exemplo n.º 1
0
    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(qdevices.QDevice("ahci", {'id': 'in_bridge'},
                                     parent_bus={'type': ('PCI', 'PCIE'),
                                                 'aobject': 'pci_bridge'}))

        qdev.insert(qdevices.QDevice("ahci", {'id': 'in_switch1'},
                                     parent_bus={'type': ('PCI', 'PCIE'),
                                                 'aobject': 'pci_switch'}))
        qdev.insert(qdevices.QDevice("ahci", {'id': 'in_switch2'},
                                     parent_bus={'type': ('PCI', 'PCIE'),
                                                 'aobject': 'pci_switch'}))
        qdev.insert(qdevices.QDevice("ahci", {'id': 'in_switch3'},
                                     parent_bus={'type': ('PCI', 'PCIE'),
                                                 'aobject': 'pci_switch'}))

        qdev.insert(qdevices.QDevice("ahci", {'id': 'in_root1'},
                                     parent_bus={'type': ('PCI', 'PCIE'),
                                                 'aobject': 'root.1'}))

        qdev.insert(qdevices.QDevice("ahci", {'id': 'in_pci.0'},
                                     parent_bus={'type': ('PCI', 'PCIE'),
                                                 'aobject': 'pci.0'}))

        exp = ("-machine 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)
Exemplo n.º 2
0
    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 = qdevices.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 = qdevices.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 = qdevices.QDevice('dev1', {'id': 'dev1'})
        dev.hotplug = lambda _monitor: ""   # override the hotplug method
        dev.verify_hotplug = lambda _out, _monitor: True
        qdev3.simple_hotplug(dev, monitor)
        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()))
Exemplo n.º 3
0
    def test_q_device(self):
        """ QDevice tests """
        qdevice = qdevices.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))
Exemplo n.º 4
0
    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()))
Exemplo n.º 5
0
    def test_usb_bus(self):
        """ Tests the specific handlings of QUSBBus """
        usbc1 = qbuses.QUSBBus(2, 'usb1.0', 'uhci')

        # Insert device into usb controller, default port
        dev = qdevices.QDevice('usb-kbd', parent_bus={'type': 'uhci'})
        assert usbc1.insert(dev) == []

        # Insert usb-hub into usb controller, default port
        dev = qdevices.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 = qdevices.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 = qdevices.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 = qdevices.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 = qdevices.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(qdevices.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(qdevices.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(qdevices.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 = qdevices.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())