1import logging, time
2from autotest_lib.client.common_lib import error
3from autotest_lib.client.virt import virt_utils
4
5
6@error.context_aware
7def run_guest_s4(test, params, env):
8    """
9    Suspend guest to disk, supports both Linux & Windows OSes.
10
11    @param test: kvm test object.
12    @param params: Dictionary with test parameters.
13    @param env: Dictionary with the test environment.
14    """
15    error.base_context("before S4")
16    vm = env.get_vm(params["main_vm"])
17    vm.verify_alive()
18    timeout = int(params.get("login_timeout", 360))
19    session = vm.wait_for_login(timeout=timeout)
20
21    error.context("checking whether guest OS supports S4", logging.info)
22    session.cmd(params.get("check_s4_support_cmd"))
23    error.context()
24
25    logging.info("Waiting until all guest OS services are fully started...")
26    time.sleep(float(params.get("services_up_timeout", 30)))
27
28    # Start up a program (tcpdump for linux & ping for Windows), as a flag.
29    # If the program died after suspend, then fails this testcase.
30    test_s4_cmd = params.get("test_s4_cmd")
31    session.sendline(test_s4_cmd)
32    time.sleep(5)
33
34    # Get the second session to start S4
35    session2 = vm.wait_for_login(timeout=timeout)
36
37    # Make sure the background program is running as expected
38    error.context("making sure background program is running")
39    check_s4_cmd = params.get("check_s4_cmd")
40    session2.cmd(check_s4_cmd)
41    logging.info("Launched background command in guest: %s", test_s4_cmd)
42    error.context()
43    error.base_context()
44
45    # Suspend to disk
46    logging.info("Starting suspend to disk now...")
47    session2.sendline(params.get("set_s4_cmd"))
48
49    # Make sure the VM goes down
50    error.base_context("after S4")
51    suspend_timeout = 240 + int(params.get("smp")) * 60
52    if not virt_utils.wait_for(vm.is_dead, suspend_timeout, 2, 2):
53        raise error.TestFail("VM refuses to go down. Suspend failed.")
54    logging.info("VM suspended successfully. Sleeping for a while before "
55                 "resuming it.")
56    time.sleep(10)
57
58    # Start vm, and check whether the program is still running
59    logging.info("Resuming suspended VM...")
60    vm.create()
61
62    # Log into the resumed VM
63    relogin_timeout = int(params.get("relogin_timeout", 240))
64    logging.info("Logging into resumed VM, timeout %s", relogin_timeout)
65    session2 = vm.wait_for_login(timeout=relogin_timeout)
66
67    # Check whether the test command is still alive
68    error.context("making sure background program is still running",
69                  logging.info)
70    session2.cmd(check_s4_cmd)
71    error.context()
72
73    logging.info("VM resumed successfuly after suspend to disk")
74    session2.cmd_output(params.get("kill_test_s4_cmd"))
75    session.close()
76    session2.close()
77