# # 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)