From 4cc3a81cc1cc7ce3fd496e854a4476890a4a6cd4 Mon Sep 17 00:00:00 2001 From: Steven Hiscocks Date: Sat, 20 Apr 2013 20:07:23 +0100 Subject: [PATCH] TST: Move test TZ changes to setUp and tearDown methods --- bin/fail2ban-testcases | 19 +------------------ fail2ban/tests/datedetectortestcase.py | 3 +++ fail2ban/tests/filtertestcase.py | 7 +++++++ fail2ban/tests/utils.py | 19 ++++++++++++++++++- 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/bin/fail2ban-testcases b/bin/fail2ban-testcases index 68a31786..cd9596a0 100755 --- a/bin/fail2ban-testcases +++ b/bin/fail2ban-testcases @@ -205,24 +205,7 @@ tests.addTest(unittest.makeSuite(servertestcase.TransmitterLogging)) # testRunner = unittest.TextTestRunner(verbosity=verbosity) -try: - # Set the time to a fixed, known value - # Sun Aug 14 12:00:00 CEST 2005 - # yoh: we need to adjust TZ to match the one used by Cyril so all the timestamps match - old_TZ = os.environ.get('TZ', None) - os.environ['TZ'] = 'Europe/Zurich' - time.tzset() - MyTime.setTime(1124013600) - - tests_results = testRunner.run(tests) - -finally: # pragma: no cover - # Just for the sake of it reset the TZ - # yoh: move all this into setup/teardown methods within tests - os.environ.pop('TZ') - if old_TZ: - os.environ['TZ'] = old_TZ - time.tzset() +tests_results = testRunner.run(tests) if not tests_results.wasSuccessful(): # pragma: no cover sys.exit(1) diff --git a/fail2ban/tests/datedetectortestcase.py b/fail2ban/tests/datedetectortestcase.py index 23f7a174..534abdbb 100644 --- a/fail2ban/tests/datedetectortestcase.py +++ b/fail2ban/tests/datedetectortestcase.py @@ -31,16 +31,19 @@ import unittest from fail2ban.server.datedetector import DateDetector from fail2ban.server.datetemplate import DateTemplate +from fail2ban.tests.utils import setUpMyTime, tearDownMyTime class DateDetectorTest(unittest.TestCase): def setUp(self): """Call before every test case.""" + setUpMyTime() self.__datedetector = DateDetector() self.__datedetector.addDefaultTemplate() def tearDown(self): """Call after every test case.""" + tearDownMyTime() def testGetEpochTime(self): log = "1138049999 [sshd] error: PAM: Authentication failure" diff --git a/fail2ban/tests/filtertestcase.py b/fail2ban/tests/filtertestcase.py index fa9340ca..053086d4 100644 --- a/fail2ban/tests/filtertestcase.py +++ b/fail2ban/tests/filtertestcase.py @@ -34,6 +34,7 @@ from fail2ban.server.filterpoll import FilterPoll from fail2ban.server.filter import FileFilter, DNSUtils from fail2ban.server.failmanager import FailManager from fail2ban.server.failmanager import FailManagerEmpty +from fail2ban.tests.utils import setUpMyTime, tearDownMyTime TEST_FILES_DIR = os.path.join(os.path.dirname(__file__), "files") @@ -213,6 +214,7 @@ class LogFileMonitor(unittest.TestCase): """ def setUp(self): """Call before every test case.""" + setUpMyTime() self.filter = self.name = 'NA' _, self.name = tempfile.mkstemp('fail2ban', 'monitorfailures') self.file = open(self.name, 'a') @@ -222,6 +224,7 @@ class LogFileMonitor(unittest.TestCase): self.filter.addFailRegex("(?:(?:Authentication failure|Failed [-/\w+]+) for(?: [iI](?:llegal|nvalid) user)?|[Ii](?:llegal|nvalid) user|ROOT LOGIN REFUSED) .*(?: from|FROM) ") def tearDown(self): + tearDownMyTime() _killfile(self.file, self.name) pass @@ -363,6 +366,7 @@ def get_monitor_failures_testcase(Filter_): count = 0 def setUp(self): """Call before every test case.""" + setUpMyTime() self.filter = self.name = 'NA' self.name = '%s-%d' % (testclass_name, self.count) MonitorFailures.count += 1 # so we have unique filenames across tests @@ -380,6 +384,7 @@ def get_monitor_failures_testcase(Filter_): def tearDown(self): + tearDownMyTime() #print "D: SLEEPING A BIT" #import time; time.sleep(5) #print "D: TEARING DOWN" @@ -543,6 +548,7 @@ class GetFailures(unittest.TestCase): def setUp(self): """Call before every test case.""" + setUpMyTime() self.filter = FileFilter(None) self.filter.setActive(True) # TODO Test this @@ -551,6 +557,7 @@ class GetFailures(unittest.TestCase): def tearDown(self): """Call after every test case.""" + tearDownMyTime() diff --git a/fail2ban/tests/utils.py b/fail2ban/tests/utils.py index 6b894193..a5d33983 100644 --- a/fail2ban/tests/utils.py +++ b/fail2ban/tests/utils.py @@ -22,9 +22,11 @@ __author__ = "Yaroslav Halchenko" __copyright__ = "Copyright (c) 2013 Yaroslav Halchenko" __license__ = "GPL" -import logging, os, re, traceback +import logging, os, re, traceback, time from os.path import basename, dirname +from fail2ban.server.mytime import MyTime + # # Following "traceback" functions are adopted from PyMVPA distributed # under MIT/Expat and copyright by PyMVPA developers (i.e. me and @@ -99,3 +101,18 @@ class FormatterWithTraceBack(logging.Formatter): def format(self, record): record.tbc = record.tb = self._tb() return logging.Formatter.format(self, record) + +old_TZ = os.environ.get('TZ', None) +def setUpMyTime(): + # Set the time to a fixed, known value + # Sun Aug 14 12:00:00 CEST 2005 + # yoh: we need to adjust TZ to match the one used by Cyril so all the timestamps match + os.environ['TZ'] = 'Europe/Zurich' + time.tzset() + MyTime.setTime(1124013600) + +def tearDownMyTime(): + os.environ.pop('TZ') + if old_TZ: + os.environ['TZ'] = old_TZ + time.tzset()