Example #1
0
    def get_guest_cpuid(self, cpu_model, feature=None, extra_params=None, qom_mode=False):
        if not qom_mode:
            test_kernel_dir = os.path.join(data_dir.get_deps_dir(), "cpuid", "src")
            os.chdir(test_kernel_dir)
            utils.make("cpuid_dump_kernel.bin")

        vm_name = params['main_vm']
        params_b = params.copy()
        if not qom_mode:
            params_b["kernel"] = os.path.join(
                test_kernel_dir, "cpuid_dump_kernel.bin")
        params_b["cpu_model"] = cpu_model
        params_b["cpu_model_flags"] = feature
        del params_b["images"]
        del params_b["nics"]
        if extra_params:
            params_b.update(extra_params)
        env_process.preprocess_vm(self, params_b, env, vm_name)
        vm = env.get_vm(vm_name)
        dbg('is dead: %r', vm.is_dead())
        vm.create()
        self.vm = vm
        if qom_mode:
            return get_qom_cpuid(self, vm)
        else:
            return get_test_kernel_cpuid(self, vm)
Example #2
0
def run(test, params, env):
    """
    Qemu guest pxe boot test:
    1). check npt/ept function enable, then boot vm
    2). execute query/info cpus in loop
    3). verify vm not paused during pxe booting

    params:
    :param test: QEMU test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment.
    """
    restore_mmu_cmd = None
    pxe_timeout = int(params.get("pxe_timeout", 60))

    error_context.context("Enable ept/npt", logging.info)
    try:
        flag = list(filter(lambda x: x in utils_misc.get_cpu_flags(),
                           ['ept', 'npt']))[0]
    except IndexError:
        logging.info("Host doesn't support ept/npt, skip the configuration")
    else:
        enable_mmu_cmd = params["enable_mmu_cmd_%s" % flag]
        check_mmu_cmd = params["check_mmu_cmd_%s" % flag]
        restore_mmu_cmd = params["restore_mmu_cmd_%s" % flag]
        status = process.system(check_mmu_cmd, timeout=120, ignore_status=True,
                                shell=True)
        if status != 0:
            _kill_vms(params, env)
            process.run(enable_mmu_cmd, shell=True)

    params["start_vm"] = "yes"
    params["kvm_vm"] = "yes"
    params["paused_after_start_vm"] = "yes"

    error_context.context("Try to boot from NIC", logging.info)
    env_process.preprocess_vm(test, params, env, params["main_vm"])
    vm = env.get_vm(params["main_vm"])
    bg = utils_misc.InterruptedThread(_capture_tftp, (test, vm, pxe_timeout))

    count = 0
    try:
        bg.start()
        error_context.context("Query cpus in loop", logging.info)
        vm.resume()
        while True:
            count += 1
            try:
                vm.monitor.info("cpus")
                vm.verify_status("running")
                if not bg.is_alive():
                    break
            except qemu_monitor.MonitorSocketError:
                test.fail("Qemu looks abnormally, please read the log")
        logging.info("Execute info/query cpus %d times", count)
    finally:
        bg.join()
        if restore_mmu_cmd:
            _kill_vms(params, env)
            process.run(restore_mmu_cmd, shell=True)
Example #3
0
    def get_guest_cpuid(self, cpu_model, feature=None):
        test_kernel_dir = os.path.join(test.virtdir, "deps",
                                       "cpuid_test_kernel")
        os.chdir(test_kernel_dir)
        utils.make("cpuid_dump_kernel.bin")

        vm_name = params.get('main_vm')
        params_b = params.copy()
        params_b["kernel"] = os.path.join(test_kernel_dir, "cpuid_dump_kernel.bin")
        params_b["cpu_model"] = cpu_model
        params_b["cpu_model_flags"] = feature
        del params_b["images"]
        del params_b["nics"]
        env_process.preprocess_vm(self, params_b, env, vm_name)
        vm = env.get_vm(vm_name)
        vm.create()
        self.vm = vm
        vm.resume()

        timeout = float(params.get("login_timeout", 240))
        f = lambda: re.search("==END TEST==", vm.serial_console.get_output())
        if not utils_misc.wait_for(f, timeout, 1):
            raise error.TestFail("Could not get test complete message.")

        test_sig = re.compile("==START TEST==\n((?:.*\n)*)\n*==END TEST==")
        test_output = test_sig.search(vm.serial_console.get_output())
        if test_output == None:
            raise error.TestFail("Test output signature not found in "
                                 "output:\n %s", vm.serial_console.get_output())
        self.clean()
        return test_output.group(1)
def run(test, params, env):
    """
    KVM boot with negative parameter test:
    1) Try to boot VM with negative parameters.
    2) Verify that qemu could handle the negative parameters.
       Check the negative message (optional)

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

    neg_msg = params.get("negative_msg")
    if params.get("start_vm") == "yes":
        test.error("Please set start_vm to no")
    params["start_vm"] = "yes"
    try:
        error_context.context("Try to boot VM with negative parameters",
                              logging.info)
        case_fail = False
        env_process.preprocess_vm(test, params, env, params.get("main_vm"))
        case_fail = True
    except Exception as e:
        if neg_msg:
            error_context.context("Check qemu-qemu error message",
                                  logging.info)
            if neg_msg not in str(e):
                msg = "Could not find '%s' in error message '%s'" % (
                    neg_msg, e)
                test.fail(msg)
        logging.debug("Could not boot up vm, %s" % e)
    if case_fail:
        test.fail("Did not raise exception during vm boot up")
Example #5
0
    def start(self):
        """
        Start mlock basic test
        """
        error_context.context("Get nr_mlock and nr_unevictable in host"
                              " before VM start!", logging.info)
        self.mlock_pre = read_from_vmstat("nr_mlock")
        self.unevictable_pre = read_from_vmstat("nr_unevictable")
        logging.info("mlock_pre is %d and unevictable_pre is %d.",
                     self.mlock_pre, self.unevictable_pre)
        self.params["start_vm"] = "yes"

        error_context.context("Starting VM!", logging.info)
        env_process.preprocess_vm(self.test, self.params,
                                  self.env, self.params["main_vm"])
        self.vm = self.env.get_vm(self.params["main_vm"])
        self.vm.verify_alive()

        error_context.context("Get nr_mlock and nr_unevictable in host"
                              " after VM start!", logging.info)
        self.mlock_post = read_from_vmstat("nr_mlock")
        self.unevictable_post = read_from_vmstat("nr_unevictable")
        logging.info("mlock_post is %d and unevictable_post is %d.",
                     self.mlock_post, self.unevictable_post)

        self._check_mlock_unevictable()
Example #6
0
    def get_guest_cpuid(self, cpu_model, feature=None, extra_params=None):
        test_kernel_dir = os.path.join(data_dir.get_deps_dir(), "cpuid", "src")
        os.chdir(test_kernel_dir)
        utils.make("cpuid_dump_kernel.bin")

        vm_name = params['main_vm']
        params_b = params.copy()
        params_b["kernel"] = os.path.join(
            test_kernel_dir, "cpuid_dump_kernel.bin")
        params_b["cpu_model"] = cpu_model
        params_b["cpu_model_flags"] = feature
        del params_b["images"]
        del params_b["nics"]
        if extra_params:
            params_b.update(extra_params)
        env_process.preprocess_vm(self, params_b, env, vm_name)
        vm = env.get_vm(vm_name)
        dbg('is dead: %r', vm.is_dead())
        vm.create()
        self.vm = vm
        vm.resume()

        timeout = float(params.get("login_timeout", 240))
        f = lambda: re.search("==END TEST==", vm.serial_console.get_output())
        if not utils_misc.wait_for(f, timeout, 1):
            raise error.TestFail("Could not get test complete message.")

        test_output = parse_cpuid_dump(vm.serial_console.get_output())
        if test_output is None:
            raise error.TestFail("Test output signature not found in "
                                 "output:\n %s", vm.serial_console.get_output())
        vm.destroy(gracefully=False)
        return test_output
Example #7
0
def run(test, params, env):
    """
    Qemu invalid parameter in qemu command line test:
    1) Try boot up guest with invalid parameters
    2) Catch the error message shows by qemu process

    :param test: QEMU test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment.
    """
    vm_name = params["main_vm"]
    params['start_vm'] = "yes"
    try:
        error_context.context("Start guest with invalid parameters.")
        env_process.preprocess_vm(test, params, env, vm_name)
        vm = env.get_vm(vm_name)
        vm.destroy()
    except Exception as emsg:
        error_context.context("Check guest exit status.")
        if "(core dumped)" in str(emsg):
            test.fail("Guest core dumped with invalid parameters.")
        else:
            logging.info("Guest quit as expect: %s" % str(emsg))
            return

    test.fail("Guest start normally, didn't quit as expect.")
Example #8
0
def run_valgrind_memalign(test, params, env):
    """
    This case is from [general operation] Work around valgrind choking on our
    use of memalign():
    1.download valgrind form valgrind download page: www.valgrind.org.
    2.install the valgrind in host.
    3.run # valgrind /usr/libexec/qemu-kvm  -vnc :0 -S -m 384 -monitor stdio
    4.check the status and do continue the VM.
    5.quit the VM.

    @param test: QEMU test object
    @param params: Dictionary with the test parameters
    @param env: Dictionary with test environment
    """
    interval = float(params.get("interval_time", "10"))
    def valgrind_intall():
        valgrind_install_cmd = params.get("valgrind_install_cmd")
        s = utils.system(valgrind_install_cmd, timeout=3600)
        if s != 0:
            raise error.TestError("Fail to install valgrind")
        else:
            logging.info("Install valgrind successfully.")

    valgring_support_check_cmd = params.get("valgring_support_check_cmd")
    try:
        utils.system(valgring_support_check_cmd,timeout=interval)
    except Exception:
        valgrind_intall()

    params["start_vm"] = "yes"
    env_process.preprocess_vm(test, params, env, params.get("main_vm"))
    vm = env.get_vm(params["main_vm"])

    time.sleep(interval)
    vm.verify_status("running")
Example #9
0
 def get_vm(self):
     params = self.params
     vm_name = params["main_vm"]
     params["start_vm"] = "yes"
     vm = self.env.get_vm(vm_name)
     env_process.preprocess_vm(self.test, params, self.env, vm_name)
     vm.verify_alive()
     return vm
def run(test, params, env):
    """
    [pci-bridge]Check stress when 31 block devices attached to 1 pci-bridge, this case will:
    1) Attach one pci-bridge to guest.
    2) Create 31 disks to this pci-bridge.
    3) Start the guest.
    4) Check 'info block'.
    5) Read and write data on disks under pci bridge.

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

    error_context.context("Modify params!", logging.info)
    image_parent_bus = params.get("image_parent_bus")
    image_num = int(params.get("image_num", 0))
    if image_num != 0:
        for index in xrange(image_num):
            image = "stg%s" % index
            params["images"] = ' '.join([params["images"], image])
            params["disk_pci_bus_%s" % image] = image_parent_bus
            params["image_name_%s" % image] = "images/%s" % image
            params["image_size_%s" % image] = "100M"
            params["force_create_image_%s" % image] = "yes"
            params["remove_image_%s" % image] = "yes"
            params["blk_extra_params_%s" % image] = "serial=TARGET_DISK%s" % index

    env_process.process_images(env_process.preprocess_image, test, params)
    env_process.preprocess_vm(test, params, env, params["main_vm"])

    error_context.context("Get the main VM!", logging.info)
    vm = env.get_vm(params["main_vm"])
    vm.verify_alive()

    login_timeout = int(params.get("login_timeout", 360))
    session = vm.wait_for_login(timeout=login_timeout)

    error_context.context("Check 'info block'!", logging.info)
    monitor_info_block = vm.monitor.info_block(False)
    if image_num + 1 != len(monitor_info_block.keys()):
        raise error.TestFail("Check 'info block' failed!")
    logging.info("Check 'info block' succeed!")

    error_context.context("Read and write data on all disks!", logging.info)
    sub_test_type = params.get("sub_test_type", "dd_test")
    images = params["images"]
    images = images.split()
    images.pop(0)
    for image in images:
        if params.get("dd_if") == "ZERO":
            params["dd_of"] = image
        else:
            params["dd_if"] = image
        utils_test.run_virt_sub_test(test, params, env, sub_test_type)

    logging.info("Read and write data on all disks succeed!")
    session.close()
Example #11
0
def run(test, params, env):
    """
    KVM boot time test:
    1) Set init run level to 1
    2) Send a shutdown command to the guest, or issue a system_powerdown
       monitor command (depending on the value of shutdown_method)
    3) Boot up the guest and measure the boot time
    4) set init run level back to the old one

    :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)

    error.context("Set guest run level to 1", logging.info)
    single_user_cmd = params['single_user_cmd']
    session.cmd(single_user_cmd)

    try:
        error.context("Shut down guest", logging.info)
        session.cmd('sync')
        vm.destroy()

        error.context("Boot up guest and measure the boot time", logging.info)
        utils_memory.drop_caches()
        vm.create()
        vm.verify_alive()
        session = vm.wait_for_serial_login(timeout=timeout)
        boot_time = utils_misc.monotonic_time() - vm.start_monotonic_time
        test.write_test_keyval({'result': "%ss" % boot_time})
        expect_time = int(params.get("expect_bootup_time", "17"))
        logging.info("Boot up time: %ss" % boot_time)

    finally:
        try:
            error.context("Restore guest run level", logging.info)
            restore_level_cmd = params['restore_level_cmd']
            session.cmd(restore_level_cmd)
            session.cmd('sync')
            vm.destroy(gracefully=False)
            env_process.preprocess_vm(test, params, env, vm.name)
            vm.verify_alive()
            vm.wait_for_login(timeout=timeout)
        except Exception:
            logging.warning("Can not restore guest run level, "
                            "need restore the image")
            params["restore_image_after_testing"] = "yes"

    if boot_time > expect_time:
        raise error.TestFail(
            "Guest boot up is taking too long: %ss" % boot_time)

    session.close()
Example #12
0
def run(test, params, env):
    """
    KVM shutdown test:
    1) Log into a guest
    2) Send a shutdown command to the guest, or issue a system_powerdown
       monitor command (depending on the value of shutdown_method)
    3) Wait until the guest is down

    :param test: QEMU test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment
    """
    timeout = int(params.get("login_timeout", 360))
    shutdown_count = int(params.get("shutdown_count", 1))
    shutdown_method = params.get("shutdown_method", "shell")
    sleep_time = float(params.get("sleep_before_powerdown", 10))
    shutdown_command = params.get("shutdown_command")

    for i in xrange(shutdown_count):
        vm = env.get_vm(params["main_vm"])
        vm.verify_alive()
        session = vm.wait_for_login(timeout=timeout)
        error.base_context("shutting down the VM %s/%s" % (i + 1,
                                                           shutdown_count),
                           logging.info)
        if params.get("setup_runlevel") == "yes":
            error.context("Setup the runlevel for guest", logging.info)
            expect_runlevel = params.get("expect_runlevel", "3")

            ori_runlevel = session.cmd("runlevel")
            ori_runlevel = re.findall("\d+", ori_runlevel)[-1]
            if ori_runlevel == expect_runlevel:
                logging.info("Guest runlevel is the same as expect.")
            else:
                session.cmd("init %s" % expect_runlevel)
                tmp_runlevel = session.cmd("runlevel")
                tmp_runlevel = re.findall("\d+", tmp_runlevel)[-1]
                if tmp_runlevel != expect_runlevel:
                    logging.warn("Failed to setup runlevel for guest")

        if shutdown_method == "shell":
            # Send a shutdown command to the guest's shell
            session.sendline(shutdown_command)
            error.context("waiting VM to go down (shutdown shell cmd)",
                          logging.info)
        elif shutdown_method == "system_powerdown":
            # Sleep for a while -- give the guest a chance to finish booting
            time.sleep(sleep_time)
            # Send a system_powerdown monitor command
            vm.monitor.cmd("system_powerdown")
            error.context("waiting VM to go down "
                          "(system_powerdown monitor cmd)", logging.info)

        if not utils_misc.wait_for(vm.is_dead, 360, 0, 1):
            raise error.TestFail("Guest refuses to go down")
        if i < shutdown_count - 1:
            session.close()
            env_process.preprocess_vm(test, params, env, params["main_vm"])
Example #13
0
        def test(self):
            create_floppy(params)
            params["start_vm"] = "yes"
            vm_name = params.get("main_vm", "vm1")
            env_process.preprocess_vm(test, params, env, vm_name)
            vm = env.get_vm(vm_name)
            vm.verify_alive()
            self.session = vm.wait_for_login(timeout=login_timeout)

            self.dest_dir = params.get("mount_dir")
            # If mount_dir specified, treat guest as a Linux OS
            # Some Linux distribution does not load floppy at boot and Windows
            # needs time to load and init floppy driver
            if self.dest_dir:
                lsmod = self.session.cmd("lsmod")
                if not "floppy" in lsmod:
                    self.session.cmd("modprobe floppy")
            else:
                time.sleep(20)

            error.context("Formating floppy disk before using it")
            format_cmd = params["format_floppy_cmd"]
            self.session.cmd(format_cmd, timeout=120)
            logging.info("Floppy disk formatted successfully")

            if self.dest_dir:
                error.context("Mounting floppy")
                self.session.cmd("mount -t vfat %s %s" % (guest_floppy_path, self.dest_dir))
            error.context("Testing floppy")
            self.session.cmd(params["test_floppy_cmd"])

            error.context("Copying file to the floppy")
            md5_cmd = params.get("md5_cmd")
            if md5_cmd:
                md5_source = self.session.cmd("%s %s" % (params["md5_cmd"], source_file))
                try:
                    md5_source = md5_source.split(" ")[0]
                except IndexError:
                    error.TestError("Failed to get md5 from source file," " output: '%s'" % md5_source)
            else:
                md5_source = None

            self.session.cmd("%s %s %s" % (params["copy_cmd"], source_file, dest_file))
            logging.info("Succeed to copy file '%s' into floppy disk" % source_file)

            error.context("Checking if the file is unchanged after copy")
            if md5_cmd:
                md5_dest = self.session.cmd("%s %s" % (params["md5_cmd"], dest_file))
                try:
                    md5_dest = md5_dest.split(" ")[0]
                except IndexError:
                    error.TestError("Failed to get md5 from dest file," " output: '%s'" % md5_dest)
                if md5_source != md5_dest:
                    raise error.TestFail("File changed after copy to floppy")
            else:
                md5_dest = None
                self.session.cmd("%s %s %s" % (params["diff_file_cmd"], source_file, dest_file))
Example #14
0
def run(test, params, env):
    """
    Boots VMs until one of them becomes unresponsive, and records the maximum
    number of VMs successfully started:
    1) boot the first vm
    2) boot the second vm cloned from the first vm, check whether it boots up
       and all booted vms respond to shell commands
    3) go on until cannot create VM anymore or cannot allocate memory for VM

    :param test:   kvm test object
    :param params: Dictionary with the test parameters
    :param env:    Dictionary with test environment.
    """
    error_context.base_context("waiting for the first guest to be up",
                               logging.info)
    vm = env.get_vm(params["main_vm"])
    vm.verify_alive()
    login_timeout = float(params.get("login_timeout", 240))
    session = vm.wait_for_login(timeout=login_timeout)

    num = 2
    sessions = [session]

    # Boot the VMs
    try:
        try:
            while num <= int(params.get("max_vms")):
                # Clone vm according to the first one
                error_context.base_context("booting guest #%d" % num,
                                           logging.info)
                vm_name = "vm%d" % num
                vm_params = vm.params.copy()
                curr_vm = vm.clone(vm_name, vm_params)
                env.register_vm(vm_name, curr_vm)
                env_process.preprocess_vm(test, vm_params, env, vm_name)
                params["vms"] += " " + vm_name

                session = curr_vm.wait_for_login(timeout=login_timeout)
                sessions.append(session)
                logging.info("Guest #%d booted up successfully", num)

                # Check whether all previous shell sessions are responsive
                for i, se in enumerate(sessions):
                    error_context.context("checking responsiveness of guest"
                                          " #%d" % (i + 1), logging.debug)
                    se.cmd(params.get("alive_test_cmd"))
                num += 1
        except Exception as emsg:
            test.fail("Expect to boot up %s guests."
                      "Failed to boot up #%d guest with "
                      "error: %s." % (params["max_vms"], num, emsg))
    finally:
        for se in sessions:
            se.close()
        logging.info("Total number booted: %d" % (num - 1))
Example #15
0
def run(test, params, env):
    """
    [Memory][Numa]binding 128 guest nodes to node0 - x86 with 32M and Power
    with 256M, this case will:
    1) Boot guest with 128 numa nodes
    2) Check query-memdev
    3) Check memory in procfs on host
    4) Check numa node amount in guest

    :param test: QEMU test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment
    """
    error_context.context("Modify params to boot guest with 128 numa nodes",
                          logging.info)
    node_num = int(params["numa_nodes"])
    node_size = params["node_size"]
    prealloc_mem = params.get("prealloc_mem", "no")
    mem_devs = ""
    guest_numa_nodes = ""
    for index in range(node_num):
        guest_numa_nodes += "node%s " % index
        mem_devs += "mem%s " % index
        params["numa_memdev_node%s" % index] = "mem-mem%s" % index
        params["size_mem%s" % index] = node_size
        params["prealloc_mem%s" % index] = prealloc_mem

    params["guest_numa_nodes"] = guest_numa_nodes
    params["mem_devs"] = mem_devs
    params["start_vm"] = "yes"
    env_process.preprocess_vm(test, params, env, params["main_vm"])
    error_context.context("Get the main VM!", logging.info)
    vm = env.get_vm(params["main_vm"])

    login_timeout = int(params.get("login_timeout", 360))
    session = vm.wait_for_login(timeout=login_timeout)

    error_context.context("Check memdevs from monitor", logging.info)
    numa_memdev_options.check_query_memdev(test, params, vm)

    error_context.context("Check memory in host procfs", logging.info)
    numa_memdev_options.check_memory_in_procfs(test, params, vm)

    error_context.context("Check numa node for linux guest", logging.info)
    if params["os_type"] == "linux":
        error_context.context("Check numa node in guest", logging.info)
        numa_cmd = params["numa_cmd"]
        numa_expected = params["numa_expected"]
        guest_numa = session.cmd_output(numa_cmd).strip()
        if guest_numa != numa_expected:
            test.fail("Guest numa node is %s while expected numa node is %s"
                      % (guest_numa, numa_expected))

    session.close()
 def boot_guest_with_vectors(vectors):
     error.context("Boot guest with vectors = %s" % vectors, logging.info)
     params["vectors"] = vectors
     params["start_vm"] = "yes"
     try:
         env_process.preprocess_vm(test, params, env, params.get("main_vm"))
     except virt_vm.VMError, err:
         if int(vectors) < 0:
             txt = "Parameter 'vectors' expects uint32_t"
             if re.findall(txt, str(err)):
                 return
 def powerup(self):
     """
     bootup guest with target image;
     """
     params = self.parser_test_args()
     vm_name = params['main_vm']
     logging.info("Target image: %s" % self.target_image)
     error_context.context("powerup vm with target image", logging.info)
     env_process.preprocess_vm(self.test, params, self.env, vm_name)
     vm = self.env.get_vm(vm_name)
     vm.verify_alive()
     self.vm = vm
Example #18
0
def run(test, params, env):
    """
    enforce quit test:
    steps:
    1). boot guest with enforce params
    2). guest will quit if flags is not supported in host

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

    guest_cpumodel = params.get("cpu_model", "Conroe").split(",")[0]
    host_cpumodel = utils_misc.get_host_cpu_models()
    host_flags = utils_misc.get_cpu_flags()
    extra_flags = params.get("cpu_model_flags", " ")

    lack_flags = []
    flags = re.findall("\+(\w+)", extra_flags)
    for flag in flags:
        if flag not in host_flags:
            lack_flags.append(flag)
    force_quit = False
    # force quit if flag is not no host
    if lack_flags:
        force_quit = True
    # force quit if 'svm' is added
    if "svm" in extra_flags:
        force_quit = True
    # force quit if guest cpu is not included in host cpu cluster
    if guest_cpumodel not in host_cpumodel and guest_cpumodel != "host":
        force_quit = True

    if "enforce" not in extra_flags:
        raise error.TestError("pls add 'enforce' params to the cmd line")

    msg_unavailable = params.get("msg_unavailable", "").split(":")
    msg_unknow = params.get("msg_unknow", "not found")
    try:
        error.context("boot guest with -cpu %s,%s" %
                      (guest_cpumodel, extra_flags),
                      logging.info)
        params["start_vm"] = "yes"
        env_process.preprocess_vm(test, params, env, params.get("main_vm"))
    except Exception, err:
        tmp_flag = False
        for msg in msg_unavailable:
            if msg in str(err):
                tmp_flag = True
        if tmp_flag or msg_unknow in str(err):
            logging.info("unavailable host feature, guest force quit")
        else:
            raise error.TestFail("guest quit with error\n%s" % str(err))
Example #19
0
def run(test, params, env):
    """
    Measure overhead of IPI with and without x2apic:
    1) Enable ept if the host supports it.
    2) Boot guest with x2apic cpu flag (at least 2 vcpu).
    3) Check x2apic flag in guest.
    4) Run pipetest script in guest.
    5) Boot guest without x2apic.
    6) Run pipetest script in guest.
    7) Compare the result of step4 and step6.

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

    smp = params.get("smp")
    if int(smp) < 2:
        params["smp"] = 2
        logging.warn(
            "This case need at least 2 vcpu, but only 1 specified in" " configuration. So change the vcpu to 2."
        )
    vm_name = params.get("main_vm")
    error.context("Boot guest with x2apic cpu flag.", logging.info)
    env_process.preprocess_vm(test, params, env, vm_name)
    vm = env.get_vm(params["main_vm"])
    vm.verify_alive()

    session = vm.wait_for_login(timeout=int(params.get("login_timeout", 360)))

    check_x2apic_cmd = params.get("check_x2apic_cmd")
    if check_x2apic_cmd:
        error.context("Check x2apic flag in guest", logging.info)
        x2apic_output = session.cmd_output(check_x2apic_cmd).strip()
        x2apic_check_string = params.get("x2apic_check_string").split(",")
        for check_string in x2apic_check_string:
            if check_string.strip() not in x2apic_output:
                msg = "%s is not displayed in output" % check_string
                raise error.TestFail(msg)

    pipetest_cmd = params.get("pipetest_cmd")
    if session.get_command_status("test -x %s" % pipetest_cmd):
        file_link = os.path.join(test.virtdir, "scripts/pipetest.c")
        vm.copy_files_to(file_link, "/tmp/pipetest.c")
        build_pipetest_cmd = params.get("build_pipetest_cmd")
        error.context("Build pipetest script in guest.", logging.info)
        session.cmd(build_pipetest_cmd, timeout=180)

    error.context("Run pipetest script in guest.", logging.info)
    try:
        o = session.cmd(pipetest_cmd, timeout=180)
    except aexpect.ShellTimeoutError, e:
        o = e
def run(test, params, env):
    """
    KVM reset test:
    1) Boot guest.
    2) Check the guest boot up time.(optional)
    3) Reset system by monitor command for several times. The interval time
       should can be configured by cfg file or based on the boot time get
       from step 2.
    4) Log into the guest to verify it could normally boot.

    :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"])
    timeout = float(params.get("login_timeout", 240))
    reset_times = int(params.get("reset_times", 20))
    interval = int(params.get("reset_interval", 10))
    wait_time = int(params.get("wait_time_for_reset", 60))
    params["start_vm"] = "yes"

    if params.get("get_boot_time") == "yes":
        error_context.context("Check guest boot up time", logging.info)
        env_process.preprocess_vm(test, params, env, vm.name)
        vm.wait_for_login(timeout=timeout)
        bootup_time = time.time() - vm.start_time
        if params.get("reset_during_boot") == "yes":
            interval = int(bootup_time)
            wait_time = random.randint(0, int(bootup_time))
        vm.destroy()

    error_context.context("Boot the guest", logging.info)
    env_process.preprocess_vm(test, params, env, vm.name)
    logging.info("Wait for %d seconds before reset" % wait_time)
    time.sleep(wait_time)

    for i in range(1, reset_times + 1):
        error_context.context("Reset guest system for %s times" % i,
                              logging.info)

        vm.monitor.cmd("system_reset")

        interval_tmp = interval
        if params.get("fixed_interval", "yes") != "yes":
            interval_tmp = random.randint(0, interval * 1000) / 1000.0

        logging.debug("Reset the system by monitor cmd"
                      " after %ssecs" % interval_tmp)
        time.sleep(interval_tmp)

    error_context.context("Try to login guest after reset", logging.info)
    vm.wait_for_login(timeout=timeout)
Example #21
0
def run(test, params, env):
    """
    KVM Seabios bin file test:
    1) Get available bin files
    2) Get supported machine types
    3) Check bin file in use

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

    bin_dict = {'rhel6': 'bios.bin', 'rhel7': 'bios-256k.bin'}

    error_context.context("Get available bin files", logging.info)
    output = process.system_output('ls /usr/share/seabios', shell=True).decode()
    for value in bin_dict.values():
        if value not in output:
            test.fail("%s is not available" % value)

    error_context.context("Get supported machine types", logging.info)
    qemu_binary = utils_misc.get_qemu_binary(params)
    machine_type_cmd = qemu_binary + " -machine help | awk '{ print $1 }'"
    output = process.system_output(machine_type_cmd, shell=True).decode()
    machine_types = output.splitlines()
    machine_type_remove = params['machine_type_remove'].split()
    for i in machine_type_remove:
        machine_types.remove(i)
    logging.info(machine_types)

    for machine in machine_types:
        error_context.context("Check bin file with machine type: %s" % machine,
                              logging.info)
        for key in bin_dict:
            if key in machine:
                bin_file = bin_dict[key]
                break
        else:
            test.error("Uncertain which bin file in use for machine type: %s"
                       % machine)

        params['machine_type'] = machine
        params['start_vm'] = 'yes'
        env_process.preprocess_vm(test, params, env, params.get("main_vm"))
        vm = env.get_vm(params["main_vm"])
        info_roms = vm.monitor.info("roms")
        if bin_file not in info_roms:
            test.fail("Checking bin file fails with %s, info roms: %s"
                      % (machine, info_roms))
        vm.destroy()
Example #22
0
def run(test, params, env):
    """
    Qemu numa basic test:
    1) Get host numa topological structure
    2) Start a guest and bind it on the cpus of one node
    3) Check the memory status of qemu process. It should mainly use the
       memory in the same node.
    4) Destroy the guest
    5) Repeat step 2 ~ 4 on every node in host

    :param test: QEMU test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment.
    """
    error.context("Get host numa topological structure", logging.info)
    timeout = float(params.get("login_timeout", 240))
    host_numa_node = utils_misc.NumaInfo()
    node_list = host_numa_node.online_nodes
    for node_id in node_list:
        error.base_context("Bind qemu process to numa node %s" % node_id,
                           logging.info)
        vm = "vm_bind_to_%s" % node_id
        params['qemu_command_prefix'] = "numactl --cpunodebind=%s" % node_id
        utils_memory.drop_caches()
        env_process.preprocess_vm(test, params, env, vm)
        vm = env.get_vm(vm)
        vm.verify_alive()
        session = vm.wait_for_login(timeout=timeout)
        session.close()

        error.context("Check the memory use status of qemu process",
                      logging.info)
        memory_status, _ = utils_test.qemu.get_numa_status(host_numa_node,
                                                           vm.get_pid())
        node_used_most = 0
        memory_sz_used_most = 0
        for index in range(len(node_list)):
            if memory_sz_used_most < memory_status[index]:
                memory_sz_used_most = memory_status[index]
                node_used_most = node_list[index]
            logging.debug("Qemu used %s pages in node"
                          " %s" % (memory_status[index], node_list[index]))
        if node_used_most != node_id:
            raise error.TestFail("Qemu still use memory from other node."
                                 " Expect: %s, used: %s" % (node_id,
                                                            node_used_most))

        error.context("Destroy guest.", logging.info)
        vm.destroy()
Example #23
0
def run_boot_cpu_model(test, params, env):
    """
    boot cpu model test:
    steps:
    1). boot guest with cpu model
    2). check flags if enable_check == "yes", otherwise shutdown guest

    :param test: QEMU test object
    :param params: Dictionary with the test parameters

    """
    cpu_vendor = utils_misc.get_cpu_vendor()
    host_model = utils_misc.get_host_cpu_models()

    model_list = params.get("cpu_model")
    if not model_list:
        if cpu_vendor == "unknow":
            raise error.TestError("unknow cpu vendor")
        else:
            model_list = params.get("cpu_model_%s" % cpu_vendor,
                                    host_model[-1])

    extra_flags = params.get("cpu_model_flags_%s" % cpu_vendor, "")
    if extra_flags:
        cpu_flags = params.get("cpu_model_flags", "") + extra_flags
        params["cpu_model_flags"] = cpu_flags

    if model_list:
        model_list = model_list.split(" ")
        for model in model_list:
            if model in host_model or model == "host":
                params["cpu_model"] = model
                params["start_vm"] = "yes"
                env_process.preprocess_vm(test, params, env, params["main_vm"])
                # check guest flags
                if params.get("enable_check", "no") == "yes":
                    utils_test.run_virt_sub_test(test, params,
                                                 env, sub_type="flag_check")
                else:
                    # log in and shutdown guest
                    utils_test.run_virt_sub_test(test, params,
                                                 env, sub_type="shutdown")
                    logging.info("shutdown guest successfully")
            else:
                if params.get("enable_check", "no") == "yes":
                    raise error.TestWarn("Can not test %s model on %s host, "
                                         "pls use %s host" % (model,
                                                              host_model[0],
                                                              model))
    def get_vm(self, create=False):
        """
        Get a live vm, if not create it;

        :param create: recreate VM or get a live VM.
        :return: Qemu vm object;
        """
        vm_name = self.params["main_vm"]
        if create:
            params = self.params
            params["start_vm"] = "yes"
            env_process.preprocess_vm(self.test, params, self.env, vm_name)
        vm = self.env.get_vm(vm_name)
        vm.verify_alive()
        return vm
    def get_vm(self, vm_name=None):
        """
        Get a live vm, if not create it;

        :param vm_name: vm name;
        :return: qemu vm object;
        :rtype: VM object;
        """
        name = vm_name or self.params["main_vm"]
        self.params["start_vm"] = "yes"
        self.params["restart_vm"] = "no"
        env_process.preprocess_vm(self.test, self.params, self.env, name)
        vm = self.env.get_vm(name)
        vm.verify_alive()
        return vm
def run_drive_mirror_complete(test, params, env):
    """
    Test block mirroring functionality

    1). boot vm, then mirror $source_image to $target_image
    2). wait for mirroring job go into ready status
    3). compare $source image and $target_image file
    4). reopen $target_image file if $open_target_image is 'yes'
    5). boot vm from $target_image , and check guest alive

    "qemu-img compare" is used to verify disk is mirrored successfully.
    """
    tag = params.get("source_images", "image1")
    qemu_img = qemu_storage.QemuImg(params, data_dir.get_data_dir(), tag)
    mirror_test = drive_mirror.DriveMirror(test, params, env, tag)
    try:
        source_image = mirror_test.get_image_file()
        target_image = mirror_test.get_target_image()
        mirror_test.start()
        mirror_test.wait_for_steady()
        mirror_test.vm.pause()
        time.sleep(5)
        if params.get("open_target_image", "no") == "yes":
            mirror_test.reopen()
            device_id = mirror_test.vm.get_block({"file": target_image})
            if device_id != mirror_test.device:
                raise error.TestError("Mirrored image not being used by guest")
        error.context("Compare fully mirrored images", logging.info)
        qemu_img.compare_images(source_image, target_image)
        mirror_test.vm.resume()
        mirror_test.vm.destroy()
        if params.get("boot_target_image", "no") == "yes":
            params = params.object_params(tag)
            if params.get("target_image_type") == "iscsi":
                params["image_name"] = mirror_test.target_image
                params["image_raw_device"] = "yes"
            else:
                params["image_name"] = params["target_image"]
                params["image_format"] = params["target_format"]
            env_process.preprocess_vm(test, params, env, params["main_vm"])
            vm = env.get_vm(params["main_vm"])
            timeout = int(params.get("login_timeout", 600))
            session = vm.wait_for_login(timeout=timeout)
            session.cmd(params.get("alive_check_cmd", "dir"), timeout=120)
            session.close()
            vm.destroy()
    finally:
        mirror_test.clean()
Example #27
0
    def _boot(img_name, img_fmt):
        """
        Boot test:
        1) Login guest
        2) Run dd in rhel guest
        3) Shutdown guest

        :param img_name: image name
        :param img_fmt: image format
        """
        params['image_name'] = img_name
        params['image_format'] = img_fmt
        image_name = "%s.%s" % (img_name, img_fmt)
        msg = "Try to boot vm with image %s" % image_name
        error.context(msg, logging.info)
        vm_name = params.get("main_vm")
        dd_timeout = int(params.get("dd_timeout", 60))
        params['vms'] = vm_name
        env_process.preprocess_vm(test, params, env, vm_name)
        vm = env.get_vm(params.get("main_vm"))
        vm.verify_alive()
        login_timeout = int(params.get("login_timeout", 360))
        session = vm.wait_for_login(timeout=login_timeout)

        # Run dd in linux guest
        if params.get("os_type") == 'linux':
            cmd = "dd if=/dev/zero of=/mnt/test bs=1000 count=1000"
            status = session.get_command_status(cmd, timeout=dd_timeout)
            if status != 0:
                raise error.TestError("dd failed")

        # Shutdown guest
        error.context("Shutdown command is sent, guest is going down...",
                      logging.info)
        try:
            session.sendline(params.get("shutdown_command"))
            vm.pause()
            if not vm.wait_until_dead(login_timeout):
                vm.destroy(gracefully=True)
                image_filename = _get_image_filename(img_name,
                                                     enable_gluster,
                                                     img_fmt)
                backup_img_chain(image_filename)
                raise error.TestFail("Can not shutdown guest")

            logging.info("Guest is down")
        finally:
            session.close()
Example #28
0
def run(test, params, env):
    """
    This case is from [general operation] Work around valgrind choking on our
    use of memalign():
    1.download valgrind form valgrind download page: www.valgrind.org.
    2.install the valgrind in host.
    3.run # valgrind /usr/libexec/qemu-kvm  -vnc :0 -S -m 384 -monitor stdio
    4.check the status and do continue the VM.
    5.quit the VM.

    :param test: QEMU test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment
    """
    interval = float(params.get("interval_time", "10"))

    def valgrind_intall():
        valgrind_install_cmd = params.get("valgrind_install_cmd")
        s = process.system(valgrind_install_cmd, timeout=3600, shell=True)
        if s != 0:
            test.error("Fail to install valgrind")
        else:
            logging.info("Install valgrind successfully.")

    valgring_support_check_cmd = params.get("valgring_support_check_cmd")
    error_context.context("Check valgrind installed in host", logging.info)
    try:
        process.system(valgring_support_check_cmd, timeout=interval,
                       shell=True)
    except Exception:
        valgrind_intall()

    params['mem'] = 384
    params["start_vm"] = "yes"
    error_context.context("Start guest with specific parameters", logging.info)
    env_process.preprocess_vm(test, params, env, params.get("main_vm"))
    vm = env.get_vm(params["main_vm"])

    time.sleep(interval)
    error_context.context("Verify guest status is running after cont",
                          logging.info)
    vm.verify_status(params.get("expected_status", "running"))

    error_context.context("Quit guest and check the process quit normally",
                          logging.info)
    vm.monitor.quit()
    vm.wait_until_dead(5, 0.5, 0.5)
    vm.verify_userspace_crash()
Example #29
0
def run(test, params, env):
    """
    Test tap device deleted after vm quit with error

    1) Boot a with invaild params.
    1) Check qemu-kvm quit with error.
    2) Check vm tap device delete from ovs bridge.

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

    def get_ovs_ports(ovs):
        cmd = "ovs-vsctl list-ports %s" % ovs
        return set(utils.system_output(cmd).splitlines())

    os_dep.command("ovs-vsctl")
    netdst = params.get("netdst")
    if netdst not in utils.system_output("ovs-vsctl list-br"):
        raise error.TestError("%s isn't is openvswith bridge" % netdst)

    deps_dir = data_dir.get_deps_dir("ovs")
    nic_script = utils_misc.get_path(deps_dir, params["nic_script"])
    nic_downscript = utils_misc.get_path(deps_dir, params["nic_downscript"])
    params["nic_script"] = nic_script
    params["nic_downscript"] = nic_downscript

    params["qemu_command_prefix"] = "export SHELL=/usr/bin/bash;"
    params["start_vm"] = "yes"
    params["nettype"] = "bridge"
    params["nic_model"] = "virtio-net-pci"

    try:
        ports = get_ovs_ports(netdst)
        env_process.preprocess_vm(test, params, env, params["main_vm"])
        env.get_vm(params["main_vm"])
    except virt_vm.VMStartError:
        ports = get_ovs_ports(netdst) - ports
        if ports:
            for p in ports:
                utils.system("ovs-vsctl del-if %s %s" % (netdst, p))
            raise error.TestFail("%s not delete after qemu quit." % ports)
    else:
        raise error.TestFail("Qemu should quit with error")
Example #30
0
def run_invalid_para_mq(test, params, env):
    """
    Enable MULTI_QUEUE feature in guest

    1) Boot up VM with wrong queues number
    2) check the qemu can report the error.

    @param test: QEMU test object.
    @param params: Dictionary with the test parameters.
    @param env: Dictionary with test environment.
    """
    params["start_vm"] = "yes"
    error.context("Boot the vm using queues %s'" % params.get("queues"), logging.info)
    try:
        env_process.preprocess_vm(test, params, env, params.get("main_vm"))
        env.get_vm(params["main_vm"])
    except utils_net.TAPCreationError, e:
        logging.info("Error when open tap, error info is '%s'" % e)
Example #31
0
def run(test, params, env):
    """
    Timer device boot guest:

    1) Check host clock's sync status with chronyd
    2) Add some load on host (Optional)
    3) Boot the guest with specific clock source
    4) Check the clock source currently used on guest
    5) Do some file operation on guest (Optional)
    6) Check the system time on guest and host (Optional)
    7) Check the hardware time on guest (linux only)
    8) Sleep period of time before reboot (Optional)
    9) Reboot guest (Optional)
    10) Check the system time on guest and host (Optional)
    11) Check the hardware time on guest (Optional)
    12) Restore guest's clock source

    :param test: QEMU test object.
    :param params: Dictionary with test parameters.
    :param env: Dictionary with the test environment.
    """
    def get_hwtime(session):
        """
        Get guest's hardware clock.

        :param session: VM session.
        """
        hwclock_time_command = params.get("hwclock_time_command", "hwclock -u")
        hwclock_time_filter_re = params.get("hwclock_time_filter_re",
                                            r"(\d+-\d+-\d+ \d+:\d+:\d+)")
        hwclock_time_format = params.get("hwclock_time_format",
                                         "%Y-%m-%d %H:%M:%S")
        output = session.cmd_output_safe(hwclock_time_command)
        try:
            str_time = re.findall(hwclock_time_filter_re, output)[0]
            guest_time = time.mktime(
                time.strptime(str_time, hwclock_time_format))
        except Exception as err:
            logging.debug("(time_format, time_string): (%s, %s)",
                          hwclock_time_format, str_time)
            raise err
        return guest_time

    def verify_timedrift(session, is_hardware=False):
        """
        Verify timedrift between host and guest.

        :param session: VM session.
        :param is_hardware: if need to verify guest's hardware time.
        """
        # Command to run to get the current time
        time_command = params["time_command"]
        # Filter which should match a string to be passed to time.strptime()
        time_filter_re = params["time_filter_re"]
        # Time format for time.strptime()
        time_format = params["time_format"]
        timerdevice_drift_threshold = float(
            params.get("timerdevice_drift_threshold", 3))

        time_type = "system" if not is_hardware else "harware"
        error_context.context("Check the %s time on guest" % time_type,
                              logging.info)
        host_time, guest_time = utils_test.get_time(session, time_command,
                                                    time_filter_re,
                                                    time_format)
        if is_hardware:
            guest_time = get_hwtime(session)
        drift = abs(float(host_time) - float(guest_time))
        if drift > timerdevice_drift_threshold:
            test.fail("The guest's %s time is different with"
                      " host's system time. Host time: '%s', guest time:"
                      " '%s'" % (time_type, host_time, guest_time))

    def get_current_clksrc(session):
        cmd = "cat /sys/devices/system/clocksource/"
        cmd += "clocksource0/current_clocksource"
        current_clksrc = session.cmd_output_safe(cmd)
        if "kvm-clock" in current_clksrc:
            return "kvm-clock"
        elif "tsc" in current_clksrc:
            return "tsc"
        elif "timebase" in current_clksrc:
            return "timebase"
        elif "acpi_pm" in current_clksrc:
            return "acpi_pm"
        return current_clksrc

    def update_clksrc(session, clksrc):
        """
        Update guest's clocksource, this func can work when not login
        into guest with ssh.

        :param session: VM session.
        :param clksrc: expected guest's clocksource.
        """
        avail_cmd = "cat /sys/devices/system/clocksource/clocksource0/"
        avail_cmd += "available_clocksource"
        avail_clksrc = session.cmd_output_safe(avail_cmd)
        if clksrc in avail_clksrc:
            clksrc_cmd = "echo %s > /sys/devices/system/clocksource/" % clksrc
            clksrc_cmd += "clocksource0/current_clocksource"
            status, output = session.cmd_status_output(clksrc_cmd)
            if status:
                test.fail(
                    "fail to update guest's clocksource to %s,"
                    "details: %s" % clksrc, output)
        else:
            test.error("please check the clocksource you want to set, "
                       "it's not supported by current guest, current "
                       "available clocksources: %s" % avail_clksrc)

    error_context.context("sync host time with NTP server", logging.info)
    clock_sync_command = params["clock_sync_command"]
    process.system(clock_sync_command, shell=True)

    timerdevice_host_load_cmd = params.get("timerdevice_host_load_cmd")
    if timerdevice_host_load_cmd:
        error_context.context("Add some load on host", logging.info)
        host_cpu_cnt_cmd = params["host_cpu_cnt_cmd"]
        host_cpu_cnt = int(
            process.system_output(host_cpu_cnt_cmd, shell=True).strip())
        if params["os_type"] == "linux":
            timerdevice_host_load_cmd = timerdevice_host_load_cmd % host_cpu_cnt
            process.system(timerdevice_host_load_cmd,
                           shell=True,
                           ignore_bg_processes=True)
        else:
            timerdevice_host_load_cmd = timerdevice_host_load_cmd % int(
                host_cpu_cnt / 2)
            stress_bg = utils_test.HostStress(
                "stress", params, stress_args=timerdevice_host_load_cmd)
            stress_bg.load_stress_tool()
        host_load_stop_cmd = params.get("timerdevice_host_load_stop_cmd",
                                        "pkill -f 'do X=1'")
        funcatexit.register(env, params["type"], _system, host_load_stop_cmd)

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

    vm = env.get_vm(params["main_vm"])
    vm.verify_alive()

    error_context.context("Sync guest timezone before test", logging.info)
    timeout = int(params.get("login_timeout", 360))
    if params["os_type"] == 'linux':
        utils_time.sync_timezone_linux(vm, timeout)
    else:
        utils_time.sync_timezone_win(vm, timeout)

    session = vm.wait_for_serial_login(timeout=timeout)

    timerdevice_clksource = params.get("timerdevice_clksource")
    need_restore_clksrc = False
    if timerdevice_clksource:
        origin_clksrc = get_current_clksrc(session)
        logging.info("guest is booted with %s", origin_clksrc)

        if timerdevice_clksource != origin_clksrc:
            update_clksrc(session, timerdevice_clksource)
            need_restore_clksrc = True

    error_context.context("check timedrift between guest and host.",
                          logging.info)
    verify_timedrift(session)
    if params["os_type"] == "linux":
        verify_timedrift(session, is_hardware=True)

    repeat_nums = params.get_numeric("repeat_nums")
    if repeat_nums:
        sleep_time = params["sleep_time"]
        for index in range(repeat_nums):
            time.sleep(int(sleep_time))
            verify_timedrift(session)
            if params["os_type"] == "linux":
                verify_timedrift(session, is_hardware=True)

    if params.get("timerdevice_reboot_test") == "yes":
        sleep_time = params.get("timerdevice_sleep_time")
        if sleep_time:
            error_context.context("Sleep '%s' secs before reboot" % sleep_time,
                                  logging.info)
            sleep_time = int(sleep_time)
            time.sleep(sleep_time)

        error_context.context(
            "Check timedrift between guest and host "
            "after reboot.", logging.info)
        vm.reboot(timeout=timeout, serial=True)
        verify_timedrift(session)
        if params["os_type"] == "linux":
            verify_timedrift(session, is_hardware=True)
    if need_restore_clksrc:
        update_clksrc(session, origin_clksrc)
    session.close()
Example #32
0
def run(test, params, env):
    """
    Configure watchdog, crash the guest and check if watchdog_action occurs.
    Test Step:
        1. see every function step
    Params:
        :param test: QEMU test object.
        :param params: Dictionary with test parameters.
        :param env: Dictionary with the test environment.
    """

    timeout = int(params.get("login_timeout", '360'))
    relogin_timeout = int(params.get("relogin_timeout", '240'))

    watchdog_device_type = params.get("watchdog_device_type", "i6300esb")
    watchdog_action = params.get("watchdog_action", "reset")
    trigger_cmd = params.get("trigger_cmd", "echo c > /dev/watchdog")

    # internal function
    def _watchdog_device_check(session, watchdog_device):
        """
        Check the watchdog device have been found and init successfully. if  not
        will raise error.
        """
        # when using ib700 need modprobe it's driver manually.
        if watchdog_device == "ib700":
            session.cmd("modprobe ib700wdt")

        # when wDT is 6300esb need check pci info
        if watchdog_device == "i6300esb":
            error.context("checking pci info to ensure have WDT device",
                          logging.info)
            o = session.cmd_output("lspci")
            if o:
                wdt_pci_info = re.findall(".*6300ESB Watchdog Timer", o)
                if not wdt_pci_info:
                    raise error.TestFail("Can not find watchdog pci")
            logging.info("Found watchdog pci device : %s" % wdt_pci_info)

        # checking watchdog init info using dmesg
        error.context("Checking watchdog init info using dmesg", logging.info)
        dmesg_info = params.get("dmesg_info", "(i6300ESB|ib700wdt).*init")
        (s,
         o) = session.cmd_status_output("dmesg | grep -i '%s' " % dmesg_info)
        if s != 0:
            error_msg = "Wactchdog device '%s' initialization failed "
            raise error.TestError(error_msg % watchdog_device)
        logging.info("Watchdog device '%s' add and init successfully" %
                     watchdog_device)
        logging.debug("Init info : '%s'" % o)

    def _trigger_watchdog(session, trigger_cmd=None):
        """
        Trigger watchdog action
        Params:
            @session: guest connect session.
            @trigger_cmd: cmd trigger the watchdog
        """
        if trigger_cmd is not None:
            error.context("Trigger Watchdog action using:'%s'." % trigger_cmd,
                          logging.info)
            session.sendline(trigger_cmd)

    def _action_check(session, watchdog_action):
        """
        Check whether or not the watchdog action occurred. if the action was
        not occurred will raise error.
        """
        # when watchdog action is pause, shutdown, reset, poweroff
        # the vm session will lost responsive
        response_timeout = int(params.get("response_timeout", '240'))
        error.context(
            "Check whether or not watchdog action '%s' took effect" %
            watchdog_action, logging.info)
        if not utils_misc.wait_for(lambda: not session.is_responsive(),
                                   response_timeout, 0, 1):
            if watchdog_action == "none" or watchdog_action == "debug":
                logging.info("OK, the guest session is responsive still")
            else:
                txt = "Oops, seems action '%s' took no" % watchdog_action
                txt += " effect, guest is still responsive."
                raise error.TestFail(txt)

        # when action is poweroff or shutdown(without no-shutdown option), the vm
        # will dead, and qemu exit.
        # The others the vm monitor still responsive, can report the vm status.
        if (watchdog_action == "poweroff"
                or (watchdog_action == "shutdown"
                    and params.get("disable_shutdown") != "yes")):
            if not utils_misc.wait_for(lambda: vm.is_dead(), response_timeout,
                                       0, 1):
                txt = "Oops, seems '%s' action took no effect, " % watchdog_action
                txt += "guest is still alive!"
                raise error.TestFail(txt)
        else:
            if watchdog_action == "pause":
                f_param = "paused"
            elif watchdog_action == "shutdown":
                f_param = "shutdown"
            else:
                f_param = "running"

            if not utils_misc.wait_for(
                    lambda: vm.monitor.verify_status(f_param),
                    response_timeout, 0, 1):
                logging.debug("Monitor status is:%s" % vm.monitor.get_status())
                txt = "Oops, seems action '%s' took no effect" % watchdog_action
                txt += " , Wrong monitor status!"
                raise error.TestFail(txt)

        # when the action is reset, need can relogin the guest.
        if watchdog_action == "reset":
            logging.info("Try to login the guest after reboot")
            vm.wait_for_login(timeout=relogin_timeout)
        logging.info("Watchdog action '%s' come into effect." %
                     watchdog_action)

    def check_watchdog_support():
        """
        check the host qemu-kvm support watchdog device
        Test Step:
        1. Send qemu command 'qemu -watchdog ?'
        2. Check the watchdog type that the host support.
        """
        qemu_binary = utils_misc.get_qemu_binary(params)

        watchdog_type_check = params.get("watchdog_type_check",
                                         " -watchdog '?'")
        qemu_cmd = qemu_binary + watchdog_type_check

        # check the host support watchdog types.
        error.context(
            "Checking whether or not the host support WDT '%s'" %
            watchdog_device_type, logging.info)
        watchdog_device = utils.system_output("%s 2>&1" % qemu_cmd,
                                              retain_output=True)
        if watchdog_device:
            if re.findall(watchdog_device_type, watchdog_device, re.I):
                logging.info("The host support '%s' type watchdog device" %
                             watchdog_device_type)
            else:
                logging.info("The host support watchdog device type is: '%s'" %
                             watchdog_device)
                raise error.TestNAError("watdog %s isn't supported!" %
                                        watchdog_device_type)
        else:
            raise error.TestNAError(
                "No watchdog device supported by the host!")

    def guest_boot_with_watchdog():
        """
        check the guest can boot with watchdog device
        Test Step:
        1. Boot guest with watchdog device
        2. Check watchdog device have been initialized successfully in guest
        """
        _watchdog_device_check(session, watchdog_device_type)

    def watchdog_action_test():
        """
        Watchdog action test
        Test Step:
        1. Boot guest with watchdog device
        2. Check watchdog device have been initialized successfully in guest
        3.Trigger wathchdog action through open /dev/watchdog
        4.Ensure watchdog_action take effect.
        """

        _watchdog_device_check(session, watchdog_device_type)
        _trigger_watchdog(session, trigger_cmd)
        _action_check(session, watchdog_action)

    def magic_close_support():
        """
        Magic close the watchdog action.
        Test Step:
        1. Boot guest with watchdog device
        2. Check watchdog device have been initialized successfully in guest
        3. Inside guest, trigger watchdog action"
        4. Inside guest, before heartbeat expires, close this action"
        5. Wait heartbeat timeout check the watchdog action deactive.
        """

        response_timeout = int(params.get("response_timeout", '240'))
        magic_cmd = params.get("magic_close_cmd", "echo V > /dev/watchdog")

        _watchdog_device_check(session, watchdog_device_type)
        _trigger_watchdog(session, trigger_cmd)

        # magic close
        error.context("Magic close is start", logging.info)
        _trigger_watchdog(session, magic_cmd)

        if utils_misc.wait_for(lambda: not session.is_responsive(),
                               response_timeout, 0, 1):
            error_msg = "Oops,Watchdog action took effect, magic close FAILED"
            raise error.TestFail(error_msg)
        logging.info("Magic close took effect.")

    def migration_when_wdt_timeout():
        """
        Migration when WDT timeout
        Test Step:
        1. Boot guest with watchdog device
        2. Check watchdog device have been initialized successfully in guest
        3. Start VM with watchdog device, action reset|pause
        4. Inside RHEL guest, trigger watchdog
        5. Before WDT timeout, do vm migration
        6. After migration, check the watchdog action take effect
        """

        mig_timeout = float(params.get("mig_timeout", "3600"))
        mig_protocol = params.get("migration_protocol", "tcp")
        mig_cancel_delay = int(params.get("mig_cancel") == "yes") * 2

        _watchdog_device_check(session, watchdog_device_type)
        _trigger_watchdog(session, trigger_cmd)

        error.context(
            "Do migration(protocol:%s),Watchdog have been triggered." %
            mig_protocol, logging.info)
        args = (mig_timeout, mig_protocol, mig_cancel_delay)
        migrate_thread = utils.InterruptedThread(vm.migrate, args)
        migrate_thread.start()
        _action_check(session, watchdog_action)
        migrate_thread.join(timeout=mig_timeout)

    def hotplug_unplug_watchdog_device():
        """
        Hotplug/unplug watchdog device
        Test Step:
        1. Start VM with "-watchdog-action pause" CLI option
        2. Add WDT via monitor
        3. Trigger watchdog action in guest
        4. Remove WDT device through monitor cmd "device_del"
        5. Resume and relogin the guest, check the device have been removed.
        """

        session = vm.wait_for_login(timeout=timeout)
        o = session.cmd_output("lspci")
        if o:
            wdt_pci_info = re.findall(".*6300ESB Watchdog Timer", o)
            if wdt_pci_info:
                raise error.TestFail("Can find watchdog pci")

        plug_watchdog_device = params.get("plug_watchdog_device", "i6300esb")
        watchdog_device_add = ("device_add driver=%s, id=%s" %
                               (plug_watchdog_device, "watchdog"))
        watchdog_device_del = ("device_del id=%s" % "watchdog")

        error.context("Hotplug watchdog device '%s'" % plug_watchdog_device,
                      logging.info)
        vm.monitor.send_args_cmd(watchdog_device_add)

        # wait watchdog device init
        time.sleep(5)
        _watchdog_device_check(session, plug_watchdog_device)
        _trigger_watchdog(session, trigger_cmd)
        _action_check(session, watchdog_action)

        error.context("Hot unplug watchdog device", logging.info)
        vm.monitor.send_args_cmd(watchdog_device_del)

        error.context("Resume the guest, check the WDT have been removed",
                      logging.info)
        vm.resume()
        session = vm.wait_for_login(timeout=timeout)
        o = session.cmd_output("lspci")
        if o:
            wdt_pci_info = re.findall(".*6300ESB Watchdog Timer", o)
            if wdt_pci_info:
                raise error.TestFail("Oops, find watchdog pci, unplug failed")
            logging.info("The WDT remove successfully")

    # main procedure
    test_type = params.get("test_type")
    check_watchdog_support()

    error.context("'%s' test starting ... " % test_type, logging.info)
    error.context(
        "Boot VM with WDT(Device:'%s', Action:'%s'),and try to login" %
        (watchdog_device_type, watchdog_action), logging.info)
    params["start_vm"] = "yes"
    env_process.preprocess_vm(test, params, env, params.get("main_vm"))
    vm = env.get_vm(params["main_vm"])
    session = vm.wait_for_login(timeout=timeout)

    if params.get("setup_runlevel") == "yes":
        error.context("Setup the runlevel for guest", logging.info)
        utils_test.qemu.setup_runlevel(params, session)

    if (test_type in locals()):
        test_running = locals()[test_type]
        test_running()
    else:
        raise error.TestError(
            "Oops test %s doesn't exist, have a check please." % test_type)
def run_timerdevice_change_guest_clksource(test, params, env):
    """
    Timer device check guest after update kernel line without kvmclock:

    1) Boot a guest with kvm-clock
    2) Check the current clocksource in guest
    3) Check the available clocksource in guest
    4) Update "clocksource=" parameter in guest kernel cli
    5) Boot guest system
    6) Check the current clocksource in guest

    :param test: QEMU test object.
    :param params: Dictionary with test parameters.
    :param env: Dictionary with the test environment.
    """
    def verify_guest_clock_source(session, expected):
        error.context("Check the current clocksource in guest", logging.info)
        cmd = "cat /sys/devices/system/clocksource/"
        cmd += "clocksource0/current_clocksource"
        if not expected in session.cmd(cmd):
            raise error.TestFail("Guest didn't use '%s' clocksource" %
                                 expected)

    error.context("Boot a guest with kvm-clock", logging.info)
    vm = env.get_vm(params["main_vm"])
    vm.verify_alive()

    timeout = int(params.get("login_timeout", 360))
    session = vm.wait_for_login(timeout=timeout)

    error.context("Check the current clocksource in guest", logging.info)
    cmd = "cat /sys/devices/system/clocksource/"
    cmd += "clocksource0/current_clocksource"
    if not "kvm-clock" in session.cmd(cmd):
        grub_file = params.get("grub_file", "/boot/grub2/grub.cfg")
        if "clocksource=" not in session.cmd("cat %s" % grub_file):
            raise error.TestFail("Guest didn't use 'kvm-clock' clocksource")

        error.context("Shutdown guest")
        vm.destroy()
        env.unregister_vm(vm.name)
        error.context("Update guest kernel cli to kvm-clock", logging.info)
        image_filename = storage.get_image_filename(params,
                                                    data_dir.get_data_dir())
        kernel_cfg_pattern = params.get("kernel_cfg_pos_reg",
                                        r".*vmlinuz-\d+.*")

        disk_obj = utils_disk.GuestFSModiDisk(image_filename)
        kernel_cfg_original = disk_obj.read_file(grub_file)
        try:
            logging.warn("Update the first kernel entry to" " kvm-clock only")
            kernel_cfg = re.findall(kernel_cfg_pattern, kernel_cfg_original)[0]
        except IndexError, detail:
            raise error.TestError("Couldn't find the kernel config, regex"
                                  " pattern is '%s', detail: '%s'" %
                                  (kernel_cfg_pattern, detail))

        if "clocksource=" in kernel_cfg:
            kernel_cfg_new = re.sub("clocksource=[a-z\- ]+", " ", kernel_cfg)
            disk_obj.replace_image_file_content(grub_file, kernel_cfg,
                                                kernel_cfg_new)

        error.context("Boot the guest", logging.info)
        vm_name = params["main_vm"]
        cpu_model_flags = params.get("cpu_model_flags")
        params["cpu_model_flags"] = cpu_model_flags + ",-kvmclock"
        env_process.preprocess_vm(test, params, env, vm_name)
        vm = env.get_vm(vm_name)
        vm.verify_alive()
        session = vm.wait_for_login(timeout=timeout)
Example #34
0
        return f_fail

    if params.get("catch_serial_cmd") is not None:
        length = int(params.get("length", "20"))
        id_leng = random.randint(0, length)
        drive_serial = ""
        convert_str = "!\"#$%&\'()*+./:;<=>?@[\\]^`{|}~"
        drive_serial = utils_misc.generate_random_string(id_leng,
                                                         ignore_str=",", convert_str=convert_str)

        params["drive_serial"] = drive_serial
        params["start_vm"] = "yes"

        vm = params["main_vm"]
        vm_params = params.object_params(vm)
        env_process.preprocess_vm(test, vm_params, env, vm)
        vm = env.get_vm(vm)
    else:
        vm = env.get_vm(params["main_vm"])

    vm.verify_alive()
    timeout = int(params.get("login_timeout", 360))
    chk_timeout = int(params.get("chk_timeout", 240))
    session = vm.wait_for_login(timeout=timeout)
    qtree = qemu_qtree.QtreeContainer()
    try:
        qtree.parse_info_qtree(vm.monitor.info('qtree'))
    except AttributeError:  # monitor doesn't support info qtree
        qtree = None

    logging.info("Starting physical resources check test")
Example #35
0
def run(test, params, env):
    """
    Try to kill the guest after/during network stress in guest.
    1) Boot up VM and log VM with serial.
    For driver mode test:
    2) Unload network driver(s).
    3) Load network driver(s) again.
    4) Repeat step 2 and 3 for 50 times.
    5) Check that we can kill VM with signal 0.
    For load mode test:
    2) Stop iptables in guest and host.
    3) Setup run netperf server in host and guest.
    4) Start heavy network load host <=> guest by running netperf
       client in host and guest.
    5) During netperf running, Check that we can kill VM with signal 0.
    6) Clean up netperf server in host and guest.(guest may already killed)

    :param test: QEMU test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment.
    """
    def get_ethernet_driver(session):
        """
        Get driver of network cards.

        :param session: session to machine
        """
        modules = []
        cmd = params.get("nic_module_cmd")
        out = session.cmd(cmd)
        for module in out.split("\n"):
            if cmd not in module:
                modules.append(module.split("/")[-1])
        modules.remove("")
        return set(modules)

    def kill_and_check(test, vm):
        vm.destroy(gracefully=False)
        if not vm.wait_until_dead(timeout=60):
            test.fail("VM is not dead after destroy operation")
        logging.info("VM is dead as expected")

    def netperf_stress(test, params, vm):
        """
        Netperf stress test.
        """
        n_client = utils_netperf.NetperfClient(
            vm.get_address(),
            params.get("client_path"),
            netperf_source=os.path.join(data_dir.get_deps_dir("netperf"),
                                        params.get("netperf_client_link")),
            client=params.get("shell_client"),
            port=params.get("shell_port"),
            username=params.get("username"),
            password=params.get("password"),
            prompt=params.get("shell_prompt"),
            linesep=params.get("shell_linesep",
                               "\n").encode().decode('unicode_escape'),
            status_test_command=params.get("status_test_command", ""),
            compile_option=params.get("compile_option", ""))
        n_server = utils_netperf.NetperfServer(
            utils_net.get_host_ip_address(params),
            params.get("server_path", "/var/tmp"),
            netperf_source=os.path.join(data_dir.get_deps_dir("netperf"),
                                        params.get("netperf_server_link")),
            password=params.get("hostpassword"),
            compile_option=params.get("compile_option", ""))

        try:
            n_server.start()
            # Run netperf with message size defined in range.
            test_duration = params.get_numeric("netperf_test_duration")
            test_protocols = params.get("test_protocol")
            netperf_output_unit = params.get("netperf_output_unit")
            test_option = params.get("test_option", "")
            test_option += " -l %s" % test_duration
            if params.get("netperf_remote_cpu") == "yes":
                test_option += " -C"
            if params.get("netperf_local_cpu") == "yes":
                test_option += " -c"
            if netperf_output_unit in "GMKgmk":
                test_option += " -f %s" % netperf_output_unit
            t_option = "%s -t %s" % (test_option, test_protocols)
            n_client.bg_start(utils_net.get_host_ip_address(params),
                              t_option,
                              params.get_numeric("netperf_para_sessions"),
                              params.get("netperf_cmd_prefix", ""),
                              package_sizes=params.get("netperf_sizes"))
            if utils_misc.wait_for(n_client.is_netperf_running, 10, 0, 1,
                                   "Wait netperf test start"):
                logging.info("Netperf test start successfully.")
            else:
                test.error("Can not start netperf client.")
        finally:
            n_server.stop()
            n_server.cleanup(True)
            n_client.cleanup(True)

    def netload_kill_problem(test, session_serial):
        firewall_flush = params.get("firewall_flush", "service iptables stop")
        error_context.context("Stop firewall in guest and host.", logging.info)
        try:
            process.run(firewall_flush, shell=True)
        except Exception:
            logging.warning("Could not stop firewall in host")

        try:
            session_serial.cmd(firewall_flush)
        except Exception:
            logging.warning("Could not stop firewall in guest")

        try:
            error_context.context(("Run subtest netperf_stress between"
                                   " host and guest.", logging.info))
            stress_thread = None
            wait_time = int(params.get("wait_bg_time", 60))
            bg_stress_run_flag = params.get("bg_stress_run_flag")
            vm_wait_time = int(params.get("wait_before_kill_vm"))
            env[bg_stress_run_flag] = False
            stress_thread = utils_misc.InterruptedThread(
                netperf_stress, (test, params, vm))
            stress_thread.start()
            utils_misc.wait_for(lambda: wait_time, 0, 1,
                                "Wait netperf_stress test start")
            logging.info("Sleep %ss before killing the VM", vm_wait_time)
            time.sleep(vm_wait_time)
            msg = "During netperf running, Check that we can kill VM with signal 0"
            error_context.context(msg, logging.info)
            kill_and_check(test, vm)
        finally:
            try:
                stress_thread.join(60)
            except Exception:
                pass

    def netdriver_kill_problem(test, session_serial):
        times = params.get_numeric("repeat_times", 10)
        modules = get_ethernet_driver(session_serial)
        logging.debug("Guest network driver(s): %s", modules)
        msg = "Repeatedly load/unload network driver(s) for %s times." % times
        error_context.context(msg, logging.info)
        for i in range(times):
            for module in modules:
                error_context.context("Unload driver %s. Repeat: %s/%s" %
                                      (module, i, times))
                session_serial.cmd_output_safe("rmmod %s" % module)
            for module in modules:
                error_context.context("Load driver %s. Repeat: %s/%s" %
                                      (module, i, times))
                session_serial.cmd_output_safe("modprobe %s" % module)

        error_context.context("Check that we can kill VM with signal 0.",
                              logging.info)
        kill_and_check(test, vm)

    vm = env.get_vm(params["main_vm"])
    vm.verify_alive()
    login_timeout = params.get_numeric("login_timeout", 360)
    session = vm.wait_for_login(timeout=login_timeout)
    session.close()
    session_serial = vm.wait_for_serial_login(timeout=login_timeout)
    mode = params.get("mode")
    if mode == "driver":
        netdriver_kill_problem(test, session_serial)
    elif mode == "load":
        netload_kill_problem(test, session_serial)
    session_serial.close()
    env_process.preprocess_vm(test, params, env, params["main_vm"])
    vm = env.get_vm(params["main_vm"])
    vm.verify_alive()
    vm.wait_for_login(timeout=login_timeout)
Example #36
0
def run(test, params, env):
    """
    Qemu device options value check test:

    1) Boot up guest with setted option value
    2) Check the value is correct inside guest

    :param test: QEMU test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment.
    """
    error_context.context("Boot up guest.", logging.info)
    timeout = float(params.get("login_timeout", 240))
    vm = env.get_vm(params["main_vm"])
    parameter_value = params.get("parameter_value", "random")
    params_name = params["params_name"]
    parameter_prefix = params.get("parameter_prefix", "")
    check_cmds = params["check_cmds"]
    convert_str = params.get("convert_str")

    if params.get("start_vm") == "no":
        if parameter_value == "random":
            parameter_len = int(params.get("parameter_len", 4))
            random_ignore_str = params.get("ignore_str")
            func_generate_random_string = utils_misc.generate_random_string
            args = (parameter_len, )
            if random_ignore_str:
                args += ("ignore_str=%s" % random_ignore_str, )
            if convert_str:
                args += ("convert_str=%s" % convert_str, )
            parameter_value = func_generate_random_string(*args)

        params[params_name] = parameter_prefix + parameter_value
        logging.debug("Setup '%s' to '%s'" %
                      (params_name, params[params_name]))

        params["start_vm"] = "yes"
        env_process.preprocess_vm(test, params, env, vm.name)

    if convert_str:
        tmp_str = re.sub(r'\\\\', 'Abackslash', parameter_value)
        tmp_str = re.sub(r'\\', '', tmp_str)
        tmp_str = re.sub('Abackslash', r"\\", tmp_str)
        parameter_value_raw = tmp_str
    else:
        parameter_value_raw = parameter_value

    if params.get("check_in_qtree") == "yes":
        error_context.context("Check option in qtree", logging.info)
        qtree = qemu_qtree.QtreeContainer()
        try:
            qtree.parse_info_qtree(vm.monitor.info('qtree'))
            keyword = params['qtree_check_keyword']
            qtree_check_value = params['qtree_check_value']
            qtree_check_option = params['qtree_check_option']

            for qdev in qtree.get_nodes():
                if keyword not in qdev.qtree:
                    continue
                if qdev.qtree[keyword] != qtree_check_value:
                    continue

                qtree_value = None
                for node in qemu_qtree.traverse(qdev):
                    if qtree_check_option in node.qtree:
                        qtree_value = str(node.qtree.get(qtree_check_option))
                        break

                if qtree_value is not None:
                    break
            else:
                test.fail(
                    "Can not find property '%s' from info qtree where '%s' is "
                    "'%s'" % (qtree_check_option, keyword, qtree_check_value))

            qtree_value = re.findall('"?(.*)"?$', qtree_value)[0]
            if (qtree_value != parameter_value_raw
                    and parameter_value_raw not in qtree_value):
                test.fail(
                    "Value from info qtree is not match with the value from"
                    "command line: '%s' vs '%s'" %
                    (qtree_value, parameter_value_raw))
        except AttributeError:
            logging.debug("Monitor deson't support info qtree skip this test")

    session = vm.wait_for_login(timeout=timeout)

    failed_log = ""
    for check_cmd in check_cmds.split():
        check_cmd_params = params.object_params(check_cmd)
        cmd = check_cmd_params['cmd']
        cmd = utils_misc.set_winutils_letter(session, cmd)
        pattern = check_cmd_params['pattern'] % parameter_value_raw

        error_context.context("Check option with command %s" % cmd,
                              logging.info)
        _, output = session.cmd_status_output(cmd)
        if not re.findall(r'%s' % pattern, output):
            failed_log += ("Can not find option %s from guest."
                           " Guest output is '%s'" % (params_name, output))

    session.close()

    if failed_log:
        test.fail(failed_log)
Example #37
0
    def check_ksm(mem, threshold_reached=False):
        """
        :param mem: Boot guest with given memory, in KB
        :ksmtuned_enabled: ksmtuned threshold is reached or not
        """
        def heavyload_install():
            if session.cmd_status(test_install_cmd) != 0:
                logging.warning("Could not find installed heavyload in guest, "
                                "will install it via winutils.iso ")
                winutil_drive = utils_misc.get_winutils_vol(session)
                if not winutil_drive:
                    test.cancel("WIN_UTILS CDROM not found.")
                install_cmd = params["install_cmd"] % winutil_drive
                session.cmd(install_cmd)

        def check_qemu_used_mem(qemu_pid, mem):
            qemu_used_page = process.getoutput(get_qemu_used_mem % qemu_pid,
                                               shell=True)
            qemu_used_mem = float(qemu_used_page) * pagesize
            if qemu_used_mem < mem * mem_thres:
                return False
            return True

        params['mem'] = mem // 1024
        params['start_vm'] = 'yes'
        vm_name = params['main_vm']
        env_process.preprocess_vm(test, params, env, vm_name)
        vm = env.get_vm(vm_name)
        session = vm.wait_for_login()
        qemu_pid = vm.get_pid()
        if params["os_type"] == "linux":
            params['stress_args'] = ('--cpu 4 --io 4 --vm 2 --vm-bytes %sM' %
                                     (int(params['mem']) // 2))
            stress_test = VMStress(vm, "stress", params)
            stress_test.load_stress_tool()
        else:
            install_path = params["install_path"]
            test_install_cmd = 'dir "%s" | findstr /I heavyload' % install_path
            heavyload_install()
            heavyload_bin = r'"%s\heavyload.exe" ' % install_path
            heavyload_options = ["/MEMORY 100", "/START"]
            start_cmd = heavyload_bin + " ".join(heavyload_options)
            stress_tool = BackgroundTest(
                session.cmd, (start_cmd, stress_timeout, stress_timeout))
            stress_tool.start()
            if not utils_misc.wait_for(stress_tool.is_alive, stress_timeout):
                test.error("Failed to start heavyload process")
        if not utils_misc.wait_for(lambda: check_qemu_used_mem(qemu_pid, mem),
                                   stress_timeout, 10, 10):
            test.error("QEMU used memory doesn't reach %s of guest mem %sM in "
                       "%ss" % (mem_thres, mem // 1024, stress_timeout))
        time.sleep(30)
        free_mem_host = utils_memory.freememtotal()
        ksm_status = process.getoutput(params['cmd_check_ksm_status'])
        vm.destroy()
        logging.info(
            "The ksm threshold is %sM, QEMU used memory is %sM, "
            "and the total free memory on host is %sM", ksm_thres // 1024,
            mem // 1024, free_mem_host // 1024)
        if threshold_reached:
            if free_mem_host > ksm_thres:
                test.error("Host memory is not consumed as much as expected")
            if ksm_status == '0':
                test.fail("KSM should be running")
        else:
            if free_mem_host < ksm_thres:
                test.error("Host memory is consumed too much more than "
                           "expected")
            if ksm_status != '0':
                test.fail("KSM should not be running")
Example #38
0
def run(test, params, env):
    """
    KVM whql env setup test:
    1) Log into a guest
    2) Update Windows kernel to the newest version
    3) Un-check Automatically restart in system failure
    4) Disable UAC
    5) Get the symbol files
    6) Set VM to physical memory + 100M
    7) Update the nic configuration
    8) Install debug view and make it auto run

    :param test: QEMU test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment.
    """
    log_path = "%s/../debug" % test.resultsdir
    # Prepare the tools iso
    error.context("Prepare the tools iso", logging.info)
    src_list = params.get("src_list")
    src_path = params.get("src_path", "%s/whql_src" % test.tmpdir)
    if not os.path.exists(src_path):
        os.makedirs(src_path)
    if src_list is not None:
        for i in re.split(",", src_list):
            utils.unmap_url(src_path, i, src_path)

    # Make iso for src
    cdrom_whql = params.get("cdrom_whql")
    cdrom_whql = utils_misc.get_path(data_dir.get_data_dir(), cdrom_whql)
    cdrom_whql_dir = os.path.split(cdrom_whql)[0]
    if not os.path.exists(cdrom_whql_dir):
        os.makedirs(cdrom_whql_dir)
    cmd = "mkisofs -J -o %s %s" % (cdrom_whql, src_path)
    utils.system(cmd)
    params["cdroms"] += " whql"

    vm = "vm1"
    vm_params = params.object_params(vm)
    env_process.preprocess_vm(test, vm_params, env, vm)
    vm = env.get_vm(vm)

    timeout = float(params.get("login_timeout", 240))
    session = vm.wait_for_login(timeout=timeout)
    error_log = utils_misc.get_path(log_path, "whql_setup_error_log")
    run_guest_log = params.get("run_guest_log",
                               "%s/whql_qemu_comman" % test.tmpdir)

    # Record qmmu command line in a log file
    error.context("Record qemu command line", logging.info)
    if os.path.isfile(run_guest_log):
        fd = open(run_guest_log, "r+")
        fd.read()
    else:
        fd = open(run_guest_log, "w")
    fd.write("%s\n" % vm.qemu_command)
    fd.close()

    # Get set up commands
    update_cmd = params.get("update_cmd", "")
    timezone_cmd = params.get("timezone_cmd", "")
    auto_restart = params.get("auto_restart", "")
    qxl_install = params.get("qxl_install", "")
    debuggers_install = params.get("debuggers_install", "")
    disable_uas = params.get("disable_uas", "")
    symbol_files = params.get("symbol_files", "")
    vm_size = int(params.get("mem")) + 100
    nic_cmd = params.get("nic_config_cmd", "")
    dbgview_cmd = params.get("dbgview_cmd", "")
    format_cmd = params.get("format_cmd", "")
    disable_firewall = params.get("disable_firewall", "")
    disable_update = params.get("disable_update", "")
    setup_timeout = int(params.get("setup_timeout", "7200"))
    disk_init_cmd = params.get("disk_init_cmd", "")
    disk_driver_install = params.get("disk_driver_install", "")

    vm_ma_cmd = "wmic computersystem set AutomaticManagedPagefile=False"
    vm_cmd = "wmic pagefileset where name=\"C:\\\\pagefile.sys\" set "
    vm_cmd += "InitialSize=%s,MaximumSize=%s" % (vm_size, vm_size)
    vm_ma_cmd = ""
    vm_cmd = ""
    if symbol_files:
        symbol_cmd = "del  C:\\\\symbols &&"
        symbol_cmd += "git clone %s C:\\\\symbol_files C:\\\\symbols" % \
                      symbol_files
    else:
        symbol_cmd = ""
    wmic_prepare_cmd = "echo exit > cmd && cmd /s wmic"

    error.context("Configure guest system", logging.info)
    cmd_list = [
        wmic_prepare_cmd, auto_restart, disable_uas, symbol_cmd, vm_ma_cmd,
        vm_cmd, dbgview_cmd, qxl_install, disable_firewall, timezone_cmd
    ]
    if nic_cmd:
        for index, nic in enumerate(re.split("\s+", params.get("nics"))):
            setup_params = params.get("nic_setup_params_%s" % nic, "")
            if params.get("vm_arch_name", "") == "x86_64":
                nic_cmd = re.sub("set", "set_64", nic_cmd)
            cmd_list.append("%s %s %s" %
                            (nic_cmd, str(index + 1), setup_params))
    if disk_init_cmd:
        disk_num = len(re.split("\s+", params.get("images")))
        if disk_driver_install:
            cmd_list.append(disk_driver_install + str(disk_num - 1))
        labels = "IJKLMNOPQRSTUVWXYZ"
        for index, images in enumerate(re.split("\s+", params.get("images"))):
            if index > 0:
                cmd_list.append(disk_init_cmd %
                                (str(index), labels[index - 1]))
                format_cmd_image = format_cmd % (
                    labels[index - 1], params.get("win_format_%s" % images))
                if params.get("win_extra_%s" % images):
                    format_cmd_image += " %s" % params.get(
                        "win_extra_%s" % images)
                cmd_list.append(format_cmd_image)

    cmd_list += [update_cmd, disable_update]

    failed_flag = 0

    # Check symbol files in guest
    if symbol_files:
        error.context("Update symbol files", logging.info)
        install_check_tool = False
        check_tool_chk = params.get("check_tool_chk",
                                    "C:\debuggers\symchk.exe")
        output = session.cmd_output(check_tool_chk)
        if "cannot find" in output:
            install_check_tool = True

        if install_check_tool:
            output = session.cmd_output(debuggers_install)
        symbol_file_check = params.get("symbol_file_check")
        symbol_file_download = params.get("symbol_file_download")

        symbol_check_pattern = params.get("symbol_check_pattern")
        symbol_pid_pattern = params.get("symbol_pid_pattern")
        download = utils_test.BackgroundTest(
            session.cmd, (symbol_file_download, setup_timeout))

        sessioncheck = vm.wait_for_login(timeout=timeout)
        download.start()
        while download.is_alive():
            o = sessioncheck.cmd_output(symbol_file_check, setup_timeout)
            if symbol_check_pattern in o:
                # Check is done kill download process
                cmd = "tasklist /FO list"
                s, o = sessioncheck.cmd_status_output(cmd)
                pid = re.findall(symbol_pid_pattern, o, re.S)
                if pid:
                    cmd = "taskkill /PID %s /F" % pid[0]
                    try:
                        sessioncheck.cmd(cmd)
                    except Exception:
                        pass
                    break
            time.sleep(5)
        sessioncheck.close()
        download.join()

    for cmd in cmd_list:
        if len(cmd) > 0:
            s = 0
            try:
                s, o = session.cmd_status_output(cmd, timeout=setup_timeout)
            except Exception, err:
                failed_flag += 1
                utils_misc.log_line(error_log,
                                    "Unexpected exception: %s" % err)
            if s != 0:
                failed_flag += 1
                utils_misc.log_line(error_log, o)
def run(test, params, env):
    """
    1) Boot the vm with multiple data disks.
    2) Run some operation in guest if request.
    3) Execute device_del for data disks.
    4) Sleep some time.
    5) Execute device_add for data disks.
    6) Sleep some time.
    7) repeat step 3-6 if request.

    :param test: QEMU test object.
    :param params: Dictionary with the test parameters.
    :param env: Dictionary with test environment.
    """
    def _run_sg_luns():
        file_name = "guest_sg_luns.sh"
        guest_dir = "/tmp/"
        deps_dir = virttest_data_dir.get_deps_dir()
        host_file = os.path.join(deps_dir, file_name)
        guest_file = guest_dir + file_name
        vm.copy_files_to(host_file, guest_dir)
        session.sendline("$SHELL " + guest_file)

    def _configure_images_params():
        for i in range(image_num):
            name = "stg%d" % i
            params['image_name_%s' % name] = "images/%s" % name
            params['image_size_%s' % name] = stg_image_size
            params["images"] = params["images"] + " " + name
            if params["drive_format"] == "scsi-hd":
                params["drive_bus_%s" % name] = 1
                params["blk_extra_params_%s" % name] = "lun=%d" % i
            image_params = params.object_params(name)
            env_process.preprocess_image(test, image_params, name)

    def _get_images_params():
        for i in range(image_num):
            name = "stg%d" % i
            dev = vm.devices.get_by_qid(name)[0]
            images_params[name] = copy.deepcopy(dev.params)

    def _hotplug_images():
        for i in range(1, image_num):
            name = "stg%d" % i
            try:
                vm.monitor.cmd("device_add", images_params[name], debug=False)
            except QMPCmdError as e:
                logging.warning('Ignore hotplug error: %s', str(e))

    def _hotunplug_images():
        for i in range(1, image_num):
            name = "stg%d" % i
            try:
                vm.monitor.cmd("device_del", {"id": name}, debug=False)
            except QMPCmdError as e:
                logging.warning('Ignore hotunplug error: %s', str(e))

    stg_image_size = params.get("stg_image_size", "256M")
    image_num = params.get_numeric("stg_image_num", 20)
    repeat_num = params.get_numeric("repeat_num", 1)
    unplug_time = params.get_numeric("unplug_time", 5)
    plug_time = params.get_numeric("plug_time", 5)
    images_params = {}
    error_context.context("Create images %d" % image_num, logging.info)
    _configure_images_params()
    params['start_vm'] = 'yes'
    env_process.preprocess_vm(test, params, env, params["main_vm"])
    vm = env.get_vm(params['main_vm'])
    session = vm.wait_for_login(timeout=int(params.get("login_timeout", 360)))

    error_context.context("Get images params", logging.info)
    _get_images_params()

    disks_num = int(session.cmd("lsblk -d -n|wc -l", timeout=60))
    logging.info("There are total %d disks", disks_num)

    guest_operation = params.get("guest_operation")
    if guest_operation:
        logging.info("Run %s in guest ", guest_operation)
        locals_var = locals()
        locals_var[guest_operation]()

    for n in range(repeat_num):
        error_context.context("Start unplug loop:%d" % n, logging.info)
        _hotunplug_images()
        time.sleep(unplug_time)
        error_context.context("Start plug loop:%d" % n, logging.info)
        _hotplug_images()
        time.sleep(plug_time)

    error_context.context("Check disks in guest.", logging.info)
    # re-login in case previous session is expired
    session = vm.wait_for_login(timeout=int(params.get("login_timeout", 360)))
    new_disks_num = int(session.cmd("lsblk -d -n|wc -l", timeout=300))
    logging.info("There are total %d disks after hotplug", new_disks_num)
    if new_disks_num != disks_num:
        logging.warning("Find unmatched disk numbers %d %d", disks_num,
                        new_disks_num)
Example #40
0
def run_trans_hugepage_swapping(test, params, env):
    """
    KVM khugepage user side test:
    1) Verify that the hugepages can be swapped in/out.

    @param test: KVM test object.
    @param params: Dictionary with test parameters.
    @param env: Dictionary with the test environment.
    """
    def get_args(args_list):
        """
        Get the memory arguments from system
        """
        args_list_tmp = args_list.copy()
        for line in file('/proc/meminfo', 'r').readlines():
            for key in args_list_tmp.keys():
                if line.startswith("%s" % args_list_tmp[key]):
                    args_list_tmp[key] = int(re.split('\s+', line)[1])
        return args_list_tmp

    try:
        # Swapping test
        logging.info("Swapping test start")
        # Parameters of memory information
        # @total: Memory size
        # @free: Free memory size
        # @swap_size: Swap size
        # @swap_free: Free swap size
        # @hugepage_size: Page size of one hugepage
        # @page_size: The biggest page size that app can ask for
        args_dict_check = {"free" : "MemFree", "swap_size" : "SwapTotal",
                           "swap_free" : "SwapFree", "total" : "MemTotal",
                           "hugepage_size" : "Hugepagesize",}
        args_dict = get_args(args_dict_check)
        swap_free = []
        total = int(args_dict['total']) / 1024
        free = int(args_dict['free']) / 1024
        swap_size = int(args_dict['swap_size']) / 1024
        swap_free.append(int(args_dict['swap_free'])/1024)
        hugepage_size = int(args_dict['hugepage_size']) / 1024
        login_timeout = float(params.get("login_timeout", 360))
        check_cmd_timeout = float(params.get("check_cmd_timeout", 900))
        mem_path = os.path.join(test.tmpdir, 'thp_space')

        # If swap is enough fill all memory with dd
        if swap_free > (total - free):
            count = total / hugepage_size
            tmpfs_size = total
        else:
            count = free / hugepage_size
            tmpfs_size = free

        if swap_size <= 0:
            logging.warning("Host does not have swap enabled")
        session = None
        try:
            if not os.path.isdir(mem_path):
                os.makedirs(mem_path)
            utils.run("mount -t tmpfs  -o size=%sM none %s" % (tmpfs_size,
                                                               mem_path))

            # Set the memory size of vm
            # To ignore the oom killer set it to the free swap size
            vm = env.get_vm(params.get("main_vm"))
            vm.verify_alive()
            if int(params['mem']) > swap_free[0]:
                vm.destroy()
                vm_name = 'vmsw'
                vm0 =  params.get("main_vm")
                vm0_key = env.get_vm(vm0)
                params['vms'] = params['vms'] + " " + vm_name
                params['mem'] = str(swap_free[0])
                vm_key = vm0_key.clone(vm0, params)
                env.register_vm(vm_name, vm_key)
                env_process.preprocess_vm(test, params, env, vm_name)
                vm_key.create()
                session = vm_key.wait_for_login(timeout=login_timeout)
            else:
                session = vm.wait_for_login(timeout=login_timeout)

            error.context("making guest to swap memory")
            cmd = ("dd if=/dev/zero of=%s/zero bs=%s000000 count=%s" %
                   (mem_path, hugepage_size, count))
            utils.run(cmd)

            args_dict = get_args(args_dict_check)
            swap_free.append(int(args_dict['swap_free'])/1024)

            if swap_free[1] - swap_free[0] >= 0:
                raise error.TestFail("No data was swapped to memory")

            # Try harder to make guest memory to be swapped
            session.cmd("find / -name \"*\"", timeout=check_cmd_timeout)
        finally:
            if session is not None:
                utils.run("umount %s" % mem_path)

        logging.info("Swapping test succeed")

    finally:
        if session is not None:
            session.close()
Example #41
0
def run(test, params, env):
    """
    KVM multi test:
    1) Log into guests
    2) Check all the nics available or not
    3) Ping among guest nic and host
       3.1) Ping with different packet size
       3.2) Flood ping test
       3.3) Final ping test
    4) Transfer files among guest nics and host
       4.1) Create file by dd command in guest
       4.2) Transfer file between nics
       4.3) Compare original file and transferred file
    5) ping among different nics
       5.1) Ping with different packet size
       5.2) Flood ping test
       5.3) Final ping test
    6) Transfer files among different nics
       6.1) Create file by dd command in guest
       6.2) Transfer file between nics
       6.3) Compare original file and transferred file
    7) Repeat step 3 - 6 on every nic.

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

    def ping(session, nic, dst_ip, strick_check, flood_minutes):
        d_packet_size = [1, 4, 48, 512, 1440, 1500, 1505, 4054, 4055, 4096,
                         4192, 8878, 9000, 32767, 65507]
        packet_size = params.get("packet_size", "").split() or d_packet_size
        for size in packet_size:
            error.context("Ping with packet size %s" % size, logging.info)
            status, output = utils_test.ping(dst_ip, 10, interface=nic,
                                             packetsize=size, timeout=30,
                                             session=session)
            if strict_check:
                ratio = utils_test.get_loss_ratio(output)
                if ratio != 0:
                    raise error.TestFail("Loss ratio is %s for packet size"
                                         " %s" % (ratio, size))
            else:
                if status != 0:
                    raise error.TestFail("Ping returns non-zero value %s" %
                                         output)

        error.context("Flood ping test", logging.info)
        utils_test.ping(dst_ip, None, interface=nic, flood=True,
                        output_func=None, timeout=flood_minutes * 60,
                        session=session)
        error.context("Final ping test", logging.info)
        counts = params.get("ping_counts", 100)
        status, output = utils_test.ping(dst_ip, counts, interface=nic,
                                         timeout=float(counts) * 1.5,
                                         session=session)
        if strick_check == "yes":
            ratio = utils_test.get_loss_ratio(output)
            if ratio != 0:
                raise error.TestFail("Packet loss ratio is %s after flood"
                                     % ratio)
        else:
            if status != 0:
                raise error.TestFail("Ping returns non-zero value %s" %
                                     output)

    def file_transfer(session, src, dst):
        username = params.get("username", "")
        password = params.get("password", "")
        src_path = "/tmp/1"
        dst_path = "/tmp/2"
        port = int(params["file_transfer_port"])

        cmd = "dd if=/dev/urandom of=%s bs=100M count=1" % src_path
        cmd = params.get("file_create_cmd", cmd)

        error.context("Create file by dd command, cmd: %s" % cmd, logging.info)
        session.cmd(cmd)

        transfer_timeout = int(params.get("transfer_timeout"))
        log_filename = "scp-from-%s-to-%s.log" % (src, dst)
        error.context("Transfer file from %s to %s" % (src, dst), logging.info)
        remote.scp_between_remotes(src, dst, port, password, password,
                                   username, username, src_path, dst_path,
                                   log_filename=log_filename,
                                   timeout=transfer_timeout)
        src_path = dst_path
        dst_path = "/tmp/3"
        log_filename = "scp-from-%s-to-%s.log" % (dst, src)
        error.context("Transfer file from %s to %s" % (dst, src), logging.info)
        remote.scp_between_remotes(dst, src, port, password, password,
                                   username, username, src_path, dst_path,
                                   log_filename=log_filename,
                                   timeout=transfer_timeout)
        error.context("Compare original file and transferred file",
                      logging.info)

        cmd1 = "md5sum /tmp/1"
        cmd2 = "md5sum /tmp/3"
        md5sum1 = session.cmd(cmd1).split()[0]
        md5sum2 = session.cmd(cmd2).split()[0]
        if md5sum1 != md5sum2:
            raise error.TestError("File changed after transfer")

    nic_interface_list = []
    check_irqbalance_cmd = params.get("check_irqbalance_cmd")
    stop_irqbalance_cmd = params.get("stop_irqbalance_cmd")
    start_irqbalance_cmd = params.get("start_irqbalance_cmd")
    status_irqbalance = params.get("status_irqbalance")
    vms = params["vms"].split()
    host_mem = utils_memory.memtotal() / (1024 * 1024)
    host_cpu_count = len(utils_misc.get_cpu_processors())
    vhost_count = 0
    if params.get("vhost"):
        vhost_count = 1
    if host_cpu_count < (1 + vhost_count) * len(vms):
        raise error.TestError("The host don't have enough cpus to start guest"
                              "pcus: %d, minimum of vcpus and vhost: %d" %
                              (host_cpu_count, (1 + vhost_count) * len(vms)))
    params['mem'] = host_mem / len(vms) * 1024
    params['smp'] = host_cpu_count / len(vms) - vhost_count
    if params['smp'] % 2 != 0:
        params['vcpu_sockets'] = 1
    params["start_vm"] = "yes"
    for vm_name in vms:
        env_process.preprocess_vm(test, params, env, vm_name)
    timeout = float(params.get("login_timeout", 360))
    strict_check = params.get("strick_check", "no")
    host_ip = utils_net.get_ip_address_by_interface(params.get("netdst"))
    host_ip = params.get("srchost", host_ip)
    flood_minutes = float(params["flood_minutes"])
    error.context("Check irqbalance service status", logging.info)
    o = process.system_output(check_irqbalance_cmd, ignore_status=True)
    check_stop_irqbalance = False
    if re.findall(status_irqbalance, o):
        logging.debug("stop irqbalance")
        process.run(stop_irqbalance_cmd)
        check_stop_irqbalance = True
        o = process.system_output(check_irqbalance_cmd, ignore_status=True)
        if re.findall(status_irqbalance, o):
            raise error.TestError("Can not stop irqbalance")
    thread_list = []
    nic_interface = []
    for vm_name in vms:
        guest_ifname = ""
        guest_ip = ""
        vm = env.get_vm(vm_name)
        session = vm.wait_for_login(timeout=timeout)
        thread_list.extend(vm.vcpu_threads)
        thread_list.extend(vm.vhost_threads)
        error.context("Check all the nics available or not", logging.info)
        for index, nic in enumerate(vm.virtnet):
            guest_ifname = utils_net.get_linux_ifname(session, nic.mac)
            guest_ip = vm.get_address(index)
            if not (guest_ifname and guest_ip):
                err_log = "vms %s get ip or ifname failed." % vm_name
                err_log = "ifname: %s, ip: %s." % (guest_ifname, guest_ip)
                raise error.TestFail(err_log)
            nic_interface = [guest_ifname, guest_ip, session]
            nic_interface_list.append(nic_interface)
    error.context("Pin vcpus and vhosts to host cpus", logging.info)
    host_numa_nodes = utils_misc.NumaInfo()
    vthread_num = 0
    for numa_node_id in host_numa_nodes.nodes:
        numa_node = host_numa_nodes.nodes[numa_node_id]
        for _ in range(len(numa_node.cpus)):
            if vthread_num >= len(thread_list):
                break
            vcpu_tid = thread_list[vthread_num]
            logging.debug("pin vcpu/vhost thread(%s) to cpu(%s)" %
                          (vcpu_tid, numa_node.pin_cpu(vcpu_tid)))
            vthread_num += 1

    nic_interface_list_len = len(nic_interface_list)
    # ping and file transfer test
    for src_ip_index in range(nic_interface_list_len):
        error.context("Ping test from guest to host", logging.info)
        src_ip_info = nic_interface_list[src_ip_index]
        ping(src_ip_info[2], src_ip_info[0], host_ip, strict_check,
             flood_minutes)
        error.context("File transfer test between guest and host",
                      logging.info)
        file_transfer(src_ip_info[2], src_ip_info[1], host_ip)
        for dst_ip in nic_interface_list[src_ip_index:]:
            txt = "Ping test between %s and %s" % (src_ip_info[1], dst_ip[1])
            error.context(txt, logging.info)
            ping(src_ip_info[2], src_ip_info[0], dst_ip[1], strict_check,
                 flood_minutes)
            txt = "File transfer test between %s " % src_ip_info[1]
            txt += "and %s" % dst_ip[1]
            error.context(txt, logging.info)
            file_transfer(src_ip_info[2], src_ip_info[1], dst_ip[1])
    if check_stop_irqbalance:
        process.run(start_irqbalance_cmd)
Example #42
0
def run(test, params, env):
    """
    Try to kill the guest after/during network stress in guest.
    1) Boot up VM and log VM with serial.
    For driver mode test:
    2) Unload network driver(s).
    3) Load network driver(s) again.
    4) Repeat step 2 and 3 for 50 times.
    5) Check that we can kill VM with signal 0.
    For load mode test:
    2) Stop iptables in guest and host.
    3) Setup run netperf server in host and guest.
    4) Start heavy network load host <=> guest by running netperf
       client in host and guest.
    5) During netperf running, Check that we can kill VM with signal 0.
    6) Clean up netperf server in host and guest.(guest may already killed)

    :param test: QEMU test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment.
    """
    def get_ethernet_driver(session):
        """
        Get driver of network cards.

        :param session: session to machine
        """
        modules = []
        cmd = params.get("nic_module_cmd")
        out = session.cmd(cmd)
        for module in out.split("\n"):
            if cmd not in module:
                modules.append(module.split("/")[-1])
        modules.remove("")
        return set(modules)

    def kill_and_check(test, vm):
        vm.destroy(gracefully=False)
        if not vm.wait_until_dead(timeout=60):
            test.fail("VM is not dead after destroy operation")
        logging.info("VM is dead as expected")

    def netload_kill_problem(test, session_serial):
        firewall_flush = params.get("firewall_flush", "service iptables stop")
        error_context.context("Stop firewall in guest and host.", logging.info)
        try:
            process.run(firewall_flush, shell=True)
        except Exception:
            logging.warning("Could not stop firewall in host")

        try:
            session_serial.cmd(firewall_flush)
        except Exception:
            logging.warning("Could not stop firewall in guest")

        try:
            bg_stress_test = params.get("background_stress_test",
                                        'netperf_stress')
            error_context.context(
                ("Run subtest %s between host and guest." % bg_stress_test),
                logging.info)
            stress_thread = None
            wait_time = int(params.get("wait_bg_time", 60))
            bg_stress_run_flag = params.get("bg_stress_run_flag")
            vm_wait_time = int(params.get("wait_before_kill_vm"))
            env[bg_stress_run_flag] = False
            stress_thread = utils_misc.InterruptedThread(
                utils_test.run_virt_sub_test, (test, params, env),
                {"sub_type": bg_stress_test})
            stress_thread.start()
            if not utils_misc.wait_for(lambda: env.get(bg_stress_run_flag),
                                       wait_time, 0, 1,
                                       "Wait %s test start" % bg_stress_test):
                err = "Fail to start netperf test between guest and host"
                test.error(err)

            logging.info("Sleep %ss before killing the VM", vm_wait_time)
            time.sleep(vm_wait_time)
            msg = "During netperf running, Check that we can kill VM with signal 0"
            error_context.context(msg, logging.info)
            kill_and_check(test, vm)
        finally:
            try:
                stress_thread.join(60)
            except Exception:
                pass

    def netdriver_kill_problem(test, session_serial):
        times = int(params.get("repeat_times", 10))
        modules = get_ethernet_driver(session_serial)
        logging.debug("Guest network driver(s): %s", modules)
        msg = "Repeatedly load/unload network driver(s) for %s times." % times
        error_context.context(msg, logging.info)
        for i in range(times):
            for module in modules:
                error_context.context("Unload driver %s. Repeat: %s/%s" %
                                      (module, i, times))
                session_serial.cmd_output_safe("rmmod %s" % module)
            for module in modules:
                error_context.context("Load driver %s. Repeat: %s/%s" %
                                      (module, i, times))
                session_serial.cmd_output_safe("modprobe %s" % module)

        error_context.context("Check that we can kill VM with signal 0.",
                              logging.info)
        kill_and_check(test, vm)

    vm = env.get_vm(params["main_vm"])
    vm.verify_alive()
    login_timeout = int(params.get("login_timeout", 360))
    session = vm.wait_for_login(timeout=login_timeout)
    session.close()
    session_serial = vm.wait_for_serial_login(timeout=login_timeout)

    mode = params.get("mode")
    if mode == "driver":
        netdriver_kill_problem(test, session_serial)
    elif mode == "load":
        netload_kill_problem(test, session_serial)
    env_process.preprocess_vm(test, params, env, params["main_vm"])
    vm = env.get_vm(params["main_vm"])
    vm.verify_alive()
    vm.wait_for_login(timeout=login_timeout)
Example #43
0
def run(test, params, env):
    """
    ple test:
    1) Check if ple is enabled on host, if not, enable it
    2) Boot guest and run unixbench inside guest
    3) Record benchmark scores and shutdown guest
    4) Disable ple on host
    5) Boot guest and run unixbench inside guest again
    6) Compare benchmark scores with step 3)
    7) Restore env, set ple back

    :param test: QEMU test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment.
    """
    def reload_module(value):
        """
        Reload module
        """
        process.system("rmmod %s" % module)
        cmd = "modprobe %s %s=%s" % (module, mod_param, value)
        process.system(cmd)

    def run_unixbench(cmd):
        """
        Run unixbench inside guest, return benchmark scores
        """
        error_context.context("Run unixbench inside guest", logging.info)
        output = session.cmd_output_safe(cmd, timeout=4800)
        scores = re.findall(r"System Benchmarks Index Score\s+(\d+\.?\d+)",
                            output)
        return [float(i) for i in scores]

    module = params["module_name"]
    mod_param = params["mod_param"]
    read_cmd = "cat /sys/module/%s/parameters/%s" % (module, mod_param)
    origin_ple = process.getoutput(read_cmd)
    error_context.context("Enable ple on host if it's disabled", logging.info)
    if origin_ple == 0:
        reload_module(params["ple_value"])

    host_cpu_count = cpu.online_count()
    params["smp"] = host_cpu_count
    params["vcpu_maxcpus"] = host_cpu_count
    params["start_vm"] = "yes"
    env_process.preprocess_vm(test, params, env, params["main_vm"])

    vm = env.get_vm(params["main_vm"])
    vm.verify_alive()
    session = vm.wait_for_login()
    session.cmd(params["get_unixbench"])
    try:
        cmd = params["run_unixbench"]
        scores_on = run_unixbench(cmd)
        logging.info("Unixbench scores are %s when ple is on" % scores_on)
        vm.destroy()

        error_context.context("Disable ple on host", logging.info)
        reload_module(0)
        vm.create(params=params)
        session = vm.wait_for_login()
        scores_off = run_unixbench(cmd)
        logging.info("Unixbench scores are %s when ple is off" % scores_off)
        scores_off = [x*0.96 for x in scores_off]
        if scores_on[0] < scores_off[0] or scores_on[1] < scores_off[1]:
            test.fail("Scores is much lower when ple is on than off")
    finally:
        session.cmd_output_safe("rm -rf %s" % params["unixbench_dir"])
        session.close()
        vm.destroy()
        reload_module(origin_ple)
Example #44
0
def run(test, params, env):
    """
    Check physical resources assigned to KVM virtual machines:
    1) Log into the guest
    2) Verify whether cpu counts ,memory size, nics' model,
       count and drives' format & count, drive_serial, UUID
       reported by the guest OS matches what has been assigned
       to the VM (qemu command line)
    3) Verify all MAC addresses for guest NICs

    :param test: QEMU test object.
    :param params: Dictionary with the test parameters.
    :param env: Dictionary with test environment.
    """
    # Define a function for checking number of hard drivers & NICs
    def check_num(devices, info_cmd, check_str):
        f_fail = []
        expected_num = params.objects(devices).__len__()
        o = ""
        try:
            o = vm.monitor.human_monitor_cmd("info %s " % info_cmd)
        except qemu_monitor.MonitorError as e:
            fail_log = str(e) + "\n"
            fail_log += "info/query monitor command failed (%s)" % info_cmd
            f_fail.append(fail_log)
            logging.error(fail_log)

        actual_num = o.count(check_str)
        if expected_num != actual_num:
            fail_log = "%s number mismatch:\n" % str(devices)
            fail_log += "    Assigned to VM: %d\n" % expected_num
            fail_log += "    Reported by OS: %d" % actual_num
            f_fail.append(fail_log)
            logging.error(fail_log)
        return f_fail

    # Define a function for checking hard drives & NICs' model
    def chk_fmt_model(device, fmt_model, info_cmd, regexp):
        f_fail = []
        devices = params.objects(device)
        for chk_device in devices:
            expected = params.object_params(chk_device).get(fmt_model)
            if not expected:
                expected = "rtl8139"
            o = ""
            try:
                o = vm.monitor.human_monitor_cmd("info %s" % info_cmd)
            except qemu_monitor.MonitorError as e:
                fail_log = str(e) + "\n"
                fail_log += "info/query monitor command failed (%s)" % info_cmd
                f_fail.append(fail_log)
                logging.error(fail_log)

            device_found = re.findall(regexp, o)
            logging.debug("Found devices: %s", device_found)
            found = False
            for fm in device_found:
                if expected in fm:
                    found = True

            if not found:
                fail_log = "%s model mismatch:\n" % str(device)
                fail_log += "    Assigned to VM: %s\n" % expected
                fail_log += "    Reported by OS: %s" % device_found
                f_fail.append(fail_log)
                logging.error(fail_log)
        return f_fail

    # Define a function to verify UUID & Serial number
    def verify_device(expect, name, verify_cmd):
        f_fail = []
        if verify_cmd:
            actual = session.cmd_output(verify_cmd)
            if not re.findall(expect, actual, re.I):
                fail_log = "%s mismatch:\n" % name
                fail_log += "    Assigned to VM: %s\n" % expect.upper()
                fail_log += "    Reported by OS: %s" % actual
                f_fail.append(fail_log)
                logging.error(fail_log)
        return f_fail

    def get_cpu_number(chk_type, chk_timeout):
        """
        Get cpu sockets/cores/threads number.

        :param chk_type: Should be one of 'sockets', 'cores', 'threads'.
        :param chk_timeout: timeout of running chk_cmd.

        :return: Actual number of guest cpu number.
        """
        chk_str = params["mem_chk_re_str"]
        chk_cmd = params.get("cpu_%s_chk_cmd" % chk_type)

        if chk_cmd is None:
            fail_log = "Unknown cpu number checking type: '%s'" % chk_type
            logging.error(fail_log)
            return -1

        s, output = session.cmd_status_output(chk_cmd, timeout=chk_timeout)
        num = re.findall(chk_str, output)
        if s != 0 or not num:
            fail_log = "Failed to get guest %s number, " % chk_type
            fail_log += "guest output: '%s'" % output
            logging.error(fail_log)
            return -2

        logging.info("CPU %s number: %d", chk_type.capitalize(), int(num[-1]))
        return int(num[-1])

    def check_cpu_number(chk_type, actual_n, expected_n):
        """
        Checking cpu sockets/cores/threads number.

        :param chk_type: Should be one of 'sockets', 'cores', 'threads'.
        :param actual_n: Actual number of guest cpu number.
        :param expected_n: Expected number of guest cpu number.

        :return: a list that contains fail report.
        """
        f_fail = []

        if actual_n == -1:
            fail_log = "Unknown cpu number checking type: '%s'" % chk_type
            logging.error(fail_log)
            f_fail.append(fail_log)
            return f_fail

        if actual_n == -2:
            fail_log = "Failed to get guest %s number." % chk_type
            logging.error(fail_log)
            f_fail.append(fail_log)
            return f_fail

        logging.info("CPU %s number check", chk_type.capitalize())

        if actual_n != expected_n:
            fail_log = "%s output mismatch:\n" % chk_type.capitalize()
            fail_log += "    Assigned to VM: '%s'\n" % expected_n
            fail_log += "    Reported by OS: '%s'" % actual_n
            f_fail.append(fail_log)
            logging.error(fail_log)
            return f_fail

        logging.debug("%s check pass. Expected: '%s', Actual: '%s'",
                      chk_type.capitalize(), expected_n, actual_n)
        return f_fail

    def verify_machine_type():
        f_fail = []
        cmd = params.get("check_machine_type_cmd")
        fail_log = ""

        if cmd is None:
            return f_fail

        status, actual_mtype = session.cmd_status_output(cmd)
        if status != 0:
            test.error("Failed to get machine type from vm")

        machine_type_cmd = "%s -M ?" % utils_misc.get_qemu_binary(params)
        machine_types = process.system_output(machine_type_cmd,
                                              ignore_status=True)
        machine_types = machine_types.split(':')[-1]
        machine_type_map = {}
        for machine_type in machine_types.splitlines():
            if not machine_type:
                continue
            type_pair = re.findall(r"([\w\.-]+)\s+([^(]+).*", machine_type)
            if len(type_pair) == 1 and len(type_pair[0]) == 2:
                machine_type_map[type_pair[0][0]] = type_pair[0][1]
            else:
                logging.warn("Unexpect output from qemu-kvm -M "
                             "?: '%s'" % machine_type)
        try:
            expect_mtype = machine_type_map[params['machine_type']].strip()
        except KeyError:
            logging.warn("Can not find machine type '%s' from qemu-kvm -M ?"
                         " output. Skip this test." % params['machine_type'])
            return f_fail

        if expect_mtype not in actual_mtype:
            fail_log += "    Assigned to VM: '%s' \n" % expect_mtype
            fail_log += "    Reported by OS: '%s'" % actual_mtype
            f_fail.append(fail_log)
            logging.error(fail_log)
        else:
            logging.info("MachineType check pass. Expected: %s, Actual: %s" %
                         (expect_mtype, actual_mtype))
        return f_fail

    if params.get("catch_serial_cmd") is not None:
        length = int(params.get("length", "20"))
        id_leng = random.randint(0, length)
        drive_serial = ""
        convert_str = "!\"#$%&\'()*+./:;<=>?@[\\]^`{|}~"
        drive_serial = utils_misc.generate_random_string(id_leng,
                                                         ignore_str=",",
                                                         convert_str=convert_str)

        params["drive_serial"] = drive_serial
        params["start_vm"] = "yes"

        vm = params["main_vm"]
        vm_params = params.object_params(vm)
        env_process.preprocess_vm(test, vm_params, env, vm)
        vm = env.get_vm(vm)
    else:
        vm = env.get_vm(params["main_vm"])

    vm.verify_alive()
    timeout = int(params.get("login_timeout", 360))
    chk_timeout = int(params.get("chk_timeout", 240))

    error_context.context("Login to the guest", logging.info)
    session = vm.wait_for_login(timeout=timeout)

    qtree = qemu_qtree.QtreeContainer()
    try:
        qtree.parse_info_qtree(vm.monitor.info('qtree'))
    except AttributeError:  # monitor doesn't support info qtree
        qtree = None

    logging.info("Starting physical resources check test")
    logging.info("Values assigned to VM are the values we expect "
                 "to see reported by the Operating System")
    # Define a failure counter, as we want to check all physical
    # resources to know which checks passed and which ones failed
    n_fail = []

    # We will check HDs with the image name
    image_name = storage.get_image_filename(params, data_dir.get_data_dir())

    # Check cpu count
    error_context.context("CPU count check", logging.info)
    actual_cpu_nr = vm.get_cpu_count()
    cpu_cores_num = get_cpu_number("cores", chk_timeout)
    cpu_lp_num = get_cpu_number("logical_processors", chk_timeout)
    cpu_threads_num = get_cpu_number("threads", chk_timeout)
    cpu_sockets_num = get_cpu_number("sockets", chk_timeout)

    if ((params.get("os_type") == 'windows') and cpu_cores_num > 0 and
            cpu_lp_num > 0 and cpu_sockets_num > 0):
        actual_cpu_nr = cpu_lp_num * cpu_sockets_num
        cpu_threads_num = cpu_lp_num / cpu_cores_num

    if vm.cpuinfo.smp != actual_cpu_nr:
        fail_log = "CPU count mismatch:\n"
        fail_log += "    Assigned to VM: %s \n" % vm.cpuinfo.smp
        fail_log += "    Reported by OS: %s" % actual_cpu_nr
        n_fail.append(fail_log)
        logging.error(fail_log)

    n_fail.extend(check_cpu_number("cores", cpu_cores_num, vm.cpuinfo.cores))

    n_fail.extend(check_cpu_number("threads",
                                   cpu_threads_num, vm.cpuinfo.threads))

    n_fail.extend(check_cpu_number("sockets",
                                   cpu_sockets_num, vm.cpuinfo.sockets))

    # Check the cpu vendor_id
    expected_vendor_id = params.get("cpu_model_vendor")
    cpu_vendor_id_chk_cmd = params.get("cpu_vendor_id_chk_cmd")
    if expected_vendor_id and cpu_vendor_id_chk_cmd:
        output = session.cmd_output(cpu_vendor_id_chk_cmd)

        if expected_vendor_id not in output:
            fail_log = "CPU vendor id check failed.\n"
            fail_log += "    Assigned to VM: '%s'\n" % expected_vendor_id
            fail_log += "    Reported by OS: '%s'" % output
            n_fail.append(fail_log)
            logging.error(fail_log)

    # Check memory size
    error_context.context("Memory size check", logging.info)
    expected_mem = int(params["mem"])
    actual_mem = vm.get_memory_size()
    if actual_mem != expected_mem:
        fail_log = "Memory size mismatch:\n"
        fail_log += "    Assigned to VM: %s\n" % expected_mem
        fail_log += "    Reported by OS: %s\n" % actual_mem
        n_fail.append(fail_log)
        logging.error(fail_log)

    error_context.context("Hard drive count check", logging.info)
    f_fail = check_num("images", "block", image_name)
    n_fail.extend(f_fail)

    error_context.context("NIC count check", logging.info)
    f_fail = check_num("nics", "network", "model=")
    n_fail.extend(f_fail)

    error_context.context("NICs model check", logging.info)
    f_fail = chk_fmt_model("nics", "nic_model", "network", "model=(.*),")
    n_fail.extend(f_fail)

    if qtree is not None:
        error_context.context("Images params check", logging.info)
        logging.debug("Found devices: %s", params.objects('images'))
        qdisks = qemu_qtree.QtreeDisksContainer(qtree.get_nodes())
        disk_errors = sum(qdisks.parse_info_block(
                      vm.monitor.info_block()))
        disk_errors += qdisks.generate_params()
        disk_errors += qdisks.check_disk_params(params)
        if disk_errors:
            disk_errors = ("Images check failed with %s errors, "
                           "check the log for details" % disk_errors)
            logging.error(disk_errors)
            n_fail.append("\n".join(qdisks.errors))
    else:
        logging.info("Images check param skipped (qemu monitor doesn't "
                     "support 'info qtree')")

    error_context.context("Network card MAC check", logging.info)
    o = ""
    try:
        o = vm.monitor.human_monitor_cmd("info network")
    except qemu_monitor.MonitorError as e:
        fail_log = str(e) + "\n"
        fail_log += "info/query monitor command failed (network)"
        n_fail.append(fail_log)
        logging.error(fail_log)
    found_mac_addresses = re.findall(r"macaddr=(\S+)", o)
    logging.debug("Found MAC adresses: %s", found_mac_addresses)

    num_nics = len(params.objects("nics"))
    for nic_index in range(num_nics):
        mac = vm.get_mac_address(nic_index)
        if mac.lower() not in found_mac_addresses:
            fail_log = "MAC address mismatch:\n"
            fail_log += "    Assigned to VM (not found): %s" % mac
            n_fail.append(fail_log)
            logging.error(fail_log)

    error_context.context("UUID check", logging.info)
    if vm.get_uuid():
        f_fail = verify_device(vm.get_uuid(), "UUID",
                               params.get("catch_uuid_cmd"))
        n_fail.extend(f_fail)

    error_context.context("Hard Disk serial number check", logging.info)
    catch_serial_cmd = params.get("catch_serial_cmd")
    f_fail = verify_device(params.get("drive_serial"), "Serial",
                           catch_serial_cmd)
    n_fail.extend(f_fail)

    # only check if the MS Windows VirtIO driver is digital signed.
    chk_cmd = params.get("vio_driver_chk_cmd")
    if chk_cmd:
        error_context.context("Virtio Driver Check", logging.info)
        chk_output = session.cmd_output(chk_cmd, timeout=chk_timeout)
        if "FALSE" in chk_output:
            fail_log = "VirtIO driver is not digitally signed!"
            fail_log += "    VirtIO driver check output: '%s'" % chk_output
            n_fail.append(fail_log)
            logging.error(fail_log)

    error_context.context("Machine Type Check", logging.info)
    f_fail = verify_machine_type()
    n_fail.extend(f_fail)

    if n_fail:
        session.close()
        test.fail("Physical resources check test "
                  "reported %s failures:\n%s" %
                  (len(n_fail), "\n".join(n_fail)))

    session.close()
Example #45
0
def run(test, params, env):
    """
    Qemu host nic bonding test:
    1) Load bonding module with mode 802.3ad
    2) Bring up bond interface
    3) Add nics to bond interface
    4) Add a new bridge and add bond interface to it
    5) Get ip address for bridge
    6) Boot up guest with the bridge
    7) Checking guest netowrk via ping
    8) Start file transfer between guest and host
    9) Disable and enable physical interfaces during file transfer

    :param test: QEMU test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment.
    """
    bond_iface = params.get("bond_iface", "bond0")
    bond_br_name = params.get("bond_br_name", "br_bond0")
    timeout = int(params.get("login_timeout", 240))
    remote_host = params.get("dsthost")
    ping_timeout = int(params.get("ping_timeout", 240))
    bonding_timeout = int(params.get("bonding_timeout", 1))
    bonding_mode = params.get("bonding_mode", "1")
    bonding_miimon = params.get("bonding_miimon", "100")
    bonding_max_bonds = params.get("bonding_max_bonds", "1")
    params['netdst'] = bond_br_name
    host_bridges = utils_net.Bridge()

    error_context.context("Load bonding module with mode 802.3ad",
                          logging.info)
    if not process.system("lsmod|grep bonding", ignore_status=True,
                          shell=True):
        process.system("modprobe -r bonding")

    process.system("modprobe bonding mode=%s miimon=%s max_bonds=%s" %
                   (bonding_mode, bonding_miimon, bonding_max_bonds))

    error_context.context("Bring up %s" % bond_iface, logging.info)
    host_ifaces = utils_net.get_host_iface()

    if bond_iface not in host_ifaces:
        test.error("Can not find %s in host" % bond_iface)

    bond_iface = utils_net.Interface(bond_iface)
    bond_iface.up()
    bond_mac = bond_iface.get_mac()

    host_ph_iface_pre = params.get("host_ph_iface_prefix", "en")
    host_iface_bonding = int(params.get("host_iface_bonding", 2))

    ph_ifaces = [_ for _ in host_ifaces if re.match(host_ph_iface_pre, _)]
    host_ph_ifaces = [_ for _ in ph_ifaces if utils_net.Interface(_).is_up()]

    ifaces_in_use = host_bridges.list_iface()
    host_ph_ifaces_un = list(set(host_ph_ifaces) - set(ifaces_in_use))

    if (len(host_ph_ifaces_un) < 2
            or len(host_ph_ifaces_un) < host_iface_bonding):
        test.cancel("Host need %s nics at least." % host_iface_bonding)

    error_context.context("Add nics to %s" % bond_iface.name, logging.info)
    host_ifaces_bonding = host_ph_ifaces_un[:host_iface_bonding]
    ifenslave_cmd = "ifenslave %s" % bond_iface.name
    op_ifaces = []
    for host_iface_bonding in host_ifaces_bonding:
        op_ifaces.append(utils_net.Interface(host_iface_bonding))
        ifenslave_cmd += " %s" % host_iface_bonding
    process.system(ifenslave_cmd)

    error_context.context(
        "Add a new bridge and add %s to it." % bond_iface.name, logging.info)
    if bond_br_name not in host_bridges.list_br():
        host_bridges.add_bridge(bond_br_name)
    host_bridges.add_port(bond_br_name, bond_iface.name)

    error_context.context("Get ip address for bridge", logging.info)
    process.system("dhclient -r; dhclient %s" % bond_br_name, shell=True)

    error_context.context("Boot up guest with bridge %s" % bond_br_name,
                          logging.info)
    params["start_vm"] = "yes"
    vm_name = params.get("main_vm")
    env_process.preprocess_vm(test, params, env, vm_name)
    vm = env.get_vm(vm_name)
    session = vm.wait_for_login(timeout=timeout)

    error_context.context("Checking guest netowrk via ping.", logging.info)
    ping_cmd = params.get("ping_cmd")
    ping_cmd = re.sub("REMOTE_HOST", remote_host, ping_cmd)
    session.cmd(ping_cmd, timeout=ping_timeout)

    error_context.context("Start file transfer", logging.info)
    f_transfer = utils_misc.InterruptedThread(
        utils_test.run_virt_sub_test,
        args=(
            test,
            params,
            env,
        ),
        kwargs={"sub_type": "file_transfer"})
    f_transfer.start()
    utils_misc.wait_for(
        lambda: process.system_output("pidof scp", ignore_status=True), 30)

    error_context.context(
        "Disable and enable physical "
        "interfaces in %s" % bond_br_name, logging.info)
    while True:
        for op_iface in op_ifaces:
            logging.debug("Turn down %s", op_iface.name)
            op_iface.down()
            time.sleep(bonding_timeout)
            logging.debug("Bring up %s", op_iface.name)
            op_iface.up()
            time.sleep(bonding_timeout)
        if not f_transfer.is_alive():
            break
    f_transfer.join()
Example #46
0
def run(test, params, env):
    """
    Re-assign nr-hugepages

    1. Set up hugepage with 1G page size and hugepages=8
    2. Boot up guest using /mnt/kvm_hugepage as backend in QEMU CML
    3. Change the nr_hugepages after the guest boot up (6 and 10)
    4. Run the stress test **inside guest**
    5. change the nr_hugepages after the stress test (6 and 10)

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

    def set_hugepage():
        """Set nr_hugepages"""
        for h_size in (origin_nr - 2, origin_nr + 2):
            hp_config.target_hugepages = h_size
            hp_config.set_hugepages()
            if params.get('on_numa_node'):
                logging.info('Set hugepage size %s to target node %s' % (
                    h_size, target_node))
                hp_config.set_node_num_huge_pages(h_size, target_node,
                                                  hugepage_size)

    origin_nr = int(params['origin_nr'])
    host_numa_node = utils_misc.NumaInfo()
    mem = int(float(utils_misc.normalize_data_size("%sM" % params["mem"])))
    if params.get('on_numa_node'):
        for target_node in host_numa_node.get_online_nodes_withmem():
            node_mem_free = host_numa_node.read_from_node_meminfo(
                target_node, 'MemFree')
            if int(node_mem_free) > mem:
                params['target_nodes'] = target_node
                params["qemu_command_prefix"] = ("numactl --membind=%s" %
                                                 target_node)
                params['target_num_node%s' % target_node] = origin_nr
                break
            logging.info(
                'The free memory of node %s is %s, is not enough for'
                ' guest memory: %s' % (target_node, node_mem_free, mem))
        else:
            test.cancel("No node on your host has sufficient free memory for "
                        "this test.")
    hp_config = test_setup.HugePageConfig(params)
    hp_config.target_hugepages = origin_nr
    logging.info('Setup hugepage number to %s' % origin_nr)
    hp_config.setup()
    hugepage_size = utils_memory.get_huge_page_size()

    params['start_vm'] = "yes"
    vm_name = params['main_vm']
    env_process.preprocess_vm(test, params, env, vm_name)
    vm = env.get_vm(vm_name)
    params['stress_args'] = '--vm %s --vm-bytes 256M --timeout 30s' % (
            mem // 512)
    logging.info('Loading stress on guest.')
    stress = utils_test.VMStress(vm, 'stress', params)
    stress.load_stress_tool()
    time.sleep(30)
    stress.unload_stress()
    set_hugepage()
    hp_config.cleanup()
    vm.verify_kernel_crash()
Example #47
0
def run_time_manage(test, params, env):
    """
    Time manage test:

    1) Generate stress in host.
    2) Run atleast 15 vms with "driftfix=slew" option
    3) Reboot the guest.
    4) Repeat the step 3 for all guests and check whether the guest
       responds properly(not any watchdog reported).
    5) TODO: Improve the way of checking the response and
        run some stress inside guest too.
    6) Continue the step 4 for 10 iterations and
       record the guest/host realtime, calculate drift in time for
       each iterations.
    7) Print the drift values for all sessions
    8) TODO: Validate if the drift value has to be within defined value

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

    # Collect test parameters
    login_timeout = float(params.get("login_timeout", 240))
    host_load_command = params.get("host_load_command")
    host_load_kill_command = params.get("host_load_kill_command")
    time_command = params.get("time_command")
    time_filter_re = params.get("time_filter_re")
    time_format = params.get("time_format")

    # Intialize the variables
    itr = 0
    num = 2
    host_load_sessions = []
    sessions = [session]
    prev_time = []
    curr_time = []
    timedrift = []
    totaldrift = []
    vmnames = ["vm1"]

    # Run some load on the host
    logging.info("Starting load on host.")
    host_load_sessions.append(
        aexpect.run_bg(host_load_command,
                       output_func=logging.debug,
                       output_prefix="host load ",
                       timeout=0.5))
    # Boot the VMs
    try:
        while num <= int(params.get("max_vms")):
            # Clone vm according to the first one
            vm_name = "vm%d" % num
            vmnames.append(vm_name)
            vm_params = vm.params.copy()
            curr_vm = vm.clone(vm_name, vm_params)
            env.register_vm(vm_name, curr_vm)
            env_process.preprocess_vm(test, vm_params, env, vm_name)
            params["vms"] += " " + vm_name

            sessions.append(curr_vm.wait_for_login(timeout=login_timeout))
            logging.info("Guest #%d booted up successfully", num)

            # Check whether all previous shell sessions are responsive
            error.context("checking responsiveness of the booted guest")
            for se in sessions:
                se.cmd(params.get("alive_test_cmd"))
            num += 1

        while itr <= int(params.get("max_itrs")):
            for vmid, se in enumerate(sessions):
                # Get the respective vm object
                vmname = "vm%d" % (vmid + 1)
                vm = env.get_vm(vmname)
                # Run current iteration
                logging.info("Rebooting:vm%d iteration %d " %
                             ((vmid + 1), itr))
                se = vm.reboot(se, timeout=timeout)
                # Remember the current changed session
                sessions[vmid] = se
                error.context("checking responsiveness of guest")
                se.cmd(params.get("alive_test_cmd"))
                if itr == 0:
                    (ht0, gt0) = utils_test.get_time(se, time_command,
                                                     time_filter_re,
                                                     time_format)
                    prev_time.append((ht0, gt0))
                else:
                    (ht1, gt1) = utils_test.get_time(se, time_command,
                                                     time_filter_re,
                                                     time_format)
                    curr_time.append((ht1, gt1))
            if itr != 0:
                for i in range(int(params.get("max_vms"))):
                    hdelta = curr_time[i][0] - prev_time[i][0]
                    gdelta = curr_time[i][1] - prev_time[i][1]
                    drift = format(100.0 * (hdelta - gdelta) / hdelta, ".2f")
                    timedrift.append(drift)
                totaldrift.append(timedrift)
                prev_time = curr_time
                timedrift = []
                curr_time = []
            # Wait for some time before next iteration
            time.sleep(30)
            itr += 1

        logging.info("The time drift values for all VM sessions/iterations")
        logging.info("VM-Name:%s" % vmnames)
        for idx, value in enumerate(totaldrift):
            logging.info("itr-%2d:%s" % (idx + 1, value))

    finally:
        for se in sessions:
            # Closing all the sessions.
            se.close()
        logging.info("killing load on host.")
        host_load_sessions.append(
            aexpect.run_bg(host_load_kill_command,
                           output_func=logging.debug,
                           output_prefix="host load kill",
                           timeout=0.5))
Example #48
0
        def test(self):
            self.iso_image_orig = create_iso_image(params, "orig")
            self.iso_image_new = create_iso_image(params, "new")
            self.cdrom_dir = os.path.dirname(self.iso_image_new)
            if params.get("not_insert_at_start") == "yes":
                target_cdrom = params["target_cdrom"]
                params[target_cdrom] = ""
            params["start_vm"] = "yes"
            env_process.preprocess_vm(test, params, env, params["main_vm"])
            vm = env.get_vm(params["main_vm"])

            self.session = vm.wait_for_login(timeout=login_timeout)
            pre_cmd = params.get("pre_cmd")
            if pre_cmd:
                self.session.cmd(pre_cmd, timeout=120)
                self.session = vm.reboot()
            iso_image = self.iso_image_orig
            error.context("Query cdrom devices in guest")
            cdrom_dev_list = list_guest_cdroms(self.session)
            logging.debug("cdrom_dev_list: '%s'", cdrom_dev_list)

            if params.get('not_insert_at_start') == "yes":
                error.context("Locked without media present", logging.info)
                # XXX: The device got from monitor might not match with the guest
                # defice if there are multiple cdrom devices.
                qemu_cdrom_device = get_empty_cdrom_device(vm)
                guest_cdrom_device = cdrom_dev_list[-1]
                if vm.check_block_locked(qemu_cdrom_device):
                    raise error.TestFail("Device should not be locked just"
                                         " after booting up")
                cmd = params["lock_cdrom_cmd"] % guest_cdrom_device
                self.session.cmd(cmd)
                if not vm.check_block_locked(qemu_cdrom_device):
                    raise error.TestFail("Device is not locked as expect.")
                return

            error.context("Detecting the existence of a cdrom (guest OS side)",
                          logging.info)
            cdrom_dev_list = list_guest_cdroms(self.session)
            try:
                guest_cdrom_device = cdrom_dev_list[-1]
            except IndexError:
                raise error.TestFail("Could not find a valid cdrom device")

            error.context("Detecting the existence of a cdrom (qemu side)",
                          logging.info)
            qemu_cdrom_device = get_device(vm, iso_image)

            self.session.get_command_output("umount %s" % guest_cdrom_device)
            if params.get('cdrom_test_autounlock') == 'yes':
                error.context("Trying to unlock the cdrom", logging.info)
                _f = lambda: not vm.check_block_locked(qemu_cdrom_device)
                if not utils_misc.wait_for(_f, 300):
                    raise error.TestFail("Device %s could not be"
                                         " unlocked" % (qemu_cdrom_device))
                del _f

            max_test_times = int(params.get("cdrom_max_test_times", 100))
            if params.get("cdrom_test_eject") == "yes":
                eject_test_via_monitor(vm, qemu_cdrom_device,
                                       guest_cdrom_device, self.iso_image_orig,
                                       self.iso_image_new, max_test_times)

            if params.get('cdrom_test_tray_status') == 'yes':
                check_tray_status_test(vm, qemu_cdrom_device,
                                       guest_cdrom_device, max_test_times)

            if params.get('cdrom_test_locked') == 'yes':
                check_tray_locked_test(vm, qemu_cdrom_device,
                                       guest_cdrom_device)

            error.context("Check whether the cdrom is read-only", logging.info)
            cmd = params["readonly_test_cmd"] % guest_cdrom_device
            try:
                self.session.cmd(cmd)
                raise error.TestFail("Attempt to format cdrom %s succeeded" %
                                     (guest_cdrom_device))
            except aexpect.ShellError:
                pass

            sub_test = params.get("sub_test")
            if sub_test:
                error.context(
                    "Run sub test '%s' before doing file"
                    " operation" % sub_test, logging.info)
                utils_test.run_virt_sub_test(test, params, env, sub_test)

            if params.get("cdrom_test_file_operation") == "yes":
                file_operation_test(self.session, guest_cdrom_device,
                                    max_test_times)

            error.context("Cleanup")
            # Return the self.iso_image_orig
            cdfile = get_cdrom_file(vm, qemu_cdrom_device)
            if cdfile != self.iso_image_orig:
                time.sleep(workaround_eject_time)
                self.session.cmd(params["eject_cdrom_cmd"] %
                                 guest_cdrom_device)
                vm.eject_cdrom(qemu_cdrom_device)
                if get_cdrom_file(vm, qemu_cdrom_device) is not None:
                    raise error.TestFail("Device %s was not ejected"
                                         " in clearup stage" %
                                         qemu_cdrom_device)

                vm.change_media(qemu_cdrom_device, self.iso_image_orig)
                if get_cdrom_file(vm,
                                  qemu_cdrom_device) != self.iso_image_orig:
                    raise error.TestFail("It wasn't possible to change"
                                         " cdrom %s" % iso_image)
            post_cmd = params.get("post_cmd")
            if post_cmd:
                self.session.cmd(post_cmd)
            if params.get("guest_suspend_type"):
                self.session = vm.reboot()
            if "clocksource=" in kernel_cfg:
                kernel_cfg_new = re.sub("clocksource=[a-z \-_]+",
                                        "clocksource=%s " % clksrc, kernel_cfg)
            else:
                kernel_cfg_new = "%s %s" % (kernel_cfg,
                                            "clocksource=%s" % clksrc)
            disk_obj.replace_image_file_content(grub_file, kernel_cfg,
                                                kernel_cfg_new)

            error.context("Boot the guest", logging.info)
            if clksrc != "kvm-clock":
                cpu_model_flags = params.get("cpu_model_flags")
                if "-kvmclock" not in cpu_model_flags:
                    params["cpu_model_flags"] = cpu_model_flags + ",-kvmclock"
            vm_name = params["main_vm"]
            env_process.preprocess_vm(test, params, env, vm_name)
            vm = env.get_vm(vm_name)
            vm.verify_alive()
            session = vm.wait_for_login(timeout=timeout)

            error.context("Check the current clocksource in guest",
                          logging.info)
            verify_guest_clock_source(session, clksrc)
    finally:
        try:
            error.context("Shutdown guest")
            vm.destroy()
            error.context("Restore guest kernel cli", logging.info)
            image_filename = storage.get_image_filename(
                params, data_dir.get_data_dir())
            grub_file = params.get("grub_file", "/boot/grub2/grub.cfg")
Example #50
0
def run(test, params, env):
    """
    Expose host MTU to guest test

    1) Boot up guest with param 'host_mtu=4000' in nic part
    2) Disable NetworkManager in guest
    3) set mtu of guest tap (eg: tap0) and physical nic (eg: eno1) to
       4000 in host
    4) check the mtu in guest
    5) ping from guest to external host with packet size 3972

    :param test: kvm test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment
    """
    def cleanup_ovs_ports(netdst, ports):
        """
        Clean up created ovs ports in this case

        :param netdst: netdst get from command line
        :param ports: existing ports need to be remain before this test
        """

        host_bridge = utils_net.find_bridge_manager(netdst)
        if utils_net.ovs_br_exists(netdst) is True:
            ports = set(host_bridge.list_ports(netdst)) - set(ports)
            for p in ports:
                utils_net.find_bridge_manager(netdst).del_port(netdst, p)

    netdst = params.get("netdst", "switch")
    mtu_value = params.get_numeric("mtu_value")
    host_bridge = utils_net.find_bridge_manager(netdst)
    localhost = LocalHost()
    try:
        if netdst in utils_net.Bridge().list_br():
            host_hw_interface = utils_net.Bridge().list_iface(netdst)[0]
        else:
            host_hw_interface = host_bridge.list_ports(netdst)
            tmp_ports = re.findall(r"t[0-9]{1,}-[a-zA-Z0-9]{6}",
                                   ' '.join(host_hw_interface))
            if tmp_ports:
                for p in tmp_ports:
                    host_bridge.del_port(netdst, p)
                host_hw_interface = host_bridge.list_ports(netdst)
    except IndexError:
        host_hw_interface = netdst

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

    vm = env.get_vm(params["main_vm"])
    vm.verify_alive()

    vm_iface = vm.get_ifname()
    # Get host interface original mtu value before setting
    if netdst in utils_net.Bridge().list_br():
        host_hw_iface = NetworkInterface(host_hw_interface, localhost)
    elif utils_net.ovs_br_exists(netdst) is True:
        host_hw_iface = NetworkInterface(' '.join(host_hw_interface),
                                         localhost)
    host_mtu_origin = host_hw_iface.get_mtu()

    set_mtu_host(vm_iface, mtu_value)
    host_hw_iface.set_mtu(mtu_value)

    os_type = params.get("os_type", "linux")
    login_timeout = float(params.get("login_timeout", 360))
    session = vm.wait_for_login(timeout=login_timeout)

    host_ip = utils_net.get_ip_address_by_interface(params["netdst"])
    if os_type == "linux":
        session.cmd_output_safe(params["nm_stop_cmd"])
        guest_ifname = utils_net.get_linux_ifname(session,
                                                  vm.get_mac_address())
        output = session.cmd_output_safe(params["check_linux_mtu_cmd"] %
                                         guest_ifname)
        error_context.context(output, logging.info)
        match_string = "mtu %s" % params["mtu_value"]
        if match_string not in output:
            test.fail("host mtu %s not exposed to guest" % params["mtu_value"])
    elif os_type == "windows":
        connection_id = utils_net.get_windows_nic_attribute(
            session, "macaddress", vm.get_mac_address(), "netconnectionid")
        output = session.cmd_output_safe(params["check_win_mtu_cmd"] %
                                         connection_id)
        error_context.context(output, logging.info)
        lines = output.strip().splitlines()
        lines_len = len(lines)

        line_table = lines[0].split('  ')
        line_value = lines[2].split('  ')
        while '' in line_table:
            line_table.remove('')
        while '' in line_value:
            line_value.remove('')
        index = 0
        for name in line_table:
            if re.findall("MTU", name):
                break
            index += 1
        guest_mtu_value = line_value[index]
        logging.info("MTU is %s", guest_mtu_value)
        if not int(guest_mtu_value) == mtu_value:
            test.fail("Host mtu %s is not exposed to "
                      "guest!" % params["mtu_value"])

    logging.info("Ping from guest to host with packet size 3972")
    status, output = utils_test.ping(host_ip,
                                     10,
                                     packetsize=3972,
                                     timeout=30,
                                     session=session)
    ratio = utils_test.get_loss_ratio(output)
    if ratio != 0:
        test.fail("Loss ratio is %s", ratio)

    # Restore host mtu after finish testing
    set_mtu_host(vm_iface, host_mtu_origin)
    host_hw_iface.set_mtu(host_mtu_origin)

    if netdst not in utils_net.Bridge().list_br():
        cleanup_ovs_ports(netdst, host_hw_interface)
    session.close()
Example #51
0
def run(test, params, env):
    """
    Device bit check test:
    We can set up some properties bits though qemu-kvm command line. This case
    will check if those properties bits set up correctly by monitor command
    'qtree' or inside guest by sysfs(only linux).
    1) Boot up a guest with specific parameter
    2) Verify the relevant bit of the device set correctly in the monitor
       or inside guest(if it is possible)

    :param test: qemu test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment.
    """
    default_value = params.get("default_value", "1 1").split()
    option_add = params.get("option_add", "off off").split()
    options = params.get("options", "indirect_desc event_idx").split()
    options_offset = params.get("options_offset", "28 29").split()
    test_loop = params.get("test_loop", "default").split(";")
    timeout = float(params.get("login_timeout", 240))
    dev_type = params.get("dev_type", "virtio-blk-pci")
    dev_param_name = params.get("dev_param_name", "blk_extra_params")
    dev_pattern = params.get("dev_pattern", "(dev: %s.*?)dev:" % dev_type)
    pci_id_pattern = params.get("pci_id_pattern")
    convert_dict = {"1": ["on", "true"], "0": ["off", "false"]}
    orig_extra_params = params.get(dev_param_name, "")
    for properties in test_loop:
        if properties != "default":
            properties = properties.strip().split()
            extra_params = orig_extra_params
            for index, value in enumerate(properties):
                if value != default_value[index]:
                    extra_params += ",%s=%s" % (options[index],
                                                option_add[index])
            params[dev_param_name] = extra_params.lstrip(",")
        else:
            properties = default_value

        error.context(
            "Boot up guest with properites: %s value as: %s" %
            (str(options), properties), logging.info)
        vm_name = params["main_vm"]
        params["start_vm"] = 'yes'
        env_process.preprocess_vm(test, params, env, vm_name)

        vm = env.get_vm(vm_name)

        session = vm.wait_for_login(timeout=timeout)
        qtree_info = vm.monitor.info("qtree")
        dev_info = re.findall(dev_pattern, qtree_info, re.S)
        if not dev_info:
            raise error.TestError("Can't get device info from qtree result.")

        for index, option in enumerate(options):
            option_regex = "%s\s+=\s+(\w+)" % option
            option_value = re.findall(option_regex, dev_info[0], re.M)
            if not option_value:
                logging.debug("dev info in qtree: %s" % dev_info[0])
                raise error.TestError("Can't get the property info from qtree"
                                      " result")
            if option_value[0] not in convert_dict[properties[index]]:
                msg = "'%s' value get '%s', " % (option, option_value)
                msg += "expect value '%s'" % convert_dict[properties[index]]
                logging.debug(msg)
                raise error.TestFail("Properity bit for %s is wrong." % option)

            logging.info("Properity bit in qtree is right for %s." % option)
            if params.get("check_in_guest", "yes") == "yes":
                pci_info = session.cmd_output("lspci")
                pci_n = re.findall(pci_id_pattern, pci_info)
                if not pci_n:
                    raise error.TestError("Can't get the pci id for device")
                cmd = "cat /sys/bus/pci/devices/0000:%s/" % pci_n[0]
                cmd += "virtio*/features"
                bitstr = session.cmd_output(cmd)
                bitstr = re.findall("[01]+", bitstr)[-1]

                if bitstr[int(options_offset[index])] != properties[index]:
                    msg = "bit string in guest: %s" % bitstr
                    msg += "expect bit string: %s" % properties[index]
                    logging.debug(msg)
                    raise error.TestFail("Properity bit for %s is wrong"
                                         " inside guest." % option)
            logging.info("Properity bit in qtree is right for %s"
                         " in guest." % option)
        session.close()
        vm.destroy()
Example #52
0
def run_kernbench(test, params, env):
    """
    Run kernbench for performance testing.

    1) Set up testing environment.
    2) Get a kernel code.
    3) Make the kernel with kernbench -M or time make -j 2*smp

    @param test: QEMU test object
    @param params: Dictionary with the test parameters
    @param env: Dictionary with test environment.
    """
    def download_if_not_exists():
        if not os.path.exists(file_name):
            cmd = "wget -t 10 -c -P %s %s" % (tmp_dir, file_link)
            utils.system(cmd)


    def cmd_status_output(cmd, timeout=360):
        s = 0
        o = ""
        if "guest" in test_type:
            (s, o) = session.cmd_status_output(cmd, timeout=timeout)
        else:
            (s, o) = commands.getstatusoutput(cmd)
        return (s, o)


    def check_ept():
        output = utils.system_output("grep 'flags' /proc/cpuinfo")
        flags = output.splitlines()[0].split(':')[1].split()
        need_ept = params.get("need_ept", "no")
        if 'ept' not in flags and "yes" in need_ept:
            raise error.TestNAError("This test requires a host that supports EPT")
        elif 'ept' in flags and "no" in need_ept:
            cmd = "modprobe -r kvm_intel && modprobe kvm_intel ept=0"
            utils.system(cmd, timeout=100)
        elif 'ept' in flags and "yes" in need_ept:
            cmd = "modprobe -r kvm_intel && modprobe kvm_intel ept=1"
            utils.system(cmd, timeout=100)


    def install_gcc():
        logging.info("Update gcc to request version....")
        cmd = "rpm -q gcc"
        cpp_link = params.get("cpp_link")
        gcc_link = params.get("gcc_link")
        libgomp_link = params.get("libgomp_link")
        libgcc_link = params.get("libgcc_link")
        (s, o) = cmd_status_output(cmd)
        if s:
            cmd = "rpm -ivh %s --nodeps; rpm -ivh %s --nodeps; rpm -ivh %s"\
                  " --nodeps; rpm -ivh %s --nodeps" % (libgomp_link,
                                          libgcc_link, cpp_link, gcc_link)
        else:
            gcc = o.splitlines()[0].strip()
            if gcc in gcc_link:
                cmd = "rpm -e %s && rpm -ivh %s" % (gcc, gcc_link)
            else:
                cmd = "rpm -ivh %s --nodeps; rpm -ivh %s --nodeps; rpm -ivh"\
                      " %s --nodeps; rpm -ivh %s --nodeps" % (libgomp_link,
                                             libgcc_link, cpp_link, gcc_link)
        (s, o) = cmd_status_output(cmd)
        if s:
            logging.debug("Fail to install gcc.output:%s" % o)


    def record_result(result):
        re_result = params.get("re_result")
        (m_value, s_value) = re.findall(re_result, result)[0]
        s_value = float(m_value) * 60 + float(s_value)
        shortname = params.get("shortname")
        result_str = "%s: %ss\n" % (shortname, s_value)
        result_file = params.get("result_file")
        f1 = open(result_file, "a+")
        result = f1.read()
        result += result_str
        f1.write(result_str)
        f1.close()
        open(os.path.basename(result_file),'w').write(result)
        logging.info("Test result got from %s:\n%s" % (result_file, result))


    test_type = params.get("test_type")
    guest_thp_cmd = params.get("guest_thp_cmd")
    cmd_timeout = int(params.get("cmd_timeout", 1200))
    tmp_dir = params.get("tmp_dir", "/tmp/kernbench/")
    check_ept()
    vm_name = params.get("main_vm", "vm1")
    cpu_multiplier = int(params.get("cpu_multiplier", 2))
    session = None
    if "guest" in test_type:
        env_process.preprocess_vm(test, params, env, vm_name)
        vm = env.get_vm(params["main_vm"])
        vm.verify_alive()
        session = vm.wait_for_login(timeout=int(params.get("login_timeout", 360)))

    # Create tmp folder and download files if need.
    if not os.path.exists(tmp_dir):
        utils.system("mkdir %s" % tmp_dir)
    files = params.get("files_need").split()
    for file in files:
        file_link = params.get("%s_link" % file)
        file_name = os.path.join(tmp_dir, os.path.basename(file_link))
        download_if_not_exists()

    if "guest" in test_type:
        logging.info("test in guest")
        vm.copy_files_to(tmp_dir, os.path.dirname(tmp_dir))
        if guest_thp_cmd is not None:
            session.cmd_output(guest_thp_cmd)
    try:
        if params.get("update_gcc") and params.get("update_gcc") == "yes":
            install_gcc()
        pre_cmd = params.get("pre_cmd")
        (s, o) = cmd_status_output(pre_cmd, timeout=cmd_timeout)
        if s:
            raise error.TestError("Fail command:%s\nOutput: %s" % (pre_cmd, o))

        if "guest" in test_type:
            cpu_num = params.get("smp")
        else:
            cpu_num = utils.count_cpus()
        test_cmd = params.get("test_cmd") % (int(cpu_num)*cpu_multiplier)
        logging.info("Start making the kernel ....")
        (s, o) = cmd_status_output(test_cmd, timeout=cmd_timeout)
        if s:
            raise error.TestError("Fail command:%s\n Output:%s" % (test_cmd, o))
        else:
            logging.info("Output for command %s is:\n %s" % (test_cmd, o))
            record_result(o)
    finally:
        if params.get("post_cmd"):
            cmd_status_output(params.get("post_cmd"), timeout=cmd_timeout)
        if session:
            session.close()
Example #53
0
    def gagent_check_pkg_update(self, test, params, env):
        """
        Update qemu-ga-win pkg.

        steps:
            1)boot up guest.
            2)install the previous qemu-ga in guest.
                a.for virtio-win method,change to download iso.
                b.for url method,need to find the previous version and get
                the download cmd.
            3)update to the latest one.
            4)qemu-ga basic test.

        :param test: kvm test object
        :param params: Dictionary with the test parameters
        :param env: Dictionary with test environment.
        """
        def _change_agent_media(cdrom_virtio):
            """
            Only for virtio-win method,change virtio-win iso.

            :param cdrom_virtio: iso file
            """
            logging.info("Change cdrom to %s", cdrom_virtio)
            virtio_iso = utils_misc.get_path(data_dir.get_data_dir(),
                                             cdrom_virtio)
            vm.change_media("drive_virtio", virtio_iso)

            logging.info("Wait until device is ready")
            vol_virtio_key = "VolumeName like '%virtio-win%'"
            timeout = 10
            end_time = time.time() + timeout
            while time.time() < end_time:
                time.sleep(2)
                virtio_win_letter = utils_misc.get_win_disk_vol(
                    session, vol_virtio_key)
                if virtio_win_letter:
                    break
            if not virtio_win_letter:
                test.fail("Couldn't get virtio-win volume letter.")

        def _get_pkg_download_cmd():
            """
            Only for url method to get the previous qemu-ga version and get the
            download cmd for this pkg.
            """
            qga_html = "/tmp/qemu-ga.html"
            qga_url = params["qga_url"]
            qga_html_download_cmd = "wget %s -O %s" % (qga_url, qga_html)
            process.system(qga_html_download_cmd,
                           float(params.get("login_timeout", 360)))

            with open(qga_html, "r") as f:
                lines = f.readlines()
            list_qga = []
            for line in lines:
                if "qemu-ga-win" in line:
                    list_qga.append(line)
            tgt_line = list_qga[-2]
            # qemu-ga-win-7.5.0-2.el7ev
            qga_pattern = re.compile(r"%s" % params["qga_pattern"])
            qga_pre_pkg = qga_pattern.findall(tgt_line)[0]
            logging.info("The previous qemu-ga version is %s.", qga_pre_pkg)

            # https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/
            #   archive-qemu-ga/qemu-ga-win-7.5.0-2.el7ev/
            qga_url_pre = r"%s/%s/%s" % (qga_url, qga_pre_pkg,
                                         self.qemu_ga_pkg)
            qga_host_path = params["gagent_host_path"]
            params["gagent_download_cmd"] = "wget %s -O %s" % (qga_url_pre,
                                                               qga_host_path)

        def _qga_install():
            """
            Install qemu-ga pkg.
            """
            qga_pkg_path = self.get_qga_pkg_path(self.qemu_ga_pkg, test,
                                                 session, params, vm)
            self.gagent_install_cmd = params.get(
                "gagent_install_cmd") % qga_pkg_path
            self.gagent_install(session, vm)

        error_context.context("Boot up vm.", logging.info)
        params["start_vm"] = "yes"
        latest_qga_download_cmd = params["gagent_download_cmd"]
        env_process.preprocess_vm(test, params, env, params["main_vm"])
        vm = self.env.get_vm(params["main_vm"])
        session = self._get_session(params, vm)

        if params.get("driver_uninstall", "no") == "yes":
            error_context.context("Uninstall vioser driver in guest.",
                                  logging.info)
            device_name = params["device_name"]
            driver_name = params["driver_name"]
            inf_names_get_cmd = wmic.make_query("path win32_pnpsigneddriver",
                                                "DeviceName like '%s'" %
                                                device_name,
                                                props=["InfName"],
                                                get_swch=wmic.FMT_TYPE_LIST)
            inf_names = wmic.parse_list(
                session.cmd(inf_names_get_cmd, timeout=360))
            for inf_name in inf_names:
                uninst_store_cmd = "pnputil /f /d %s" % inf_name
                s, o = session.cmd_status_output(uninst_store_cmd, 360)
                if s:
                    test.error("Failed to uninstall driver '%s' from store, "
                               "details:\n%s" % (driver_name, o))

        error_context.context("Install the previous qemu-ga in guest.",
                              logging.info)
        if self._check_ga_pkg(session, params["gagent_pkg_check_cmd"]):
            logging.info("Uninstall the one which is installed.")
            self.gagent_uninstall(session, vm)

        if self.gagent_src_type == "virtio-win":
            _change_agent_media(params["cdrom_virtio_downgrade"])
        elif self.gagent_src_type == "url":
            _get_pkg_download_cmd()
        else:
            self.test.error("Only support 'url' and 'virtio-win' method.")

        _qga_install()

        error_context.context("Update qemu-ga to the latest one.",
                              logging.info)
        if self.gagent_src_type == "virtio-win":
            _change_agent_media(params["cdrom_virtio"])
        else:
            params["gagent_download_cmd"] = latest_qga_download_cmd

        _qga_install()

        error_context.context("Basic test for qemu-ga.", logging.info)
        args = [params.get("gagent_serial_type"), params.get("gagent_name")]
        self.gagent_create(params, vm, *args)
        self.gagent_verify(params, vm)
Example #54
0
def run(test, params, env):
    """
    Qemu numa consistency test:
    1) Get host numa topological structure
    2) Start a guest with the same node as the host, each node has one cpu
    3) Get the vcpu thread used cpu id in host and the cpu belongs which node
    4) Allocate memory inside guest and bind the allocate process to one of
       its vcpu.
    5) The memory used in host should increase in the same node if the vcpu
       thread is not switch to other node.
    6) Repeat step 3~5 for each vcpu thread of the guest.

    :param test: QEMU test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment.
    """
    def get_vcpu_used_node(numa_node_info, vcpu_thread):
        cpu_used_host = utils_misc.get_thread_cpu(vcpu_thread)[0]
        node_used_host = ([
            _ for _ in node_list
            if cpu_used_host in numa_node_info.nodes[_].cpus
        ][0])
        return node_used_host

    error.context("Get host numa topological structure", logging.info)
    timeout = float(params.get("login_timeout", 240))
    host_numa_node = utils_misc.NumaInfo()
    node_list = host_numa_node.online_nodes
    if len(node_list) < 2:
        raise error.TestNAError("This host only has one NUMA node, "
                                "skipping test...")
    node_list.sort()
    params['smp'] = len(node_list)
    params['vcpu_cores'] = 1
    params['vcpu_threads'] = 1
    params['vcpu_sockets'] = params['smp']
    params['guest_numa_nodes'] = ""
    for node_id in range(len(node_list)):
        params['guest_numa_nodes'] += " node%d" % node_id
    params['start_vm'] = 'yes'

    utils_memory.drop_caches()
    vm = params['main_vm']
    env_process.preprocess_vm(test, params, env, vm)
    vm = env.get_vm(vm)
    vm.verify_alive()
    vcpu_threads = vm.vcpu_threads
    session = vm.wait_for_login(timeout=timeout)

    dd_size = 256
    if dd_size * len(vcpu_threads) > int(params['mem']):
        dd_size = int(int(params['mem']) / 2 / len(vcpu_threads))

    mount_size = dd_size * len(vcpu_threads)

    mount_cmd = "mount -o size=%dM -t tmpfs none /tmp" % mount_size

    qemu_pid = vm.get_pid()
    drop = 0
    for cpuid in range(len(vcpu_threads)):
        error.context("Get vcpu %s used numa node." % cpuid, logging.info)
        memory_status, _ = utils_test.qemu.get_numa_status(
            host_numa_node, qemu_pid)
        node_used_host = get_vcpu_used_node(host_numa_node,
                                            vcpu_threads[cpuid])
        node_used_host_index = node_list.index(node_used_host)
        memory_used_before = memory_status[node_used_host_index]
        error.context("Allocate memory in guest", logging.info)
        session.cmd(mount_cmd)
        binded_dd_cmd = "taskset %s" % str(2**int(cpuid))
        binded_dd_cmd += " dd if=/dev/urandom of=/tmp/%s" % cpuid
        binded_dd_cmd += " bs=1M count=%s" % dd_size
        session.cmd(binded_dd_cmd)
        error.context("Check qemu process memory use status", logging.info)
        node_after = get_vcpu_used_node(host_numa_node, vcpu_threads[cpuid])
        if node_after != node_used_host:
            logging.warn("Node used by vcpu thread changed. So drop the"
                         " results in this round.")
            drop += 1
            continue
        memory_status, _ = utils_test.qemu.get_numa_status(
            host_numa_node, qemu_pid)
        memory_used_after = memory_status[node_used_host_index]
        page_size = resource.getpagesize() / 1024
        memory_allocated = (memory_used_after -
                            memory_used_before) * page_size / 1024
        if 1 - float(memory_allocated) / float(dd_size) > 0.05:
            numa_hardware_cmd = params.get("numa_hardware_cmd")
            if numa_hardware_cmd:
                numa_info = utils.system_output(numa_hardware_cmd,
                                                ignore_status=True)
            msg = "Expect malloc %sM memory in node %s," % (dd_size,
                                                            node_used_host)
            msg += "but only malloc %sM \n" % memory_allocated
            msg += "Please check more details of the numa node: %s" % numa_info
            raise error.TestFail(msg)
    session.close()

    if drop == len(vcpu_threads):
        raise error.TestError("All test rounds are dropped."
                              " Please test it again.")
def run(test, params, env):
    """
    QEMU flow caches stress test test

    1) Make sure nf_conntrack is disabled in host and guest.
       If nf_conntrack is enabled in host, skip this case.
    2) Boot guest with vhost=on/off.
    3) Enable multi queues support in guest (optional).
    4) After installation of netperf, run netserver in host.
    5) Run netperf TCP_CRR protocal test in guest.
    6) Transfer file between guest and host.
    7) Check the md5 of copied file.

    This is a sample QEMU test, so people can get used to some of the test APIs.

    :param test: QEMU test object.
    :param params: Dictionary with the test parameters.
    :param env: Dictionary with test environment.
    """
    msg = "Make sure nf_conntrack is disabled in host and guest."
    error.context(msg, logging.info)
    if "nf_conntrack" in utils.system_output("lsmod"):
        err = "nf_conntrack load in host, skip this case"
        raise error.TestNAError(err)

    params["start_vm"] = "yes"
    error.context("Boot up guest", logging.info)
    env_process.preprocess_vm(test, params, env, params["main_vm"])
    vm = env.get_vm(params["main_vm"])
    vm.verify_alive()

    timeout = int(params.get("login_timeout", 360))
    session = vm.wait_for_login(timeout=timeout)
    if "nf_conntrack" in session.cmd_output("lsmod"):
        msg = "Unload nf_conntrack module in guest."
        error.context(msg, logging.info)
        black_str = "#disable nf_conntrack\nblacklist nf_conntrack\n" \
                    "blacklist nf_conntrack_ipv6\nblacklist xt_conntrack\n" \
                    "blacklist nf_conntrack_ftp\nblacklist xt_state\n" \
                    "blacklist iptable_nat\nblacklist ipt_REDIRECT\n" \
                    "blacklist nf_nat\nblacklist nf_conntrack_ipv4"
        cmd = "echo -e '%s' >> /etc/modprobe.d/blacklist.conf" % black_str
        session.cmd(cmd)
        session = vm.reboot(session, timeout=timeout)
        if "nf_conntrack" in session.cmd_output("lsmod"):
            err = "Fail to unload nf_conntrack module in guest."
            error.TestError(err)

    netperf_link = utils_misc.get_path(data_dir.get_deps_dir("netperf"),
                                       params["netperf_link"])
    md5sum = params.get("pkg_md5sum")
    win_netperf_link = params.get("win_netperf_link")
    if win_netperf_link:
        win_netperf_link = utils_misc.get_path(
            data_dir.get_deps_dir("netperf"), win_netperf_link)
    win_netperf_md5sum = params.get("win_netperf_md5sum")
    server_path = params.get("server_path", "/var/tmp/")
    client_path = params.get("client_path", "/var/tmp/")
    win_netperf_path = params.get("win_netperf_path", "c:\\")
    client_num = params.get("netperf_client_num", 520)
    netperf_timeout = int(params.get("netperf_timeout", 600))
    netperf_client_ip = vm.get_address()
    host_ip = utils_net.get_host_ip_address(params)
    netperf_server_ip = params.get("netperf_server_ip", host_ip)

    username = params.get("username", "root")
    password = params.get("password", "123456")
    passwd = params.get("hostpasswd", "123456")
    client = params.get("shell_client", "ssh")
    port = params.get("shell_port", "22")
    compile_option_client = params.get("compile_option_client", "")
    compile_option_server = params.get("compile_option_server", "")

    if int(params.get("queues", 1)) > 1 and params.get("os_type") == "linux":
        error.context("Enable multi queues support in guest.", logging.info)
        guest_mac = vm.get_mac_address()
        ifname = utils_net.get_linux_ifname(session, guest_mac)
        cmd = "ethtool -L %s combined  %s" % (ifname, params.get("queues"))
        status, out = session.cmd_status_output(cmd)
        msg = "Fail to enable multi queues support in guest."
        msg += "Command %s fail output: %s" % (cmd, out)
        error.TestError(msg)

    if params.get("os_type") == "linux":
        session.cmd("iptables -F", ignore_all_errors=True)
        g_client_link = netperf_link
        g_client_path = client_path
        g_md5sum = md5sum
    elif params.get("os_type") == "windows":
        g_client_link = win_netperf_link
        g_client_path = win_netperf_path
        g_md5sum = win_netperf_md5sum

    error.context("Setup netperf in guest and host", logging.info)
    netperf_client = utils_netperf.NetperfClient(
        netperf_client_ip,
        g_client_path,
        g_md5sum,
        g_client_link,
        username=username,
        password=password,
        compile_option=compile_option_client)

    netperf_server = utils_netperf.NetperfServer(
        netperf_server_ip,
        server_path,
        md5sum,
        netperf_link,
        client,
        port,
        password=passwd,
        compile_option=compile_option_server)
    try:
        error.base_context("Run netperf test between host and guest.")
        error.context("Start netserver in host.", logging.info)
        netperf_server.start()

        error.context("Start Netperf in guest for %ss." % netperf_timeout,
                      logging.info)
        test_option = "-t TCP_CRR -l %s -- -b 10 -D" % netperf_timeout
        netperf_client.bg_start(netperf_server_ip, test_option, client_num)

        utils_misc.wait_for(lambda: not netperf_client.is_netperf_running(),
                            timeout=netperf_timeout,
                            first=590,
                            step=2)

        utils_test.run_file_transfer(test, params, env)
    finally:
        netperf_server.stop()
        netperf_client.package.env_cleanup(True)
        if session:
            session.close()
Example #56
0
def run(test, params, env):
    """
    QEMU boot from device:

    1) Start guest from device(hd/usb/scsi-hd)
    2) Check the boot result
    3) Log into the guest if it's up
    4) Shutdown the guest if it's up

    :param test: QEMU test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment.
    """
    def create_cdroms():
        """
        Create 'test' cdrom with one file on it
        """

        logging.info("creating test cdrom")
        cdrom_test = params.get("cdrom_test")
        cdrom_test = utils_misc.get_path(data_dir.get_data_dir(), cdrom_test)
        utils.run("dd if=/dev/urandom of=test bs=10M count=1")
        utils.run("mkisofs -o %s test" % cdrom_test)
        utils.run("rm -f test")

    def cleanup_cdroms():
        """
        Removes created cdrom
        """

        logging.info("cleaning up temp cdrom images")
        cdrom_test = utils_misc.get_path(data_dir.get_data_dir(),
                                         params.get("cdrom_test"))
        os.remove(cdrom_test)

    def preprocess_remote_storage():
        """
        Prepare remote ISCSI storage for block image, and login session for
        iscsi device.
        """

        iscsidevice = iscsi.Iscsi(params)
        iscsidevice.login()
        device_name = iscsidevice.get_device_name()
        if not device_name:
            iscsidevice.logout()
            raise error.TestError("Fail to get iscsi device name")

    def postprocess_remote_storage():
        """
        Logout from target.
        """

        iscsidevice = iscsi.Iscsi(params)
        iscsidevice.logout()

    def cleanup(dev_name):
        if dev_name == "scsi-cd":
            cleanup_cdroms()
        elif dev_name == "iscsi-dev":
            postprocess_remote_storage()

    def check_boot_result(boot_fail_info, device_name):
        """
        Check boot result, and logout from iscsi device if boot from iscsi.
        """

        logging.info("Wait for display and check boot info.")
        infos = boot_fail_info.split(';')
        serial_output = vm.serial_console.get_stripped_output()
        utils_misc.wait_for(lambda: re.search(infos[0], serial_output),
                            timeout, 1)

        logging.info("Try to boot from '%s'" % device_name)
        try:
            if dev_name == "hard-drive" or (dev_name == "scsi-hd" and
                                            not params.get("image_name_stg")):
                error.context("Log into the guest to verify it's up",
                              logging.info)
                session = vm.wait_for_login(timeout=timeout)
                session.close()
                vm.destroy()
                return

            output = vm.serial_console.get_stripped_output()

            for i in infos:
                if not re.search(i, output):
                    raise error.TestFail("Could not boot from"
                                         " '%s'" % device_name)
        finally:
            cleanup(device_name)

    dev_name = params.get("dev_name")
    if dev_name == "scsi-cd":
        create_cdroms()
        params["start_vm"] = "yes"
        env_process.preprocess_vm(test, params, env, params.get("main_vm"))
        vm = env.get_vm(params["main_vm"])
        vm.verify_alive()
    elif dev_name == "iscsi-dev":
        preprocess_remote_storage()
        params["start_vm"] = "yes"
        env_process.preprocess_vm(test, params, env, params.get("main_vm"))
        vm = env.get_vm(params["main_vm"])
        vm.verify_alive()
    else:
        vm = env.get_vm(params["main_vm"])
        vm.verify_alive()

    timeout = int(params.get("login_timeout", 360))
    boot_menu_key = params.get("boot_menu_key", 'f12')
    boot_menu_hint = params.get("boot_menu_hint")
    boot_fail_info = params.get("boot_fail_info")
    boot_device = params.get("boot_device")

    if boot_device:
        serial_output = vm.serial_console.get_stripped_output()
        if not utils_misc.wait_for(
                lambda: re.search(boot_menu_hint, serial_output), timeout, 1):
            cleanup(dev_name)
            raise error.TestFail("Could not get boot menu message. "
                                 "Excepted Result: '%s'" % boot_menu_hint)

        # Send boot menu key in monitor.
        vm.send_key(boot_menu_key)

        output = vm.serial_console.get_stripped_output()
        boot_list = re.findall("^\d+\. (.*)\s", output, re.M)

        if not boot_list:
            cleanup(dev_name)
            raise error.TestFail("Could not get boot entries list.")

        logging.info("Got boot menu entries: '%s'", boot_list)
        for i, v in enumerate(boot_list, start=1):
            if re.search(boot_device, v, re.I):
                logging.info("Start guest from boot entry '%s'" % boot_device)
                vm.send_key(str(i))
                break
        else:
            raise error.TestFail("Could not get any boot entry match "
                                 "pattern '%s'" % boot_device)

    check_boot_result(boot_fail_info, dev_name)
def run(test, params, env):
    """
    Qemu allocate hugepage from specify node.
    Steps:
    1) Setup total of 4G mem hugepages for specify node.
    2) Setup total of 1G mem hugepages for idle node.
    3) Mount this hugepage to /mnt/kvm_hugepage.
    4) Boot guest only allocate hugepage from specify node.
    5) Check the hugepage used from every node.
    :params test: QEMU test object.
    :params params: Dictionary with the test parameters.
    :params env: Dictionary with test environment.
    """
    memory.drop_caches()
    hugepage_size = memory.get_huge_page_size()
    mem_size = int(normalize_data_size("%sM" % params["mem"], "K"))
    idle_node_mem = int(normalize_data_size("%sM" % params["idle_node_mem"], "K"))

    error_context.context("Get host numa topological structure.", logging.info)
    host_numa_node = utils_misc.NumaInfo()
    node_list = host_numa_node.get_online_nodes_withmem()
    idle_node_list = node_list.copy()
    node_meminfo = host_numa_node.get_all_node_meminfo()

    for node_id in node_list:
        error_context.base_context("Check preprocess HugePages Free on host "
                                   "numa node %s." % node_id, logging.info)
        node_memfree = int(node_meminfo[node_id]["MemFree"])
        if node_memfree < idle_node_mem:
            idle_node_list.remove(node_id)
        if node_memfree < mem_size:
            node_list.remove(node_id)

    if len(idle_node_list) < 2 or not node_list:
        test.cancel("Host node does not have enough nodes to run the test, "
                    "skipping test...")

    for node_id in node_list:
        error_context.base_context("Specify qemu process only allocate "
                                   "HugePages from node%s." % node_id, logging.info)
        params["target_nodes"] = "%s" % node_id
        params["target_num_node%s" % node_id] = math.ceil(mem_size / hugepage_size)
        error_context.context("Setup huge pages for specify node%s." %
                              node_id, logging.info)
        check_list = [_ for _ in idle_node_list if _ != node_id]
        for idle_node in check_list:
            params["target_nodes"] += " %s" % idle_node
            params["target_num_node%s" % idle_node] = math.ceil(idle_node_mem / hugepage_size)
            error_context.context("Setup huge pages for idle node%s." %
                                  idle_node, logging.info)
        params["setup_hugepages"] = "yes"
        hp_config = test_setup.HugePageConfig(params)
        hp_config.setup()
        params["qemu_command_prefix"] = "numactl --membind=%s" % node_id
        params["start_vm"] = "yes"
        params["hugepage_path"] = hp_config.hugepage_path
        env_process.preprocess_vm(test, params, env, params["main_vm"])
        try:
            vm = env.get_vm(params["main_vm"])
            vm.verify_alive()
            vm.wait_for_login()

            meminfo = host_numa_node.get_all_node_meminfo()
            for index in check_list:
                error_context.base_context("Check process HugePages Free on host "
                                           "numa node %s." % index, logging.info)
                hugepages_free = int(meminfo[index]["HugePages_Free"])
                if int(node_meminfo[index]["HugePages_Free"]) > hugepages_free:
                    test.fail("Qemu still use HugePages from other node."
                              "Expect: node%s, used: node%s." % (node_id, index))
        finally:
            vm.destroy()
            hp_config.cleanup()
Example #58
0
def run(test, params, env):
    """
    Qemu multiqueue test for virtio-scsi controller:

    1) Boot up a guest with virtio-scsi device which support multi-queue and
       the vcpu and images number of guest should match the multi-queue number.
    2) Pin the vcpus to the host cpus.
    3) Check the multi queue option from monitor.
    4) Check device init status in guest
    5) Pin the interrupts to the vcpus.
    6) Load I/O in all targets.
    7) Check the interrupt queues in guest.

    :param test: QEMU test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment.
    """
    def get_mapping_interrupts2vcpus(irqs, pattern):
        """ Get the mapping of between virtio interrupts and vcpus. """
        regex = r'(\d+):(\s+(\d+\s+){%d})\s+.+\s%s\s' % (
            len(re.findall(r"\s+CPU\d+", irqs, re.M)), pattern)
        return {f[0]: {'count': f[1].split()} for f in re.findall(regex, irqs, re.M)}

    def create_data_images():
        """ Create date image objects. """
        for extra_image in range(images_num):
            image_tag = "stg%s" % extra_image
            params["images"] += " %s" % image_tag
            params["image_name_%s" % image_tag] = "images/%s" % image_tag
            params["image_size_%s" % image_tag] = extra_image_size
            params["force_create_image_%s" % image_tag] = "yes"
            image_params = params.object_params(image_tag)
            env_process.preprocess_image(test, image_params, image_tag)

    def check_irqbalance_status():
        """ Check the status of irqbalance service. """
        error_context.context("Check irqbalance service status.", test.log.info)
        return re.findall("Active: active", session.cmd_output(status_cmd))

    def start_irqbalance_service():
        """ Start the irqbalance service. """
        error_context.context("Start the irqbalance service.", test.log.info)
        session.cmd("systemctl start irqbalance")
        output = utils_misc.strip_console_codes(session.cmd_output(status_cmd))
        if not re.findall("Active: active", output):
            test.cancel("Can not start irqbalance inside guest.Skip this test.")

    def pin_vcpus2host_cpus():
        """ Pint the vcpus to the host cpus. """
        error_context.context("Pin vcpus to host cpus.", test.log.info)
        host_numa_nodes = utils_misc.NumaInfo()
        vcpu_num = 0
        for numa_node_id in host_numa_nodes.nodes:
            numa_node = host_numa_nodes.nodes[numa_node_id]
            for _ in range(len(numa_node.cpus)):
                if vcpu_num >= len(vm.vcpu_threads):
                    break
                vcpu_tid = vm.vcpu_threads[vcpu_num]
                test.log.debug(
                    "pin vcpu thread(%s) to cpu(%s)",
                    vcpu_tid, numa_node.pin_cpu(vcpu_tid))
                vcpu_num += 1

    def verify_num_queues():
        """ Verify the number of queues. """
        error_context.context("Verify num_queues from monitor.", test.log.info)
        qtree = qemu_qtree.QtreeContainer()
        try:
            qtree.parse_info_qtree(vm.monitor.info('qtree'))
        except AttributeError:
            test.cancel("Monitor deson't supoort qtree skip this test")
        error_msg = "Number of queues mismatch: expect %s report from monitor: %s(%s)"
        scsi_bus_addr = ""
        qtree_num_queues_full = ""
        qtree_num_queues = ""
        for node in qtree.get_nodes():
            type = node.qtree['type']
            if isinstance(node, qemu_qtree.QtreeDev) and (
                    type == "virtio-scsi-device"):
                qtree_num_queues_full = node.qtree["num_queues"]
                qtree_num_queues = re.search(
                    "[0-9]+", qtree_num_queues_full).group()
            elif (isinstance(node, qemu_qtree.QtreeDev)) and (
                    type == "virtio-scsi-pci"):
                scsi_bus_addr = node.qtree['addr']

        if qtree_num_queues != num_queues:
            error_msg = error_msg % (
                num_queues, qtree_num_queues, qtree_num_queues_full)
            test.fail(error_msg)
        if not scsi_bus_addr:
            test.error("Didn't find addr from qtree. Please check the log.")

    def check_interrupts():
        """ Check the interrupt queues in guest. """
        error_context.context("Check the interrupt queues in guest.", test.log.info)
        return session.cmd_output(irq_check_cmd)

    def check_interrupts2vcpus(irq_map):
        """ Check the status of interrupters to vcpus. """
        error_context.context(
            "Check the status of interrupters to vcpus.", test.log.info)
        cpu_selects = {}
        cpu_select = 1
        for _ in range(int(num_queues)):
            val = ','.join([_[::-1] for _ in re.findall(r'\w{8}|\w+', format(
                cpu_select, 'x')[::-1])][::-1])
            cpu_selects[val] = format(cpu_select, 'b').count('0')
            cpu_select = cpu_select << 1
        irqs_id_reset = []
        for irq_id in irq_map.keys():
            cmd = 'cat /proc/irq/%s/smp_affinity' % irq_id
            cpu_selected = re.sub(
                r'(^[0+,?0+]+)|(,)', '', session.cmd_output(cmd)).strip()
            if cpu_selected not in cpu_selects:
                irqs_id_reset.append(irq_id)
            else:
                cpu_irq_map[irq_id] = cpu_selects[cpu_selected]
                del cpu_selects[cpu_selected]
        return irqs_id_reset, cpu_selects

    def pin_interrupts2vcpus(irqs_id_reset, cpu_selects):
        """ Pint the interrupts to vcpus. """
        bind_cpu_cmd = []
        for irq_id, cpu_select in zip(irqs_id_reset, cpu_selects):
            bind_cpu_cmd.append(
                "echo %s > /proc/irq/%s/smp_affinity" % (cpu_select, irq_id))
            cpu_irq_map[irq_id] = cpu_selects[cpu_select]
        if bind_cpu_cmd:
            error_context.context("Pin interrupters to vcpus", test.log.info)
            session.cmd(' && '.join(bind_cpu_cmd))
        return cpu_irq_map

    def _get_data_disks(session):
        """ Get the data disks. """
        output = session.cmd_output(params.get("get_dev_cmd", "ls /dev/[svh]d*"))
        system_dev = re.search(r"/dev/([svh]d\w+)(?=\d+)", output, re.M).group(1)
        return (dev for dev in output.split() if system_dev not in dev)

    def check_io_status(timeout):
        """ Check the status of I/O. """
        chk_session = vm.wait_for_login(timeout=360)
        while int(chk_session.cmd_output("pgrep -lx dd | wc -l", timeout)):
            time.sleep(5)
        chk_session.close()

    def load_io_data_disks():
        """ Load I/O on data disks. """
        error_context.context("Load I/O in all targets", test.log.info)
        dd_session = vm.wait_for_login(timeout=360)
        dd_timeout = int(re.findall(r"\d+", extra_image_size)[0])
        cmd = "dd of=%s if=/dev/urandom bs=1M count=%s oflag=direct &"
        cmds = [cmd % (dev, dd_timeout) for dev in _get_data_disks(dd_session)]
        if len(cmds) != images_num:
            test.error(
                "Disks are not all show up in system, only %s disks." % len(cmds))

        # As Bug 1177332 exists, mq is not supported completely.
        # So don't considering performance currently, dd_timeout is longer.
        dd_session.cmd(' '.join(cmds), dd_timeout * images_num * 2)
        check_io_status(dd_timeout)
        dd_session.close()

    def compare_interrupts(prev_irqs, cur_irqs):
        """ Compare the interrupts between after and before IO. """
        cpu_not_used = []
        diff_interrupts = {}
        for irq in prev_irqs.keys():
            cpu = int(cpu_irq_map[irq])
            diff_val = int(
                cur_irqs[irq]['count'][cpu]) - int(prev_irqs[irq]['count'][cpu])
            if diff_val == 0:
                cpu_not_used.append('CPU%s' % cpu)
            else:
                diff_interrupts[cpu] = diff_val
        test.log.debug('The changed number of interrupts:')
        for k, v in sorted(diff_interrupts.items()):
            test.log.debug('  CPU%s: %d', k, v)
        if cpu_not_used:
            cpus = " ".join(cpu_not_used)
            error_msg = ("%s are not used during test. "
                         "Please check debug log for more information.")
            test.fail(error_msg % cpus)

    def wmi_facility_test(session):
        driver_name = params["driver_name"]
        wmi_check_cmd = params["wmi_check_cmd"]
        pattern = params["pattern"]
        session = utils_test.qemu.windrv_check_running_verifier(
            session, vm, test, driver_name, timeout)
        wmi_check_cmd = utils_misc.set_winutils_letter(session, wmi_check_cmd)
        error_context.context("Run wmi check in guest.", test.log.info)
        output = session.cmd_output(wmi_check_cmd)
        queue_num = re.findall(pattern, output, re.M)
        try:
            if not queue_num or queue_num[0] != num_queues:
                test.fail("The queue_num from guest is not match with expected.\n"
                          "queue_num from guest is %s, expected is %s"
                          % (queue_num, num_queues))
        finally:
            session.close()

    cpu_irq_map = {}
    timeout = float(params.get("login_timeout", 240))
    num_queues = params['vcpu_maxcpus']
    params['smp'] = num_queues
    params['num_queues'] = num_queues
    images_num = int(num_queues)
    extra_image_size = params.get("image_size_extra_images", "512M")
    system_image = params.get("images")
    system_image_drive_format = params.get("system_image_drive_format", "virtio")
    params["drive_format_%s" % system_image] = system_image_drive_format
    irq_check_cmd = params.get("irq_check_cmd", "cat /proc/interrupts")
    irq_name = params.get("irq_regex")
    status_cmd = "systemctl status irqbalance"

    error_context.context("Boot up guest with block devcie with num_queues"
                          " is %s and smp is %s" % (num_queues, params['smp']),
                          test.log.info)
    for vm in env.get_all_vms():
        if vm.is_alive():
            vm.destroy()
    create_data_images()
    params["start_vm"] = "yes"
    vm = env.get_vm(params["main_vm"])
    env_process.preprocess_vm(test, params, env, vm.name)
    session = vm.wait_for_login(timeout=timeout)
    if params["os_type"] == "windows":
        wmi_facility_test(session)
        return
    if not check_irqbalance_status():
        start_irqbalance_service()
    pin_vcpus2host_cpus()
    verify_num_queues()
    prev_irqs = check_interrupts()
    prev_mapping = get_mapping_interrupts2vcpus(prev_irqs, irq_name)
    pin_interrupts2vcpus(*check_interrupts2vcpus(prev_mapping))
    load_io_data_disks()
    cur_irqs = check_interrupts()
    cur_mapping = get_mapping_interrupts2vcpus(cur_irqs, irq_name)
    compare_interrupts(prev_mapping, cur_mapping)
Example #59
0
def run(test, params, env):
    """
    Run nvdimm cases:
    1) Boot guest with nvdimm device backed by a host file
    2) Login to the guest
    3) Check nvdimm in monitor and guest
    4) Format nvdimm device in guest
    5) Mount nvdimm device in guest
    6) Create a file in the mount point in guest
    7) Check the md5 value of the file
    8) Umount the nvdimm device and check calltrace in guest
    9) Reboot the guest
    10) Remount nvdimm device in guest
    11) Check if the md5 value of the nv_file changes

    :param test: QEMU test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment.
    """
    if params["start_vm"] == "no":
        error_context.context("Check nvdimm backend in host", logging.info)
        try:
            process.system("grep 'memmap=' /proc/cmdline")
        except process.CmdError:
            test.error("Please add kernel param 'memmap' before start test.")
        if params.get("nvdimm_dax") == "yes":
            try:
                process.system(params["ndctl_install_cmd"], shell=True)
            except process.CmdError:
                test.error("ndctl is not available in host!")
            ndctl_ver = process.system_output("ndctl -v", shell=True)
            if float(ndctl_ver) < 56:
                test.cancel("ndctl version should be equal or greater than 56!"
                            "Current ndctl version is %s." % ndctl_ver)
            try:
                process.system(params["create_dax_cmd"], shell=True)
            except process.CmdError:
                test.error("Creating dax failed!")
        if not os.path.exists(params["nv_backend"]):
            test.fail("Check nv_backend in host failed!")
        params["start_vm"] = "yes"
        vm_name = params['main_vm']
        env_process.preprocess_vm(test, params, env, vm_name)

    nvdimm_test = NvdimmTest(test, params, env)
    vm = env.get_vm(params["main_vm"])
    vm.verify_alive()
    try:
        error_context.context("Login to the guest", logging.info)
        login_timeout = int(params.get("login_timeout", 360))
        nvdimm_test.session = vm.wait_for_login(timeout=login_timeout)
        mems = params.objects("mem_devs")
        target_mems = params.objects("target_mems")
        if target_mems:
            hotplug_test = MemoryHotplugTest(test, params, env)
            for mem in target_mems:
                hotplug_test.hotplug_memory(vm, mem)
            time.sleep(10)
            mems += target_mems
        error_context.context("Verify nvdimm in monitor and guest",
                              logging.info)
        nvdimm_ns_create_cmd = params.get("nvdimm_ns_create_cmd")
        if nvdimm_ns_create_cmd:
            nvdimm_test.run_guest_cmd(nvdimm_ns_create_cmd)
        nvdimm_test.verify_nvdimm(vm, mems)
        error_context.context("Format and mount nvdimm in guest", logging.info)
        nvdimm_test.mount_nvdimm()
        nv_file = params.get("nv_file", "/mnt/nv")
        error_context.context(
            "Create a file in nvdimm mount dir in guest, and get "
            "original md5 of the file", logging.info)
        dd_cmd = "dd if=/dev/urandom of=%s bs=1K count=200" % nv_file
        nvdimm_test.run_guest_cmd(dd_cmd)
        orig_md5 = nvdimm_test.md5_hash(nv_file)
        nvdimm_test.umount_nvdimm()
        nvdimm_test.session = vm.reboot()
        error_context.context("Verify nvdimm after reboot", logging.info)
        nvdimm_test.verify_nvdimm(vm, mems)
        nvdimm_test.mount_nvdimm(format_device="no")
        new_md5 = nvdimm_test.md5_hash(nv_file)
        error_context.context("Compare current md5 to original md5",
                              logging.info)
        if new_md5 != orig_md5:
            test.fail(
                "'%s' changed. The original md5 is '%s', current md5 is '%s'" %
                (nv_file, orig_md5, new_md5))
        nvdimm_test.umount_nvdimm()
        error_context.context("Check if error and calltrace in guest",
                              logging.info)
        vm.verify_kernel_crash()

    finally:
        if nvdimm_test.session:
            nvdimm_test.session.close()
        vm.destroy()
        if params.get("nvdimm_dax") == "yes":
            try:
                process.system(params["del_dax_cmd"], timeout=240, shell=True)
            except process.CmdError:
                logging.warn("Host dax configuration cannot be deleted!")
Example #60
0
def run(test, params, env):
    """
    Timer device measure clock drift after sleep in guest with kvmclock:

    1) Sync the host system time with ntp server
    2) Boot a guest with multiple vcpus, using kvm-clock
    3) Check the clock source currently used on guest
    4) Stop auto sync service in guest (Optional)
    5) Sync time from guest to ntpserver
    6) Pin (only 1/none/all) vcpus to host cpu.
    7) Sleep a while and check the time drift on guest

    :param test: QEMU test object.
    :param params: Dictionary with test parameters.
    :param env: Dictionary with the test environment.
    """
    def verify_elapsed_time():
        usleep_cmd = r'echo "for n in \$(seq 1000);'
        usleep_cmd += ' do usleep 10000; done"' ' > /tmp/usleep.sh'
        session.cmd(usleep_cmd)

        get_time_cmd = 'for (( i=0; i<$(grep "processor" /proc/cpuinfo'
        get_time_cmd += ' | wc -l); i+=1 )); do /usr/bin/time -f"%e"'
        get_time_cmd += ' taskset -c $i sh /tmp/usleep.sh; done'
        output = session.cmd_output(get_time_cmd, timeout=timeout)

        times_list = output.splitlines()[1:]
        times_list = [_ for _ in times_list if _ > 10.0 or _ < 11.0]

        if times_list:
            raise error.TestFail("Unexpected time drift found:"
                                 " Detail: '%s'" % output)

    error.context("Sync the host system time with ntp server", logging.info)
    utils.system("yum install -y ntpdate; ntpdate clock.redhat.com")

    error.context("Boot the guest", logging.info)
    vm = env.get_vm(params["main_vm"])
    vm.verify_alive()

    timeout = int(params.get("login_timeout", 360))
    session = vm.wait_for_login(timeout=timeout)

    error.context("Check the clock source currently used on guest",
                  logging.info)
    cmd = "cat /sys/devices/system/clocksource/"
    cmd += "clocksource0/current_clocksource"
    if not "kvm-clock" in session.cmd(cmd):
        grub_file = params.get("grub_file", "/boot/grub2/grub.cfg")
        if "clocksource=" not in session.cmd("cat %s" % grub_file):
            raise error.TestFail("Guest didn't use 'kvm-clock' clocksource")

        error.context("Shutdown guest")
        vm.destroy()
        env.unregister_vm(vm.name)
        error.context("Update guest kernel cli to kvm-clock", logging.info)
        image_filename = storage.get_image_filename(params,
                                                    data_dir.get_data_dir())
        kernel_cfg_pattern = params.get("kernel_cfg_pos_reg",
                                        r".*vmlinuz-\d+.*")

        disk_obj = utils_disk.GuestFSModiDisk(image_filename)
        kernel_cfg_original = disk_obj.read_file(grub_file)
        try:
            logging.warn("Update the first kernel entry to kvm-clock only")
            kernel_cfg = re.findall(kernel_cfg_pattern, kernel_cfg_original)[0]
        except IndexError, detail:
            raise error.TestError("Couldn't find the kernel config, regex"
                                  " pattern is '%s', detail: '%s'" %
                                  (kernel_cfg_pattern, detail))

        if "clocksource=" in kernel_cfg:
            kernel_cfg_new = re.sub(r"clocksource=[a-z\- ]+", " ", kernel_cfg)
            disk_obj.replace_image_file_content(grub_file, kernel_cfg,
                                                kernel_cfg_new)

        error.context("Boot the guest", logging.info)
        vm_name = params["main_vm"]
        cpu_model_flags = params.get("cpu_model_flags")
        params["cpu_model_flags"] = cpu_model_flags + ",-kvmclock"
        env_process.preprocess_vm(test, params, env, vm_name)
        vm = env.get_vm(vm_name)
        vm.verify_alive()
        session = vm.wait_for_login(timeout=timeout)