1import os, logging
2from autotest_lib.client.virt import virt_utils
3
4
5def run_guest_test(test, params, env):
6    """
7    A wrapper for running customized tests in guests.
8
9    1) Log into a guest.
10    2) Run script.
11    3) Wait for script execution to complete.
12    4) Pass/fail according to exit status of script.
13
14    @param test: KVM test object.
15    @param params: Dictionary with test parameters.
16    @param env: Dictionary with the test environment.
17    """
18    login_timeout = int(params.get("login_timeout", 360))
19    reboot = params.get("reboot", "no")
20
21    vm = env.get_vm(params["main_vm"])
22    vm.verify_alive()
23    if params.get("serial_login") == "yes":
24        session = vm.wait_for_serial_login(timeout=login_timeout)
25    else:
26        session = vm.wait_for_login(timeout=login_timeout)
27
28    if reboot == "yes":
29        logging.debug("Rebooting guest before test ...")
30        session = vm.reboot(session, timeout=login_timeout)
31
32    try:
33        logging.info("Starting script...")
34
35        # Collect test parameters
36        interpreter = params.get("interpreter")
37        script = params.get("guest_script")
38        dst_rsc_path = params.get("dst_rsc_path", "script.au3")
39        script_params = params.get("script_params", "")
40        test_timeout = float(params.get("test_timeout", 600))
41
42        logging.debug("Starting preparing resouce files...")
43        # Download the script resource from a remote server, or
44        # prepare the script using rss?
45        if params.get("download") == "yes":
46            download_cmd = params.get("download_cmd")
47            rsc_server = params.get("rsc_server")
48            rsc_dir = os.path.basename(rsc_server)
49            dst_rsc_dir = params.get("dst_rsc_dir")
50
51            # Change dir to dst_rsc_dir, and remove the guest script dir there
52            rm_cmd = "cd %s && (rmdir /s /q %s || del /s /q %s)" % \
53                     (dst_rsc_dir, rsc_dir, rsc_dir)
54            session.cmd(rm_cmd, timeout=test_timeout)
55            logging.debug("Clean directory succeeded.")
56
57            # then download the resource.
58            rsc_cmd = "cd %s && %s %s" % (dst_rsc_dir, download_cmd, rsc_server)
59            session.cmd(rsc_cmd, timeout=test_timeout)
60            logging.info("Download resource finished.")
61        else:
62            session.cmd_output("del %s" % dst_rsc_path, internal_timeout=0)
63            script_path = virt_utils.get_path(test.bindir, script)
64            vm.copy_files_to(script_path, dst_rsc_path, timeout=60)
65
66        cmd = "%s %s %s" % (interpreter, dst_rsc_path, script_params)
67
68        try:
69            logging.info("------------ Script output ------------")
70            session.cmd(cmd, print_func=logging.info, timeout=test_timeout)
71        finally:
72            logging.info("------------ End of script output ------------")
73
74        if reboot == "yes":
75            logging.debug("Rebooting guest after test ...")
76            session = vm.reboot(session, timeout=login_timeout)
77
78        logging.debug("guest test PASSED.")
79    finally:
80        session.close()
81