Beispiel #1
0
    def test_q_base_device(self):
        """ QBaseDevice tests """
        qdevice = qdevices.QBaseDevice('MyType',
                                       {'ParamA': 'ValueA',
                                        'AUTOREMOVE': None},
                                       'Object1',
                                       {'type': 'pci'})
        self.assertEqual(qdevice['ParamA'], 'ValueA', 'Param added during '
                         '__init__ is corrupted %s != %s' % (qdevice['ParamA'],
                                                             'ValueA'))
        qdevice['ParamA'] = 'ValueB'
        qdevice.set_param('BoolTrue', True)
        qdevice.set_param('BoolFalse', 'off', bool)
        qdevice['Empty'] = 'EMPTY_STRING'

        out = """MyType
  aid = None
  aobject = Object1
  parent_bus = {'type': 'pci'}
  child_bus = []
  params:
    ParamA = ValueB
    BoolTrue = on
    BoolFalse = off
    Empty = ""
"""
        self.assertEqual(qdevice.str_long(), out, "Device output doesn't match"
                         "\n%s\n\n%s" % (qdevice.str_long(), out))
Beispiel #2
0
    def test_qdev_hotplug(self):
        """ Test the hotplug/unplug functionality """
        qdev = self.create_qdev('vm1', False, True)
        devs = qdev.machine_by_params(ParamsDict({'machine_type': 'pc'}))
        for dev in devs:
            qdev.insert(dev)
        monitor = MockHMPMonitor()

        out = qdev.get_state()
        assert out == -1, ("Status after init is not -1"
                           " (%s)" % out)
        out = len(qdev)
        assert out == 6, "Number of devices of this VM is not 5 (%s)" % out

        dev1, dev2 = qdev.images_define_by_variables('disk', '/tmp/a',
                                                     fmt="virtio")

        out = dev1.hotplug_hmp()
        exp = "drive_add auto id=drive_disk,if=none,file=/tmp/a"
        assert out == exp, ("Hotplug command of drive is incorrect:\n%s\n%s"
                            % (exp, out))

        # hotplug of drive will return "  OK" (pass)
        dev1.hotplug = lambda _monitor: "OK"
        dev1.verify_hotplug = lambda _out, _monitor: True
        out, ver_out = qdev.simple_hotplug(dev1, monitor)
        assert out == "OK", "Return value of hotplug is not OK (%s)" % out
        assert ver_out is True, ("Return value of hotplug"
                                 " is not True (%s)" % ver_out)
        out = qdev.get_state()
        assert out == 0, ("Status after verified hotplug is not 0 (%s)" % out)

        # hotplug of virtio-blk-pci will return ""
        out = dev2.hotplug_hmp()
        exp = "device_add virtio-blk-pci,id=disk,drive=drive_disk"
        assert out == exp, ("Hotplug command of device is incorrect:\n%s\n%s"
                            % (exp, out))
        dev2.hotplug = lambda _monitor: ""
        dev2.verify_hotplug = lambda _out, _monitor: ""
        out, ver_out = qdev.simple_hotplug(dev2, monitor)
        # automatic verification is not supported, hotplug returns the original
        # monitor message ("")
        assert ver_out == "", ("Return value of hotplug is"
                               " not "" (%s)" % ver_out)
        assert out == "", 'Return value of hotplug is not "" (%s)' % out
        out = qdev.get_state()
        assert out == 1, ("Status after verified hotplug is not 1 (%s)" % out)
        qdev.hotplug_verified()
        out = qdev.get_state()
        assert out == 0, ("Status after verified hotplug is not 0 (%s)" % out)

        out = len(qdev)
        assert out == 8, "Number of devices of this VM is not 8 (%s)" % out

        # Hotplug is expected to pass but monitor reports failure
        dev3 = qdevices.QDrive('a_dev1')
        dev3.hotplug = lambda _monitor: ("could not open disk image /tmp/qqq: "
                                         "No such file or directory")

        out, ver_out = qdev.simple_hotplug(dev3, monitor)
        exp = "could not open disk image /tmp/qqq: No such file or directory"
        assert out, "Return value of hotplug is incorrect:\n%s\n%s" % (out,
                                                                       exp)
        out = qdev.get_state()
        assert out == 1, ("Status after failed hotplug is not 1 (%s)" % out)
        # device is still in qdev, but is not in qemu, we should remove it
        qdev.remove(dev3, recursive=False)
        out = qdev.get_state()
        assert out == 1, ("Status after verified hotplug is not 1 (%s)" % out)
        qdev.hotplug_verified()
        out = qdev.get_state()
        assert out == 0, ("Status after verified hotplug is not 0 (%s)" % out)

        # Hotplug is expected to fail, qdev should stay unaffected
        dev4 = qdevices.QBaseDevice("bad_dev", parent_bus={'type': "XXX"})
        dev4.hotplug = lambda _monitor: ("")
        self.assertRaises(qcontainer.DeviceHotplugError, qdev.simple_hotplug,
                          dev4, True)
        out = qdev.get_state()
        assert out == 0, "Status after impossible hotplug is not 0 (%s)" % out

        # Unplug
        # Unplug used drive (automatic verification not supported)
        out = dev1.unplug_hmp()
        exp = "drive_del drive_disk"
        assert out == exp, ("Hotplug command of device is incorrect:\n%s\n%s"
                            % (exp, out))
        dev1.unplug = lambda _monitor: ""
        dev1.verify_unplug = lambda _monitor, _out: ""
        out, ver_out = qdev.simple_unplug(dev1, monitor)
        # I verified, that device was unplugged successfully
        qdev.hotplug_verified()
        out = qdev.get_state()
        assert out == 0, ("Status after verified hotplug is not 0 (%s)" % out)
        out = len(qdev)
        assert out == 7, "Number of devices of this VM is not 7 (%s)" % out
        # Removal of drive should also set drive of the disk device to None
        out = dev2.get_param('drive')
        assert out is None, "Drive was not removed from disk device"