diff --git a/qemu/tests/cfg/qemu_img_map_unaligned_image.cfg b/qemu/tests/cfg/qemu_img_map_unaligned_image.cfg new file mode 100644 index 0000000000000000000000000000000000000000..cca38cd4b0c2ae439e64aa4923c6035e1ccb0b00 --- /dev/null +++ b/qemu/tests/cfg/qemu_img_map_unaligned_image.cfg @@ -0,0 +1,10 @@ +- qemu_img_map_unaligned_image: + only raw + virt_test_type = qemu + type = qemu_img_map_unaligned_image + kill_vm = yes + start_vm = no + images = "test" + image_size_test = 1G + image_name_test = images/unaligned_image + image_format_test = raw diff --git a/qemu/tests/qemu_img_map_unaligned_image.py b/qemu/tests/qemu_img_map_unaligned_image.py new file mode 100644 index 0000000000000000000000000000000000000000..ba9380f012284bbc9e2e6655808f640843ec2592 --- /dev/null +++ b/qemu/tests/qemu_img_map_unaligned_image.py @@ -0,0 +1,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)