live_snapshot.py 3.3 KB
Newer Older
S
Suqin Huang 已提交
1
import time, logging
Y
Yiqiao Pu 已提交
2
from autotest.client.shared import error
X
Xu Tian 已提交
3
from virttest import utils_test
Y
Yiqiao Pu 已提交
4
from tests import file_transfer
S
Suqin Huang 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18

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.
    """

X
Xu Tian 已提交
19
    @error.context_aware
S
Suqin Huang 已提交
20 21 22 23 24 25 26
    def create_snapshot(vm):
        """
        Create live snapshot:
        1). Check which monitor is used
        2). Get device info
        3). Create snapshot
        """
X
Xu Tian 已提交
27
        error.context("Creating live snapshot ...", logging.info)
S
Suqin Huang 已提交
28
        block_info = vm.monitor.info("block")
29
        if vm.monitor.protocol == 'qmp':
S
Suqin Huang 已提交
30 31
            device = block_info[0]["device"]
        else:
X
Xu Tian 已提交
32
            device = "".join(block_info).split(":")[0]
S
Suqin Huang 已提交
33
        snapshot_name = params.get("snapshot_name")
X
Xu Tian 已提交
34 35
        format = params.get("snapshot_format", "qcow2")
        vm.monitor.live_snapshot(device, snapshot_name, format)
S
Suqin Huang 已提交
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

        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 已提交
61
            bg = utils_test.BackgroundTest(session.cmd_output, args)
S
Suqin Huang 已提交
62 63 64 65 66 67
            bg.start()
            time.sleep(5)
            create_snapshot(vm)
            if bg.is_alive():
                try:
                    bg.join()
Y
Yiqiao Pu 已提交
68
                except Exception:
S
Suqin Huang 已提交
69 70 71 72 73 74
                    raise
        finally:
            session.close()

    def reboot_test():
        try:
Y
Yiqiao Pu 已提交
75
            bg = utils_test.BackgroundTest(vm.reboot, (session,))
S
Suqin Huang 已提交
76 77 78 79 80 81 82 83
            logging.info("Rebooting guest ...")
            bg.start()
            sleep_time = int(params.get("sleep_time"))
            time.sleep(sleep_time)
            create_snapshot(vm)
        finally:
            bg.join()

84 85 86 87
    def file_transfer_test():
        try:
            bg_cmd = file_transfer.run_file_transfer
            args = (test, params, env)
Y
Yiqiao Pu 已提交
88
            bg = utils_test.BackgroundTest(bg_cmd, args)
89 90 91 92 93 94 95
            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 已提交
96
                except Exception:
97 98 99
                    raise
        finally:
            session.close()
S
Suqin Huang 已提交
100 101
    subcommand = params.get("subcommand")
    eval("%s_test()" % subcommand)