Browse Source

TST: Move test TZ changes to setUp and tearDown methods

pull/181/head
Steven Hiscocks 12 years ago
parent
commit
4cc3a81cc1
  1. 19
      bin/fail2ban-testcases
  2. 3
      fail2ban/tests/datedetectortestcase.py
  3. 7
      fail2ban/tests/filtertestcase.py
  4. 19
      fail2ban/tests/utils.py

19
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)

3
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"

7
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) <HOST>")
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()

19
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()

Loading…
Cancel
Save