drive_mirror.py 6.2 KB
Newer Older
1
import os
L
Lucas Meneghel Rodrigues 已提交
2 3
import logging
import time
4 5
from autotest.client.shared import error, utils
from virttest import utils_misc, storage, qemu_storage, nfs
X
Xu Tian 已提交
6 7
from qemu.tests import block_copy

L
Lucas Meneghel Rodrigues 已提交
8

X
Xu Tian 已提交
9
class DriveMirror(block_copy.BlockCopy):
L
Lucas Meneghel Rodrigues 已提交
10

X
Xu Tian 已提交
11 12 13 14 15 16
    """
    base class for block mirror tests;
    """

    def __init__(self, test, params, env, tag):
        super(DriveMirror, self).__init__(test, params, env, tag)
17
        self.target_image = self.get_target_image()
X
Xu Tian 已提交
18 19 20 21 22 23 24

    def parser_test_args(self):
        """
        paraser test args and set default value;
        """
        params = super(DriveMirror, self).parser_test_args()
        params["create_mode"] = params.get("create_mode", "absolute-path")
25 26
        params["target_format"] = params.get("target_format",
                                             params["image_format"])
X
Xu Tian 已提交
27 28 29
        params["reopen_timeout"] = int(params.get("reopen_timeout", 60))
        params["full_copy"] = params.get("full_copy", "").lower()
        params["check_event"] = params.get("check_event", "no").lower()
30
        if params["block_mirror_cmd"].startswith("__"):
X
Xu Tian 已提交
31 32 33
            params["full_copy"] = (params["full_copy"] == "full")
        return params

34 35 36 37 38
    def get_target_image(self):
        params = self.parser_test_args()
        t_params = {}
        t_params["image_name"] = params["target_image"]
        t_params["image_format"] = params["target_format"]
39 40 41 42 43 44 45 46 47 48 49 50 51 52
        target_image = storage.get_image_filename(t_params,
                                                  self.data_dir)
        if params.get("target_image_type") == "nfs":
            image = nfs.Nfs(params)
            image.setup()
            # sleep 30s to wait nfs ready, it's requried by some rhel6 host
            time.sleep(30)
        elif params.get("target_image_type") == "iscsi":
            image = qemu_storage.Iscsidev(params, self.data_dir, "")
            target_image = image.setup()
        if (params["create_mode"] == "existing" and
                not os.path.exists(target_image)):
            image = qemu_storage.QemuImg(t_params, self.data_dir, "")
            image.create(t_params)
53 54
        return target_image

X
Xu Tian 已提交
55 56 57 58 59 60
    @error.context_aware
    def start(self):
        """
        start block device mirroring job;
        """
        params = self.parser_test_args()
61 62 63 64 65 66 67 68 69
        target_image = self.target_image
        device = self.device
        default_speed = params["default_speed"]
        target_format = params["target_format"]
        create_mode = params["create_mode"]
        full_copy = params["full_copy"]

        error.context("Start to mirror block device", logging.info)
        self.vm.block_mirror(device, target_image, default_speed,
X
Xu Tian 已提交
70 71 72 73
                             full_copy, target_format, create_mode)
        time.sleep(0.5)
        started = self.get_status()
        if not started:
74
            raise error.TestFail("No active mirroring job found")
X
Xu Tian 已提交
75 76 77 78 79 80 81 82 83
        self.trash.append(target_image)

    @error.context_aware
    def reopen(self):
        """
        reopen target image, then check if image file of the device is
        target images;
        """
        params = self.parser_test_args()
84 85
        target_format = params["target_format"]
        timeout = params["reopen_timeout"]
X
Xu Tian 已提交
86 87

        def is_opened():
88
            device = self.vm.get_block({"file": self.target_image})
X
Xu Tian 已提交
89 90 91
            ret = (device == self.device)
            if self.vm.monitor.protocol == "qmp":
                ret &= bool(self.vm.monitor.get_event("BLOCK_JOB_COMPLETED"))
L
Lucas Meneghel Rodrigues 已提交
92
            return ret
X
Xu Tian 已提交
93 94 95

        error.context("reopen new target image", logging.info)
        if self.vm.monitor.protocol == "qmp":
96 97 98
            self.vm.monitor.clear_event("BLOCK_JOB_COMPLETED")
        self.vm.block_reopen(self.device, self.target_image, target_format)
        opened = utils_misc.wait_for(is_opened, first=3.0, timeout=timeout)
X
Xu Tian 已提交
99
        if not opened:
100
            msg = "Target image not used,wait timeout in %ss" % timeout
X
Xu Tian 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
            raise error.TestFail(msg)

    def is_steady(self):
        """
        check block device mirroring job is steady status or not;
        """
        params = self.parser_test_args()
        info = self.get_status()
        ret = (info["len"] == info["offset"])
        if self.vm.monitor.protocol == "qmp":
            check_event = params.get("check_event")
            if check_event == "yes":
                ret &= bool(self.vm.monitor.get_event("BLOCK_JOB_READY"))
        return ret

    def wait_for_steady(self):
        """
        check block device mirroring status, utils timeout; if still not go
        into steady status, raise TestFail exception;
        """
        params = self.parser_test_args()
        timeout = params.get("wait_timeout")
123 124
        if self.vm.monitor.protocol == "qmp":
            self.vm.monitor.clear_event("BLOCK_JOB_READY")
125
        steady = utils_misc.wait_for(self.is_steady, step=2.0,
L
Lucas Meneghel Rodrigues 已提交
126
                                     timeout=timeout)
X
Xu Tian 已提交
127
        if not steady:
128
            raise error.TestFail("Wait mirroring job ready "
X
Xu Tian 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
                                 "timeout in %ss" % timeout)

    def action_before_start(self):
        """
        run steps before job in steady status;
        """
        return self.do_steps("before_start")

    def action_before_steady(self):
        """
        run steps before job in steady status;
        """
        return self.do_steps("before_steady")

    def action_when_steady(self):
        """
        run steps when job in steady status;
        """
        self.wait_for_steady()
        return self.do_steps("when_steady")

    def action_after_reopen(self):
        """
        run steps after reopened new target image;
        """
        return self.do_steps("after_reopen")

156 157 158 159 160 161 162 163 164 165 166 167 168
    def clean(self):
        params = self.parser_test_args()
        if params.get("target_image_type") == "iscsi":
            image = qemu_storage.Iscsidev(params, self.data_dir, "")
            # cleanup iscsi disk to ensure it works for other test
            utils.run("dd if=/dev/zero of=%s bs=1M count=512"
                      % self.target_image)
            image.cleanup()
        elif params.get("target_image_type") == "nfs":
            image = nfs.Nfs(params)
            image.cleanup()
        super(DriveMirror, self).clean()

L
Lucas Meneghel Rodrigues 已提交
169

X
Xu Tian 已提交
170 171
def run_drive_mirror(test, params, env):
    pass