multi_disk_random_hotplug.py 15.7 KB
Newer Older
1 2 3 4 5 6 7
"""
multi_disk_random_hotplug test for Autotest framework.

:copyright: 2013 Red Hat Inc.
"""
import logging
import random
8
import time
9
from autotest.client.shared import error
10
from virttest import funcatexit, arch
11
from virttest import qemu_qtree, utils_test, env_process
12
from virttest.qemu_devices import utils
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59


def stop_stresser(vm, stop_cmd):
    """
    Wrapper which connects to vm and sends the stop_cmd
    :param vm: Virtual Machine
    :type vm: virttest.virt_vm.BaseVM
    :param stop_cmd: Command to stop the stresser
    :type stop_cmd: string
    """
    session = vm.wait_for_login(timeout=10)
    session.cmd(stop_cmd)
    session.close()


# TODO: Remove this silly function when qdev vs. qtree comparison is available
def convert_params(params, args):
    """
    Updates params according to images_define_by_params arguments.
    :note: This is only temporarily solution until qtree vs. qdev verification
           is available.
    :param params: Dictionary with the test parameters
    :type param: virttest.utils_params.Params
    :param args: Dictionary of images_define_by_params arguments
    :type args: dictionary
    :return: Updated dictionary with the test parameters
    :rtype: virttest.utils_params.Params
    """
    convert = {'fmt': 'drive_format', 'cache': 'drive_cache',
               'werror': 'drive_werror', 'rerror': 'drive_rerror',
               'serial': 'drive_serial', 'snapshot': 'image_snapshot',
               'bus': 'drive_bus', 'unit': 'drive_unit', 'port': 'drive_port',
               'readonly': 'image_readonly', 'scsiid': 'drive_scsiid',
               'lun': 'drive_lun', 'aio': 'image_aio',
               'imgfmt': 'image_format', 'pci_addr': 'drive_pci_addr',
               'x_data_plane': 'x-data-plane',
               'scsi': 'virtio-blk-pci_scsi'}
    name = args.pop('name')
    params['images'] += " %s" % name
    params['image_name_%s' % name] = args.pop('filename')
    params['image_raw_device_%s' % name] = 'yes'
    for key, value in args.iteritems():
        params["%s_%s" % (convert.get(key, key), name)] = value
    return params


@error.context_aware
60
def run(test, params, env):
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    """
    This tests the disk hotplug/unplug functionality.
    1) prepares multiple disks to be hotplugged
    2) hotplugs them
    3) verifies that they are in qtree/guest system/...
    4) unplugs them
    5) verifies they are not in qtree/guest system/...
    6) repeats $repeat_times
    *) During the whole test stress_cmd might be executed

    :param test: QEMU test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment.
    """
    def verify_qtree(params, info_qtree, info_block, proc_scsi, qdev):
        """
        Verifies that params, info qtree, info block and /proc/scsi/ matches
        :param params: Dictionary with the test parameters
        :type params: virttest.utils_params.Params
        :param info_qtree: Output of "info qtree" monitor command
        :type info_qtree: string
        :param info_block: Output of "info block" monitor command
        :type info_block: dict of dicts
        :param proc_scsi: Output of "/proc/scsi/scsi" guest file
        :type proc_scsi: string
86 87
        :param qdev: qcontainer representation
        :type qdev: virttest.qemu_devices.qcontainer.DevContainer
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
        """
        err = 0
        qtree = qemu_qtree.QtreeContainer()
        qtree.parse_info_qtree(info_qtree)
        disks = qemu_qtree.QtreeDisksContainer(qtree.get_nodes())
        (tmp1, tmp2) = disks.parse_info_block(info_block)
        err += tmp1 + tmp2
        err += disks.generate_params()
        err += disks.check_disk_params(params)
        (tmp1, tmp2, _, _) = disks.check_guests_proc_scsi(proc_scsi)
        err += tmp1 + tmp2
        if err:
            logging.error("info qtree:\n%s", info_qtree)
            logging.error("info block:\n%s", info_block)
            logging.error("/proc/scsi/scsi:\n%s", proc_scsi)
            logging.error(qdev.str_bus_long())
            raise error.TestFail("%s errors occurred while verifying"
                                 " qtree vs. params" % err)

    def insert_into_qdev(qdev, param_matrix, no_disks, params):
        """
        Inserts no_disks disks int qdev using randomized args from param_matrix
        :param qdev: qemu devices container
111
        :type qdev: virttest.qemu_devices.qcontainer.DevContainer
112 113 114 115 116 117 118 119 120
        :param param_matrix: Matrix of randomizable params
        :type param_matrix: list of lists
        :param no_disks: Desired number of disks
        :type no_disks: integer
        :param params: Dictionary with the test parameters
        :type params: virttest.utils_params.Params
        :return: (newly added devices, number of added disks)
        :rtype: tuple(list, integer)
        """
121
        error.context("Insert devices into qdev", logging.debug)
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
        new_devices = []
        _new_devs_fmt = ""
        _formats = param_matrix.pop('fmt', [params.get('drive_format')])
        formats = _formats[:]
        i = 0
        while i < no_disks:
            # Set the format
            if len(formats) < 1:
                logging.warn("Can't create desired number '%s' of disk types "
                             "'%s'. Using '%d' no disks.", no_disks,
                             _formats, i)
                break
            name = 'stg%d' % i
            args = {'name': name, 'filename': stg_image_name % i}
            fmt = random.choice(formats)
            if fmt == 'virtio_scsi':
                args['fmt'] = 'scsi-hd'
                args['scsi_hba'] = 'virtio-scsi-pci'
            elif fmt == 'lsi_scsi':
                args['fmt'] = 'scsi-hd'
                args['scsi_hba'] = 'lsi53c895a'
143 144 145
            elif fmt == 'spapr_vscsi':
                args['fmt'] = 'scsi-hd'
                args['scsi_hba'] = 'spapr-vscsi'
146 147 148 149 150 151 152 153 154
            else:
                args['fmt'] = fmt
            # Other params
            for key, value in param_matrix.iteritems():
                args[key] = random.choice(value)

            devs = qdev.images_define_by_variables(**args)
            try:
                for dev in devs:
155
                    qdev.insert(dev)
156
            except utils.DeviceInsertError:
157 158 159 160 161 162 163 164 165 166 167 168 169
                # All buses are full, (TODO add bus) or remove this format
                for dev in devs:
                    if dev in qdev:
                        qdev.remove(dev, recursive=True)
                formats.remove(fmt)
                continue

            # TODO: Modify check_disk_params to use vm.devices
            # 1) modify PCI bus to accept full pci addr (02.0, 01.3, ...)
            # 2) add all devices into qemu_devices according to qtree
            # 3) check qtree vs. qemu_devices PCI representation (+children)
            #    (use qtree vs devices, if key and value_qtree == value_devices
            #     match the device and remove it from comparison.
L
Lucas Meneghel Rodrigues 已提交
170
            #     Also use blacklist to remove unnecessary stuff (like
171 172 173 174 175 176 177 178 179 180
            #     kvmclock, smbus-eeprom, ... from qtree and drive, ... from
            #     devices)
            # => then modify this to use qtree verification
            params = convert_params(params, args)
            env_process.preprocess_image(test, params.object_params(name),
                                         name)
            new_devices.extend(devs)
            _new_devs_fmt += "%s(%s) " % (name, fmt)
            i += 1
        if _new_devs_fmt:
181
            logging.info("Using disks: %s", _new_devs_fmt[:-1])
182 183 184 185 186 187 188
        param_matrix['fmt'] = _formats
        return new_devices, params

    def hotplug_serial(new_devices, monitor):
        """
        Do the actual hotplug of the new_devices using monitor monitor.
        :param new_devices: List of devices which should be hotplugged
189
        :type new_devices: List of virttest.qemu_devices.qdevice.QBaseDevice
190 191 192
        :param monitor: Monitor which should be used for hotplug
        :type monitor: virttest.qemu_monitor.Monitor
        """
193
        error.context("Hotplug the devices", logging.debug)
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
        err = []
        for device in new_devices:
            time.sleep(float(params.get('wait_between_hotplugs', 0)))
            out = device.hotplug(monitor)
            out = device.verify_hotplug(out, monitor)
            err.append(out)
        if err == [True] * len(err):    # No failures or unverified states
            logging.debug("Hotplug status: verified %d", len(err))
            return
        failed = err.count(False)
        passed = err.count(True)
        unverif = len(err) - failed - passed
        if failed == 0:
            logging.warn("Hotplug status: verified %d, unverified %d", passed,
                         unverif)
        else:
            logging.error("Hotplug status: verified %d, unverified %d, failed "
                          "%d", passed, unverif, failed)
            raise error.TestFail("Hotplug of some devices failed.")

    def unplug_serial(new_devices, qdev, monitor):
        """
        Do the actual unplug of new_devices using monitor monitor
        :param new_devices: List of devices which should be hotplugged
218
        :type new_devices: List of virttest.qemu_devices.qdevice.QBaseDevice
219
        :param qdev: qemu devices container
220
        :type qdev: virttest.qemu_devices.qcontainer.DevContainer
221 222 223
        :param monitor: Monitor which should be used for hotplug
        :type monitor: virttest.qemu_monitor.Monitor
        """
224
        error.context("Unplug and remove the devices", logging.debug)
225 226 227 228 229 230
        failed = 0
        passed = 0
        unverif = 0
        for device in new_devices[::-1]:
            if device in qdev:
                time.sleep(float(params.get('wait_between_unplugs', 0)))
231 232 233 234 235 236 237 238 239 240 241
                _out = device.unplug(monitor)
                for _ in xrange(50):    # unplug waits for VM response
                    out = device.verify_unplug(_out, monitor)
                    if out is True:
                        break
                    time.sleep(0.1)
                # Remove from qdev even when unplug failed because further in
                # this test we compare VM with qdev, which should be without
                # these devices. We can do this because we already set the VM
                # as dirty.
                qdev.remove(device)
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
            else:
                continue
            if out is True:
                passed += 1
            elif out is False:
                failed += 1
            else:
                unverif += 1
        # remove the images
        _disks = []
        for disk in params['images'].split(' '):
            if disk.startswith("stg"):
                env_process.postprocess_image(test, params.object_params(disk),
                                              disk)
            else:
                _disks.append(disk)
        params['images'] = " ".join(_disks)
        if failed == 0 and unverif == 0:
            logging.debug("Unplug status: verified %d", passed)
        elif failed == 0:
            logging.warn("Unplug status: verified %d, unverified %d", passed,
                         unverif)
        else:
            logging.error("Unplug status: verified %d, unverified %d, failed "
                          "%d", passed, unverif, failed)
            raise error.TestFail("Unplug of some devices failed.")

    vm = env.get_vm(params['main_vm'])
    monitor = vm.monitor
    qdev = vm.devices
    session = vm.wait_for_login(timeout=int(params.get("login_timeout", 360)))
    out = vm.monitor.human_monitor_cmd("info qtree", debug=False)
    if "unknown command" in str(out):
        verify_qtree = lambda _1, _2, _3: logging.warn("info qtree not "
L
Lucas Meneghel Rodrigues 已提交
276 277
                                                       "supported. Can't verify qtree"
                                                       "vs. guest disks.")
278 279 280 281 282 283 284 285 286 287 288

    stg_image_name = params['stg_image_name']
    stg_image_num = int(params['stg_image_num'])
    stg_params = params.get('stg_params', '').split(' ')
    i = 0
    while i < len(stg_params) - 1:
        if not stg_params[i].strip():
            i += 1
            continue
        if stg_params[i][-1] == '\\':
            stg_params[i] = '%s %s' % (stg_params[i][:-1],
L
Lucas Meneghel Rodrigues 已提交
289
                                       stg_params.pop(i + 1))
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
        i += 1

    param_matrix = {}
    for i in xrange(len(stg_params)):
        if not stg_params[i].strip():
            continue
        (cmd, parm) = stg_params[i].split(':', 1)
        # ',' separated list of values
        parm = parm.split(',')
        j = 0
        while j < len(parm) - 1:
            if parm[j][-1] == '\\':
                parm[j] = '%s,%s' % (parm[j][:-1], parm.pop(j + 1))
            j += 1

        param_matrix[cmd] = parm

    # Modprobe the module if specified in config file
    module = params.get("modprobe_module")
    if module:
        session.cmd("modprobe %s" % module)

    stress_cmd = params.get('stress_cmd')
    if stress_cmd:
        funcatexit.register(env, params.get('type'), stop_stresser, vm,
                            params.get('stress_kill_cmd'))
        stress_session = vm.wait_for_login(timeout=10)
        for _ in xrange(int(params.get('no_stress_cmds', 1))):
            stress_session.sendline(stress_cmd)

    rp_times = int(params.get("repeat_times", 1))
    context_msg = "Running sub test '%s' %s"
322
    error.context("Verify disk before test", logging.info)
323 324 325 326 327 328 329 330 331 332 333 334
    info_qtree = vm.monitor.info('qtree', False)
    info_block = vm.monitor.info_block(False)
    proc_scsi = session.cmd_output('cat /proc/scsi/scsi')
    verify_qtree(params, info_qtree, info_block, proc_scsi, qdev)
    _images = params['images']
    for iteration in xrange(rp_times):
        sub_type = params.get("sub_type_before_plug")
        if sub_type:
            error.context(context_msg % (sub_type, "before hotplug"),
                          logging.info)
            utils_test.run_virt_sub_test(test, params, env, sub_type)

335 336
        error.context("Hotplugging/unplugging devices, iteration %d"
                      % iteration, logging.info)
337 338 339 340 341
        qdev.set_dirty()
        new_devices, params = insert_into_qdev(qdev, param_matrix,
                                               stg_image_num, params)
        hotplug_serial(new_devices, monitor)
        time.sleep(float(params.get('wait_after_hotplug', 0)))
342
        error.context("Verify disks after hotplug", logging.debug)
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
        info_qtree = vm.monitor.info('qtree', False)
        info_block = vm.monitor.info_block(False)
        proc_scsi = session.cmd_output('cat /proc/scsi/scsi')
        verify_qtree(params, info_qtree, info_block, proc_scsi, qdev)
        qdev.set_clean()

        sub_type = params.get("sub_type_after_plug")
        if sub_type:
            error.context(context_msg % (sub_type, "after hotplug"),
                          logging.info)
            utils_test.run_virt_sub_test(test, params, env, sub_type)

        sub_type = params.get("sub_type_before_unplug")
        if sub_type:
            error.context(context_msg % (sub_type, "before hotunplug"),
                          logging.info)
            utils_test.run_virt_sub_test(test, params, env, sub_type)
        unplug_serial(new_devices, qdev, monitor)
        time.sleep(float(params.get('wait_after_unplug', 0)))
362
        error.context("Verify disks after unplug", logging.debug)
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
        info_qtree = vm.monitor.info('qtree', False)
        info_block = vm.monitor.info_block(False)
        proc_scsi = session.cmd_output('cat /proc/scsi/scsi')
        verify_qtree(params, info_qtree, info_block, proc_scsi, qdev)
        # we verified the unplugs, set the state to 0
        for _ in xrange(qdev.get_state()):
            qdev.set_clean()

        sub_type = params.get("sub_type_after_unplug")
        if sub_type:
            error.context(context_msg % (sub_type, "after hotunplug"),
                          logging.info)
            utils_test.run_virt_sub_test(test, params, env, sub_type)

    # Check for various KVM failures
    vm.verify_alive()