Example #1
0
def run(test, params, env):
    """
    Run kvm-unit-tests for Hyper-V testdev device

    1) compile kvm-unit-tests tools source code
    2) Run each unit tests by compiled binary tools
    3) For each unit test, compare the test result to expected value
    """
    tmp_dir = data_dir.get_tmp_dir()
    kvm_unit_tests_dir = data_dir.get_deps_dir("kvm_unit_tests")
    compile_cmd = params["compile_cmd"] % (tmp_dir, kvm_unit_tests_dir)
    test_cmd = params["test_cmd"]
    unit_tests_mapping = params["unit_tests_mapping"]
    skip_tests = params.get("skip_tests", "").split()
    cpu_flags = params["cpu_model_flags"]
    cpu_model = cpu.get_qemu_best_cpu_model(params)
    cpu_param = cpu_model + cpu_flags

    error_context.context("Copy & compile kvm-unit-test tools", logging.info)
    process.system(compile_cmd, shell=True)

    error_context.context("Run unit tests", logging.info)
    for unit_test, unit_test_result in json.loads(unit_tests_mapping).items():
        if unit_test in skip_tests:
            continue
        logging.info("Start running unit test %s" % unit_test)
        unit_test_cmd = test_cmd % (tmp_dir, unit_test, cpu_param)
        result_output = process.system_output(unit_test_cmd, shell=True)
        result_output = result_output.decode()
        find_result = re.findall('^%s' % unit_test_result[0], result_output,
                                 re.M)
        if len(find_result) != int(unit_test_result[1]):
            test.fail("Unit test result mismatch target, "
                      "target=%s, output=%s" %
                      (unit_test_result[1], result_output))
Example #2
0
def run(test, params, env):
    """
    Boot latest cpu model on old host

    :param test: QEMU test object.
    :param params: Dictionary with test parameters.
    :param env: Dictionary with the test environment.
    """
    def start_with_model(test_model):
        """
        Start vm with tested model
        :param test_model: The model been tested
        """
        vm = None
        params['cpu_model'] = test_model
        logging.info('Start vm with cpu model %s' % test_model)
        try:
            env_process.preprocess_vm(test, params, env, vm_name)
            vm = env.get_vm(vm_name)
            output = vm.process.get_output()
            if warning_text not in output:
                test.fail("Qemu should output warning for lack flags"
                          " while it does not.")
        except Exception as e:
            if boot_expected == 'no':
                logging.info('Expect vm boot up failed when enforce is set.')
                if warning_text not in str(e):
                    raise
            else:
                raise
        else:
            if boot_expected == 'no':
                test.fail('The vm should not boot successfully'
                          ' when cpu enforce mode is on')
        finally:
            if vm and vm.is_alive():
                vm.verify_kernel_crash()
                vm.destroy()

    fd = open("/proc/cpuinfo")
    cpu_info = fd.read()
    fd.close()
    vendor = cpu.get_cpu_vendor(cpu_info)
    cpu_model_list = cpu.CPU_TYPES.get(vendor)
    latest_cpu_model = cpu_model_list[-1]
    for cpu_model in cpu_model_list:
        qemu_binary = utils_misc.get_qemu_binary(params)
        if cpu_model in cpu.get_qemu_cpu_models(qemu_binary):
            latest_cpu_model = cpu_model
            break

    host_cpu_model = cpu.get_qemu_best_cpu_model(params)
    if host_cpu_model.startswith(latest_cpu_model):
        test.cancel('The host cpu is not old enough for this test.')

    vm_name = params['main_vm']
    warning_text = params.get('warning_text')
    boot_expected = params.get('boot_expected', 'yes')
    params['start_vm'] = 'yes'
    start_with_model(latest_cpu_model)
Example #3
0
def run(test, params, env):
    """
    support Virtual SPEC_CTRL inside guest

    1. check the 'v_spec_ctrl' on supported host
    2. add guest kernel command line 'spec_store_bypass_disable=on'
    3. verify the guest sets the spec ctrl properly on all the cpus.

    :param test: QEMU test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment.
    """
    flags = params["flags"]
    check_host_flags = params.get_boolean("check_host_flags")
    if check_host_flags:
        check_cpu_flags(params, flags, test)

    supported_models = params.get("supported_models", "")
    cpu_model = params.get("cpu_model")
    if not cpu_model:
        cpu_model = cpu.get_qemu_best_cpu_model(params)
    if cpu_model not in supported_models.split():
        test.cancel("'%s' doesn't support this test case" % cpu_model)

    params["start_vm"] = "yes"
    vm_name = params['main_vm']
    env_process.preprocess_vm(test, params, env, vm_name)

    proc_cmdline = params["proc_cmdline"]
    vm = env.get_vm(vm_name)
    session = vm.wait_for_login()
    boot_option = params["boot_option"]
    check_output = str(session.cmd(proc_cmdline, timeout=60)).split()
    if boot_option and boot_option not in check_output:
        error_context.context("Add '%s' to guest" % boot_option, test.log.info)
        update_boot_option(vm, args_added=boot_option)
        session = vm.wait_for_login()

    test_dir = params["test_dir"]
    source_file = params["source_file"]
    src_msr = os.path.join(data_dir.get_deps_dir(), source_file)
    vm.copy_files_to(src_msr, test_dir)
    guest_dir = params["guest_dir"]
    compile_cmd = params["compile_cmd"]
    try:
        session.cmd(compile_cmd % guest_dir)
        check_msr = 'cd %s && ' % guest_dir + params["check_msr"]
        result = session.cmd_output(check_msr)
        nums_vcpus = session.cmd_output("grep processor /proc/cpuinfo -c")
        if result != nums_vcpus:
            test.fail("verify the guest sets the spec ctrl failed.")
    finally:
        session.cmd("rm -rf %s/msr* %s/master*" % (test_dir, test_dir))
        session.close()
        vm.verify_kernel_crash()
        if boot_option and boot_option not in check_output:
            update_boot_option(vm, args_removed=boot_option)
Example #4
0
def run(test, params, env):
    """
    Test cpu flags.
    1) Check if current flags are in the supported lists on host, if no, cancel test
    2) Otherwise, boot guest with the cpu flags
    3) Check cpu flags inside guest(only for linux guest)
    4) Reboot guest

    :param test: QEMU test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment.
    """
    flags = params["flags"]
    check_host_flags = params.get_boolean("check_host_flags")
    if check_host_flags:
        check_cpu_flags(params, flags, test)

    unsupported_models = params.get("unsupported_models", "")
    cpu_model = params.get("cpu_model")
    if not cpu_model:
        cpu_model = cpu.get_qemu_best_cpu_model(params)
    if cpu_model in unsupported_models.split():
        test.cancel("'%s' doesn't support this test case" % cpu_model)
    fallback_models_map = eval(params.get('fallback_models_map', '{}'))
    if cpu_model in fallback_models_map.keys():
        params["cpu_model"] = fallback_models_map[cpu_model]

    params["start_vm"] = "yes"
    vm_name = params['main_vm']
    env_process.preprocess_vm(test, params, env, vm_name)

    vm = env.get_vm(vm_name)
    error_context.context("Try to log into guest", logging.info)
    session = vm.wait_for_login()
    if params["os_type"] == "linux":
        if params.get("guest_flags"):
            flags = params.get("guest_flags")
        if params.get("no_flags", "") == flags:
            flags = ""
        check_guest_cmd = params.get("check_guest_cmd")
        check_cpu_flags(params, flags, test, session)
        if check_guest_cmd:
            expect_items = params.get("expect_items")
            if expect_items:
                result = session.cmd_status(check_guest_cmd % expect_items)
                if result:
                    test.fail("'%s' can't be found inside guest" %
                              expect_items)

    if params.get("reboot_method"):
        error_context.context("Reboot guest '%s'." % vm.name, logging.info)
        session = vm.reboot(session=session)

    vm.verify_kernel_crash()
    session.close()
Example #5
0
def run(test, params, env):
    """
    Runs CPU negative test:

    1. Launch qemu with improper cpu configuration
    2. Verify qemu failed to start

    :param test: QEMU test object.
    :param params: Dictionary with test parameters.
    :param env: Dictionary with the test environment.
    """

    enforce_flag = params.get('enforce_flag')
    if enforce_flag and 'CPU_MODEL' in params['wrong_cmd']:
        if enforce_flag in cpu.get_host_cpu_models():
            test.cancel('This case only test on the host without the flag'
                        ' %s.' % enforce_flag)
        cpu_model = cpu.get_qemu_best_cpu_model(params)
        params['wrong_cmd'] = params['wrong_cmd'].replace(
            'CPU_MODEL', cpu_model)

    qemu_bin = utils_misc.get_qemu_binary(params)
    if 'OUT_OF_RANGE' in params['wrong_cmd']:
        machine_type = params['machine_type'].split(':')[-1]
        m_types = utils_qemu.get_machines_info(qemu_bin)[machine_type]
        m_type = re.search(r'\(alias of (\S+)\)', m_types)[1]
        max_value = utils_qemu.get_maxcpus_hard_limit(qemu_bin, m_type)
        smp = str(max_value + 1)
        params['wrong_cmd'] = params['wrong_cmd'].replace(
            'MACHINE_TYPE', machine_type).replace('OUT_OF_RANGE', smp)
        msg = params['warning_msg'].replace('SMP_VALUE', smp).replace(
            'MAX_VALUE', str(max_value)).replace('MACHINE_TYPE', m_type)
        params['warning_msg'] = msg

    warning_msg = params['warning_msg']
    wrong_cmd = '%s %s' % (qemu_bin, params['wrong_cmd'])
    logging.info('Start qemu with command: %s', wrong_cmd)
    status, output = process.getstatusoutput(wrong_cmd)
    logging.info('Qemu prompt output:\n%s', output)
    if status == 0:
        test.fail('Qemu guest boots up while it should not.')
    if warning_msg not in output:
        test.fail('Does not get expected warning message.')
    else:
        logging.info('Test passed as qemu does not boot up and'
                     ' prompts expected message.')