live_snapshot_base.py 2.7 KB
Newer Older
1
import logging
2

X
Xu Han 已提交
3 4
from avocado.utils import crypto
from avocado.utils import process
5

X
Xu Han 已提交
6
from virttest import error_context
7 8 9 10
from virttest import utils_misc
from virttest import storage
from virttest import data_dir

L
Lucas Meneghel Rodrigues 已提交
11

X
Xu Han 已提交
12
@error_context.context_aware
13
def run(test, params, env):
14 15 16 17 18 19 20 21
    """
    live_snapshot_base test:
    1). Boot up guest
    2). Create a file on host and record md5
    3). Copy the file to guest
    3). Create live snapshot
    4). Copy the file from guest,then check md5

L
Lucas Meneghel Rodrigues 已提交
22 23 24
    :param test: Kvm test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
    """
    vm = env.get_vm(params["main_vm"])
    vm.verify_alive()
    timeout = int(params.get("login_timeout", 3600))
    session = vm.wait_for_login(timeout=timeout)

    dd_timeout = params.get("dd_timeoout", 600)
    copy_timeout = params.get("copy_timeoout", 600)
    base_file = storage.get_image_filename(params, data_dir.get_data_dir())
    device = vm.get_block({"file": base_file})
    snapshot_file = "images/%s" % params.get("snapshot_name")
    snapshot_file = utils_misc.get_path(data_dir.get_data_dir(), snapshot_file)
    snapshot_format = params.get("snapshot_format", "qcow2")
    tmp_name = utils_misc.generate_random_string(5)
    src = dst = "/tmp/%s" % tmp_name
    if params.get("os_type") != "linux":
        dst = "c:\\users\\public\\%s" % tmp_name

    try:
X
Xu Han 已提交
44 45
        error_context.context("create file on host, copy it to guest",
                              logging.info)
46
        cmd = params.get("dd_cmd") % src
X
Xu Han 已提交
47
        process.system(cmd, timeout=dd_timeout, shell=True)
X
Xu Han 已提交
48
        md5 = crypto.hash_file(src, algorithm="md5")
49
        vm.copy_files_to(src, dst, timeout=copy_timeout)
X
Xu Han 已提交
50 51
        process.system("rm -f %s" % src)
        error_context.context("create live snapshot", logging.info)
52 53
        if vm.live_snapshot(base_file, snapshot_file,
                            snapshot_format) != device:
X
Xu Han 已提交
54
            test.fail("Fail to create snapshot")
55 56
        backing_file = vm.monitor.get_backingfile(device)
        if backing_file != base_file:
L
Lucas Meneghel Rodrigues 已提交
57 58
            logging.error(
                "backing file: %s, base file: %s", backing_file, base_file)
X
Xu Han 已提交
59 60 61
            test.fail("Got incorrect backing file")
        error_context.context("copy file to host, check content not changed",
                              logging.info)
62
        vm.copy_files_from(dst, src, timeout=copy_timeout)
X
Xu Han 已提交
63
        if md5 and (md5 != crypto.hash_file(src, algorithm="md5")):
X
Xu Han 已提交
64
            test.fail("diff md5 before/after create snapshot")
65 66 67 68
        session.cmd(params.get("alive_check_cmd", "dir"))
    finally:
        if session:
            session.close()
X
Xu Han 已提交
69
        process.system("rm -f %s %s" % (snapshot_file, src))