mirror of https://github.com/fail2ban/fail2ban
fix test cases by testing with multi-threaded execution (wait for threaded execution done)
parent
bf0adc1fdf
commit
1ec6782f32
|
@ -29,16 +29,20 @@ 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
|
||||||
self.rcpttos = rcpttos
|
self.rcpttos = rcpttos
|
||||||
self.data = data
|
self.data = data
|
||||||
|
self.ready = True
|
||||||
|
|
||||||
|
|
||||||
class SMTPActionTest(unittest.TestCase):
|
class SMTPActionTest(unittest.TestCase):
|
||||||
|
@ -74,8 +78,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(
|
||||||
|
@ -83,7 +92,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(
|
||||||
|
@ -99,7 +108,7 @@ class SMTPActionTest(unittest.TestCase):
|
||||||
'ipmatches': "Test fail 1\nTest Fail2\nTest Fail3\n",
|
'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.mailfrom, "fail2ban")
|
||||||
self.assertEqual(self.smtpd.rcpttos, ["root"])
|
self.assertEqual(self.smtpd.rcpttos, ["root"])
|
||||||
subject = "Subject: [Fail2Ban] %s: banned %s" % (
|
subject = "Subject: [Fail2Ban] %s: banned %s" % (
|
||||||
|
@ -109,26 +118,26 @@ class SMTPActionTest(unittest.TestCase):
|
||||||
"%i attempts" % aInfo['failures'] in self.smtpd.data)
|
"%i attempts" % aInfo['failures'] in 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.assertTrue(aInfo['matches'] in self.smtpd.data)
|
self.assertTrue(aInfo['matches'] in 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.assertTrue(aInfo['ipjailmatches'] in self.smtpd.data)
|
self.assertTrue(aInfo['ipjailmatches'] in 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.assertTrue(aInfo['ipmatches'] in self.smtpd.data)
|
self.assertTrue(aInfo['ipmatches'] in self.smtpd.data)
|
||||||
|
|
||||||
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)
|
||||||
|
|
|
@ -611,11 +611,14 @@ class ObserverTest(LogCaptureTestCase):
|
||||||
prev_exchook = sys.__excepthook__
|
prev_exchook = sys.__excepthook__
|
||||||
x = []
|
x = []
|
||||||
sys.__excepthook__ = lambda *args: x.append(args)
|
sys.__excepthook__ = lambda *args: x.append(args)
|
||||||
|
try:
|
||||||
obs.start()
|
obs.start()
|
||||||
obs.stop()
|
obs.stop()
|
||||||
obs = None
|
obs.join()
|
||||||
self.assertTrue(self._is_logged("Unhandled exception"))
|
self.assertTrue( Utils.wait_for( lambda: len(x) and self._is_logged("Unhandled exception"), 3) )
|
||||||
|
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)
|
||||||
self.assertEqual(str(x[0][1]), 'run bad thread exception')
|
self.assertEqual(str(x[0][1]), 'run bad thread exception')
|
||||||
|
|
|
@ -960,9 +960,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