mirror of https://github.com/fail2ban/fail2ban
code review and performance optimization:
- default date templates cache + regexp compiling on the fly (if required); - better usage of shared config over all test cases; - obsolete test cases removed (simple test of memleak/gc); - skip some slow test cases in fast mode (setup test, etc.);pull/1346/head
parent
3f2b58e973
commit
da51fbf9c6
|
@ -34,6 +34,82 @@ logSys = getLogger(__name__)
|
|||
logLevel = 6
|
||||
|
||||
|
||||
class DateDetectorCache(object):
|
||||
def __init__(self):
|
||||
self.__lock = Lock()
|
||||
self.__templates = list()
|
||||
|
||||
@property
|
||||
def templates(self):
|
||||
"""List of template instances managed by the detector.
|
||||
"""
|
||||
with self.__lock:
|
||||
if self.__templates:
|
||||
return self.__templates
|
||||
self._addDefaultTemplate()
|
||||
return self.__templates
|
||||
|
||||
def _cacheTemplate(self, template):
|
||||
"""Cache Fail2Ban's default template.
|
||||
"""
|
||||
if isinstance(template, str):
|
||||
template = DatePatternRegex(template)
|
||||
self.__templates.append(template)
|
||||
|
||||
def _addDefaultTemplate(self):
|
||||
"""Add resp. cache Fail2Ban's default set of date templates.
|
||||
"""
|
||||
# asctime with optional day, subsecond and/or year:
|
||||
# Sun Jan 23 21:59:59.011 2005
|
||||
self._cacheTemplate("(?:%a )?%b %d %H:%M:%S(?:\.%f)?(?: %Y)?")
|
||||
# asctime with optional day, subsecond and/or year coming after day
|
||||
# http://bugs.debian.org/798923
|
||||
# Sun Jan 23 2005 21:59:59.011
|
||||
self._cacheTemplate("(?:%a )?%b %d %Y %H:%M:%S(?:\.%f)?")
|
||||
# simple date, optional subsecond (proftpd):
|
||||
# 2005-01-23 21:59:59
|
||||
# simple date: 2005/01/23 21:59:59
|
||||
# custom for syslog-ng 2006.12.21 06:43:20
|
||||
self._cacheTemplate("%Y(?P<_sep>[-/.])%m(?P=_sep)%d %H:%M:%S(?:,%f)?")
|
||||
# simple date too (from x11vnc): 23/01/2005 21:59:59
|
||||
# and with optional year given by 2 digits: 23/01/05 21:59:59
|
||||
# (See http://bugs.debian.org/537610)
|
||||
# 17-07-2008 17:23:25
|
||||
self._cacheTemplate("%d(?P<_sep>[-/])%m(?P=_sep)(?:%Y|%y) %H:%M:%S")
|
||||
# Apache format optional time zone:
|
||||
# [31/Oct/2006:09:22:55 -0000]
|
||||
# 26-Jul-2007 15:20:52
|
||||
self._cacheTemplate("%d(?P<_sep>[-/])%b(?P=_sep)%Y[ :]?%H:%M:%S(?:\.%f)?(?: %z)?")
|
||||
# CPanel 05/20/2008:01:57:39
|
||||
self._cacheTemplate("%m/%d/%Y:%H:%M:%S")
|
||||
# named 26-Jul-2007 15:20:52.252
|
||||
# roundcube 26-Jul-2007 15:20:52 +0200
|
||||
# 01-27-2012 16:22:44.252
|
||||
# subseconds explicit to avoid possible %m<->%d confusion
|
||||
# with previous
|
||||
self._cacheTemplate("%m-%d-%Y %H:%M:%S\.%f")
|
||||
# TAI64N
|
||||
template = DateTai64n()
|
||||
template.name = "TAI64N"
|
||||
self._cacheTemplate(template)
|
||||
# Epoch
|
||||
template = DateEpoch()
|
||||
template.name = "Epoch"
|
||||
self._cacheTemplate(template)
|
||||
# ISO 8601
|
||||
self._cacheTemplate("%Y-%m-%d[T ]%H:%M:%S(?:\.%f)?(?:%z)?")
|
||||
# Only time information in the log
|
||||
self._cacheTemplate("^%H:%M:%S")
|
||||
# <09/16/08@05:03:30>
|
||||
self._cacheTemplate("^<%m/%d/%y@%H:%M:%S>")
|
||||
# MySQL: 130322 11:46:11
|
||||
self._cacheTemplate("^%y%m%d ?%H:%M:%S")
|
||||
# Apache Tomcat
|
||||
self._cacheTemplate("%b %d, %Y %I:%M:%S %p")
|
||||
# ASSP: Apr-27-13 02:33:06
|
||||
self._cacheTemplate("^%b-%d-%y %H:%M:%S")
|
||||
|
||||
|
||||
class DateDetector(object):
|
||||
"""Manages one or more date templates to find a date within a log line.
|
||||
|
||||
|
@ -41,6 +117,7 @@ class DateDetector(object):
|
|||
----------
|
||||
templates
|
||||
"""
|
||||
_defCache = DateDetectorCache()
|
||||
|
||||
def __init__(self):
|
||||
self.__lock = Lock()
|
||||
|
@ -79,59 +156,9 @@ class DateDetector(object):
|
|||
def addDefaultTemplate(self):
|
||||
"""Add Fail2Ban's default set of date templates.
|
||||
"""
|
||||
self.__lock.acquire()
|
||||
try:
|
||||
# asctime with optional day, subsecond and/or year:
|
||||
# Sun Jan 23 21:59:59.011 2005
|
||||
self.appendTemplate("(?:%a )?%b %d %H:%M:%S(?:\.%f)?(?: %Y)?")
|
||||
# asctime with optional day, subsecond and/or year coming after day
|
||||
# http://bugs.debian.org/798923
|
||||
# Sun Jan 23 2005 21:59:59.011
|
||||
self.appendTemplate("(?:%a )?%b %d %Y %H:%M:%S(?:\.%f)?")
|
||||
# simple date, optional subsecond (proftpd):
|
||||
# 2005-01-23 21:59:59
|
||||
# simple date: 2005/01/23 21:59:59
|
||||
# custom for syslog-ng 2006.12.21 06:43:20
|
||||
self.appendTemplate("%Y(?P<_sep>[-/.])%m(?P=_sep)%d %H:%M:%S(?:,%f)?")
|
||||
# simple date too (from x11vnc): 23/01/2005 21:59:59
|
||||
# and with optional year given by 2 digits: 23/01/05 21:59:59
|
||||
# (See http://bugs.debian.org/537610)
|
||||
# 17-07-2008 17:23:25
|
||||
self.appendTemplate("%d(?P<_sep>[-/])%m(?P=_sep)(?:%Y|%y) %H:%M:%S")
|
||||
# Apache format optional time zone:
|
||||
# [31/Oct/2006:09:22:55 -0000]
|
||||
# 26-Jul-2007 15:20:52
|
||||
self.appendTemplate("%d(?P<_sep>[-/])%b(?P=_sep)%Y[ :]?%H:%M:%S(?:\.%f)?(?: %z)?")
|
||||
# CPanel 05/20/2008:01:57:39
|
||||
self.appendTemplate("%m/%d/%Y:%H:%M:%S")
|
||||
# named 26-Jul-2007 15:20:52.252
|
||||
# roundcube 26-Jul-2007 15:20:52 +0200
|
||||
# 01-27-2012 16:22:44.252
|
||||
# subseconds explicit to avoid possible %m<->%d confusion
|
||||
# with previous
|
||||
self.appendTemplate("%m-%d-%Y %H:%M:%S\.%f")
|
||||
# TAI64N
|
||||
template = DateTai64n()
|
||||
template.name = "TAI64N"
|
||||
self.appendTemplate(template)
|
||||
# Epoch
|
||||
template = DateEpoch()
|
||||
template.name = "Epoch"
|
||||
self.appendTemplate(template)
|
||||
# ISO 8601
|
||||
self.appendTemplate("%Y-%m-%d[T ]%H:%M:%S(?:\.%f)?(?:%z)?")
|
||||
# Only time information in the log
|
||||
self.appendTemplate("^%H:%M:%S")
|
||||
# <09/16/08@05:03:30>
|
||||
self.appendTemplate("^<%m/%d/%y@%H:%M:%S>")
|
||||
# MySQL: 130322 11:46:11
|
||||
self.appendTemplate("^%y%m%d ?%H:%M:%S")
|
||||
# Apache Tomcat
|
||||
self.appendTemplate("%b %d, %Y %I:%M:%S %p")
|
||||
# ASSP: Apr-27-13 02:33:06
|
||||
self.appendTemplate("^%b-%d-%y %H:%M:%S")
|
||||
finally:
|
||||
self.__lock.release()
|
||||
with self.__lock:
|
||||
for template in DateDetector._defCache.templates:
|
||||
self._appendTemplate(template)
|
||||
|
||||
@property
|
||||
def templates(self):
|
||||
|
|
|
@ -86,7 +86,6 @@ class DateTemplate(object):
|
|||
if (wordBegin and not re.search(r'^\^', regex)):
|
||||
regex = r'\b' + regex
|
||||
self._regex = regex
|
||||
self._cRegex = re.compile(regex, re.UNICODE | re.IGNORECASE)
|
||||
|
||||
regex = property(getRegex, setRegex, doc=
|
||||
"""Regex used to search for date.
|
||||
|
@ -95,6 +94,8 @@ class DateTemplate(object):
|
|||
def matchDate(self, line):
|
||||
"""Check if regex for date matches on a log line.
|
||||
"""
|
||||
if not self._cRegex:
|
||||
self._cRegex = re.compile(self.regex, re.UNICODE | re.IGNORECASE)
|
||||
dateMatch = self._cRegex.search(line)
|
||||
return dateMatch
|
||||
|
||||
|
@ -171,7 +172,7 @@ class DatePatternRegex(DateTemplate):
|
|||
regex
|
||||
pattern
|
||||
"""
|
||||
_patternRE = r"%%(%%|[%s])" % "".join(timeRE.keys())
|
||||
_patternRE = re.compile(r"%%(%%|[%s])" % "".join(timeRE.keys()))
|
||||
_patternName = {
|
||||
'a': "DAY", 'A': "DAYNAME", 'b': "MON", 'B': "MONTH", 'd': "Day",
|
||||
'H': "24hour", 'I': "12hour", 'j': "Yearday", 'm': "Month",
|
||||
|
@ -202,10 +203,9 @@ class DatePatternRegex(DateTemplate):
|
|||
@pattern.setter
|
||||
def pattern(self, pattern):
|
||||
self._pattern = pattern
|
||||
self._name = re.sub(
|
||||
self._patternRE, r'%(\1)s', pattern) % self._patternName
|
||||
super(DatePatternRegex, self).setRegex(
|
||||
re.sub(self._patternRE, r'%(\1)s', pattern) % timeRE)
|
||||
fmt = self._patternRE.sub(r'%(\1)s', pattern)
|
||||
self._name = fmt % self._patternName
|
||||
super(DatePatternRegex, self).setRegex(fmt % timeRE)
|
||||
|
||||
def setRegex(self, value):
|
||||
raise NotImplementedError("Regex derived from pattern")
|
||||
|
|
|
@ -38,12 +38,15 @@ from ..client.configurator import Configurator
|
|||
from .utils import LogCaptureTestCase
|
||||
|
||||
TEST_FILES_DIR = os.path.join(os.path.dirname(__file__), "files")
|
||||
TEST_FILES_DIR_SHARE_CFG = {}
|
||||
|
||||
from .utils import CONFIG_DIR
|
||||
CONFIG_DIR_SHARE_CFG = unittest.F2B.share_config
|
||||
|
||||
STOCK = os.path.exists(os.path.join('config','fail2ban.conf'))
|
||||
|
||||
IMPERFECT_CONFIG = os.path.join(os.path.dirname(__file__), 'config')
|
||||
IMPERFECT_CONFIG_SHARE_CFG = {}
|
||||
|
||||
|
||||
class ConfigReaderTest(unittest.TestCase):
|
||||
|
@ -162,14 +165,13 @@ class JailReaderTest(LogCaptureTestCase):
|
|||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(JailReaderTest, self).__init__(*args, **kwargs)
|
||||
self.__share_cfg = {}
|
||||
|
||||
def testIncorrectJail(self):
|
||||
jail = JailReader('XXXABSENTXXX', basedir=CONFIG_DIR, share_config=self.__share_cfg)
|
||||
jail = JailReader('XXXABSENTXXX', basedir=CONFIG_DIR, share_config=CONFIG_DIR_SHARE_CFG)
|
||||
self.assertRaises(ValueError, jail.read)
|
||||
|
||||
def testJailActionEmpty(self):
|
||||
jail = JailReader('emptyaction', basedir=IMPERFECT_CONFIG, share_config=self.__share_cfg)
|
||||
jail = JailReader('emptyaction', basedir=IMPERFECT_CONFIG, share_config=IMPERFECT_CONFIG_SHARE_CFG)
|
||||
self.assertTrue(jail.read())
|
||||
self.assertTrue(jail.getOptions())
|
||||
self.assertTrue(jail.isEnabled())
|
||||
|
@ -177,7 +179,7 @@ class JailReaderTest(LogCaptureTestCase):
|
|||
self.assertLogged('No actions were defined for emptyaction')
|
||||
|
||||
def testJailActionFilterMissing(self):
|
||||
jail = JailReader('missingbitsjail', basedir=IMPERFECT_CONFIG, share_config=self.__share_cfg)
|
||||
jail = JailReader('missingbitsjail', basedir=IMPERFECT_CONFIG, share_config=IMPERFECT_CONFIG_SHARE_CFG)
|
||||
self.assertTrue(jail.read())
|
||||
self.assertFalse(jail.getOptions())
|
||||
self.assertTrue(jail.isEnabled())
|
||||
|
@ -186,7 +188,7 @@ class JailReaderTest(LogCaptureTestCase):
|
|||
|
||||
def testJailActionBrokenDef(self):
|
||||
jail = JailReader('brokenactiondef', basedir=IMPERFECT_CONFIG,
|
||||
share_config=self.__share_cfg)
|
||||
share_config=IMPERFECT_CONFIG_SHARE_CFG)
|
||||
self.assertTrue(jail.read())
|
||||
self.assertFalse(jail.getOptions())
|
||||
self.assertTrue(jail.isEnabled())
|
||||
|
@ -200,7 +202,7 @@ class JailReaderTest(LogCaptureTestCase):
|
|||
|
||||
if STOCK:
|
||||
def testStockSSHJail(self):
|
||||
jail = JailReader('sshd', basedir=CONFIG_DIR, share_config=self.__share_cfg) # we are running tests from root project dir atm
|
||||
jail = JailReader('sshd', basedir=CONFIG_DIR, share_config=CONFIG_DIR_SHARE_CFG) # we are running tests from root project dir atm
|
||||
self.assertTrue(jail.read())
|
||||
self.assertTrue(jail.getOptions())
|
||||
self.assertFalse(jail.isEnabled())
|
||||
|
@ -316,7 +318,7 @@ class FilterReaderTest(unittest.TestCase):
|
|||
self.assertEqual(sorted(filterReader.convert()), sorted(output))
|
||||
|
||||
filterReader = FilterReader("testcase01", "testcase01", {'maxlines': "5"},
|
||||
share_config=self.__share_cfg, basedir=TEST_FILES_DIR)
|
||||
share_config=TEST_FILES_DIR_SHARE_CFG, basedir=TEST_FILES_DIR)
|
||||
filterReader.read()
|
||||
#filterReader.getOptions(["failregex", "ignoreregex"])
|
||||
filterReader.getOptions(None)
|
||||
|
@ -326,7 +328,7 @@ class FilterReaderTest(unittest.TestCase):
|
|||
def testFilterReaderSubstitionDefault(self):
|
||||
output = [['set', 'jailname', 'addfailregex', 'to=sweet@example.com fromip=<IP>']]
|
||||
filterReader = FilterReader('substition', "jailname", {},
|
||||
share_config=self.__share_cfg, basedir=TEST_FILES_DIR)
|
||||
share_config=TEST_FILES_DIR_SHARE_CFG, basedir=TEST_FILES_DIR)
|
||||
filterReader.read()
|
||||
filterReader.getOptions(None)
|
||||
c = filterReader.convert()
|
||||
|
@ -335,7 +337,7 @@ class FilterReaderTest(unittest.TestCase):
|
|||
def testFilterReaderSubstitionSet(self):
|
||||
output = [['set', 'jailname', 'addfailregex', 'to=sour@example.com fromip=<IP>']]
|
||||
filterReader = FilterReader('substition', "jailname", {'honeypot': 'sour@example.com'},
|
||||
share_config=self.__share_cfg, basedir=TEST_FILES_DIR)
|
||||
share_config=TEST_FILES_DIR_SHARE_CFG, basedir=TEST_FILES_DIR)
|
||||
filterReader.read()
|
||||
filterReader.getOptions(None)
|
||||
c = filterReader.convert()
|
||||
|
@ -346,7 +348,7 @@ class FilterReaderTest(unittest.TestCase):
|
|||
filterName, filterOpt = JailReader.extractOptions(
|
||||
'substition[honeypot="<sweet>,<known/honeypot>", sweet="test,<known/honeypot>,test2"]')
|
||||
filterReader = FilterReader('substition', "jailname", filterOpt,
|
||||
share_config=self.__share_cfg, basedir=TEST_FILES_DIR)
|
||||
share_config=TEST_FILES_DIR_SHARE_CFG, basedir=TEST_FILES_DIR)
|
||||
filterReader.read()
|
||||
filterReader.getOptions(None)
|
||||
c = filterReader.convert()
|
||||
|
@ -355,13 +357,13 @@ class FilterReaderTest(unittest.TestCase):
|
|||
def testFilterReaderSubstitionFail(self):
|
||||
# directly subst the same var :
|
||||
filterReader = FilterReader('substition', "jailname", {'honeypot': '<honeypot>'},
|
||||
share_config=self.__share_cfg, basedir=TEST_FILES_DIR)
|
||||
share_config=TEST_FILES_DIR_SHARE_CFG, basedir=TEST_FILES_DIR)
|
||||
filterReader.read()
|
||||
filterReader.getOptions(None)
|
||||
self.assertRaises(ValueError, FilterReader.convert, filterReader)
|
||||
# cross subst the same var :
|
||||
filterReader = FilterReader('substition', "jailname", {'honeypot': '<sweet>', 'sweet': '<honeypot>'},
|
||||
share_config=self.__share_cfg, basedir=TEST_FILES_DIR)
|
||||
share_config=TEST_FILES_DIR_SHARE_CFG, basedir=TEST_FILES_DIR)
|
||||
filterReader.read()
|
||||
filterReader.getOptions(None)
|
||||
self.assertRaises(ValueError, FilterReader.convert, filterReader)
|
||||
|
@ -404,6 +406,7 @@ class JailsReaderTestCache(LogCaptureTestCase):
|
|||
return cnt
|
||||
|
||||
def testTestJailConfCache(self):
|
||||
unittest.F2B.SkipIfFast()
|
||||
saved_ll = configparserinc.logLevel
|
||||
configparserinc.logLevel = logging.DEBUG
|
||||
basedir = tempfile.mkdtemp("fail2ban_conf")
|
||||
|
@ -446,7 +449,6 @@ class JailsReaderTest(LogCaptureTestCase):
|
|||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(JailsReaderTest, self).__init__(*args, **kwargs)
|
||||
self.__share_cfg = {}
|
||||
|
||||
def testProvidingBadBasedir(self):
|
||||
if not os.path.exists('/XXX'):
|
||||
|
@ -454,7 +456,7 @@ class JailsReaderTest(LogCaptureTestCase):
|
|||
self.assertRaises(ValueError, reader.read)
|
||||
|
||||
def testReadTestJailConf(self):
|
||||
jails = JailsReader(basedir=IMPERFECT_CONFIG, share_config=self.__share_cfg)
|
||||
jails = JailsReader(basedir=IMPERFECT_CONFIG, share_config=IMPERFECT_CONFIG_SHARE_CFG)
|
||||
self.assertTrue(jails.read())
|
||||
self.assertFalse(jails.getOptions())
|
||||
self.assertRaises(ValueError, jails.convert)
|
||||
|
@ -504,7 +506,7 @@ class JailsReaderTest(LogCaptureTestCase):
|
|||
msg="Action file %r is lacking [Init] section" % actionConfig)
|
||||
|
||||
def testReadStockJailConf(self):
|
||||
jails = JailsReader(basedir=CONFIG_DIR, share_config=self.__share_cfg) # we are running tests from root project dir atm
|
||||
jails = JailsReader(basedir=CONFIG_DIR, share_config=CONFIG_DIR_SHARE_CFG) # we are running tests from root project dir atm
|
||||
self.assertTrue(jails.read()) # opens fine
|
||||
self.assertTrue(jails.getOptions()) # reads fine
|
||||
comm_commands = jails.convert()
|
||||
|
@ -535,7 +537,7 @@ class JailsReaderTest(LogCaptureTestCase):
|
|||
# moreover we must have a file for it
|
||||
# and it must be readable as a Filter
|
||||
filterReader = FilterReader(filterName, jail, filterOpt,
|
||||
share_config=self.__share_cfg, basedir=CONFIG_DIR)
|
||||
share_config=CONFIG_DIR_SHARE_CFG, basedir=CONFIG_DIR)
|
||||
self.assertTrue(filterReader.read(),"Failed to read filter:" + filterName) # opens fine
|
||||
filterReader.getOptions({}) # reads fine
|
||||
|
||||
|
@ -554,8 +556,8 @@ class JailsReaderTest(LogCaptureTestCase):
|
|||
if actName == 'iptables-multiport':
|
||||
self.assertTrue('port' in actOpt)
|
||||
|
||||
actionReader = ActionReader(
|
||||
actName, jail, {}, basedir=CONFIG_DIR)
|
||||
actionReader = ActionReader(actName, jail, {},
|
||||
share_config=CONFIG_DIR_SHARE_CFG, basedir=CONFIG_DIR)
|
||||
self.assertTrue(actionReader.read())
|
||||
actionReader.getOptions({}) # populate _opts
|
||||
cmds = actionReader.convert()
|
||||
|
@ -566,7 +568,7 @@ class JailsReaderTest(LogCaptureTestCase):
|
|||
|
||||
# Verify that all filters found under config/ have a jail
|
||||
def testReadStockJailFilterComplete(self):
|
||||
jails = JailsReader(basedir=CONFIG_DIR, force_enable=True, share_config=self.__share_cfg)
|
||||
jails = JailsReader(basedir=CONFIG_DIR, force_enable=True, share_config=CONFIG_DIR_SHARE_CFG)
|
||||
self.assertTrue(jails.read()) # opens fine
|
||||
self.assertTrue(jails.getOptions()) # reads fine
|
||||
# grab all filter names
|
||||
|
@ -586,7 +588,7 @@ class JailsReaderTest(LogCaptureTestCase):
|
|||
def testReadStockJailConfForceEnabled(self):
|
||||
# more of a smoke test to make sure that no obvious surprises
|
||||
# on users' systems when enabling shipped jails
|
||||
jails = JailsReader(basedir=CONFIG_DIR, force_enable=True, share_config=self.__share_cfg) # we are running tests from root project dir atm
|
||||
jails = JailsReader(basedir=CONFIG_DIR, force_enable=True, share_config=CONFIG_DIR_SHARE_CFG) # we are running tests from root project dir atm
|
||||
self.assertTrue(jails.read()) # opens fine
|
||||
self.assertTrue(jails.getOptions()) # reads fine
|
||||
comm_commands = jails.convert(allow_no_files=True)
|
||||
|
@ -697,7 +699,7 @@ action = testaction1[actname=test1]
|
|||
filter = testfilter1
|
||||
""")
|
||||
jailfd.close()
|
||||
jails = JailsReader(basedir=basedir, share_config=self.__share_cfg)
|
||||
jails = JailsReader(basedir=basedir, share_config={})
|
||||
self.assertTrue(jails.read())
|
||||
self.assertTrue(jails.getOptions())
|
||||
comm_commands = jails.convert(allow_no_files=True)
|
||||
|
|
|
@ -249,54 +249,3 @@ class FailmanagerComplex(unittest.TestCase):
|
|||
elif i == 65535:
|
||||
self.assertEqual(str(ip), '127.1.0.0')
|
||||
|
||||
def testFailuresMemLeak1(self):
|
||||
# use factor (divisor) instead unittest.F2B.SkipIfFast() :
|
||||
modeDiv = 1
|
||||
if unittest.F2B.fast: # pragma: no cover
|
||||
modeDiv = 10
|
||||
self.__failManager.setMaxTime(self.__failManager.getMaxTime() // modeDiv)
|
||||
import gc
|
||||
gc.collect()
|
||||
timestamp = 1167606999.0
|
||||
ticktime = timestamp-(1000 // modeDiv)
|
||||
for i, ip in self._ip_range(30000 // modeDiv):
|
||||
t = FailTicket(ip, ticktime, list({'match': i}))
|
||||
ticktime += 1
|
||||
self.__failManager.addFailure(t)
|
||||
if i % (500 // modeDiv) == 0:
|
||||
self.__failManager.cleanup(timestamp)
|
||||
if i % (888 // modeDiv) == 0:
|
||||
timestamp += (1000 // modeDiv)
|
||||
ticktime = timestamp-(1000 // modeDiv)
|
||||
self.assertFalse(gc.collect())
|
||||
self.__failManager.cleanup(timestamp)
|
||||
ticktime -= timestamp - self.__failManager.getMaxTime() + 1
|
||||
self.assertEqual(self.__failManager.size(), ticktime if ticktime > 0 else 0)
|
||||
self.assertFalse(gc.collect())
|
||||
|
||||
def testFailuresMemLeak2(self):
|
||||
# use factor (divisor) instead unittest.F2B.SkipIfFast() :
|
||||
modeDiv = 1
|
||||
if unittest.F2B.fast: # pragma: no cover
|
||||
modeDiv = 10
|
||||
self.__failManager.setMaxTime(self.__failManager.getMaxTime() // modeDiv)
|
||||
import gc
|
||||
gc.collect()
|
||||
timestamp = 1167606999.0
|
||||
ticktime = timestamp-(1000 // modeDiv)
|
||||
for i, ip in self._ip_range(10000 // modeDiv):
|
||||
for j in range(0, 5):
|
||||
t = FailTicket(ip, ticktime, list({'match': i}))
|
||||
ticktime += 1
|
||||
self.__failManager.addFailure(t)
|
||||
if i % (500 // modeDiv) == 0:
|
||||
self.__failManager.cleanup(timestamp)
|
||||
if i % (888 // modeDiv) == 0 or i % (1500 // modeDiv) == 0:
|
||||
timestamp += (1000 // modeDiv)
|
||||
ticktime = timestamp-(1000 // modeDiv)
|
||||
self.assertFalse(gc.collect())
|
||||
timestamp += 20000
|
||||
self.__failManager.cleanup(timestamp)
|
||||
self.assertEqual(self.__failManager.size(), 0)
|
||||
self.assertFalse(gc.collect())
|
||||
|
||||
|
|
|
@ -80,6 +80,7 @@ def _getSysPythonVersion():
|
|||
class SetupTest(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
unittest.F2B.SkipIfFast()
|
||||
setup = os.path.join(os.path.dirname(__file__), '..', '..', 'setup.py')
|
||||
self.setup = os.path.exists(setup) and setup or None
|
||||
if not self.setup and sys.version_info >= (2,7): # pragma: no cover - running not out of the source
|
||||
|
|
|
@ -64,7 +64,8 @@ def testSampleRegexsFactory(name):
|
|||
def testFilter(self):
|
||||
|
||||
# Check filter exists
|
||||
filterConf = FilterReader(name, "jail", {}, basedir=CONFIG_DIR)
|
||||
filterConf = FilterReader(name, "jail", {},
|
||||
basedir=CONFIG_DIR, share_config=unittest.F2B.share_config)
|
||||
self.assertEqual(filterConf.getFile(), name)
|
||||
self.assertEqual(filterConf.getJailName(), "jail")
|
||||
filterConf.read()
|
||||
|
|
|
@ -47,6 +47,7 @@ except ImportError: # pragma: no cover
|
|||
filtersystemd = None
|
||||
|
||||
TEST_FILES_DIR = os.path.join(os.path.dirname(__file__), "files")
|
||||
FAST_BACKEND = "polling"
|
||||
|
||||
|
||||
class TestServer(Server):
|
||||
|
@ -72,7 +73,7 @@ class TransmitterBase(unittest.TestCase):
|
|||
self.tmp_files.append(pidfile_name)
|
||||
self.server.start(sock_name, pidfile_name, force=False)
|
||||
self.jailName = "TestJail1"
|
||||
self.server.addJail(self.jailName, "auto")
|
||||
self.server.addJail(self.jailName, FAST_BACKEND)
|
||||
|
||||
def tearDown(self):
|
||||
"""Call after every test case."""
|
||||
|
@ -195,7 +196,7 @@ class Transmitter(TransmitterBase):
|
|||
self.setGetTest("dbpurgeage", "600", 600)
|
||||
self.setGetTestNOK("dbpurgeage", "LIZARD")
|
||||
# the same file name (again with jails / not changed):
|
||||
self.server.addJail(self.jailName, "auto")
|
||||
self.server.addJail(self.jailName, FAST_BACKEND)
|
||||
self.setGetTest("dbfile", tmpFilename)
|
||||
self.server.delJail(self.jailName)
|
||||
|
||||
|
@ -213,7 +214,7 @@ class Transmitter(TransmitterBase):
|
|||
["get", "dbpurgeage"]),
|
||||
(0, None))
|
||||
# the same (again with jails / not changed):
|
||||
self.server.addJail(self.jailName, "auto")
|
||||
self.server.addJail(self.jailName, FAST_BACKEND)
|
||||
self.assertEqual(self.transm.proceed(
|
||||
["set", "dbfile", "None"]),
|
||||
(0, None))
|
||||
|
@ -252,7 +253,7 @@ class Transmitter(TransmitterBase):
|
|||
self.assertTrue(self.jailName not in self.server._Server__jails)
|
||||
|
||||
def testStartStopAllJail(self):
|
||||
self.server.addJail("TestJail2", "auto")
|
||||
self.server.addJail("TestJail2", FAST_BACKEND)
|
||||
self.assertEqual(
|
||||
self.transm.proceed(["start", self.jailName]), (0, None))
|
||||
self.assertEqual(
|
||||
|
@ -497,7 +498,7 @@ class Transmitter(TransmitterBase):
|
|||
jails = [self.jailName]
|
||||
self.assertEqual(self.transm.proceed(["status"]),
|
||||
(0, [('Number of jail', len(jails)), ('Jail list', ", ".join(jails))]))
|
||||
self.server.addJail("TestJail2", "auto")
|
||||
self.server.addJail("TestJail2", FAST_BACKEND)
|
||||
jails.append("TestJail2")
|
||||
self.assertEqual(self.transm.proceed(["status"]),
|
||||
(0, [('Number of jail', len(jails)), ('Jail list', ", ".join(jails))]))
|
||||
|
|
|
@ -59,6 +59,7 @@ class F2B(optparse.Values):
|
|||
if self.fast:
|
||||
self.memory_db = True
|
||||
self.no_gamin = True
|
||||
self.__dict__['share_config'] = {}
|
||||
def SkipIfFast(self):
|
||||
pass
|
||||
def SkipIfNoNetwork(self):
|
||||
|
|
Loading…
Reference in New Issue