Exemple #1
0
    def device_add_block(pci_num, queues=1):
        device_id = pci_type + "-" + utils_misc.generate_random_id()
        pci_info.append([device_id, device_id])

        image_format = params.get("image_format_%s" % img_list[pci_num + 1])
        if not image_format:
            image_format = params.get("image_format", "qcow2")
        image_filename = find_image(pci_num)

        pci_model = params.get("pci_model")
        controller_model = None
        if pci_model == "virtio":
            pci_model = "virtio-blk-pci"

        if pci_model == "scsi" or pci_model == "scsi-hd":
            if pci_model == "scsi":
                pci_model = "scsi-disk"
                if arch.ARCH in ('ppc64', 'ppc64le'):
                    controller_model = "spapr-vscsi"
                else:
                    controller_model = "lsi53c895a"
            if pci_model == "scsi-hd":
                controller_model = "virtio-scsi-pci"
            verify_supported_device(controller_model)
            controller_id = "controller-" + device_id
            if vm.monitor.protocol == "human":
                controller_add_cmd = ("device_add %s,id=%s" %
                                      (controller_model, controller_id))
            else:
                controller_add_cmd = ("device_add driver=%s,id=%s" %
                                      (controller_model, controller_id))
            error.context("Adding SCSI controller.", logging.info)
            vm.monitor.send_args_cmd(controller_add_cmd, convert=False)

        verify_supported_device(pci_model)
        if drive_cmd_type == "drive_add":
            driver_add_cmd = ("%s auto file=%s,if=none,format=%s,id=%s" %
                              (drive_cmd_type, image_filename, image_format,
                               pci_info[pci_num][0]))
        elif drive_cmd_type == "__com.redhat_drive_add":
            driver_add_cmd = ("%s file=%s,format=%s,id=%s" %
                              (drive_cmd_type, image_filename, image_format,
                               pci_info[pci_num][0]))
        # add block device to vm device container
        image_name = img_list[pci_num + 1]
        image_params = params.object_params(image_name)
        image_name = pci_info[pci_num][0]
        blk_insert = vm.devices.images_define_by_params(image_name,
                                                        image_params, 'disk')
        vm.devices.insert(blk_insert)
        env.register_vm(vm.name, vm)

        # add driver.
        error.context("Adding driver.", logging.info)
        vm.monitor.send_args_cmd(driver_add_cmd, convert=False)

        pci_add_cmd = ("device_add id=%s,driver=%s,drive=%s" %
                       (pci_info[pci_num][1], pci_model, pci_info[pci_num][0]))
        return device_add(pci_num, pci_add_cmd)
Exemple #2
0
    def device_add_block(pci_num, queues=1):
        device_id = pci_type + "-" + utils_misc.generate_random_id()
        pci_info.append([device_id, device_id])

        image_format = params.get("image_format_%s" % img_list[pci_num + 1])
        if not image_format:
            image_format = params.get("image_format", "qcow2")
        image_filename = find_image(pci_num)

        pci_model = params.get("pci_model")
        controller_model = None
        if pci_model == "virtio":
            pci_model = "virtio-blk-pci"

        if pci_model == "scsi" or pci_model == "scsi-hd":
            if pci_model == "scsi":
                pci_model = "scsi-disk"
                if arch.ARCH in ('ppc64', 'ppc64le'):
                    controller_model = "spapr-vscsi"
                else:
                    controller_model = "lsi53c895a"
            if pci_model == "scsi-hd":
                controller_model = "virtio-scsi-pci"
            verify_supported_device(controller_model)
            controller_id = "controller-" + device_id
            if vm.monitor.protocol == "human":
                controller_add_cmd = ("device_add %s,id=%s" %
                                      (controller_model, controller_id))
            else:
                controller_add_cmd = ("device_add driver=%s,id=%s" %
                                      (controller_model, controller_id))
            error.context("Adding SCSI controller.", logging.info)
            vm.monitor.send_args_cmd(controller_add_cmd, convert=False)

        verify_supported_device(pci_model)
        if drive_cmd_type == "drive_add":
            driver_add_cmd = ("%s auto file=%s,if=none,format=%s,id=%s" %
                              (drive_cmd_type, image_filename, image_format,
                               pci_info[pci_num][0]))
        elif drive_cmd_type == "__com.redhat_drive_add":
            driver_add_cmd = ("%s file=%s,format=%s,id=%s" %
                              (drive_cmd_type, image_filename, image_format,
                               pci_info[pci_num][0]))
        # add block device to vm device container
        image_name = img_list[pci_num + 1]
        image_params = params.object_params(image_name)
        image_name = pci_info[pci_num][0]
        blk_insert = vm.devices.images_define_by_params(
            image_name, image_params, 'disk')
        vm.devices.insert(blk_insert)
        env.register_vm(vm.name, vm)

        # add driver.
        error.context("Adding driver.", logging.info)
        vm.monitor.send_args_cmd(driver_add_cmd, convert=False)

        pci_add_cmd = ("device_add id=%s,driver=%s,drive=%s" %
                       (pci_info[pci_num][1], pci_model, pci_info[pci_num][0]))
        return device_add(pci_num, pci_add_cmd)
Exemple #3
0
    def device_add_nic(pci_num):
        device_id = pci_type + "-" + utils_misc.generate_random_id()
        pci_info.append([device_id, device_id])

        pci_model = params.get("pci_model")
        if pci_model == "virtio":
            pci_model = "virtio-net-pci"
        verify_supported_device(pci_model)
        pci_add_cmd = "device_add id=%s,driver=%s" % (pci_info[pci_num][1],
                                                      pci_model)
        return device_add(pci_num, pci_add_cmd)
 def make_device_add_cmd(pa_pci_id, pci_addr=None):
     device_id = "%s" % pci_model + "-" + utils_misc.generate_random_id()
     pci_add_cmd = "device_add id=%s,driver=pci-assign,host=%s" % (device_id, pa_pci_id)
     if pci_addr is not None:
         pci_add_cmd += ",addr=%s" % pci_addr
     if params.get("hotplug_params"):
         assign_param = params.get("hotplug_params").split()
         for param in assign_param:
             value = params.get(param)
             if value:
                 pci_add_cmd += ",%s=%s" % (param, value)
     return pci_add_cmd
Exemple #5
0
 def device_add_iov(pci_num):
     device_id = "%s" % pci_model + "-" + utils_misc.generate_random_id()
     pci_info.append([device_id])
     check_support_device("pci-assign")
     pci_add_cmd = ("device_add id=%s,driver=pci-assign,host=%s" %
                    (pci_info[pci_num][0], pa_pci_ids[pci_num]))
     if params.get("hotplug_params"):
         assign_param = params.get("hotplug_params").split()
         for param in assign_param:
             value = params.get(param)
             if value:
                 pci_add_cmd += ",%s=%s" % (param, value)
     return device_add(pci_num, pci_add_cmd)
Exemple #6
0
    def device_add_nic(pci_num, queues=1):
        device_id = pci_type + "-" + utils_misc.generate_random_id()
        pci_info.append([device_id, device_id])

        pci_model = params.get("pci_model")
        if pci_model == "virtio":
            pci_model = "virtio-net-pci"
        verify_supported_device(pci_model)
        pci_add_cmd = "device_add id=%s,driver=%s" % (pci_info[pci_num][1],
                                                      pci_model)
        if queues > 1 and "virtio" in pci_model:
            pci_add_cmd += ",mq=on"
        return device_add(pci_num, pci_add_cmd)
 def device_add_iov(pci_num):
     device_id = "%s" % pci_model + "-" + utils_misc.generate_random_id()
     pci_info.append([device_id])
     driver = params.get("device_driver", "pci-assign")
     check_support_device(driver)
     pci_add_cmd = "device_add id=%s,driver=%s,host=%s" % (pci_info[pci_num][0], driver, pa_pci_ids[pci_num])
     if params.get("hotplug_params"):
         assign_param = params.get("hotplug_params").split()
         for param in assign_param:
             value = params.get(param)
             if value:
                 pci_add_cmd += ",%s=%s" % (param, value)
     return device_add(pci_num, pci_add_cmd)
 def make_device_add_cmd(pa_pci_id, pci_addr=None):
     device_id = "%s" % pci_model + "-" + utils_misc.generate_random_id()
     pci_add_cmd = ("device_add id=%s,driver=pci-assign,host=%s" %
                    (device_id, pa_pci_id))
     if pci_addr is not None:
         pci_add_cmd += ",addr=%s" % pci_addr
     if params.get("hotplug_params"):
         assign_param = params.get("hotplug_params").split()
         for param in assign_param:
             value = params.get(param)
             if value:
                 pci_add_cmd += ",%s=%s" % (param, value)
     return pci_add_cmd
Exemple #9
0
def blockdev_backup(vm, options, wait):
    """
    Live backup block device

    :param vm: VM object
    :param options: dict for blockdev-backup cmd
    :param wait: bool type, wait for backup job finished or not
    """
    event = "BLOCK_JOB_COMPLETED"
    job_id = utils_misc.generate_random_id()
    options.setdefault("job-id", job_id)
    out = qemu_monitor.get_monitor_function(vm, "blockdev-backup")(options)
    wait and wait_for_event(vm.monitor, event)
    return out
Exemple #10
0
    def device_add_block(pci_num, queues=1, pci_id=None):
        if pci_id is not None:
            device_id = pci_type + "-" + pci_id
        else:
            device_id = pci_type + "-" + utils_misc.generate_random_id()
            pci_info.append([device_id, device_id])

        image_format = params.get("image_format_%s" % img_list[pci_num + 1])
        if not image_format:
            image_format = params.get("image_format", "qcow2")
        image_filename = find_image(pci_num)

        pci_model = params.get("pci_model")
        controller_model = None
        if pci_model == "virtio":
            pci_model = "virtio-blk-pci"

        if pci_model == "scsi":
            pci_model = "scsi-disk"
            if arch.ARCH in ('ppc64', 'ppc64le'):
                controller_model = "spapr-vscsi"
            else:
                controller_model = "lsi53c895a"
            verify_supported_device(controller_model)
            controller_id = "controller-" + device_id
            controller_add_cmd = ("device_add %s,id=%s" %
                                  (controller_model, controller_id))
            error.context("Adding SCSI controller.")
            vm.monitor.send_args_cmd(controller_add_cmd)

        verify_supported_device(pci_model)
        if drive_cmd_type == "drive_add":
            driver_add_cmd = ("%s auto file=%s,if=none,format=%s,id=%s" %
                              (drive_cmd_type, image_filename, image_format,
                               pci_info[pci_num][0]))
        elif drive_cmd_type == "__com.redhat_drive_add":
            driver_add_cmd = ("%s file=%s,format=%s,id=%s" %
                              (drive_cmd_type, image_filename, image_format,
                               pci_info[pci_num][0]))
        # add driver.
        error.context("Adding driver.")
        vm.monitor.send_args_cmd(driver_add_cmd, convert=False)

        pci_add_cmd = ("device_add id=%s,driver=%s,drive=%s" %
                       (pci_info[pci_num][1],
                        pci_model,
                        pci_info[pci_num][0])
                       )
        return device_add(pci_num, pci_add_cmd, pci_id=pci_id)
Exemple #11
0
    def device_add_block(pci_num, queues=1, pci_id=None):
        if pci_id is not None:
            device_id = pci_type + "-" + pci_id
        else:
            device_id = pci_type + "-" + utils_misc.generate_random_id()
            pci_info.append([device_id, device_id])

        image_format = params.get("image_format_%s" % img_list[pci_num + 1])
        if not image_format:
            image_format = params.get("image_format", "qcow2")
        image_filename = find_image(pci_num)

        pci_model = params.get("pci_model")
        controller_model = None
        if pci_model == "virtio":
            pci_model = "virtio-blk-pci"

        if pci_model == "scsi":
            pci_model = "scsi-disk"
            if arch.ARCH in ('ppc64', 'ppc64le'):
                controller_model = "spapr-vscsi"
            else:
                controller_model = "lsi53c895a"
            verify_supported_device(controller_model)
            controller_id = "controller-" + device_id
            controller_add_cmd = ("device_add %s,id=%s" %
                                  (controller_model, controller_id))
            error.context("Adding SCSI controller.")
            vm.monitor.send_args_cmd(controller_add_cmd)

        verify_supported_device(pci_model)
        if drive_cmd_type == "drive_add":
            driver_add_cmd = ("%s auto file=%s,if=none,format=%s,id=%s" %
                              (drive_cmd_type, image_filename, image_format,
                               pci_info[pci_num][0]))
        elif drive_cmd_type == "__com.redhat_drive_add":
            driver_add_cmd = ("%s file=%s,format=%s,id=%s" %
                              (drive_cmd_type, image_filename, image_format,
                               pci_info[pci_num][0]))
        # add driver.
        error.context("Adding driver.")
        vm.monitor.send_args_cmd(driver_add_cmd, convert=False)

        pci_add_cmd = ("device_add id=%s,driver=%s,drive=%s" %
                       (pci_info[pci_num][1],
                        pci_model,
                        pci_info[pci_num][0])
                       )
        return device_add(pci_num, pci_add_cmd, pci_id=pci_id)
Exemple #12
0
def blockdev_create(vm, options, job_id=None, wait=True):
    """wrapper for blockdev-create QMP command"""
    if not job_id:
        job_id = "blk_%s" % utils_misc.generate_random_id()
    qemu_monitor.get_monitor_function(vm, "blockdev-create")(job_id, options)
    if wait:

        def wait_func():
            job_info = job_utils.get_job_by_id(vm, job_id)
            if job_info and job_info["status"] == "concluded":
                return True
            return False

        if not utils_misc.wait_for(wait_func, 5, 1):
            return None
    return job_id
Exemple #13
0
def create_target_block_device(vm, params, backing_info):
    """Create target backup device by qemu"""
    jobs = list()
    image_dir = data_dir.get_data_dir()
    random_id = utils_misc.generate_random_id()
    img_node_name = "img_%s" % random_id
    dev_node_name = "dev_%s" % random_id
    image_size = align_image_size(params["image_size"])
    filename = storage.get_image_filename(params, image_dir)
    image_create_options = {
        "driver": params["image_type"],
        "filename": filename,
        "size": 0
    }
    image_add_options = {
        "driver": params["image_type"],
        "filename": filename,
        "node-name": img_node_name
    }
    format_image_options = {
        "driver": params["image_format"],
        "size": image_size,
        "file": img_node_name
    }
    add_device_options = {
        "driver": params["image_format"],
        "file": image_add_options["node-name"],
        "node-name": dev_node_name
    }
    if backing_info:
        format_image_options.update({
            "backing-file":
            backing_info["backing-file"],
            "backing-fmt":
            backing_info["backing-fmt"]
        })
        add_device_options.update({"backing": backing_info["backing"]})
    try:
        jobs += [blockdev_create(vm, image_create_options)]
        blockdev_add(vm, image_add_options)
        jobs += [blockdev_create(vm, format_image_options)]
        blockdev_add(vm, add_device_options)
    finally:
        list(map(partial(job_utils.job_dismiss, vm), jobs))
    if get_block_node_by_name(vm, dev_node_name):
        return dev_node_name, filename
    return None, None
    def device_add_block(pci_num, queues=1, pci_id=None):
        if pci_id is not None:
            device_id = pci_type + "-" + pci_id
        else:
            device_id = pci_type + "-" + utils_misc.generate_random_id()
            pci_info.append([device_id, device_id])

        image_format = params.get("image_format_%s" % img_list[pci_num + 1])
        if not image_format:
            image_format = params.get("image_format", "qcow2")
        image_filename = find_image(pci_num)

        pci_model = params.get("pci_model")
        controller_model = None
        bus_option = ""
        if machine_type == "q35" and drive_format == "virtio":
            bus_option = ",bus=pcie_extra_root_port_%d" % pci_num

        if pci_model == "virtio":
            pci_model = "virtio-blk-pci"

        if pci_model == "scsi":
            pci_model = "scsi-disk"
            if arch.ARCH in ('ppc64', 'ppc64le'):
                controller_model = "spapr-vscsi"
            else:
                controller_model = "lsi53c895a"
            if nonlocal_vars["verify_device_flag"]:
                verify_supported_device(controller_model)
            controller_id = "controller-" + device_id
            controller_add_cmd = ("device_add %s,id=%s" %
                                  (controller_model, controller_id))
            error_context.context("Adding SCSI controller.")
            vm.monitor.send_args_cmd(controller_add_cmd)

        if nonlocal_vars["verify_device_flag"]:
            verify_supported_device(pci_model)
        nonlocal_vars["verify_device_flag"] = False

        if drive_cmd_type == "blockdev-add":
            add_cmd = "{0} driver=file,filename={1},node-name=file_{2}".format(
                drive_cmd_type, image_filename, pci_info[pci_num][0])
            add_cmd += ";{0} driver={1},node-name={2},file=file_{2}".format(
                drive_cmd_type, image_format, pci_info[pci_num][0])
            driver_add_cmd = add_cmd

        elif drive_cmd_type == "drive_add":
            driver_add_cmd = ("%s auto file=%s,if=none,format=%s,id=%s" %
                              (drive_cmd_type, image_filename, image_format,
                               pci_info[pci_num][0]))
        elif drive_cmd_type == "__com.redhat_drive_add":
            driver_add_cmd = ("%s file=%s,format=%s,id=%s" %
                              (drive_cmd_type, image_filename, image_format,
                               pci_info[pci_num][0]))
        # add driver.
        error_context.context("Adding driver.")
        if drive_cmd_type != "blockdev-add":
            vm.monitor.send_args_cmd(driver_add_cmd, convert=False)
        elif pci_id is None:
            vm.monitor.send_args_cmd(driver_add_cmd, convert=False)

        pci_add_cmd = ("device_add id=%s,driver=%s,drive=%s%s" %
                       (pci_info[pci_num][1], pci_model, pci_info[pci_num][0],
                        bus_option))
        return device_add(pci_num, pci_add_cmd, pci_id=pci_id)
Exemple #15
0
def run_pci_hotplug(test, params, env):
    """
    Test hotplug of PCI devices.

    (Elements between [] are configurable test parameters)
    1) PCI add a deivce (NIC / block)
    2) Compare output of monitor command 'info pci'.
    3) Compare output of guest command [reference_cmd].
    4) Verify whether pci_model is shown in [pci_find_cmd].
    5) Check whether the newly added PCI device works fine.
    6) PCI delete the device, verify whether could remove the PCI device.

    @param test:   QEMU test object.
    @param params: Dictionary with the test parameters.
    @param env:    Dictionary with test environment.
    """
    vm = env.get_vm(params["main_vm"])
    vm.verify_alive()
    timeout = int(params.get("login_timeout", 360))
    session = vm.wait_for_login(timeout=timeout)

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

    # Get output of command 'info pci' as reference
    info_pci_ref = vm.monitor.info("pci")

    # Get output of command as reference
    reference = session.cmd_output(params["reference_cmd"])

    tested_model = params["pci_model"]
    test_type = params["pci_type"]
    image_format = params.get("image_format_stg")

    # Probe qemu to verify what is the supported syntax for PCI hotplug
    cmd_output = vm.monitor.cmd("?")
    if len(re.findall("\ndevice_add", cmd_output)) > 0:
        cmd_type = "device_add"
    elif len(re.findall("\npci_add", cmd_output)) > 0:
        cmd_type = "pci_add"
    else:
        raise error.TestError("Unknow version of qemu")

    # Determine syntax of drive hotplug
    # __com.redhat_drive_add == qemu-kvm-0.12 on RHEL 6
    if len(re.findall("\n__com.redhat_drive_add", cmd_output)) > 0:
        drive_cmd_type = "__com.redhat_drive_add"
    # drive_add == qemu-kvm-0.13 onwards
    elif len(re.findall("\ndrive_add", cmd_output)) > 0:
        drive_cmd_type = "drive_add"
    else:
        raise error.TestError("Unknow version of qemu")

    # Probe qemu for a list of supported devices
    devices_support = vm.monitor.cmd("%s ?" % cmd_type)

    if cmd_type == "pci_add":
        if test_type == "nic":
            pci_add_cmd = "pci_add pci_addr=auto nic model=%s" % tested_model
        elif test_type == "block":
            image_params = params.object_params("stg")
            image_filename = storage.get_image_filename(
                image_params, data_dir.get_data_dir())
            pci_add_cmd = ("pci_add pci_addr=auto storage file=%s,if=%s" %
                           (image_filename, tested_model))
        # Execute pci_add (should be replaced by a proper monitor method call)
        add_output = vm.monitor.cmd(pci_add_cmd)
        if not "OK domain" in add_output:
            raise error.TestFail("Add PCI device failed. "
                                 "Monitor command is: %s, Output: %r" %
                                 (pci_add_cmd, add_output))
        after_add = vm.monitor.info("pci")

    elif cmd_type == "device_add":
        driver_id = test_type + "-" + utils_misc.generate_random_id()
        device_id = test_type + "-" + utils_misc.generate_random_id()
        if test_type == "nic":
            if tested_model == "virtio":
                tested_model = "virtio-net-pci"
            pci_add_cmd = "device_add id=%s,driver=%s" % (device_id,
                                                          tested_model)

        elif test_type == "block":
            image_params = params.object_params("stg")
            image_filename = storage.get_image_filename(
                image_params, data_dir.get_data_dir())
            controller_model = None
            if tested_model == "virtio":
                tested_model = "virtio-blk-pci"

            if tested_model == "scsi":
                tested_model = "scsi-disk"
                controller_model = "lsi53c895a"
                if len(re.findall(controller_model, devices_support)) == 0:
                    raise error.TestError("scsi controller device (%s) not "
                                          "supported by qemu" %
                                          controller_model)

            if controller_model is not None:
                controller_id = "controller-" + device_id
                controller_add_cmd = ("device_add %s,id=%s" %
                                      (controller_model, controller_id))
                vm.monitor.cmd(controller_add_cmd)

            if drive_cmd_type == "drive_add":
                driver_add_cmd = ("drive_add auto "
                                  "file=%s,if=none,id=%s,format=%s" %
                                  (image_filename, driver_id, image_format))
            elif drive_cmd_type == "__com.redhat_drive_add":
                driver_add_cmd = ("__com.redhat_drive_add "
                                  "file=%s,format=%s,id=%s" %
                                  (image_filename, image_format, driver_id))

            pci_add_cmd = ("device_add id=%s,driver=%s,drive=%s" %
                           (device_id, tested_model, driver_id))
            vm.monitor.cmd(driver_add_cmd)

        # Check if the device is support in qemu
        if len(re.findall(tested_model, devices_support)) > 0:
            add_output = vm.monitor.cmd(pci_add_cmd)
        else:
            raise error.TestError("%s doesn't support device: %s" %
                                  (cmd_type, tested_model))
        after_add = vm.monitor.info("pci")

        if not device_id in after_add:
            raise error.TestFail("Add device failed. Monitor command is: %s"
                                 ". Output: %r" % (pci_add_cmd, add_output))

    # Define a helper function to delete the device
    def pci_del(ignore_failure=False):
        if cmd_type == "pci_add":
            result_domain, bus, slot, _ = add_output.split(',')
            domain = int(result_domain.split()[2])
            bus = int(bus.split()[1])
            slot = int(slot.split()[1])
            pci_addr = "%x:%x:%x" % (domain, bus, slot)
            cmd = "pci_del pci_addr=%s" % pci_addr
        elif cmd_type == "device_add":
            cmd = "device_del %s" % device_id
        # This should be replaced by a proper monitor method call
        vm.monitor.cmd(cmd)

        def device_removed():
            after_del = vm.monitor.info("pci")
            return after_del != after_add

        if (not utils_misc.wait_for(device_removed, 10, 0, 1)
                and not ignore_failure):
            raise error.TestFail("Failed to hot remove PCI device: %s. "
                                 "Monitor command: %s" % (tested_model, cmd))

    try:
        # Compare the output of 'info pci'
        if after_add == info_pci_ref:
            raise error.TestFail("No new PCI device shown after executing "
                                 "monitor command: 'info pci'")

        # Define a helper function to compare the output
        def new_shown():
            o = session.cmd_output(params["reference_cmd"])
            return o != reference

        secs = int(params["wait_secs_for_hook_up"])
        if not utils_misc.wait_for(new_shown, 30, secs, 3):
            raise error.TestFail("No new device shown in output of command "
                                 "executed inside the guest: %s" %
                                 params.get("reference_cmd"))

        # Define a helper function to catch PCI device string
        def find_pci():
            o = session.cmd_output(params["find_pci_cmd"])
            return params["match_string"] in o

        if not utils_misc.wait_for(find_pci, 30, 3, 3):
            raise error.TestFail(
                "PCI %s %s device not found in guest. "
                "Command was: %s" %
                (tested_model, test_type, params.get("find_pci_cmd")))

        # Test the newly added device
        try:
            session.cmd(params["pci_test_cmd"])
        except aexpect.ShellError, e:
            raise error.TestFail("Check for %s device failed after PCI "
                                 "hotplug. Output: %r" % (test_type, e.output))

        session.close()
def run(test, params, env):
    """
    Autotest regression test:

    Use Virtual Machines to test autotest.

    1) Clone the given guest OS (only Linux) image twice.
    2) Boot 2 VMs (autotest_server_vm and autotest_client_vm)
    4) Install the autotest server in the server vm
    5) Run the unittests
    6) Run the pylint checker
    7) Run a simple client sleeptest
    8) Run a simple server sleeptest
    9) Register the client vm in the autotest server
    10) Schedule a simple job sleeptest in the client. Wait for client reboot.
    11) If any of these steps have failed, fail the test and report the error

    :param test: virt test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment.
    """
    github_repo = 'git://github.com/autotest/autotest.git'

    step_failures = []
    autotest_repo = params.get('autotest_repo', github_repo)
    autotest_branch = params['autotest_branch']
    autotest_commit = params['autotest_commit']
    password = params['password']
    autotest_install_timeout = int(
        params.get('autotest_install_timeout', 1800))
    unittests_run_timeout = int(params.get('unittests_run_timeout', 1800))
    unittests_args = params.get('unittests_args', '')
    pylint_run_timeout = int(params.get('pylint_run_timeout', 1800))
    vm_names = params["vms"].split()
    has_client_vm = len(vm_names) > 1
    server_name = vm_names[0]
    vm_server = env.get_vm(server_name)
    vm_server.verify_alive()
    timeout = float(params.get("login_timeout", 240))
    session_server = vm_server.wait_for_login(timeout=timeout)
    server_ip = vm_server.get_address()

    if has_client_vm:
        client_name = vm_names[1]
        vm_client = env.get_vm(client_name)
        vm_client.verify_alive()
        session_client = vm_client.wait_for_login(timeout=timeout)
        client_ip = vm_client.get_address()

    step1 = "autotest-server-install"
    try:
        installer_file = "install-autotest-server.sh"

        if autotest_repo == github_repo:
            installer_url = ("https://raw.github.com/autotest/autotest/%s"
                             "/contrib/%s" % (autotest_branch, installer_file))
        else:
            installer_url = ("https://raw.github.com/autotest/autotest/master"
                             "/contrib/%s" % installer_file)

        # Download the install script and execute it
        download_cmd = ("python -c 'from urllib2 import urlopen; "
                        "r = urlopen(\"%s\"); "
                        "f = open(\"%s\", \"w\"); "
                        "f.write(r.read())'" % (installer_url,
                                                installer_file))
        session_server.cmd(download_cmd)
        permission_cmd = ("chmod +x install-autotest-server.sh")
        session_server.cmd(permission_cmd)
        install_cmd = ("./install-autotest-server.sh -u Aut0t3st -d Aut0t3st "
                       "-g %s -b %s" % (autotest_repo, autotest_branch))
        if autotest_commit:
            install_cmd += " -c %s" % autotest_commit
        session_server.cmd(install_cmd, timeout=autotest_install_timeout)
    except aexpect.ShellCmdError as e:
        for line in e.output.splitlines():
            logging.error(line)
        step_failures.append(step1)
    vm_server.copy_files_from(guest_path="/tmp/install-autotest-server*log",
                              host_path=test.resultsdir)

    top_commit = None
    try:
        session_server.cmd("test -d /usr/local/autotest/.git")
        session_server.cmd("cd /usr/local/autotest")
        top_commit = session_server.cmd(
            "echo `git log -n 1 --pretty=format:%H`")
        top_commit = top_commit.strip()
        logging.info("Autotest top commit for repo %s, branch %s: %s",
                     autotest_repo, autotest_branch, top_commit)
    except aexpect.ShellCmdError as e:
        for line in e.output.splitlines():
            logging.error(line)

    if top_commit is not None:
        session_server.close()
        session_server = vm_server.wait_for_login(timeout=timeout,
                                                  username='******',
                                                  password='******')

        step2 = "unittests"
        try:
            session_server.cmd("cd /usr/local/autotest")
            session_server.cmd("utils/unittest_suite.py %s" % unittests_args,
                               timeout=unittests_run_timeout)
        except aexpect.ShellCmdError as e:
            for line in e.output.splitlines():
                logging.error(line)
            step_failures.append(step2)

        step3 = "pylint"
        try:
            session_server.cmd("cd /usr/local/autotest")
            session_server.cmd("utils/check_patch.py --full --yes",
                               timeout=pylint_run_timeout)
        except aexpect.ShellCmdError as e:
            for line in e.output.splitlines():
                logging.error(line)
            step_failures.append(step3)

        step4 = "client_run"
        try:
            session_server.cmd("cd /usr/local/autotest/client")
            session_server.cmd("./autotest-local run sleeptest",
                               timeout=pylint_run_timeout)
            session_server.cmd("rm -rf results/default")
        except aexpect.ShellCmdError as e:
            for line in e.output.splitlines():
                logging.error(line)
            step_failures.append(step4)

        if has_client_vm:
            step5 = "server_run"
            try:
                session_client.cmd("iptables -F")
                session_server.cmd("cd /usr/local/autotest")
                session_server.cmd("server/autotest-remote -m %s --ssh-user root "
                                   "--ssh-pass %s "
                                   "-c client/tests/sleeptest/control" %
                                   (client_ip, password),
                                   timeout=pylint_run_timeout)
                session_server.cmd("rm -rf results-*")
            except aexpect.ShellCmdError as e:
                for line in e.output.splitlines():
                    logging.error(line)
                step_failures.append(step5)

            step6 = "registering_client_cli"
            try:
                label_name = "label-%s" % utils_misc.generate_random_id()
                create_label_cmd = ("/usr/local/autotest/cli/autotest-rpc-client "
                                    "label create -t %s -w %s" %
                                    (label_name, server_ip))
                session_server.cmd(create_label_cmd)

                list_labels_cmd = ("/usr/local/autotest/cli/autotest-rpc-client "
                                   "label list -a -w %s" % server_ip)
                list_labels_output = session_server.cmd(list_labels_cmd)
                for line in list_labels_output.splitlines():
                    logging.debug(line)
                if label_name not in list_labels_output:
                    raise ValueError("No label %s in the output of %s" %
                                     (label_name, list_labels_cmd))

                create_host_cmd = ("/usr/local/autotest/cli/autotest-rpc-client "
                                   "host create -t %s %s -w %s" %
                                   (label_name, client_ip, server_ip))
                session_server.cmd(create_host_cmd)

                list_hosts_cmd = ("/usr/local/autotest/cli/autotest-rpc-client "
                                  "host list -w %s" % server_ip)
                list_hosts_output = session_server.cmd(list_hosts_cmd)
                for line in list_hosts_output.splitlines():
                    logging.debug(line)
                if client_ip not in list_hosts_output:
                    raise ValueError("No client %s in the output of %s" %
                                     (client_ip, create_label_cmd))
                if label_name not in list_hosts_output:
                    raise ValueError("No label %s in the output of %s" %
                                     (label_name, create_label_cmd))

            except (aexpect.ShellCmdError, ValueError) as e:
                if isinstance(e, aexpect.ShellCmdError):
                    for line in e.output.splitlines():
                        logging.error(line)
                elif isinstance(e, ValueError):
                    logging.error(e)
                step_failures.append(step6)

            step7 = "running_job_cli"
            try:
                session_client.cmd("iptables -F")

                job_name = "Sleeptest %s" % utils_misc.generate_random_id()

                def job_is_status(status):
                    list_jobs_cmd = ("/usr/local/autotest/cli/autotest-rpc-client "
                                     "job list -a -w %s" % server_ip)
                    list_jobs_output = session_server.cmd(list_jobs_cmd)
                    if job_name in list_jobs_output:
                        if status in list_jobs_output:
                            return True
                        elif "Aborted" in list_jobs_output:
                            raise ValueError("Job is in aborted state")
                        elif "Failed" in list_jobs_output:
                            raise ValueError("Job is in failed state")
                        else:
                            return False
                    else:
                        raise ValueError("Job %s does not show in the "
                                         "output of %s" % (job_name, list_jobs_cmd))

                def job_is_completed():
                    return job_is_status("Completed")

                def job_is_running():
                    return job_is_status("Running")

                job_create_cmd = ("/usr/local/autotest/cli/autotest-rpc-client "
                                  "job create --test sleeptest -m %s '%s' -w %s" %
                                  (client_ip, job_name, server_ip))
                session_server.cmd(job_create_cmd)

                if not utils_misc.wait_for(job_is_running, 300, 0, 10,
                                           "Waiting for job to start running"):
                    raise ValueError("Job did not start running")

                # Wait for the session to become unresponsive
                if not utils_misc.wait_for(
                    lambda: not session_client.is_responsive(),
                        timeout=300):
                    raise ValueError("Client machine did not reboot")

                # Establish a new client session
                session_client = vm_client.wait_for_login(timeout=timeout)

                # Wait for the job to complete
                if not utils_misc.wait_for(job_is_completed, 300, 0, 10,
                                           "Waiting for job to complete"):
                    raise ValueError("Job did not complete")

                # Copy logs back so we can analyze them
                vm_server.copy_files_from(
                    guest_path="/usr/local/autotest/results/*",
                    host_path=test.resultsdir)

            except (aexpect.ShellCmdError, ValueError) as e:
                if isinstance(e, aexpect.ShellCmdError):
                    for line in e.output.splitlines():
                        logging.error(line)
                elif isinstance(e, ValueError):
                    logging.error(e)
                step_failures.append(step7)

    def report_version():
        if top_commit is not None:
            logging.info("Autotest git repo: %s", autotest_repo)
            logging.info("Autotest git branch: %s", autotest_repo)
            logging.info("Autotest top commit: %s", top_commit)

    if step_failures:
        logging.error("The autotest regression testing failed")
        report_version()
        raise error.TestFail("The autotest regression testing had the "
                             "following steps failed: %s" % step_failures)
    else:
        logging.info("The autotest regression testing passed")
        report_version()
Exemple #17
0
def run_pci_hotplug(test, params, env):
    """
    Test hotplug of PCI devices.

    (Elements between [] are configurable test parameters)
    1) PCI add a deivce (NIC / block)
    2) Compare output of monitor command 'info pci'.
    3) Compare output of guest command [reference_cmd].
    4) Verify whether pci_model is shown in [pci_find_cmd].
    5) Check whether the newly added PCI device works fine.
    6) PCI delete the device, verify whether could remove the PCI device.

    @param test:   KVM test object.
    @param params: Dictionary with the test parameters.
    @param env:    Dictionary with test environment.
    """
    vm = env.get_vm(params["main_vm"])
    vm.verify_alive()
    timeout = int(params.get("login_timeout", 360))
    session = vm.wait_for_login(timeout=timeout)

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

    # Get output of command 'info pci' as reference
    info_pci_ref = vm.monitor.info("pci")

    # Get output of command as reference
    reference = session.cmd_output(params.get("reference_cmd"))

    tested_model = params.get("pci_model")
    test_type = params.get("pci_type")
    image_format = params.get("image_format_stg")

    # Probe qemu to verify what is the supported syntax for PCI hotplug
    cmd_output = vm.monitor.cmd("?")
    if len(re.findall("\ndevice_add", cmd_output)) > 0:
        cmd_type = "device_add"
    elif len(re.findall("\npci_add", cmd_output)) > 0:
        cmd_type = "pci_add"
    else:
        raise error.TestError("Unknow version of qemu")

    # Determine syntax of drive hotplug
    # __com.redhat_drive_add == qemu-kvm-0.12 on RHEL 6
    if len(re.findall("\n__com.redhat_drive_add", cmd_output)) > 0:
        drive_cmd_type = "__com.redhat_drive_add"
    # drive_add == qemu-kvm-0.13 onwards
    elif len(re.findall("\ndrive_add", cmd_output)) > 0:
        drive_cmd_type = "drive_add"
    else:
        raise error.TestError("Unknow version of qemu")

    # Probe qemu for a list of supported devices
    devices_support = vm.monitor.cmd("%s ?" % cmd_type)

    if cmd_type == "pci_add":
        if test_type == "nic":
            pci_add_cmd = "pci_add pci_addr=auto nic model=%s" % tested_model
        elif test_type == "block":
            image_params = params.object_params("stg")
            image_filename = storage.get_image_filename(image_params,
                                                        data_dir.get_data_dir())
            pci_add_cmd = ("pci_add pci_addr=auto storage file=%s,if=%s" %
                           (image_filename, tested_model))
        # Execute pci_add (should be replaced by a proper monitor method call)
        add_output = vm.monitor.cmd(pci_add_cmd)
        if not "OK domain" in add_output:
            raise error.TestFail("Add PCI device failed. "
                                 "Monitor command is: %s, Output: %r" %
                                 (pci_add_cmd, add_output))
        after_add = vm.monitor.info("pci")

    elif cmd_type == "device_add":
        driver_id = test_type + "-" + utils_misc.generate_random_id()
        device_id = test_type + "-" + utils_misc.generate_random_id()
        if test_type == "nic":
            if tested_model == "virtio":
                tested_model = "virtio-net-pci"
            pci_add_cmd = "device_add id=%s,driver=%s" % (device_id,
                                                          tested_model)

        elif test_type == "block":
            image_params = params.object_params("stg")
            image_filename = storage.get_image_filename(image_params,
                                                        data_dir.get_data_dir())
            controller_model = None
            if tested_model == "virtio":
                tested_model = "virtio-blk-pci"

            if tested_model == "scsi":
                tested_model = "scsi-disk"
                controller_model = "lsi53c895a"
                if len(re.findall(controller_model, devices_support)) == 0:
                    raise error.TestError("scsi controller device (%s) not "
                                          "supported by qemu" %
                                          controller_model)

            if controller_model is not None:
                controller_id = "controller-" + device_id
                controller_add_cmd = ("device_add %s,id=%s" %
                                      (controller_model, controller_id))
                vm.monitor.cmd(controller_add_cmd)

            if drive_cmd_type == "drive_add":
                driver_add_cmd = ("drive_add auto "
                                  "file=%s,if=none,id=%s,format=%s" %
                                  (image_filename, driver_id, image_format))
            elif drive_cmd_type == "__com.redhat_drive_add":
                driver_add_cmd = ("__com.redhat_drive_add "
                                  "file=%s,format=%s,id=%s" %
                                  (image_filename, image_format, driver_id))

            pci_add_cmd = ("device_add id=%s,driver=%s,drive=%s" %
                           (device_id, tested_model, driver_id))
            vm.monitor.cmd(driver_add_cmd)

        # Check if the device is support in qemu
        if len(re.findall(tested_model, devices_support)) > 0:
            add_output = vm.monitor.cmd(pci_add_cmd)
        else:
            raise error.TestError("%s doesn't support device: %s" %
                                  (cmd_type, tested_model))
        after_add = vm.monitor.info("pci")

        if not device_id in after_add:
            raise error.TestFail("Add device failed. Monitor command is: %s"
                                 ". Output: %r" % (pci_add_cmd, add_output))

    # Define a helper function to delete the device
    def pci_del(ignore_failure=False):
        if cmd_type == "pci_add":
            result_domain, bus, slot, _ = add_output.split(',')
            domain = int(result_domain.split()[2])
            bus = int(bus.split()[1])
            slot = int(slot.split()[1])
            pci_addr = "%x:%x:%x" % (domain, bus, slot)
            cmd = "pci_del pci_addr=%s" % pci_addr
        elif cmd_type == "device_add":
            cmd = "device_del %s" % device_id
        # This should be replaced by a proper monitor method call
        vm.monitor.cmd(cmd)

        def device_removed():
            after_del = vm.monitor.info("pci")
            return after_del != after_add

        if (not utils_misc.wait_for(device_removed, 10, 0, 1)
            and not ignore_failure):
            raise error.TestFail("Failed to hot remove PCI device: %s. "
                                 "Monitor command: %s" %
                                 (tested_model, cmd))

    try:
        # Compare the output of 'info pci'
        if after_add == info_pci_ref:
            raise error.TestFail("No new PCI device shown after executing "
                                 "monitor command: 'info pci'")

        # Define a helper function to compare the output
        def new_shown():
            o = session.cmd_output(params.get("reference_cmd"))
            return o != reference

        secs = int(params.get("wait_secs_for_hook_up"))
        if not utils_misc.wait_for(new_shown, 30, secs, 3):
            raise error.TestFail("No new device shown in output of command "
                                 "executed inside the guest: %s" %
                                 params.get("reference_cmd"))

        # Define a helper function to catch PCI device string
        def find_pci():
            o = session.cmd_output(params.get("find_pci_cmd"))
            return params.get("match_string") in o

        if not utils_misc.wait_for(find_pci, 30, 3, 3):
            raise error.TestFail("PCI %s %s device not found in guest. "
                                 "Command was: %s" %
                                 (tested_model, test_type,
                                  params.get("find_pci_cmd")))

        # Test the newly added device
        try:
            session.cmd(params.get("pci_test_cmd"))
        except aexpect.ShellError, e:
            raise error.TestFail("Check for %s device failed after PCI "
                                 "hotplug. Output: %r" % (test_type, e.output))

        session.close()
            session_client.cmd("iptables -F")
            session_server.cmd("cd /usr/local/autotest")
            session_server.cmd("server/autotest-remote -m %s --ssh-user root "
                               "--ssh-pass %s "
                               "-c client/tests/sleeptest/control" %
                               (client_ip, password),
                               timeout=pylint_run_timeout)
            session_server.cmd("rm -rf results-*")
        except aexpect.ShellCmdError, e:
            for line in e.output.splitlines():
                logging.error(line)
            step_failures.append(step5)

        step6 = "registering_client_cli"
        try:
            label_name = "label-%s" % utils_misc.generate_random_id()
            create_label_cmd = ("/usr/local/autotest/cli/autotest-rpc-client "
                                "label create -t %s -w %s" %
                                (label_name, server_ip))
            session_server.cmd(create_label_cmd)

            list_labels_cmd = ("/usr/local/autotest/cli/autotest-rpc-client "
                               "label list -a -w %s" % server_ip)
            list_labels_output = session_server.cmd(list_labels_cmd)
            for line in list_labels_output.splitlines():
                logging.debug(line)
            if not label_name in list_labels_output:
                raise ValueError("No label %s in the output of %s" %
                                 (label_name, list_labels_cmd))

            create_host_cmd = ("/usr/local/autotest/cli/autotest-rpc-client "
                session_server.cmd("cd /usr/local/autotest")
                session_server.cmd(
                    "server/autotest-remote -m %s --ssh-user root "
                    "--ssh-pass %s "
                    "-c client/tests/sleeptest/control" %
                    (client_ip, password),
                    timeout=pylint_run_timeout)
                session_server.cmd("rm -rf results-*")
            except aexpect.ShellCmdError, e:
                for line in e.output.splitlines():
                    logging.error(line)
                step_failures.append(step5)

            step6 = "registering_client_cli"
            try:
                label_name = "label-%s" % utils_misc.generate_random_id()
                create_label_cmd = (
                    "/usr/local/autotest/cli/autotest-rpc-client "
                    "label create -t %s -w %s" % (label_name, server_ip))
                session_server.cmd(create_label_cmd)

                list_labels_cmd = (
                    "/usr/local/autotest/cli/autotest-rpc-client "
                    "label list -a -w %s" % server_ip)
                list_labels_output = session_server.cmd(list_labels_cmd)
                for line in list_labels_output.splitlines():
                    logging.debug(line)
                if label_name not in list_labels_output:
                    raise ValueError("No label %s in the output of %s" %
                                     (label_name, list_labels_cmd))
Exemple #20
0
def blockdev_add(vm, options):
    """wrapper for blockdev-add QMP command"""
    if "node-name" not in options:
        options["node-name"] = utils_misc.generate_random_id()
    qemu_monitor.get_monitor_function(vm, "blockdev-add")(options)
    return options["node-name"]