Change in vdsm[master]: tests: Add miniaml issciadm tests
nsoffer at redhat.com
nsoffer at redhat.com
Sun Oct 20 20:23:31 UTC 2013
Nir Soffer has uploaded a new change for review.
Change subject: tests: Add miniaml issciadm tests
......................................................................
tests: Add miniaml issciadm tests
Add miniaml tests for iscsiadm module before modifying it to support
debug output (BZ 1011075).
These tests do not test the iscsiadm command line tool; they ensure that
we invoke it correctly and handle its exit code, stdout and stderr.
This patch introduce a new fake module useful for faking dependencies
during unittests. I could add it in another patch, but I don't want to
create unneeded dependencies.
Change-Id: I9ed21408c7b496c384053ce02cdb66d47df9a14b
Signed-off-by: Nir Soffer <nsoffer at redhat.com>
---
M tests/Makefile.am
A tests/fake.py
A tests/fakeTests.py
A tests/iscsiadmTests.py
4 files changed, 208 insertions(+), 0 deletions(-)
git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/56/19856/8
diff --git a/tests/Makefile.am b/tests/Makefile.am
index cd0cb1d..e6d7323 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -28,6 +28,7 @@
betterPopenTests.py \
capsTests.py \
configNetworkTests.py \
+ fakeTests.py \
fileUtilTests.py \
fuserTests.py \
getAllVolumesTests.py \
@@ -36,6 +37,7 @@
guestIFTests.py \
hooksTests.py \
ipwrapperTests.py \
+ iscsiadmTests.py \
jsonRpcTests.py \
jsonRpcUtils.py \
libvirtconnectionTests.py \
diff --git a/tests/fake.py b/tests/fake.py
new file mode 100644
index 0000000..c9813a8
--- /dev/null
+++ b/tests/fake.py
@@ -0,0 +1,55 @@
+#
+# Copyright 2013 Red Hat, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301 USA
+#
+# Refer to the README and COPYING files for full details of the license
+#
+
+
+class Callable(object):
+ """
+ I pretend to be another function, collecting input arguments and returning
+ result or raising an exception.
+ """
+
+ def __init__(self, result=None, exception=None):
+ self.args = None
+ self.kw = None
+ self.result = result
+ self.exception = exception
+
+ def __call__(self, *args, **kw):
+ self.args = args
+ self.kw = kw
+ if self.exception:
+ raise self.exception
+ return self.result
+
+
+class Scope(object):
+ """
+ I'm a contenxt manager, useful for ensuring locks are taken correctly.
+ """
+
+ def __init__(self):
+ self.log = ''
+
+ def __enter__(self):
+ self.log += 'enter;'
+
+ def __exit__(self, *a, **kw):
+ self.log += 'exit;'
diff --git a/tests/fakeTests.py b/tests/fakeTests.py
new file mode 100644
index 0000000..bf84c1a
--- /dev/null
+++ b/tests/fakeTests.py
@@ -0,0 +1,59 @@
+#
+# Copyright 2013 Red Hat, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301 USA
+#
+# Refer to the README and COPYING files for full details of the license
+#
+
+from testrunner import VdsmTestCase as TestCaseBase
+import fake
+
+
+class TestFakeCallable(TestCaseBase):
+
+ def testDefaults(self):
+ cmd = fake.Callable()
+ self.assertEqual(cmd(), None)
+ self.assertEqual(cmd.args, tuple())
+ self.assertEqual(cmd.kw, dict())
+
+ def testWithArgs(self):
+ args = ('a', 'b')
+ kw = {'k': 'v'}
+ result = (2, '', 'error')
+ cmd = fake.Callable(result=result)
+ self.assertEqual(cmd(*args, **kw), result)
+ self.assertEqual(cmd.args, args)
+ self.assertEqual(cmd.kw, kw)
+
+ def testRaises(self):
+ cmd = fake.Callable(exception=ValueError())
+ self.assertRaises(ValueError, cmd)
+
+
+class TestFakeScope(TestCaseBase):
+
+ def testEnter(self):
+ s = fake.Scope()
+ with s:
+ self.assertEquals(s.log, 'enter;')
+
+ def testExit(self):
+ s = fake.Scope()
+ with s:
+ pass
+ self.assertEquals(s.log, 'enter;exit;')
diff --git a/tests/iscsiadmTests.py b/tests/iscsiadmTests.py
new file mode 100644
index 0000000..c623ecb
--- /dev/null
+++ b/tests/iscsiadmTests.py
@@ -0,0 +1,92 @@
+#
+# Copyright 2013 Red Hat, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301 USA
+#
+# Refer to the README and COPYING files for full details of the license
+#
+
+from testrunner import VdsmTestCase as TestCaseBase
+
+from vdsm import constants
+from storage import iscsiadm
+from storage import misc
+from monkeypatch import MonkeyClass, MonkeyPatch
+import fake
+
+
+ at MonkeyClass(misc, 'execCmd', fake.Callable())
+ at MonkeyClass(iscsiadm, '_iscsiadmLock', fake.Scope())
+class TestRunCommand(TestCaseBase):
+
+ def testTakeLock(self):
+ iscsiadm._iscsiadmLock.log = ''
+ iscsiadm._runCmd(['a'])
+ self.assertEqual(iscsiadm._iscsiadmLock.log, 'enter;exit;')
+
+ def testDefaults(self):
+ misc.execCmd.result = (0, '', '')
+ self.assertEqual(iscsiadm._runCmd(['a', 'b']),
+ misc.execCmd.result)
+ self.assertEqual(misc.execCmd.args,
+ ([constants.EXT_ISCSIADM, 'a', 'b'],))
+ self.assertEqual(misc.execCmd.kw,
+ {'sudo': True, 'printable': None})
+
+ def testHideValue(self):
+ self.assertEqual(iscsiadm._runCmd(['-v', 'a', 'b'], hideValue=True),
+ misc.execCmd.result)
+ self.assertEqual(misc.execCmd.args,
+ ([constants.EXT_ISCSIADM, '-v', 'a', 'b'],))
+ hidden = [constants.EXT_ISCSIADM, '-v', '****', 'b']
+ self.assertEqual(misc.execCmd.kw,
+ {'sudo': True, 'printable': hidden})
+
+ def testHideValueNoValue(self):
+ self.assertEqual(iscsiadm._runCmd(['-a', '-v'], hideValue=True),
+ misc.execCmd.result)
+ cmd = [constants.EXT_ISCSIADM, '-a', '-v']
+ self.assertEqual(misc.execCmd.args, (cmd,))
+ self.assertEqual(misc.execCmd.kw, {'sudo': True, 'printable': cmd})
+
+
+ at MonkeyClass(iscsiadm, '_runCmd', fake.Callable())
+class TestCommands(TestCaseBase):
+
+ @MonkeyPatch(iscsiadm, 'iface_exists', lambda name: False)
+ def test_iface_new(self):
+ iscsiadm._runCmd.result = (0, 'output', '')
+ iscsiadm.iface_new('name')
+ self.assertEquals(iscsiadm._runCmd.args,
+ (['-m', 'iface', '-I', 'name', '--op=new'],))
+ self.assertEquals(iscsiadm._runCmd.kw, {})
+
+ def test_iface_new_reserved(self):
+ for name in ('default', 'tcp', 'iser'):
+ self.assertRaises(iscsiadm.ReservedInterfaceNameError,
+ iscsiadm.iface_new, name)
+
+ @MonkeyPatch(iscsiadm, 'iface_exists', lambda name: False)
+ def test_iface_new_create_error(self):
+ iscsiadm._runCmd.result = (1, '', 'error') # XXX Use real rc
+ self.assertRaises(iscsiadm.IsciInterfaceCreationError,
+ iscsiadm.iface_new, 'name')
+
+ @MonkeyPatch(iscsiadm, 'iface_exists', lambda name: True)
+ def test_iface_new_already_exists(self):
+ iscsiadm._runCmd.result = (2, '', 'error') # XXX Use real rc
+ self.assertRaises(iscsiadm.IsciInterfaceAlreadyExistsError,
+ iscsiadm.iface_new, 'name')
--
To view, visit http://gerrit.ovirt.org/19856
To unsubscribe, visit http://gerrit.ovirt.org/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I9ed21408c7b496c384053ce02cdb66d47df9a14b
Gerrit-PatchSet: 8
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Nir Soffer <nsoffer at redhat.com>
Gerrit-Reviewer: Allon Mureinik <amureini at redhat.com>
Gerrit-Reviewer: Dan Kenigsberg <danken at redhat.com>
Gerrit-Reviewer: Nir Soffer <nsoffer at redhat.com>
Gerrit-Reviewer: Sergey Gotliv <sgotliv at redhat.com>
Gerrit-Reviewer: oVirt Jenkins CI Server
More information about the vdsm-patches
mailing list