1import logging, time, types
2from autotest_lib.client.common_lib import error
3from autotest_lib.client.virt import virt_utils
4
5
6def run_migration(test, params, env):
7    """
8    KVM migration test:
9    1) Get a live VM and clone it.
10    2) Verify that the source VM supports migration.  If it does, proceed with
11            the test.
12    3) Send a migration command to the source VM and wait until it's finished.
13    4) Kill off the source VM.
14    3) Log into the destination VM after the migration is finished.
15    4) Compare the output of a reference command executed on the source with
16            the output of the same command on the destination machine.
17
18    @param test: kvm test object.
19    @param params: Dictionary with test parameters.
20    @param env: Dictionary with the test environment.
21    """
22    def get_functions(func_names, locals_dict):
23        """
24        Find sub function(s) in this function with the given name(s).
25        """
26        if not func_names:
27            return []
28        funcs = []
29        for f in func_names.split():
30            f = locals_dict.get(f)
31            if isinstance(f, types.FunctionType):
32                funcs.append(f)
33        return funcs
34
35    def mig_set_speed():
36        mig_speed = params.get("mig_speed", "1G")
37        return vm.monitor.migrate_set_speed(mig_speed)
38
39    vm = env.get_vm(params["main_vm"])
40    vm.verify_alive()
41    timeout = int(params.get("login_timeout", 360))
42    session = vm.wait_for_login(timeout=timeout)
43
44    mig_timeout = float(params.get("mig_timeout", "3600"))
45    mig_protocol = params.get("migration_protocol", "tcp")
46    mig_cancel_delay = int(params.get("mig_cancel") == "yes") * 2
47    offline = params.get("offline", "no") == "yes"
48    check = params.get("vmstate_check", "no") == "yes"
49
50    # Get the output of migration_test_command
51    test_command = params.get("migration_test_command")
52    reference_output = session.cmd_output(test_command)
53
54    # Start some process in the background (and leave the session open)
55    background_command = params.get("migration_bg_command", "")
56    session.sendline(background_command)
57    time.sleep(5)
58
59    # Start another session with the guest and make sure the background
60    # process is running
61    session2 = vm.wait_for_login(timeout=timeout)
62
63    try:
64        check_command = params.get("migration_bg_check_command", "")
65        session2.cmd(check_command, timeout=30)
66        session2.close()
67
68        # run some functions before migrate start.
69        pre_migrate = get_functions(params.get("pre_migrate"), locals())
70        for func in pre_migrate:
71            func()
72
73        # Migrate the VM
74        vm.migrate(mig_timeout, mig_protocol, mig_cancel_delay, offline, check)
75
76        # run some functions after migrate finish.
77        post_migrate = get_functions(params.get("post_migrate"), locals())
78        for func in post_migrate:
79            func()
80
81        # Log into the guest again
82        logging.info("Logging into guest after migration...")
83        session2 = vm.wait_for_login(timeout=30)
84        logging.info("Logged in after migration")
85
86        # Make sure the background process is still running
87        session2.cmd(check_command, timeout=30)
88
89        # Get the output of migration_test_command
90        output = session2.cmd_output(test_command)
91
92        # Compare output to reference output
93        if output != reference_output:
94            logging.info("Command output before migration differs from "
95                         "command output after migration")
96            logging.info("Command: %s", test_command)
97            logging.info("Output before:" +
98                         virt_utils.format_str_for_message(reference_output))
99            logging.info("Output after:" +
100                         virt_utils.format_str_for_message(output))
101            raise error.TestFail("Command '%s' produced different output "
102                                 "before and after migration" % test_command)
103
104    finally:
105        # Kill the background process
106        if session2 and session2.is_alive():
107            session2.cmd_output(params.get("migration_bg_kill_command", ""))
108
109    session2.close()
110    session.close()
111