1import logging, time, shutil
2from autotest_lib.client.common_lib import error
3from autotest_lib.client.virt import virt_utils
4
5
6def run_watchdog(test, params, env):
7    """
8    Configure watchdog, crash the guest and check if watchdog_action occurs.
9    @param test: kvm test object.
10    @param params: Dictionary with test parameters.
11    @param env: Dictionary with the test environment.
12    """
13    vm = env.get_vm(params["main_vm"])
14    vm.verify_alive()
15    timeout = int(params.get("login_timeout", 360))
16    session = vm.wait_for_login(timeout=timeout)
17    relogin_timeout = int(params.get("relogin_timeout", 240))
18    watchdog_enable_cmd = "chkconfig watchdog on && service watchdog start"
19
20    def watchdog_action_reset():
21        """
22        Trigger a crash dump through sysrq-trigger
23        Ensure watchdog_action(reset) occur.
24        """
25        session = vm.wait_for_login(timeout=timeout)
26
27        logging.info("Triggering crash on vm")
28        crash_cmd = "echo c > /proc/sysrq-trigger"
29        session.sendline(crash_cmd)
30
31        if not virt_utils.wait_for(lambda: not session.is_responsive(),
32                                   240, 0, 1):
33            raise error.TestFail("Could not trigger crash")
34
35        logging.info("Waiting for kernel watchdog_action to take place")
36        session = vm.wait_for_login(timeout=relogin_timeout)
37
38    logging.info("Enabling watchdog service...")
39    session.cmd(watchdog_enable_cmd, timeout=320)
40    watchdog_action_reset()
41
42    # Close stablished session
43    session.close()
44