Nir Soffer has uploaded a new change for review.
Change subject: qemuimg: Simplify and complete map tests ......................................................................
qemuimg: Simplify and complete map tests
Trying to understand map output: - Use pprint to prety print the map - Separte the single volume tests to make it easier to understand the map - Start to add assertions about the single volume cases.
To see the test output run with the -s flag:
./run_tests_local.sh qemuimg_test.py:TestMap -s
This should be squashed into the previous patch.
Change-Id: Ie765a13167ebd6f92c2125e6e634f227606854bb Signed-off-by: Nir Soffer nsoffer@redhat.com --- M lib/vdsm/qemuimg.py M tests/qemuimg_test.py 2 files changed, 86 insertions(+), 16 deletions(-)
git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/51/65151/1
diff --git a/lib/vdsm/qemuimg.py b/lib/vdsm/qemuimg.py index a6e89d8..9692cca 100644 --- a/lib/vdsm/qemuimg.py +++ b/lib/vdsm/qemuimg.py @@ -211,16 +211,14 @@
def map(image): cmd = [_qemuimg.cmd, "map", "--output", "json", image] - # For simplicity, we always run commit in the image directory. rc, out, err = commands.execCmd(cmd, raw=True) if rc != 0: raise QImgError(rc, out, err)
try: - print "out: %s" % out + return json.loads(out) except ValueError: raise QImgError(rc, out, err, "Failed to process qemuimg map output") -
class QemuImgOperation(object): diff --git a/tests/qemuimg_test.py b/tests/qemuimg_test.py index f00b830..56c3abb 100644 --- a/tests/qemuimg_test.py +++ b/tests/qemuimg_test.py @@ -20,6 +20,7 @@
import json import os +import pprint from functools import partial
from monkeypatch import MonkeyPatch, MonkeyPatchScope @@ -440,20 +441,87 @@ @expandPermutations class TestMap(TestCaseBase):
- @permutations([ - # format, size - (qemuimg.FORMAT.RAW, 0), - (qemuimg.FORMAT.QCOW2, 0), - (qemuimg.FORMAT.RAW, 1048576), - (qemuimg.FORMAT.QCOW2, 1048576) - ]) - def test_map_single_volume(self, format, size): + def test_raw_empty(self): + with namedTemporaryDir() as tmpdir: + size = 1048576 + image = os.path.join(tmpdir, "base.img") + qemuimg.create(image, size=size, format=qemuimg.FORMAT.RAW) + img_map = qemuimg.map(image) + print + pprint.pprint(img_map, indent=4) + + self.assertEqual(1, len(img_map)) + + # Empty run + run = img_map[0] + self.assertEqual(run["length"], size) + self.assertFalse(run["data"]) + self.assertTrue(run["zero"]) + + def test_raw_one_block(self): with namedTemporaryDir() as tmpdir: image = os.path.join(tmpdir, "base.img") - make_image(image, size, format, 0, "1.1") - map = qemuimg.map(image) + qemuimg.create(image, size=1048576, format=qemuimg.FORMAT.RAW) + qemu_pattern_write(image, qemuimg.FORMAT.RAW, offset="64k", + len='4k', pattern=0xf0) + img_map = qemuimg.map(image) + print + pprint.pprint(img_map, indent=4)
- def test_map_chain(self): + self.assertEqual(3, len(img_map)) + + # Empty run + run = img_map[0] + self.assertEqual(run["length"], 64*1024) + self.assertFalse(run["data"]) + self.assertTrue(run["zero"]) + + # Data run + run = img_map[1] + self.assertEqual(run["length"], 4*1024) + self.assertTrue(run["data"]) + self.assertFalse(run["zero"]) + + # Empty run + run = img_map[2] + self.assertEqual(run["length"], (1024-68)*1024) + self.assertFalse(run["data"]) + self.assertTrue(run["zero"]) + + @permutations([("0.10",), ("1.1",)]) + def test_qcow2_empty(self, qcow2_compat): + with namedTemporaryDir() as tmpdir: + size = 1048576 + image = os.path.join(tmpdir, "base.img") + qemuimg.create(image, size=size, format=qemuimg.FORMAT.QCOW2, + qcow2Compat=qcow2_compat) + img_map = qemuimg.map(image) + print + pprint.pprint(img_map, indent=4) + + self.assertEqual(1, len(img_map)) + + # Empty run + run = img_map[0] + self.assertEqual(run["length"], size) + self.assertFalse(run["data"]) + self.assertTrue(run["zero"]) + + + @permutations([("0.10",), ("1.1",)]) + def test_qcow2_one_block(self, qcow2_compat): + with namedTemporaryDir() as tmpdir: + image = os.path.join(tmpdir, "base.img") + qemuimg.create(image, size=1048576, format=qemuimg.FORMAT.QCOW2, + qcow2Compat=qcow2_compat) + qemu_pattern_write(image, qemuimg.FORMAT.RAW, offset="64k", + len='32k', pattern=0xf0) + img_map = qemuimg.map(image) + print + pprint.pprint(img_map, indent=4) + + @permutations([("0.10",), ("1.1",)]) + def test_chain(self, qcow2_compat): with namedTemporaryDir() as tmpdir: parent = None chain = [] @@ -461,11 +529,15 @@ vol = os.path.join(tmpdir, "vol%d.img" % i) format = (qemuimg.FORMAT.RAW if i == 0 else qemuimg.FORMAT.QCOW2) - make_image(vol, 1048576, format, i, "1.1", parent) + make_image(vol, 1048576, format, i, qcow2_compat, parent) chain.append(vol) parent = vol
- map = qemuimg.map(chain[0]) + print + for vol in reversed(chain): + print os.path.basename(vol) + img_map = qemuimg.map(vol) + pprint.pprint(img_map, indent=4)
def make_image(path, size, format, index, qcow2_compat, backing=None):