timerdevice_boot.py 8.0 KB
Newer Older
L
Lucas Meneghel Rodrigues 已提交
1 2 3
import logging
import re
import time
4

5
from avocado.utils import process
6 7 8 9 10
from virttest import data_dir
from virttest import storage
from virttest import utils_disk
from virttest import utils_test
from virttest import env_process
11
from virttest import funcatexit
12
from virttest import error_context
13

L
Lucas Meneghel Rodrigues 已提交
14

15
@error_context.context_aware
16
def run(test, params, env):
17 18 19 20
    """
    Timer device boot guest:

    1) Sync the host system time with ntp server
21 22 23 24 25 26 27 28 29 30
    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)
31

L
Lucas Meneghel Rodrigues 已提交
32 33 34
    :param test: QEMU test object.
    :param params: Dictionary with test parameters.
    :param env: Dictionary with the test environment.
35 36
    """
    def verify_guest_clock_source(session, expected):
37 38
        error_context.context("Check the current clocksource in guest",
                              logging.info)
39 40
        cmd = "cat /sys/devices/system/clocksource/"
        cmd += "clocksource0/current_clocksource"
41
        if expected not in session.cmd(cmd):
42
            test.fail("Guest didn't use '%s' clocksource" % expected)
43

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

48 49
    timerdevice_host_load_cmd = params.get("timerdevice_host_load_cmd")
    if timerdevice_host_load_cmd:
50 51
        error_context.context("Add some load on host", logging.info)
        process.system(timerdevice_host_load_cmd, shell=True)
52
        host_load_stop_cmd = params["timerdevice_host_load_stop_cmd"]
53
        funcatexit.register(env, params["type"], process.system,
54 55
                            host_load_stop_cmd)

56
    error_context.context("Boot a guest with kvm-clock", logging.info)
57 58 59 60 61 62 63 64 65 66 67 68
    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
69
            error_context.context("Shutdown guest")
70 71
            vm.destroy()
            env.unregister_vm(vm.name)
72 73
            error_context.context("Update guest kernel cli to '%s'" % clksrc,
                                  logging.info)
74
            image_filename = storage.get_image_filename(params,
L
Lucas Meneghel Rodrigues 已提交
75
                                                        data_dir.get_data_dir())
76 77
            grub_file = params.get("grub_file", "/boot/grub2/grub.cfg")
            kernel_cfg_pattern = params.get("kernel_cfg_pos_reg",
L
Lucas Meneghel Rodrigues 已提交
78
                                            r".*vmlinuz-\d+.*")
79 80 81 82 83 84 85 86 87

            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:
88 89 90
                test.error("Couldn't find the kernel config, regex"
                           " pattern is '%s', detail: '%s'" %
                           (kernel_cfg_pattern, detail))
91 92 93

            if "clocksource=" in kernel_cfg:
                kernel_cfg_new = re.sub("clocksource=.*?\s",
L
Lucas Meneghel Rodrigues 已提交
94
                                        "clocksource=%s" % clksrc, kernel_cfg)
95 96 97 98 99 100 101
            else:
                kernel_cfg_new = "%s %s" % (kernel_cfg,
                                            "clocksource=%s" % clksrc)

            disk_obj.replace_image_file_content(grub_file, kernel_cfg,
                                                kernel_cfg_new)

102
            error_context.context("Boot the guest", logging.info)
103 104 105 106 107 108 109 110
            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)

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

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

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

123 124 125 126 127 128 129 130
    # 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)

131 132
    error_context.context("Check the system time on guest and host",
                          logging.info)
133 134 135 136
    (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:
137 138 139
        test.fail("The guest's system time is different with"
                  " host's. Host time: '%s', guest time:"
                  " '%s'" % (host_time, guest_time))
140 141 142

    get_hw_time_cmd = params.get("get_hw_time_cmd")
    if get_hw_time_cmd:
143
        error_context.context(
L
Lucas Meneghel Rodrigues 已提交
144
            "Check the hardware time on guest and host", logging.info)
145
        host_time = process.system_output(get_hw_time_cmd, shell=True)
146 147 148
        guest_time = session.cmd(get_hw_time_cmd)
        drift = abs(float(host_time) - float(guest_time))
        if drift > timerdevice_drift_threshold:
149 150 151
            test.fail("The guest's hardware time is different with"
                      " host's. Host time: '%s', guest time:"
                      " '%s'" % (host_time, guest_time))
152 153 154 155

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

        session = vm.reboot()
162 163
        error_context.context("Check the system time on guest and host",
                              logging.info)
164
        (host_time, guest_time) = utils_test.get_time(session, time_command,
L
Lucas Meneghel Rodrigues 已提交
165
                                                      time_filter_re, time_format)
166 167
        drift = abs(float(host_time) - float(guest_time))
        if drift > timerdevice_drift_threshold:
168 169 170
            test.fail("The guest's system time is different with"
                      " host's. Host time: '%s', guest time:"
                      " '%s'" % (host_time, guest_time))
171 172 173

        get_hw_time_cmd = params.get("get_hw_time_cmd")
        if get_hw_time_cmd:
174
            error_context.context(
L
Lucas Meneghel Rodrigues 已提交
175
                "Check the hardware time on guest and host", logging.info)
176
            host_time = process.system_output(get_hw_time_cmd, shell=True)
177 178 179
            guest_time = session.cmd(get_hw_time_cmd)
            drift = abs(float(host_time) - float(guest_time))
            if drift > timerdevice_drift_threshold:
180 181 182
                test.fail("The guest's hardware time is different with"
                          " host's. Host time: '%s', guest time:"
                          " '%s'" % (host_time, guest_time))