mirror of https://github.com/fail2ban/fail2ban
fix test cases by testing with multi-threaded execution (wait for threaded execution done)
(cherry picked from commit 1ec6782f32
)
# Conflicts:
# fail2ban/tests/observertestcase.py (not yet available in 0.10)
pull/1460/head
parent
f35aa6d258
commit
63f7916886
|
@ -30,11 +30,14 @@ else:
|
||||||
|
|
||||||
from ..dummyjail import DummyJail
|
from ..dummyjail import DummyJail
|
||||||
|
|
||||||
from ..utils import CONFIG_DIR, asyncserver
|
from ..utils import CONFIG_DIR, asyncserver, Utils
|
||||||
|
|
||||||
|
|
||||||
class TestSMTPServer(smtpd.SMTPServer):
|
class TestSMTPServer(smtpd.SMTPServer):
|
||||||
|
|
||||||
|
def __init__(self, *args):
|
||||||
|
smtpd.SMTPServer.__init__(self, *args)
|
||||||
|
self.ready = False
|
||||||
|
|
||||||
def process_message(self, peer, mailfrom, rcpttos, data):
|
def process_message(self, peer, mailfrom, rcpttos, data):
|
||||||
self.peer = peer
|
self.peer = peer
|
||||||
self.mailfrom = mailfrom
|
self.mailfrom = mailfrom
|
||||||
|
@ -42,6 +45,7 @@ class TestSMTPServer(smtpd.SMTPServer):
|
||||||
self.org_data = data
|
self.org_data = data
|
||||||
# replace new line (with tab or space) for possible mime translations (word wrap):
|
# replace new line (with tab or space) for possible mime translations (word wrap):
|
||||||
self.data = re.sub(r"\n[\t ]", " ", data)
|
self.data = re.sub(r"\n[\t ]", " ", data)
|
||||||
|
self.ready = True
|
||||||
|
|
||||||
|
|
||||||
class SMTPActionTest(unittest.TestCase):
|
class SMTPActionTest(unittest.TestCase):
|
||||||
|
@ -78,8 +82,13 @@ class SMTPActionTest(unittest.TestCase):
|
||||||
self._active = False
|
self._active = False
|
||||||
self._loop_thread.join()
|
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):
|
def testStart(self):
|
||||||
self.action.start()
|
self._exec_and_wait(self.action.start)
|
||||||
self.assertEqual(self.smtpd.mailfrom, "fail2ban")
|
self.assertEqual(self.smtpd.mailfrom, "fail2ban")
|
||||||
self.assertEqual(self.smtpd.rcpttos, ["root"])
|
self.assertEqual(self.smtpd.rcpttos, ["root"])
|
||||||
self.assertTrue(
|
self.assertTrue(
|
||||||
|
@ -87,7 +96,7 @@ class SMTPActionTest(unittest.TestCase):
|
||||||
in self.smtpd.data)
|
in self.smtpd.data)
|
||||||
|
|
||||||
def testStop(self):
|
def testStop(self):
|
||||||
self.action.stop()
|
self._exec_and_wait(self.action.stop)
|
||||||
self.assertEqual(self.smtpd.mailfrom, "fail2ban")
|
self.assertEqual(self.smtpd.mailfrom, "fail2ban")
|
||||||
self.assertEqual(self.smtpd.rcpttos, ["root"])
|
self.assertEqual(self.smtpd.rcpttos, ["root"])
|
||||||
self.assertTrue(
|
self.assertTrue(
|
||||||
|
@ -105,7 +114,7 @@ class SMTPActionTest(unittest.TestCase):
|
||||||
if restored:
|
if restored:
|
||||||
aInfo['restored'] = 1
|
aInfo['restored'] = 1
|
||||||
|
|
||||||
self.action.ban(aInfo)
|
self._exec_and_wait(lambda: self.action.ban(aInfo))
|
||||||
if restored: # no mail, should raises attribute error:
|
if restored: # no mail, should raises attribute error:
|
||||||
self.assertRaises(AttributeError, lambda: self.smtpd.mailfrom)
|
self.assertRaises(AttributeError, lambda: self.smtpd.mailfrom)
|
||||||
return
|
return
|
||||||
|
@ -118,15 +127,15 @@ class SMTPActionTest(unittest.TestCase):
|
||||||
"%i attempts" % aInfo['failures'], self.smtpd.data)
|
"%i attempts" % aInfo['failures'], self.smtpd.data)
|
||||||
|
|
||||||
self.action.matches = "matches"
|
self.action.matches = "matches"
|
||||||
self.action.ban(aInfo)
|
self._exec_and_wait(lambda: self.action.ban(aInfo))
|
||||||
self.assertIn(aInfo['matches'], self.smtpd.data)
|
self.assertIn(aInfo['matches'], self.smtpd.data)
|
||||||
|
|
||||||
self.action.matches = "ipjailmatches"
|
self.action.matches = "ipjailmatches"
|
||||||
self.action.ban(aInfo)
|
self._exec_and_wait(lambda: self.action.ban(aInfo))
|
||||||
self.assertIn(aInfo['ipjailmatches'], self.smtpd.data)
|
self.assertIn(aInfo['ipjailmatches'], self.smtpd.data)
|
||||||
|
|
||||||
self.action.matches = "ipmatches"
|
self.action.matches = "ipmatches"
|
||||||
self.action.ban(aInfo)
|
self._exec_and_wait(lambda: self.action.ban(aInfo))
|
||||||
self.assertIn(aInfo['ipmatches'], self.smtpd.data)
|
self.assertIn(aInfo['ipmatches'], self.smtpd.data)
|
||||||
|
|
||||||
def testBan(self):
|
def testBan(self):
|
||||||
|
@ -136,14 +145,14 @@ class SMTPActionTest(unittest.TestCase):
|
||||||
self._testBan(restored=True)
|
self._testBan(restored=True)
|
||||||
|
|
||||||
def testOptions(self):
|
def testOptions(self):
|
||||||
self.action.start()
|
self._exec_and_wait(self.action.start)
|
||||||
self.assertEqual(self.smtpd.mailfrom, "fail2ban")
|
self.assertEqual(self.smtpd.mailfrom, "fail2ban")
|
||||||
self.assertEqual(self.smtpd.rcpttos, ["root"])
|
self.assertEqual(self.smtpd.rcpttos, ["root"])
|
||||||
|
|
||||||
self.action.fromname = "Test"
|
self.action.fromname = "Test"
|
||||||
self.action.fromaddr = "test@example.com"
|
self.action.fromaddr = "test@example.com"
|
||||||
self.action.toaddr = "test@example.com, test2@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.assertEqual(self.smtpd.mailfrom, "test@example.com")
|
||||||
self.assertTrue("From: %s <%s>" %
|
self.assertTrue("From: %s <%s>" %
|
||||||
(self.action.fromname, self.action.fromaddr) in self.smtpd.data)
|
(self.action.fromname, self.action.fromaddr) in self.smtpd.data)
|
||||||
|
|
|
@ -992,9 +992,10 @@ class LoggingTests(LogCaptureTestCase):
|
||||||
badThread = _BadThread()
|
badThread = _BadThread()
|
||||||
badThread.start()
|
badThread.start()
|
||||||
badThread.join()
|
badThread.join()
|
||||||
self.assertLogged("Unhandled exception")
|
self.assertTrue( Utils.wait_for( lambda: len(x) and self._is_logged("Unhandled exception"), 3) )
|
||||||
finally:
|
finally:
|
||||||
sys.__excepthook__ = prev_exchook
|
sys.__excepthook__ = prev_exchook
|
||||||
|
self.assertLogged("Unhandled exception")
|
||||||
self.assertEqual(len(x), 1)
|
self.assertEqual(len(x), 1)
|
||||||
self.assertEqual(x[0][0], RuntimeError)
|
self.assertEqual(x[0][0], RuntimeError)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue