better handling of setup in test-cases (prevent execution of 2to3 twice, full logging in heavydebug, etc.)

pull/1510/head
sebres 2016-08-12 11:07:31 +02:00
parent d71a525a85
commit 461e9bab56
3 changed files with 40 additions and 13 deletions

View File

@ -113,6 +113,7 @@ else: # pragma: no cover
# ticking, unless like with '-l critical' which would be silent # ticking, unless like with '-l critical' which would be silent
# unless error occurs # unless error occurs
logSys.setLevel(getattr(logging, 'CRITICAL')) logSys.setLevel(getattr(logging, 'CRITICAL'))
opts.log_level = logSys.level
# Add the default logging handler # Add the default logging handler
stdout = logging.StreamHandler(sys.stdout) stdout = logging.StreamHandler(sys.stdout)
@ -140,7 +141,7 @@ logSys.addHandler(stdout)
# #
# Let know the version # Let know the version
# #
if not opts.log_level or opts.log_level != 'critical': # pragma: no cover if opts.log_level != logging.CRITICAL: # pragma: no cover
print("Fail2ban %s test suite. Python %s. Please wait..." \ print("Fail2ban %s test suite. Python %s. Please wait..." \
% (version, str(sys.version).replace('\n', ''))) % (version, str(sys.version).replace('\n', '')))

View File

@ -87,6 +87,7 @@ else:
def _getSysPythonVersion(): def _getSysPythonVersion():
return _sh_call("fail2ban-python -c 'import sys; print(tuple(sys.version_info))'") return _sh_call("fail2ban-python -c 'import sys; print(tuple(sys.version_info))'")
class SetupTest(unittest.TestCase): class SetupTest(unittest.TestCase):
def setUp(self): def setUp(self):
@ -108,9 +109,11 @@ class SetupTest(unittest.TestCase):
if not self.setup: if not self.setup:
return # if verbose skip didn't work out return # if verbose skip didn't work out
tmp = tempfile.mkdtemp() tmp = tempfile.mkdtemp()
# suppress stdout (and stderr) if not heavydebug
supdbgout = ' >/dev/null' if unittest.F2B.log_level >= logging.DEBUG else '' # HEAVYDEBUG
try: try:
os.system("%s %s install --root=%s >/dev/null" os.system("%s %s install --disable-2to3 --dry-run --root=%s%s"
% (sys.executable, self.setup, tmp)) % (sys.executable, self.setup, tmp, supdbgout))
def strippath(l): def strippath(l):
return [x[len(tmp)+1:] for x in l] return [x[len(tmp)+1:] for x in l]
@ -161,8 +164,8 @@ class SetupTest(unittest.TestCase):
# clean up # clean up
shutil.rmtree(tmp) shutil.rmtree(tmp)
# remove build directory # remove build directory
os.system("%s %s clean --all >/dev/null 2>&1" os.system("%s %s clean --all%s"
% (sys.executable, self.setup)) % (sys.executable, self.setup, (supdbgout + ' 2>&1') if supdbgout else ''))
class TestsUtilsTest(LogCaptureTestCase): class TestsUtilsTest(LogCaptureTestCase):

View File

@ -31,17 +31,19 @@ except ImportError:
setuptools = None setuptools = None
from distutils.core import setup from distutils.core import setup
# all versions
from distutils.command.build_py import build_py
from distutils.command.build_scripts import build_scripts
from distutils.command.install_scripts import install_scripts
from distutils.command.install import install
try: try:
# python 3.x # python 3.x
from distutils.command.build_py import build_py_2to3 as build_py from distutils.command.build_py import build_py_2to3
from distutils.command.build_scripts \ from distutils.command.build_scripts import build_scripts_2to3
import build_scripts_2to3 as build_scripts _2to3 = True
except ImportError: except ImportError:
# python 2.x # python 2.x
from distutils.command.build_py import build_py _2to3 = False
from distutils.command.build_scripts import build_scripts
# all versions
from distutils.command.install_scripts import install_scripts
import os import os
from os.path import isfile, join, isdir, realpath from os.path import isfile, join, isdir, realpath
@ -66,6 +68,27 @@ class install_scripts_f2b(install_scripts):
updatePyExec(bindir) updatePyExec(bindir)
return outputs return outputs
# Wrapper to specify fail2ban own options:
class install_command_f2b(install):
user_options = install.user_options + [
('disable-2to3', None, 'Specify to deactivate 2to3, e.g. if the install runs from fail2ban test-cases.'),
]
def initialize_options(self):
self.disable_2to3 = None
install.initialize_options(self)
def finalize_options(self):
global _2to3
## in the test cases 2to3 should be already done (fail2ban-2to3):
if self.disable_2to3:
_2to3 = False
if _2to3:
cmdclass = self.distribution.cmdclass
cmdclass['build_py'] = build_py_2to3
cmdclass['build_scripts'] = build_scripts_2to3
install.finalize_options(self)
def run(self):
install.run(self)
# Update fail2ban-python env to current python version (where f2b-modules located/installed) # Update fail2ban-python env to current python version (where f2b-modules located/installed)
rootdir = os.path.realpath(os.path.dirname( rootdir = os.path.realpath(os.path.dirname(
@ -143,7 +166,7 @@ setup(
platforms = "Posix", platforms = "Posix",
cmdclass = { cmdclass = {
'build_py': build_py, 'build_scripts': build_scripts, 'build_py': build_py, 'build_scripts': build_scripts,
'install_scripts': install_scripts_f2b 'install_scripts': install_scripts_f2b, 'install': install_command_f2b
}, },
scripts = [ scripts = [
'bin/fail2ban-client', 'bin/fail2ban-client',