2007-01-21 22:19:57 +00:00
|
|
|
#!/usr/bin/python
|
2011-10-07 19:47:50 +00:00
|
|
|
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: t -*-
|
|
|
|
# vi: set ft=python sts=4 ts=4 sw=4 noet :
|
2012-06-16 02:21:16 +00:00
|
|
|
"""Script to run Fail2Ban tests battery
|
|
|
|
"""
|
2011-10-07 19:47:50 +00:00
|
|
|
|
2006-06-26 20:05:00 +00:00
|
|
|
# This file is part of Fail2Ban.
|
|
|
|
#
|
|
|
|
# Fail2Ban is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Fail2Ban is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Fail2Ban; if not, write to the Free Software
|
2011-11-21 12:20:20 +00:00
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2006-06-26 20:05:00 +00:00
|
|
|
|
|
|
|
__author__ = "Cyril Jaquier"
|
2012-11-09 13:58:19 +00:00
|
|
|
__copyright__ = "Copyright (c) 2004 Cyril Jaquier, 2012- Yaroslav Halchenko"
|
2006-06-26 20:05:00 +00:00
|
|
|
__license__ = "GPL"
|
|
|
|
|
2014-06-22 14:56:50 +00:00
|
|
|
import logging
|
2015-07-03 17:02:50 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
import unittest
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2013-04-01 18:06:13 +00:00
|
|
|
# Check if local fail2ban module exists, and use if it exists by
|
|
|
|
# modifying the path. This is such that tests can be used in dev
|
|
|
|
# environment.
|
|
|
|
if os.path.exists("fail2ban/__init__.py"):
|
|
|
|
sys.path.insert(0, ".")
|
2013-03-31 17:18:21 +00:00
|
|
|
from fail2ban.version import version
|
2013-04-20 19:13:21 +00:00
|
|
|
|
2014-04-18 22:27:30 +00:00
|
|
|
from fail2ban.tests.utils import gatherTests
|
2014-06-10 19:36:19 +00:00
|
|
|
from fail2ban.helpers import FormatterWithTraceBack, getLogger
|
2013-03-31 17:18:21 +00:00
|
|
|
from fail2ban.server.mytime import MyTime
|
2006-10-18 22:35:32 +00:00
|
|
|
|
2012-06-16 02:21:16 +00:00
|
|
|
from optparse import OptionParser, Option
|
|
|
|
|
|
|
|
def get_opt_parser():
|
|
|
|
# use module docstring for help output
|
|
|
|
p = OptionParser(
|
2012-12-11 15:59:01 +00:00
|
|
|
usage="%s [OPTIONS] [regexps]\n" % sys.argv[0] + __doc__,
|
2012-06-16 02:21:16 +00:00
|
|
|
version="%prog " + version)
|
|
|
|
|
|
|
|
p.add_options([
|
|
|
|
Option('-l', "--log-level", type="choice",
|
|
|
|
dest="log_level",
|
2014-02-20 23:01:40 +00:00
|
|
|
choices=('heavydebug', 'debug', 'info', 'notice', 'warning', 'error', 'critical'),
|
2012-06-30 04:35:43 +00:00
|
|
|
default=None,
|
2012-06-16 02:21:16 +00:00
|
|
|
help="Log level for the logger to use during running tests"),
|
2013-01-25 19:56:00 +00:00
|
|
|
Option('-n', "--no-network", action="store_true",
|
|
|
|
dest="no_network",
|
|
|
|
help="Do not run tests that require the network"),
|
2013-03-26 01:28:58 +00:00
|
|
|
Option("-t", "--log-traceback", action='store_true',
|
|
|
|
help="Enrich log-messages with compressed tracebacks"),
|
|
|
|
Option("--full-traceback", action='store_true',
|
|
|
|
help="Either to make the tracebacks full, not compressed (as by default)"),
|
|
|
|
|
2013-01-25 19:56:00 +00:00
|
|
|
])
|
|
|
|
|
2012-06-16 02:21:16 +00:00
|
|
|
return p
|
|
|
|
|
|
|
|
parser = get_opt_parser()
|
2012-12-11 15:59:01 +00:00
|
|
|
(opts, regexps) = parser.parse_args()
|
2012-06-16 02:21:16 +00:00
|
|
|
|
2012-07-19 17:30:55 +00:00
|
|
|
#
|
|
|
|
# Logging
|
|
|
|
#
|
2014-06-10 19:36:19 +00:00
|
|
|
logSys = getLogger("fail2ban")
|
2011-10-07 19:14:13 +00:00
|
|
|
|
2012-07-19 17:30:55 +00:00
|
|
|
# Numerical level of verbosity corresponding to a log "level"
|
2013-12-16 15:08:34 +00:00
|
|
|
verbosity = {'heavydebug': 4,
|
2013-06-18 23:46:25 +00:00
|
|
|
'debug': 3,
|
2012-07-19 17:30:55 +00:00
|
|
|
'info': 2,
|
2014-02-20 23:01:40 +00:00
|
|
|
'notice': 2,
|
2013-07-16 22:16:22 +00:00
|
|
|
'warning': 1,
|
2012-07-19 17:30:55 +00:00
|
|
|
'error': 1,
|
2014-02-20 23:01:40 +00:00
|
|
|
'critical': 0,
|
2012-07-19 17:30:55 +00:00
|
|
|
None: 1}[opts.log_level]
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2013-03-10 04:18:42 +00:00
|
|
|
if opts.log_level is not None: # pragma: no cover
|
2012-06-30 04:35:43 +00:00
|
|
|
# so we had explicit settings
|
|
|
|
logSys.setLevel(getattr(logging, opts.log_level.upper()))
|
2013-03-10 04:18:42 +00:00
|
|
|
else: # pragma: no cover
|
2012-06-30 04:35:43 +00:00
|
|
|
# suppress the logging but it would leave unittests' progress dots
|
2014-02-20 23:01:40 +00:00
|
|
|
# ticking, unless like with '-l critical' which would be silent
|
2012-06-30 04:35:43 +00:00
|
|
|
# unless error occurs
|
2014-02-20 23:01:40 +00:00
|
|
|
logSys.setLevel(getattr(logging, 'CRITICAL'))
|
2012-06-30 04:35:43 +00:00
|
|
|
|
2012-07-19 17:30:55 +00:00
|
|
|
# Add the default logging handler
|
|
|
|
stdout = logging.StreamHandler(sys.stdout)
|
2013-03-26 01:28:58 +00:00
|
|
|
|
|
|
|
fmt = ' %(message)s'
|
|
|
|
|
|
|
|
if opts.log_traceback:
|
|
|
|
Formatter = FormatterWithTraceBack
|
|
|
|
fmt = (opts.full_traceback and ' %(tb)s' or ' %(tbc)s') + fmt
|
|
|
|
else:
|
|
|
|
Formatter = logging.Formatter
|
|
|
|
|
2012-07-19 17:30:55 +00:00
|
|
|
# Custom log format for the verbose tests runs
|
2013-03-10 04:18:42 +00:00
|
|
|
if verbosity > 1: # pragma: no cover
|
2013-03-26 01:28:58 +00:00
|
|
|
stdout.setFormatter(Formatter(' %(asctime)-15s %(thread)s' + fmt))
|
2013-03-10 04:18:42 +00:00
|
|
|
else: # pragma: no cover
|
2012-07-19 17:30:55 +00:00
|
|
|
# just prefix with the space
|
2013-03-26 01:28:58 +00:00
|
|
|
stdout.setFormatter(Formatter(fmt))
|
2012-07-19 17:30:55 +00:00
|
|
|
logSys.addHandler(stdout)
|
|
|
|
|
|
|
|
#
|
|
|
|
# Let know the version
|
|
|
|
#
|
2014-02-20 23:01:40 +00:00
|
|
|
if not opts.log_level or opts.log_level != 'critical': # pragma: no cover
|
2013-09-08 11:02:35 +00:00
|
|
|
print("Fail2ban %s test suite. Python %s. Please wait..." \
|
|
|
|
% (version, str(sys.version).replace('\n', '')))
|
2006-06-26 20:05:00 +00:00
|
|
|
|
2013-04-20 19:13:21 +00:00
|
|
|
tests = gatherTests(regexps, opts.no_network)
|
2012-07-19 17:30:55 +00:00
|
|
|
#
|
|
|
|
# Run the tests
|
|
|
|
#
|
|
|
|
testRunner = unittest.TextTestRunner(verbosity=verbosity)
|
|
|
|
|
2013-04-20 19:07:23 +00:00
|
|
|
tests_results = testRunner.run(tests)
|
2012-07-19 17:30:55 +00:00
|
|
|
|
2013-03-10 04:18:42 +00:00
|
|
|
if not tests_results.wasSuccessful(): # pragma: no cover
|
2012-07-19 17:30:55 +00:00
|
|
|
sys.exit(1)
|