From 8f5f4537af7790be386974628c804a7bc719b738 Mon Sep 17 00:00:00 2001 From: "Bala.FA" Date: Fri, 7 Mar 2014 18:28:09 +0530 Subject: Initial commit Change-Id: Ie8fdd046d111a4a46abe0e162985e833323bfd7d Signed-off-by: Bala.FA --- tests/utilsTests.py | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 tests/utilsTests.py (limited to 'tests/utilsTests.py') 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) -- cgit