Exemplo n.º 1
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.')
def run(test, params, env):
    """
    Test hotplug maximum vCPU device.

    1) Launch a guest without vCPU device.
    2) Hotplug all vCPU devices and check successfully or not. (qemu side)
    3) Check if the number of CPUs in guest changes accordingly. (guest side)
    4) Reboot guest.
    5) Hotunplug all vCPU devices and check successfully or not. (qemu side)

    :param test:   QEMU test object.
    :param params: Dictionary with the test parameters.
    :param env:    Dictionary with test environment.
    """
    os_type = params["os_type"]
    machine_type = params["machine_type"]
    reboot_timeout = params.get_numeric("reboot_timeout")
    mismatch_text = "Actual number of guest CPUs is not equal to the expected"
    not_equal_text = "CPU quantity mismatched! Guest got %s but expected is %s"
    # Many vCPUs will be plugged, it takes some time to bring them online.
    verify_wait_timeout = params.get_numeric("verify_wait_timeout", 300)
    qemu_binary = utils_misc.get_qemu_binary(params)
    machine_info = utils_qemu.get_machines_info(qemu_binary)[machine_type]
    machine_info = re.search(r'\(alias of (\S+)\)', machine_info)
    current_machine = machine_info.group(1) if machine_info else machine_type
    supported_maxcpus = (params.get_numeric("vcpu_maxcpus")
                         or utils_qemu.get_maxcpus_hard_limit(
                             qemu_binary, current_machine))
    if not params.get_boolean("allow_pcpu_overcommit"):
        supported_maxcpus = min(supported_maxcpus, cpu.online_count())

    logging.info("Define the CPU topology of guest")
    vcpu_devices = []
    if (cpu.get_vendor() == "amd" and params.get_numeric("vcpu_threads") != 1):
        test.cancel("AMD cpu does not support multi threads")
    elif machine_type.startswith("pseries"):
        host_kernel_ver = uname()[2].split("-")[0]
        if params.get_numeric("vcpu_threads") == 8:
            supported_maxcpus -= divmod(supported_maxcpus, 8)[1]
            vcpu_devices = [
                "vcpu%d" % c for c in range(1, supported_maxcpus // 8)
            ]
        # The maximum value of vcpu_id in 'linux-3.x' is 2048, so
        # (vcpu_id * ms->smp.threads / spapr->vsmt) <= 256, need to adjust it
        elif (supported_maxcpus > 256
              and host_kernel_ver not in VersionInterval("[4, )")):
            supported_maxcpus = 256
    vcpu_devices = vcpu_devices or [
        "vcpu%d" % vcpu for vcpu in range(1, supported_maxcpus)
    ]
    params["vcpu_maxcpus"] = str(supported_maxcpus)
    params["vcpu_devices"] = " ".join(vcpu_devices)
    params["start_vm"] = "yes"

    vm = env.get_vm(params["main_vm"])
    vm.create(params=params)
    vm.verify_alive()
    session = vm.wait_for_login()
    cpuinfo = vm.cpuinfo
    smp = cpuinfo.smp
    vcpus_count = vm.params.get_numeric("vcpus_count")

    error_context.context("Hotplug all vCPU devices", logging.info)
    for vcpu_device in vcpu_devices:
        vm.hotplug_vcpu_device(vcpu_device)

    error_context.context("Check Number of vCPU in guest", logging.info)
    if not utils_misc.wait_for(lambda: vm.get_cpu_count() == supported_maxcpus,
                               verify_wait_timeout,
                               first=5,
                               step=10):
        logging.error(not_equal_text, vm.get_cpu_count(), supported_maxcpus)
        test.fail(mismatch_text)
    logging.info("CPU quantity is as expected: %s", supported_maxcpus)

    error_context.context("Check CPU topology of guest", logging.info)
    if not cpu_utils.check_guest_cpu_topology(session, os_type, cpuinfo):
        test.fail("CPU topology of guest is not as expected.")
    session = vm.reboot(session, timeout=reboot_timeout)
    if not cpu_utils.check_guest_cpu_topology(session, os_type, cpuinfo):
        test.fail("CPU topology of guest is not as expected after reboot.")

    error_context.context("Hotunplug all vCPU devices", logging.info)
    for vcpu_device in reversed(vcpu_devices):
        vm.hotunplug_vcpu_device(vcpu_device, 10 * vcpus_count)
    if not utils_misc.wait_for(lambda: vm.get_cpu_count() == smp,
                               verify_wait_timeout,
                               first=5,
                               step=10):
        logging.error(not_equal_text, vm.get_cpu_count(), smp)
        test.fail(mismatch_text)
    logging.info("CPU quantity is as expected after hotunplug: %s", smp)
    session.close()