mirror of https://github.com/fail2ban/fail2ban
test coverage for unhandled exception in run of several filter (gh-3097)
parent
e3f2fcfab4
commit
2f99d5accb
|
@ -722,7 +722,7 @@ class Filter(JailThread):
|
|||
# incr common error counter:
|
||||
self.commonError()
|
||||
|
||||
def commonError(self):
|
||||
def commonError(self, reason="common", exc=None):
|
||||
# incr error counter, stop processing (going idle) after 100th error :
|
||||
self._errors += 1
|
||||
# sleep a little bit (to get around time-related errors):
|
||||
|
|
|
@ -122,7 +122,7 @@ class FilterPoll(FileFilter):
|
|||
logSys.error("Caught unhandled exception in main cycle: %r", e,
|
||||
exc_info=logSys.getEffectiveLevel()<=logging.DEBUG)
|
||||
# incr common error counter:
|
||||
self.commonError()
|
||||
self.commonError("unhandled", e)
|
||||
logSys.debug("[%s] filter terminated", self.jailName)
|
||||
return True
|
||||
|
||||
|
|
|
@ -363,7 +363,7 @@ class FilterPyinotify(FileFilter):
|
|||
logSys.error("Caught unhandled exception in main cycle: %r", e,
|
||||
exc_info=logSys.getEffectiveLevel()<=logging.DEBUG)
|
||||
# incr common error counter:
|
||||
self.commonError()
|
||||
self.commonError("unhandled", e)
|
||||
|
||||
logSys.debug("[%s] filter exited (pyinotifier)", self.jailName)
|
||||
self.__notifier = None
|
||||
|
|
|
@ -334,7 +334,7 @@ class FilterSystemd(JournalFilter): # pragma: systemd no cover
|
|||
logSys.error("Caught unhandled exception in main cycle: %r", e,
|
||||
exc_info=logSys.getEffectiveLevel()<=logging.DEBUG)
|
||||
# incr common error counter:
|
||||
self.commonError()
|
||||
self.commonError("unhandled", e)
|
||||
|
||||
logSys.debug("[%s] filter terminated", self.jailName)
|
||||
|
||||
|
|
|
@ -979,6 +979,10 @@ class CommonMonitorTestCase(unittest.TestCase):
|
|||
super(CommonMonitorTestCase, self).setUp()
|
||||
self._failTotal = 0
|
||||
|
||||
def tearDown(self):
|
||||
super(CommonMonitorTestCase, self).tearDown()
|
||||
self.assertFalse(hasattr(self, "_unexpectedError"))
|
||||
|
||||
def waitFailTotal(self, count, delay=1):
|
||||
"""Wait up to `delay` sec to assure that expected failure `count` reached
|
||||
"""
|
||||
|
@ -1004,6 +1008,16 @@ class CommonMonitorTestCase(unittest.TestCase):
|
|||
last_ticks = self.filter.ticks
|
||||
return Utils.wait_for(lambda: self.filter.ticks >= last_ticks + ticks, _maxWaitTime(delay))
|
||||
|
||||
def commonFltError(self, reason="common", exc=None):
|
||||
""" Mock-up for default common error handler to find catched unhandled exceptions
|
||||
could occur in filters
|
||||
"""
|
||||
self._commonFltError(reason, exc)
|
||||
if reason == "unhandled":
|
||||
DefLogSys.critical("Caught unhandled exception in main cycle of %r : %r", self.filter, exc, exc_info=True)
|
||||
self._unexpectedError = True
|
||||
# self.assertNotEqual(reason, "unhandled")
|
||||
|
||||
|
||||
def get_monitor_failures_testcase(Filter_):
|
||||
"""Generator of TestCase's for different filters/backends
|
||||
|
@ -1026,6 +1040,8 @@ def get_monitor_failures_testcase(Filter_):
|
|||
self.file = open(self.name, 'a')
|
||||
self.jail = DummyJail()
|
||||
self.filter = Filter_(self.jail)
|
||||
# mock-up common error to find catched unhandled exceptions:
|
||||
self._commonFltError, self.filter.commonError = self.filter.commonError, self.commonFltError
|
||||
self.filter.addLogPath(self.name, autoSeek=False)
|
||||
# speedup search using exact date pattern:
|
||||
self.filter.setDatePattern(r'^(?:%a )?%b %d %H:%M:%S(?:\.%f)?(?: %ExY)?')
|
||||
|
@ -1324,6 +1340,8 @@ def get_monitor_failures_journal_testcase(Filter_): # pragma: systemd no cover
|
|||
def _initFilter(self, **kwargs):
|
||||
self._getRuntimeJournal() # check journal available
|
||||
self.filter = Filter_(self.jail, **kwargs)
|
||||
# mock-up common error to find catched unhandled exceptions:
|
||||
self._commonFltError, self.filter.commonError = self.filter.commonError, self.commonFltError
|
||||
self.filter.addJournalMatch([
|
||||
"SYSLOG_IDENTIFIER=fail2ban-testcases",
|
||||
"TEST_FIELD=1",
|
||||
|
|
Loading…
Reference in New Issue