mirror of https://github.com/fail2ban/fail2ban
ENH: fail2ban-testcases -- custom logging format to ease debugging, non-0 exit code in case of failure
parent
6ac9fd5d26
commit
08564bda1a
|
@ -63,20 +63,19 @@ parser = get_opt_parser()
|
||||||
(opts, files) = parser.parse_args()
|
(opts, files) = parser.parse_args()
|
||||||
assert(not len(files))
|
assert(not len(files))
|
||||||
|
|
||||||
# Set the time to a fixed, known value
|
#
|
||||||
# Sun Aug 14 12:00:00 CEST 2005
|
# Logging
|
||||||
|
#
|
||||||
# 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)
|
|
||||||
|
|
||||||
# Gets the instance of the logger.
|
|
||||||
logSys = logging.getLogger("fail2ban")
|
logSys = logging.getLogger("fail2ban")
|
||||||
# Add the default logging handler
|
|
||||||
stdout = logging.StreamHandler(sys.stdout)
|
# Numerical level of verbosity corresponding to a log "level"
|
||||||
logSys.addHandler(stdout)
|
verbosity = {'debug': 3,
|
||||||
|
'info': 2,
|
||||||
|
'warn': 1,
|
||||||
|
'error': 1,
|
||||||
|
'fatal': 0,
|
||||||
|
None: 1}[opts.log_level]
|
||||||
|
|
||||||
if opts.log_level is not None:
|
if opts.log_level is not None:
|
||||||
# so we had explicit settings
|
# so we had explicit settings
|
||||||
logSys.setLevel(getattr(logging, opts.log_level.upper()))
|
logSys.setLevel(getattr(logging, opts.log_level.upper()))
|
||||||
|
@ -86,9 +85,27 @@ else:
|
||||||
# unless error occurs
|
# unless error occurs
|
||||||
logSys.setLevel(getattr(logging, 'FATAL'))
|
logSys.setLevel(getattr(logging, 'FATAL'))
|
||||||
|
|
||||||
|
# Add the default logging handler
|
||||||
|
stdout = logging.StreamHandler(sys.stdout)
|
||||||
|
# Custom log format for the verbose tests runs
|
||||||
|
if verbosity > 1:
|
||||||
|
stdout.setFormatter(logging.Formatter(' %(asctime)-15s %(thread)s %(message)s'))
|
||||||
|
else:
|
||||||
|
# just prefix with the space
|
||||||
|
stdout.setFormatter(logging.Formatter(' %(message)s'))
|
||||||
|
logSys.addHandler(stdout)
|
||||||
|
|
||||||
|
|
||||||
|
#
|
||||||
|
# Let know the version
|
||||||
|
#
|
||||||
if not opts.log_level or opts.log_level != 'fatal':
|
if not opts.log_level or opts.log_level != 'fatal':
|
||||||
print "Fail2ban " + version + " test suite. Please wait..."
|
print "Fail2ban " + version + " test suite. Please wait..."
|
||||||
|
|
||||||
|
|
||||||
|
#
|
||||||
|
# Gather the tests
|
||||||
|
#
|
||||||
tests = unittest.TestSuite()
|
tests = unittest.TestSuite()
|
||||||
|
|
||||||
# Server
|
# Server
|
||||||
|
@ -140,19 +157,31 @@ tests.addTest(unittest.makeSuite(filtertestcase.DNSUtilsTests))
|
||||||
# DateDetector
|
# DateDetector
|
||||||
tests.addTest(unittest.makeSuite(datedetectortestcase.DateDetectorTest))
|
tests.addTest(unittest.makeSuite(datedetectortestcase.DateDetectorTest))
|
||||||
|
|
||||||
# Tests runner
|
|
||||||
testRunner = unittest.TextTestRunner(
|
|
||||||
verbosity={'debug': 3,
|
|
||||||
'info': 2,
|
|
||||||
'warn': 1,
|
|
||||||
'error': 1,
|
|
||||||
'fatal': 0,
|
|
||||||
None: 1}[opts.log_level])
|
|
||||||
testRunner.run(tests)
|
|
||||||
|
|
||||||
# Just for the sake of it reset the TZ
|
|
||||||
# yoh is planing to move all this into setup/teardown methods within tests
|
#
|
||||||
os.environ.pop('TZ')
|
# Run the tests
|
||||||
if old_TZ:
|
#
|
||||||
os.environ['TZ'] = old_TZ
|
testRunner = unittest.TextTestRunner(verbosity=verbosity)
|
||||||
time.tzset()
|
|
||||||
|
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:
|
||||||
|
# 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()
|
||||||
|
|
||||||
|
if not tests_results.wasSuccessful():
|
||||||
|
sys.exit(1)
|
||||||
|
|
Loading…
Reference in New Issue