summaryrefslogtreecommitdiffstats
path: root/tests/utilsTests.py
diff options
context:
space:
mode:
authorBala.FA <barumuga@redhat.com>2014-03-07 18:28:09 +0530
committerBala.FA <barumuga@redhat.com>2014-04-28 16:20:46 +0530
commit8f5f4537af7790be386974628c804a7bc719b738 (patch)
treeea134e1411640bdacf116332afc6a82c86ee28b0 /tests/utilsTests.py
parent79b4c3e202e0ce07d13d28b88f04c4dc79edfb12 (diff)
Initial commitv0.1.0
Change-Id: Ie8fdd046d111a4a46abe0e162985e833323bfd7d Signed-off-by: Bala.FA <barumuga@redhat.com>
Diffstat (limited to 'tests/utilsTests.py')
-rw-r--r--tests/utilsTests.py78
1 files changed, 78 insertions, 0 deletions
diff --git a/tests/utilsTests.py b/tests/utilsTests.py
new file mode 100644
index 0000000..a608dd9
--- /dev/null
+++ b/tests/utilsTests.py
@@ -0,0 +1,78 @@
+#
+# Copyright 2014 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
+#
+
+import errno
+
+from testrunner import GlusterNagiosTestCase as TestCaseBase
+from glusternagios import utils
+
+
+class RetryTests(TestCaseBase):
+ def testStopCallback(self):
+ counter = [0]
+ limit = 4
+
+ def stopCallback():
+ counter[0] += 1
+ if counter[0] == limit:
+ return True
+
+ return False
+
+ def foo():
+ raise RuntimeError("If at first you don't succeed, try, try again."
+ "Then quit. There's no point in being a damn"
+ "fool about it.")
+ # W. C. Fields
+
+ self.assertRaises(RuntimeError, utils.retry, foo, tries=(limit + 10),
+ sleep=0, stopCallback=stopCallback)
+ # Make sure we had the proper amount of iterations before failing
+ self.assertEquals(counter[0], limit)
+
+
+class CommandPathTests(TestCaseBase):
+ def testExisting(self):
+ cp = utils.CommandPath('sh', 'utter nonsense', '/bin/sh')
+ self.assertEquals(cp.cmd, '/bin/sh')
+
+ def testMissing(self):
+ NAME = 'nonsense'
+ try:
+ utils.CommandPath(NAME, 'utter nonsense').cmd
+ except OSError as e:
+ self.assertEquals(e.errno, errno.ENOENT)
+ self.assertIn(NAME, e.strerror)
+
+
+class ExecCmdTests(TestCaseBase):
+ def testSuccess(self):
+ (rc, out, err) = utils.execCmd(["true"])
+ self.assertEquals(rc, 0)
+
+ def testFailure(self):
+ (rc, out, err) = utils.execCmd(["false"])
+ self.assertEquals(rc, 1)
+
+ def testOSError(self):
+ def _runUnknown():
+ (rc, out, err) = utils.execCmd(["unknown"])
+
+ self.assertRaises(OSError, _runUnknown)