From 1ec6782f321bf0fca43a2a598cc33f70b39e7f35 Mon Sep 17 00:00:00 2001 From: sebres Date: Sun, 6 Mar 2016 15:46:52 +0100 Subject: [PATCH] fix test cases by testing with multi-threaded execution (wait for threaded execution done) --- fail2ban/tests/action_d/test_smtp.py | 29 ++++++++++++++++++---------- fail2ban/tests/observertestcase.py | 13 ++++++++----- fail2ban/tests/servertestcase.py | 3 ++- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/fail2ban/tests/action_d/test_smtp.py b/fail2ban/tests/action_d/test_smtp.py index 1385fe82..4155cfa0 100644 --- a/fail2ban/tests/action_d/test_smtp.py +++ b/fail2ban/tests/action_d/test_smtp.py @@ -29,16 +29,20 @@ else: from ..dummyjail import DummyJail -from ..utils import CONFIG_DIR, asyncserver - +from ..utils import CONFIG_DIR, asyncserver, Utils class TestSMTPServer(smtpd.SMTPServer): + def __init__(self, *args): + smtpd.SMTPServer.__init__(self, *args) + self.ready = False + def process_message(self, peer, mailfrom, rcpttos, data): self.peer = peer self.mailfrom = mailfrom self.rcpttos = rcpttos self.data = data + self.ready = True class SMTPActionTest(unittest.TestCase): @@ -74,8 +78,13 @@ class SMTPActionTest(unittest.TestCase): self._active = False self._loop_thread.join() + def _exec_and_wait(self, doaction): + self.smtpd.ready = False + doaction() + Utils.wait_for(lambda: self.smtpd.ready, 3) + def testStart(self): - self.action.start() + self._exec_and_wait(self.action.start) self.assertEqual(self.smtpd.mailfrom, "fail2ban") self.assertEqual(self.smtpd.rcpttos, ["root"]) self.assertTrue( @@ -83,7 +92,7 @@ class SMTPActionTest(unittest.TestCase): in self.smtpd.data) def testStop(self): - self.action.stop() + self._exec_and_wait(self.action.stop) self.assertEqual(self.smtpd.mailfrom, "fail2ban") self.assertEqual(self.smtpd.rcpttos, ["root"]) self.assertTrue( @@ -99,7 +108,7 @@ class SMTPActionTest(unittest.TestCase): 'ipmatches': "Test fail 1\nTest Fail2\nTest Fail3\n", } - self.action.ban(aInfo) + self._exec_and_wait(lambda: self.action.ban(aInfo)) self.assertEqual(self.smtpd.mailfrom, "fail2ban") self.assertEqual(self.smtpd.rcpttos, ["root"]) subject = "Subject: [Fail2Ban] %s: banned %s" % ( @@ -109,26 +118,26 @@ class SMTPActionTest(unittest.TestCase): "%i attempts" % aInfo['failures'] in self.smtpd.data) self.action.matches = "matches" - self.action.ban(aInfo) + self._exec_and_wait(lambda: self.action.ban(aInfo)) self.assertTrue(aInfo['matches'] in self.smtpd.data) self.action.matches = "ipjailmatches" - self.action.ban(aInfo) + self._exec_and_wait(lambda: self.action.ban(aInfo)) self.assertTrue(aInfo['ipjailmatches'] in self.smtpd.data) self.action.matches = "ipmatches" - self.action.ban(aInfo) + self._exec_and_wait(lambda: self.action.ban(aInfo)) self.assertTrue(aInfo['ipmatches'] in self.smtpd.data) def testOptions(self): - self.action.start() + self._exec_and_wait(self.action.start) self.assertEqual(self.smtpd.mailfrom, "fail2ban") self.assertEqual(self.smtpd.rcpttos, ["root"]) self.action.fromname = "Test" self.action.fromaddr = "test@example.com" self.action.toaddr = "test@example.com, test2@example.com" - self.action.start() + self._exec_and_wait(self.action.start) self.assertEqual(self.smtpd.mailfrom, "test@example.com") self.assertTrue("From: %s <%s>" % (self.action.fromname, self.action.fromaddr) in self.smtpd.data) diff --git a/fail2ban/tests/observertestcase.py b/fail2ban/tests/observertestcase.py index e1c29cc9..3e4bfd10 100644 --- a/fail2ban/tests/observertestcase.py +++ b/fail2ban/tests/observertestcase.py @@ -611,11 +611,14 @@ class ObserverTest(LogCaptureTestCase): prev_exchook = sys.__excepthook__ x = [] sys.__excepthook__ = lambda *args: x.append(args) - obs.start() - obs.stop() - obs = None - self.assertTrue(self._is_logged("Unhandled exception")) - sys.__excepthook__ = prev_exchook + try: + obs.start() + obs.stop() + obs.join() + self.assertTrue( Utils.wait_for( lambda: len(x) and self._is_logged("Unhandled exception"), 3) ) + finally: + sys.__excepthook__ = prev_exchook + self.assertLogged("Unhandled exception") self.assertEqual(len(x), 1) self.assertEqual(x[0][0], RuntimeError) self.assertEqual(str(x[0][1]), 'run bad thread exception') diff --git a/fail2ban/tests/servertestcase.py b/fail2ban/tests/servertestcase.py index d593355e..58f5e173 100644 --- a/fail2ban/tests/servertestcase.py +++ b/fail2ban/tests/servertestcase.py @@ -960,9 +960,10 @@ class LoggingTests(LogCaptureTestCase): badThread = _BadThread() badThread.start() badThread.join() - self.assertLogged("Unhandled exception") + self.assertTrue( Utils.wait_for( lambda: len(x) and self._is_logged("Unhandled exception"), 3) ) finally: sys.__excepthook__ = prev_exchook + self.assertLogged("Unhandled exception") self.assertEqual(len(x), 1) self.assertEqual(x[0][0], RuntimeError)