From cfcf841ae4324a8a87b410ae60424f7bb7adb923 Mon Sep 17 00:00:00 2001 From: Steven Hiscocks Date: Sat, 4 Jan 2014 23:07:59 +0000 Subject: [PATCH] TST: Added some more tests for Python actions --- fail2ban/tests/actionstestcase.py | 24 +++++++++++++++++++ .../tests/files/action.d/action_errors.py | 21 ++++++++++++++++ .../tests/files/action.d/action_noAction.py | 5 ++++ .../tests/files/action.d/action_nomethod.py | 12 ++++++++++ 4 files changed, 62 insertions(+) create mode 100644 fail2ban/tests/files/action.d/action_errors.py create mode 100644 fail2ban/tests/files/action.d/action_noAction.py create mode 100644 fail2ban/tests/files/action.d/action_nomethod.py diff --git a/fail2ban/tests/actionstestcase.py b/fail2ban/tests/actionstestcase.py index 83319cb8..ccc0263d 100644 --- a/fail2ban/tests/actionstestcase.py +++ b/fail2ban/tests/actionstestcase.py @@ -55,6 +55,10 @@ class ExecuteActions(LogCaptureTestCase): self.__ip.actioncheck = 'echo ip check >> "%s"' % self.__tmpfilename self.__ip.actionstop = 'echo ip stop >> "%s"' % self.__tmpfilename + def testActionsAddDuplicateName(self): + self.__actions.add('test') + self.assertRaises(ValueError, self.__actions.add, 'test') + def testActionsManipulation(self): self.__actions.add('test') self.assertTrue(self.__actions['test']) @@ -115,3 +119,23 @@ class ExecuteActions(LogCaptureTestCase): self.assertRaises( TypeError, self.__actions.add, "Action5", os.path.join(TEST_FILES_DIR, "action.d/action.py"), {}) + + def testAddPythonActionNOK(self): + self.assertRaises(RuntimeError, self.__actions.add, + "Action", os.path.join(TEST_FILES_DIR, + "action.d/action_noAction.py"), + {}) + self.assertRaises(RuntimeError, self.__actions.add, + "Action", os.path.join(TEST_FILES_DIR, + "action.d/action_nomethod.py"), + {}) + self.__actions.add( + "Action", os.path.join(TEST_FILES_DIR, + "action.d/action_errors.py"), + {}) + self.__actions.start() + time.sleep(3) + self.assertTrue(self._is_logged("Failed to start")) + self.__actions.stop() + self.__actions.join() + self.assertTrue(self._is_logged("Failed to stop")) diff --git a/fail2ban/tests/files/action.d/action_errors.py b/fail2ban/tests/files/action.d/action_errors.py new file mode 100644 index 00000000..767848c1 --- /dev/null +++ b/fail2ban/tests/files/action.d/action_errors.py @@ -0,0 +1,21 @@ + +from fail2ban.server.action import ActionBase + +class TestAction(ActionBase): + + def __init__(self, jail, name): + super(TestAction, self).__init__(jail, name) + + def start(self): + raise Exception() + + def stop(self): + raise Exception() + + def ban(self): + raise Exception() + + def unban(self): + raise Exception() + +Action = TestAction diff --git a/fail2ban/tests/files/action.d/action_noAction.py b/fail2ban/tests/files/action.d/action_noAction.py new file mode 100644 index 00000000..1aa25e67 --- /dev/null +++ b/fail2ban/tests/files/action.d/action_noAction.py @@ -0,0 +1,5 @@ + +from fail2ban.server.action import ActionBase + +class TestAction(ActionBase): + pass diff --git a/fail2ban/tests/files/action.d/action_nomethod.py b/fail2ban/tests/files/action.d/action_nomethod.py new file mode 100644 index 00000000..8b9fd981 --- /dev/null +++ b/fail2ban/tests/files/action.d/action_nomethod.py @@ -0,0 +1,12 @@ + +from fail2ban.server.action import ActionBase + +class TestAction(): + + def __init__(self, jail, name): + pass + + def start(self): + pass + +Action = TestAction