From f31607ded102c8fb8edca29b16af5994f78b999c Mon Sep 17 00:00:00 2001 From: sebres Date: Tue, 7 Oct 2014 14:07:50 +0200 Subject: [PATCH] test case for check the read of config files will be cached; Conflicts: fail2ban/tests/clientreadertestcase.py -- removed not needed time in imports --- fail2ban/tests/clientreadertestcase.py | 32 +++++++++++++++++++++++++- fail2ban/tests/utils.py | 4 ++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/fail2ban/tests/clientreadertestcase.py b/fail2ban/tests/clientreadertestcase.py index 0ad3c66e..f65ef87a 100644 --- a/fail2ban/tests/clientreadertestcase.py +++ b/fail2ban/tests/clientreadertestcase.py @@ -21,7 +21,7 @@ __author__ = "Cyril Jaquier, Yaroslav Halchenko" __copyright__ = "Copyright (c) 2004 Cyril Jaquier, 2011-2013 Yaroslav Halchenko" __license__ = "GPL" -import os, glob, shutil, tempfile, unittest +import os, glob, shutil, tempfile, unittest, re from ..client.configreader import ConfigReader from ..client.jailreader import JailReader @@ -335,6 +335,36 @@ class FilterReaderTest(unittest.TestCase): self.assertRaises(ValueError, FilterReader.convert, filterReader) +class JailsReaderTestCache(LogCaptureTestCase): + + def testTestJailConfCache(self): + basedir = tempfile.mkdtemp("fail2ban_conf") + try: + shutil.rmtree(basedir) + shutil.copytree(CONFIG_DIR, basedir) + shutil.copy(CONFIG_DIR + '/jail.conf', basedir + '/jail.local') + shutil.copy(CONFIG_DIR + '/fail2ban.conf', basedir + '/fail2ban.local') + + # read whole configuration like a file2ban-client ... + configurator = Configurator() + configurator.setBaseDir(basedir) + configurator.readEarly() + configurator.getEarlyOptions() + configurator.readAll() + # from here we test a cache : + self.assertTrue(configurator.getOptions(None)) + cnt = 0 + for s in self.getLog().rsplit('\n'): + if re.match(r"^Reading files: .*jail.local", s): + cnt += 1 + # if cnt > 2: + # self.printLog() + self.assertFalse(cnt > 2, "Too many times reading of config files, cnt = %s" % cnt) + self.assertFalse(cnt <= 0) + finally: + shutil.rmtree(basedir) + + class JailsReaderTest(LogCaptureTestCase): def testProvidingBadBasedir(self): diff --git a/fail2ban/tests/utils.py b/fail2ban/tests/utils.py index 912c5a90..ad504703 100644 --- a/fail2ban/tests/utils.py +++ b/fail2ban/tests/utils.py @@ -111,6 +111,7 @@ def gatherTests(regexps=None, no_network=False): tests.addTest(unittest.makeSuite(clientreadertestcase.JailReaderTest)) tests.addTest(unittest.makeSuite(clientreadertestcase.FilterReaderTest)) tests.addTest(unittest.makeSuite(clientreadertestcase.JailsReaderTest)) + tests.addTest(unittest.makeSuite(clientreadertestcase.JailsReaderTestCache)) # CSocket and AsyncServer tests.addTest(unittest.makeSuite(sockettestcase.Socket)) # Misc helpers @@ -216,5 +217,8 @@ class LogCaptureTestCase(unittest.TestCase): def _is_logged(self, s): return s in self._log.getvalue() + def getLog(self): + return self._log.getvalue() + def printLog(self): print(self._log.getvalue())