Example #1
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:
            test.fail("Unexpected time drift found: Detail: '%s'" % output)

    error_context.context("Sync the host system time with ntp server",
                          logging.info)
    process.system("yum install -y ntpdate; ntpdate clock.redhat.com",
                   shell=True)

    error_context.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.context("Check the clock source currently used on guest",
                          logging.info)
    cmd = "cat /sys/devices/system/clocksource/"
    cmd += "clocksource0/current_clocksource"
    if "kvm-clock" not in session.cmd(cmd):
        grub_file = params.get("grub_file", "/boot/grub2/grub.cfg")
        if "clocksource=" not in session.cmd("cat %s" % grub_file):
            test.fail("Guest didn't use 'kvm-clock' clocksource")

        error_context.context("Shutdown guest")
        vm.destroy()
        env.unregister_vm(vm.name)
        error_context.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 as detail:
            test.error("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.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)

    error_context.context("Stop auto sync service in guest", logging.info)
    cmd = "(service chronyd status | grep 'Loaded: loaded')"
    cmd += " && service chronyd stop"
    session.cmd_status_output(cmd)

    error_context.context("Sync time from guest to ntpserver", logging.info)
    session.cmd("yum install -y ntpdate; ntpdate clock.redhat.com",
                timeout=timeout)

    error_context.context(
        "Sleep a while and check the time drift on guest"
        " (without any pinned vcpu)", logging.info)
    verify_elapsed_time()

    error_context.context("Pin every vcpu to physical cpu", logging.info)
    host_cpu_cnt_cmd = params["host_cpu_cnt_cmd"]
    host_cpu_num = process.system_output(host_cpu_cnt_cmd, shell=True).strip()
    host_cpu_list = (_ for _ in range(int(host_cpu_num)))
    cpu_pin_list = list(zip(vm.vcpu_threads, host_cpu_list))
    if len(cpu_pin_list) < len(vm.vcpu_threads):
        test.cancel("There isn't enough physical cpu to pin all the vcpus")
    check_one_cpu_pinned = False
    for vcpu, pcpu in cpu_pin_list:
        process.system("taskset -p -c %s %s" % (pcpu, vcpu))
        if not check_one_cpu_pinned:
            error_context.context(
                "Sleep a while and check the time drift on"
                "guest (with one pinned vcpu)", logging.info)
            verify_elapsed_time()
            check_one_cpu_pinned = True

    error_context.context(
        "Sleep a while and check the time drift on"
        "guest (with all pinned vcpus)", logging.info)
    verify_elapsed_time()
Example #2
0
def run(test, params, env):
    """
    Timer device boot guest:

    1) Sync the host system time with ntp server
    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 and host (Optional)
    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 and host (Optional)

    :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.context("Check the current clocksource in guest",
                              logging.info)
        cmd = "cat /sys/devices/system/clocksource/"
        cmd += "clocksource0/current_clocksource"
        if expected not in session.cmd(cmd):
            test.fail("Guest didn't use '%s' clocksource" % expected)

    error_context.context("Sync the host system time with ntp server",
                          logging.info)
    process.system("ntpdate clock.redhat.com")

    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)
        process.system(timerdevice_host_load_cmd, shell=True)
        host_load_stop_cmd = params["timerdevice_host_load_stop_cmd"]
        funcatexit.register(env, params["type"], _system, host_load_stop_cmd)

    error_context.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)

    timerdevice_clksource = params.get("timerdevice_clksource")
    if timerdevice_clksource:
        try:
            verify_guest_clock_source(session, timerdevice_clksource)
        except Exception:
            clksrc = timerdevice_clksource
            error_context.context("Shutdown guest")
            vm.destroy()
            env.unregister_vm(vm.name)
            error_context.context("Update guest kernel cli to '%s'" % clksrc,
                                  logging.info)
            image_filename = storage.get_image_filename(
                params, data_dir.get_data_dir())
            grub_file = params.get("grub_file", "/boot/grub2/grub.cfg")
            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"
                             " '%s' only" % clksrc)
                kernel_cfg = re.findall(kernel_cfg_pattern,
                                        kernel_cfg_original)[0]
            except IndexError as detail:
                test.error("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=.*?\s",
                                        "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.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)

            error_context.context("Check the current clocksource in guest",
                                  logging.info)
            verify_guest_clock_source(session, clksrc)

        error_context.context("Kill all ntp related processes")
        session.cmd("pkill ntp; true")

    if params.get("timerdevice_file_operation") == "yes":
        error_context.context("Do some file operation on guest", logging.info)
        session.cmd("dd if=/dev/zero of=/tmp/timer-test-file bs=1M count=100")
        return

    # 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 = params.get("timerdevice_drift_threshold", 3)

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

    get_hw_time_cmd = params.get("get_hw_time_cmd")
    if get_hw_time_cmd:
        error_context.context("Check the hardware time on guest and host",
                              logging.info)
        host_time = process.system_output(get_hw_time_cmd, shell=True)
        guest_time = session.cmd(get_hw_time_cmd)
        drift = abs(float(host_time) - float(guest_time))
        if drift > timerdevice_drift_threshold:
            test.fail("The guest's hardware time is different with"
                      " host's. Host time: '%s', guest time:"
                      " '%s'" % (host_time, guest_time))

    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)

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

        get_hw_time_cmd = params.get("get_hw_time_cmd")
        if get_hw_time_cmd:
            error_context.context("Check the hardware time on guest and host",
                                  logging.info)
            host_time = process.system_output(get_hw_time_cmd, shell=True)
            guest_time = session.cmd(get_hw_time_cmd)
            drift = abs(float(host_time) - float(guest_time))
            if drift > timerdevice_drift_threshold:
                test.fail("The guest's hardware time is different with"
                          " host's. Host time: '%s', guest time:"
                          " '%s'" % (host_time, guest_time))
Example #3
0
def run(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.context("Check the current clocksource in guest",
                              logging.info)
        cmd = "cat /sys/devices/system/clocksource/"
        cmd += "clocksource0/current_clocksource"
        if expected not in session.cmd(cmd):
            test.fail("Guest didn't use '%s' clocksource" % expected)

    error_context.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.context("Check the current clocksource in guest",
                          logging.info)
    cmd = "cat /sys/devices/system/clocksource/"
    cmd += "clocksource0/current_clocksource"
    if "kvm-clock" not in session.cmd(cmd):
        grub_file = params.get("grub_file", "/boot/grub2/grub.cfg")
        if "clocksource=" not in session.cmd("cat %s" % grub_file):
            test.fail("Guest didn't use 'kvm-clock' clocksource")

        error_context.context("Shutdown guest")
        vm.destroy()
        env.unregister_vm(vm.name)
        error_context.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 as detail:
            test.error("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.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)

    error_context.context("Check the available clocksource in guest",
                          logging.info)
    cmd = "cat /sys/devices/system/clocksource/"
    cmd += "clocksource0/available_clocksource"
    try:
        available_clksrc_list = session.cmd(cmd).splitlines()[-1].split()
        available_clksrc_list = [_.strip() for _ in available_clksrc_list]
    except Exception as detail:
        test.fail("Couldn't get guest available clock source."
                  " Detail: '%s'" % detail)

    try:
        for clksrc in available_clksrc_list:
            error_context.context("Shutdown guest")
            vm.destroy()
            env.unregister_vm(vm.name)
            error_context.context("Update guest kernel cli to '%s'" % clksrc,
                                  logging.info)
            image_filename = storage.get_image_filename(
                params, data_dir.get_data_dir())
            grub_file = params.get("grub_file", "/boot/grub2/grub.cfg")
            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"
                             " '%s' only" % clksrc)
                kernel_cfg = re.findall(kernel_cfg_pattern,
                                        kernel_cfg_original)[0]
            except IndexError as detail:
                test.error("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 \-_]+",
                                        "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.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.context("Check the current clocksource in guest",
                                  logging.info)
            verify_guest_clock_source(session, clksrc)
    finally:
        try:
            error_context.context("Shutdown guest")
            vm.destroy()
            error_context.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")
            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:
                kernel_cfg = re.findall(kernel_cfg_pattern,
                                        kernel_cfg_original)[0]
            except IndexError as detail:
                test.error("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)
        except Exception as detail:
            logging.error("Failed to restore guest kernel cli."
                          " Detail: '%s'" % detail)
Example #4
0
def run_timerdevice_boot(test, params, env):
    """
    Timer device boot guest:

    1) Sync the host system time with ntp server
    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 and host (Optional)
    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 and host (Optional)

    @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("Sync the host system time with ntp server", logging.info)
    utils.system("ntpdate clock.redhat.com")

    timerdevice_host_load_cmd = params.get("timerdevice_host_load_cmd")
    if timerdevice_host_load_cmd:
        error.context("Add some load on host", logging.info)
        utils.system(timerdevice_host_load_cmd)
        host_load_stop_cmd = params["timerdevice_host_load_stop_cmd"]
        funcatexit.register(env, params["type"], utils.system,
                            host_load_stop_cmd)

    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)

    timerdevice_clksource = params.get("timerdevice_clksource")
    if timerdevice_clksource:
        try:
            verify_guest_clock_source(session, timerdevice_clksource)
        except Exception:
            clksrc = timerdevice_clksource
            error.context("Shutdown guest")
            vm.destroy()
            env.unregister_vm(vm.name)
            error.context("Update guest kernel cli to '%s'" % clksrc,
                          logging.info)
            image_filename = storage.get_image_filename(
                params, data_dir.get_data_dir())
            grub_file = params.get("grub_file", "/boot/grub2/grub.cfg")
            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"
                             " '%s' only" % clksrc)
                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=.*?\s",
                                        "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)
            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)

            error.context("Check the current clocksource in guest",
                          logging.info)
            verify_guest_clock_source(session, clksrc)

        error.context("Kill all ntp related processes")
        session.cmd("pkill ntp; true")
                             " Detail: '%s'" % detail)

    try:
        for clksrc in available_clksrc_list:
            error.context("Shutdown guest")
            vm.destroy()
            env.unregister_vm(vm.name)
            error.context("Update guest kernel cli to '%s'" % clksrc,
                          logging.info)
            image_filename = storage.get_image_filename(
                params, data_dir.get_data_dir())
            grub_file = params.get("grub_file", "/boot/grub2/grub.cfg")
            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"
                             " '%s' only" % clksrc)
                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 \-_]+",
                                        "clocksource=%s " % clksrc, kernel_cfg)
            else:
def run_timerdevice_clock_drift_with_sleep(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)
Example #7
0
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)