qemu_img_map_unaligned_image.py 2.2 KB
Newer Older
1 2 3 4 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 30 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
import logging
import random
import json
import string

from avocado.utils import process
from virttest import data_dir
from virttest.qemu_storage import QemuImg


def run(test, params, env):
    """
    qemu-img map an unaligned image.

    1.create a raw file using truncate
    2.write data into the raw file
    3.verify the dumped mete-data using qemu-img map

    :param test: Qemu test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment
    """
    def _generate_random_string(max_len=19):
        """Generate random alphabets string in the range of [1, max_len+1]."""
        random_str = ''.join(random.choice(
            string.ascii_lowercase) for _ in range(random.randint(1, max_len)))
        return random_str, len(random_str)

    def _verify_qemu_img_map(output, str_len):
        """Verify qemu-img map's output."""
        logging.info("Verify the dumped mete-data of the unaligned image.")
        expected = [{"start": 0, "length": str_len, "depth": 0,
                    "zero": False, "data": True, "offset": 0},
                    {"start": str_len, "length": 512 - (str_len % 512),
                     "depth": 0, "zero": True, "data": False,
                     "offset": str_len}]
        res = json.loads(output)
        if res != expected:
            test.fail("The dumped mete-data of the unaligned "
                      "image '%s' is not correct." % img.image_filename)

    img_param = params.object_params("test")
    img = QemuImg(img_param, data_dir.get_data_dir(), "test")

    logging.info("Create a new file %s using truncate." % img.image_filename)
    process.run("rm -f %s" % img.image_filename)
    process.run("truncate -s 1G %s " % img.image_filename)

    random_str, str_len = _generate_random_string()
    logging.info("Write '%s' into the file %s." % (random_str,
                                                   img.image_filename))
    process.run("echo -n '%s' > %s" % (random_str, img.image_filename),
                shell=True)
    res = img.map(output="json")
    if res.exit_status != 0:
        test.fail("qemu-img map error: %s." % res.stderr_text)
    _verify_qemu_img_map(res.stdout_text, str_len)