Change in vdsm[master]: tests: Add tests for qcow2 comapt option when converting images

nsoffer at redhat.com nsoffer at redhat.com
Tue Sep 23 23:56:16 UTC 2014


Nir Soffer has uploaded a new change for review.

Change subject: tests: Add tests for qcow2 comapt option when converting images
......................................................................

tests: Add tests for qcow2 comapt option when converting images

Since qemuimg.convert() uses utils.watchCmd instead of utils.execCmd,
FakeExecCmd was modified so it can patch both functions.

Since both create and convert tests need the qcow2_compat_xxx functions,
they were moved up to a new ImageBase test class.

This patch is separate from the actual fix, to allow easy backporting of
the actual fix, without backporing the testing infrastructure, which is
missing in older versions.

Change-Id: I9530a1653e73f509b4836621a8510148b72a942c
Relates-To: https://bugzilla.redhat.com/1139707
Signed-off-by: Nir Soffer <nsoffer at redhat.com>
---
M tests/qemuimgTests.py
1 file changed, 76 insertions(+), 37 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/05/33305/1

diff --git a/tests/qemuimgTests.py b/tests/qemuimgTests.py
index b9b5046..8fa7d4a 100644
--- a/tests/qemuimgTests.py
+++ b/tests/qemuimgTests.py
@@ -21,11 +21,15 @@
 from testlib import VdsmTestCase as TestCaseBase
 from vdsm import qemuimg
 from vdsm import utils
+import monkeypatch
+
+QEMU_IMG = qemuimg._qemuimg.cmd
 
 
-class FakeExecCmd(object):
+class FakeCmd(object):
 
-    def __init__(self, *calls):
+    def __init__(self, module, name, *calls):
+        self.patch = monkeypatch.Patch([(module, name, self)])
         self.calls = list(calls)
         self.saved = None
 
@@ -34,21 +38,20 @@
         return call(cmd, **kw)
 
     def __enter__(self):
-        self.saved = utils.execCmd
-        utils.execCmd = self
+        self.patch.apply()
 
     def __exit__(self, t=None, v=None, tb=None):
-        utils.execCmd = self.saved
+        self.patch.revert()
 
 
-class QemuimgTests(TestCaseBase):
+class InfoTests(TestCaseBase):
 
     def test_parse_error(self):
         def call(cmd, **kw):
             out = ["image: leaf.img", "invalid file format line"]
             return 0, out, []
 
-        with FakeExecCmd(call):
+        with FakeCmd(utils, 'execCmd', call):
             self.assertRaises(qemuimg.QImgError, qemuimg.info, 'leaf.img')
 
     def test_qemu1_no_backing_file(self):
@@ -60,7 +63,7 @@
                    "cluster_size: 65536"]
             return 0, out, []
 
-        with FakeExecCmd(call):
+        with FakeCmd(utils, 'execCmd', call):
             info = qemuimg.info('leaf.img')
             self.assertNotIn('backingfile', info)
 
@@ -74,7 +77,7 @@
                    "backing file: base.img (actual path: /tmp/base.img)"]
             return 0, out, []
 
-        with FakeExecCmd(call):
+        with FakeCmd(utils, 'execCmd', call):
             info = qemuimg.info('leaf.img')
             self.assertEquals('base.img', info['backingfile'])
 
@@ -90,7 +93,7 @@
                    "    lazy refcounts: false"]
             return 0, out, []
 
-        with FakeExecCmd(call):
+        with FakeCmd(utils, 'execCmd', call):
             info = qemuimg.info('leaf.img')
             self.assertEquals('qcow2', info['format'])
             self.assertEquals(1073741824, info['virtualsize'])
@@ -109,48 +112,84 @@
                    "    lazy refcounts: false"]
             return 0, out, []
 
-        with FakeExecCmd(call):
+        with FakeCmd(utils, 'execCmd', call):
             info = qemuimg.info('leaf.img')
             self.assertEquals('base.img', info['backingfile'])
 
 
-class QemuimgCreateTests(TestCaseBase):
+class ImageBase(TestCaseBase):
+
+    def qcow2_compat_unsupported(self, cmd, **kw):
+        expected = [QEMU_IMG, 'create', '-f', 'qcow2', '-o', '?', '/dev/null']
+        self.assertEqual(cmd, expected)
+        return 0, 'Supported options:\nsize ...\n', ''
+
+    def qcow2_compat_supported(self, cmd, **kw):
+        expected = [QEMU_IMG, 'create', '-f', 'qcow2', '-o', '?', '/dev/null']
+        self.assertEqual(cmd, expected)
+        return 0, 'Supported options:\ncompat ...\n', ''
+
+
+class CreateTests(ImageBase):
 
     def test_no_format(self):
-
-        def create_no_format(cmd, **kw):
-            assert cmd == [qemuimg._qemuimg.cmd, 'create', 'image']
+        def create(cmd, **kw):
+            expected = [QEMU_IMG, 'create', 'image']
+            self.assertEqual(cmd, expected)
             return 0, '', ''
 
-        with FakeExecCmd(create_no_format):
+        with FakeCmd(utils, 'execCmd', create):
             qemuimg.create('image')
 
-    def test_qcow2_compat_not_supported(self):
-
-        def qcow2_compat_not_supported(cmd, **kw):
-            assert cmd == [qemuimg._qemuimg.cmd, 'create', '-f', 'qcow2', '-o',
-                           '?', '/dev/null']
-            return 0, 'Supported options:\nsize ...\n', ''
-
-        def create_qcow2_no_compat(cmd, **kw):
-            assert cmd == [qemuimg._qemuimg.cmd, 'create', '-f', 'qcow2',
-                           'image']
+    def test_qcow2_compat_unsupported(self):
+        def create(cmd, **kw):
+            expected = [QEMU_IMG, 'create', '-f', 'qcow2', 'image']
+            self.assertEqual(cmd, expected)
             return 0, '', ''
 
-        with FakeExecCmd(qcow2_compat_not_supported, create_qcow2_no_compat):
+        with FakeCmd(utils, 'execCmd', self.qcow2_compat_unsupported, create):
             qemuimg.create('image', format='qcow2')
 
     def test_qcow2_compat_supported(self):
-
-        def qcow2_compat_supported(cmd, **kw):
-            assert cmd == [qemuimg._qemuimg.cmd, 'create', '-f', 'qcow2', '-o',
-                           '?', '/dev/null']
-            return 0, 'Supported options:\ncompat ...\n', ''
-
-        def create_qcow2_compat(cmd, **kw):
-            assert cmd == [qemuimg._qemuimg.cmd, 'create', '-f', 'qcow2', '-o',
-                           'compat=0.10', 'image']
+        def create(cmd, **kw):
+            expected = [QEMU_IMG, 'create', '-f', 'qcow2', '-o', 'compat=0.10',
+                        'image']
+            self.assertEqual(cmd, expected)
             return 0, '', ''
 
-        with FakeExecCmd(qcow2_compat_supported, create_qcow2_compat):
+        with FakeCmd(utils, 'execCmd', self.qcow2_compat_supported, create):
             qemuimg.create('image', format='qcow2')
+
+
+class ConvertTests(ImageBase):
+
+    def test_no_format(self):
+        def convert(cmd, **kw):
+            expected = [QEMU_IMG, 'convert', '-t', 'none', 'src', 'dst']
+            self.assertEqual(cmd, expected)
+            return 0, '', ''
+
+        with FakeCmd(utils, 'watchCmd', convert):
+            qemuimg.convert('src', 'dst', True)
+
+    def test_qcow2_compat_unsupported(self):
+        def convert(cmd, **kw):
+            expected = [QEMU_IMG, 'convert', '-t', 'none', 'src', '-O',
+                        'qcow2', 'dst']
+            self.assertEqual(cmd, expected)
+            return 0, '', ''
+
+        with FakeCmd(utils, 'execCmd', self.qcow2_compat_unsupported):
+            with FakeCmd(utils, 'watchCmd', convert):
+                qemuimg.convert('src', 'dst', True, dstFormat='qcow2')
+
+    def test_qcow2_compat_supported(self):
+        def convert(cmd, **kw):
+            expected = [QEMU_IMG, 'convert', '-t', 'none', 'src', '-O',
+                        'qcow2', '-o', 'compat=0.10', 'dst']
+            self.assertEqual(cmd, expected)
+            return 0, '', ''
+
+        with FakeCmd(utils, 'execCmd', self.qcow2_compat_supported):
+            with FakeCmd(utils, 'watchCmd', convert):
+                qemuimg.convert('src', 'dst', True, dstFormat='qcow2')


-- 
To view, visit http://gerrit.ovirt.org/33305
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I9530a1653e73f509b4836621a8510148b72a942c
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Nir Soffer <nsoffer at redhat.com>


More information about the vdsm-patches mailing list