Change in vdsm[master]: tests: Unify fake execCmd mechanism

nsoffer at redhat.com nsoffer at redhat.com
Sat Sep 13 13:05:47 UTC 2014


Nir Soffer has uploaded a new change for review.

Change subject: tests: Unify fake execCmd mechanism
......................................................................

tests: Unify fake execCmd mechanism

qemuimg tests were using two mechanisms for faking utils.execCmd. The
old one was supporting faking a single command, no arguments validation,
and no way to control the retruned values of the call. This also used
textwrap module and partial functions where simpler ways can be used.

Now all tests are using the new mechanism, supporting:

- faking multiple commands, (e.g. call foo and then call bar based on
  foo return value)
- checking input arguments (e.g. was it called with -foo "bar"?)
- returning any value from the call (e.g return 0, '', '' on success,
  or return 1, '', 'error' on errors).

Sine this change requires rewriting of all the tests, I also normalized
the tests names to pep8 style, as they were very hard to read. This
could be a separate patch, but I don't think we would like to waste time
reviewing and verifying another patch for this.

Change-Id: I1b52d49047ebcced644ef39f256d8fac863a426c
Signed-off-by: Nir Soffer <nsoffer at redhat.com>
---
M tests/qemuimgTests.py
1 file changed, 73 insertions(+), 93 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/84/32884/1

diff --git a/tests/qemuimgTests.py b/tests/qemuimgTests.py
index 7a91a8b..b9b5046 100644
--- a/tests/qemuimgTests.py
+++ b/tests/qemuimgTests.py
@@ -18,102 +18,9 @@
 # Refer to the README and COPYING files for full details of the license
 #
 
-import textwrap
-from functools import partial
-
 from testlib import VdsmTestCase as TestCaseBase
-import monkeypatch
 from vdsm import qemuimg
 from vdsm import utils
-
-
-def fakeCmd(txt, *args, **kwargs):
-    txt = textwrap.dedent(txt).split('\n')
-    if txt[0] == '':
-        txt.pop(0)
-    return (0, txt, '')
-
-
-outputParseError = """
-foo bar
-"""
-
-
-outputQemu1NoBackingFile = """
-image: base.img
-file format: qcow2
-virtual size: 1.0G (1073741824 bytes)
-disk size: 196K
-cluster_size: 65536
-"""
-
-
-outputQemu1Backing = """
-image: leaf.img
-file format: qcow2
-virtual size: 1.0G (1073741824 bytes)
-disk size: 196K
-cluster_size: 65536
-backing file: base.img (actual path: /tmp/base.img)
-"""
-
-
-outputQemu2NoBackingFile = """
-image: base.img
-file format: qcow2
-virtual size: 1.0G (1073741824 bytes)
-disk size: 196K
-cluster_size: 65536
-Format specific information:
-    compat: 1.1
-    lazy refcounts: false
-"""
-
-
-outputQemu2BackingNoCluster = """
-image: leaf.img
-file format: qcow2
-virtual size: 1.0G (1073741824 bytes)
-disk size: 196K
-backing file: base.img (actual path: /tmp/base.img)
-Format specific information:
-    compat: 1.1
-    lazy refcounts: false
-"""
-
-
-class qemuimgTests(TestCaseBase):
-    @monkeypatch.MonkeyPatch(utils, 'execCmd',
-                             partial(fakeCmd, outputParseError))
-    def testParseError(self):
-        self.assertRaises(qemuimg.QImgError, qemuimg.info, 'unused')
-
-    @monkeypatch.MonkeyPatch(utils, 'execCmd',
-                             partial(fakeCmd, outputQemu1NoBackingFile))
-    def testQemu1NoBackingFile(self):
-        info = qemuimg.info('unused')
-        self.assertNotIn('backingfile', info)
-
-    @monkeypatch.MonkeyPatch(utils, 'execCmd',
-                             partial(fakeCmd, outputQemu1Backing))
-    def testQemu1Backing(self):
-        info = qemuimg.info('unused')
-        self.assertEquals('base.img', info['backingfile'])
-
-    @monkeypatch.MonkeyPatch(utils, 'execCmd',
-                             partial(fakeCmd, outputQemu2NoBackingFile))
-    def testQemu2NoBackingFile(self):
-        info = qemuimg.info('unused')
-        self.assertEquals('qcow2', info['format'])
-        self.assertEquals(1073741824, info['virtualsize'])
-        self.assertEquals(65536, info['clustersize'])
-        self.assertNotIn('backingfile', info)
-
-    @monkeypatch.MonkeyPatch(utils, 'execCmd',
-                             partial(fakeCmd, outputQemu2BackingNoCluster))
-    def testQemu2BackingNoCluster(self):
-        info = qemuimg.info('unused')
-        self.assertEquals('base.img', info['backingfile'])
 
 
 class FakeExecCmd(object):
@@ -134,6 +41,79 @@
         utils.execCmd = self.saved
 
 
+class QemuimgTests(TestCaseBase):
+
+    def test_parse_error(self):
+        def call(cmd, **kw):
+            out = ["image: leaf.img", "invalid file format line"]
+            return 0, out, []
+
+        with FakeExecCmd(call):
+            self.assertRaises(qemuimg.QImgError, qemuimg.info, 'leaf.img')
+
+    def test_qemu1_no_backing_file(self):
+        def call(cmd, **kw):
+            out = ["image: leaf.img",
+                   "file format: qcow2",
+                   "virtual size: 1.0G (1073741824 bytes)",
+                   "disk size: 196K",
+                   "cluster_size: 65536"]
+            return 0, out, []
+
+        with FakeExecCmd(call):
+            info = qemuimg.info('leaf.img')
+            self.assertNotIn('backingfile', info)
+
+    def test_qemu1_backing(self):
+        def call(cmd, **kw):
+            out = ["image: leaf.img",
+                   "file format: qcow2",
+                   "virtual size: 1.0G (1073741824 bytes)",
+                   "disk size: 196K",
+                   "cluster_size: 65536",
+                   "backing file: base.img (actual path: /tmp/base.img)"]
+            return 0, out, []
+
+        with FakeExecCmd(call):
+            info = qemuimg.info('leaf.img')
+            self.assertEquals('base.img', info['backingfile'])
+
+    def test_qemu2_no_backing_file(self):
+        def call(cmd, **kw):
+            out = ["image: leaf.img",
+                   "file format: qcow2",
+                   "virtual size: 1.0G (1073741824 bytes)",
+                   "disk size: 196K",
+                   "cluster_size: 65536",
+                   "Format specific information:",
+                   "    compat: 1.1",
+                   "    lazy refcounts: false"]
+            return 0, out, []
+
+        with FakeExecCmd(call):
+            info = qemuimg.info('leaf.img')
+            self.assertEquals('qcow2', info['format'])
+            self.assertEquals(1073741824, info['virtualsize'])
+            self.assertEquals(65536, info['clustersize'])
+            self.assertNotIn('backingfile', info)
+
+    def test_qemu2_backing_no_cluster(self):
+        def call(cmd, **kw):
+            out = ["image: leaf.img",
+                   "file format: qcow2",
+                   "virtual size: 1.0G (1073741824 bytes)",
+                   "disk size: 196K",
+                   "backing file: base.img (actual path: /tmp/base.img)",
+                   "Format specific information:",
+                   "    compat: 1.1",
+                   "    lazy refcounts: false"]
+            return 0, out, []
+
+        with FakeExecCmd(call):
+            info = qemuimg.info('leaf.img')
+            self.assertEquals('base.img', info['backingfile'])
+
+
 class QemuimgCreateTests(TestCaseBase):
 
     def test_no_format(self):


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I1b52d49047ebcced644ef39f256d8fac863a426c
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