live_snapshot.py 3.4 KB
Newer Older
S
Suqin Huang 已提交
1
import time, logging
Y
Yiqiao Pu 已提交
2 3 4
from autotest.client.shared import error
from virttest import utils_misc, utils_test
from tests import file_transfer
S
Suqin Huang 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

def run_live_snapshot(test, params, env):
    """
    live_snapshot test:
    1). Create live snapshot during big file creating
    2). Create live snapshot when guest reboot
    3). Check if live snapshot is created
    4). Shutdown guest

    @param test: Kvm test object
    @param params: Dictionary with the test parameters
    @param env: Dictionary with test environment.
    """

    def create_snapshot(vm):
        """
        Create live snapshot:
        1). Check which monitor is used
        2). Get device info
        3). Create snapshot
        """

        cmd = params.get("create_sn_cmd")

        block_info = vm.monitor.info("block")
Y
Yiqiao Pu 已提交
30
        if utils_misc.qemu_has_option("qmp") and params.get("monitor_type") == "qmp":
S
Suqin Huang 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
            device = block_info[0]["device"]
        else:
            string = ""
            device = string.join(block_info).split(":")[0]
        cmd += " %s" % device

        snapshot_name = params.get("snapshot_name")
        cmd += " %s" % snapshot_name

        format = params.get("snapshot_format")
        if format:
            cmd += " %s" % format
        logging.info("Creating live snapshot ...")
        vm.monitor.send_args_cmd(cmd)

        logging.info("Check snapshot is created ...")
        snapshot_info = str(vm.monitor.info("block"))
        if snapshot_name not in snapshot_info:
            logging.error(snapshot_info)
            raise error.TestFail("Snapshot doesn't exist")

    vm = env.get_vm(params["main_vm"])
    vm.verify_alive()
    timeout = int(params.get("login_timeout", 360))
    dd_timeout = int(params.get("dd_timeout", 900))
    session = vm.wait_for_login(timeout=timeout)

    def runtime_test():
        try:
            clean_cmd = params.get("clean_cmd")
            file_create = params.get("file_create")
            clean_cmd += " %s" % file_create
            logging.info("Clean file before creation")
            session.cmd(clean_cmd)

            logging.info("Creating big file...")
            create_cmd = params.get("create_cmd") % file_create

            args = (create_cmd, dd_timeout)
Y
Yiqiao Pu 已提交
70
            bg = utils_test.BackgroundTest(session.cmd_output, args)
S
Suqin Huang 已提交
71 72 73 74 75 76
            bg.start()
            time.sleep(5)
            create_snapshot(vm)
            if bg.is_alive():
                try:
                    bg.join()
Y
Yiqiao Pu 已提交
77
                except Exception:
S
Suqin Huang 已提交
78 79 80 81 82 83
                    raise
        finally:
            session.close()

    def reboot_test():
        try:
Y
Yiqiao Pu 已提交
84
            bg = utils_test.BackgroundTest(vm.reboot, (session,))
S
Suqin Huang 已提交
85 86 87 88 89 90 91 92
            logging.info("Rebooting guest ...")
            bg.start()
            sleep_time = int(params.get("sleep_time"))
            time.sleep(sleep_time)
            create_snapshot(vm)
        finally:
            bg.join()

93 94 95 96
    def file_transfer_test():
        try:
            bg_cmd = file_transfer.run_file_transfer
            args = (test, params, env)
Y
Yiqiao Pu 已提交
97
            bg = utils_test.BackgroundTest(bg_cmd, args)
98 99 100 101 102 103 104
            bg.start()
            sleep_time = int(params.get("sleep_time"))
            time.sleep(sleep_time)
            create_snapshot(vm)
            if bg.is_alive():
                try:
                    bg.join()
Y
Yiqiao Pu 已提交
105
                except Exception:
106 107 108
                    raise
        finally:
            session.close()
S
Suqin Huang 已提交
109 110
    subcommand = params.get("subcommand")
    eval("%s_test()" % subcommand)