From e57321ab1e47aa12fbe4d73273477196c80584cd Mon Sep 17 00:00:00 2001 From: Yaroslav Halchenko Date: Thu, 12 May 2016 09:37:30 -0400 Subject: [PATCH] BF+ENH: import signal used in the code, withtmpdir -> with_tmpdir (more readable) case shows how evil except Exception can be ;) --- fail2ban/tests/fail2banclienttestcase.py | 19 ++++++++++--------- fail2ban/tests/utils.py | 8 +++++++- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/fail2ban/tests/fail2banclienttestcase.py b/fail2ban/tests/fail2banclienttestcase.py index 82ffe9b3..53bc41d9 100644 --- a/fail2ban/tests/fail2banclienttestcase.py +++ b/fail2ban/tests/fail2banclienttestcase.py @@ -28,6 +28,7 @@ import os import re import sys import time +import signal import unittest from threading import Thread @@ -38,7 +39,7 @@ from ..client.fail2banserver import Fail2banServer, exec_command_line as _exec_s from .. import protocol from ..server import server from ..server.utils import Utils -from .utils import LogCaptureTestCase, logSys, withtmpdir, shutil, logging +from .utils import LogCaptureTestCase, logSys, with_tmpdir, shutil, logging STOCK_CONF_DIR = "config" @@ -252,7 +253,7 @@ class Fail2banClientTest(Fail2banClientServerBase): (CLIENT, "-vq", "-V",)) self.assertLogged("Fail2Ban v" + fail2bancmdline.version) - @withtmpdir + @with_tmpdir def testClientDump(self, tmp): # use here the stock configuration (if possible) startparams = _start_params(tmp, True) @@ -261,7 +262,7 @@ class Fail2banClientTest(Fail2banClientServerBase): self.assertLogged("Loading files") self.assertLogged("logtarget") - @withtmpdir + @with_tmpdir def testClientStartBackgroundInside(self, tmp): try: # use once the stock configuration (to test starting also) @@ -300,7 +301,7 @@ class Fail2banClientTest(Fail2banClientServerBase): finally: _kill_srv(tmp) - @withtmpdir + @with_tmpdir def testClientStartBackgroundCall(self, tmp): try: global INTERACT @@ -382,7 +383,7 @@ class Fail2banClientTest(Fail2banClientServerBase): phase['end'] = True logSys.debug("-- end of test worker") - @withtmpdir + @with_tmpdir def testClientStartForeground(self, tmp): th = None try: @@ -422,7 +423,7 @@ class Fail2banClientTest(Fail2banClientServerBase): if th: th.join() - @withtmpdir + @with_tmpdir def testClientFailStart(self, tmp): try: # started directly here, so prevent overwrite test cases logger with "INHERITED" @@ -483,7 +484,7 @@ class Fail2banServerTest(Fail2banClientServerBase): self.assertLogged("Usage: " + SERVER) self.assertLogged("Report bugs to ") - @withtmpdir + @with_tmpdir def testServerStartBackground(self, tmp): try: # to prevent fork of test-cases process, start server in background via command: @@ -523,7 +524,7 @@ class Fail2banServerTest(Fail2banClientServerBase): phase['end'] = True logSys.debug("-- end of test worker") - @withtmpdir + @with_tmpdir def testServerStartForeground(self, tmp): th = None try: @@ -563,7 +564,7 @@ class Fail2banServerTest(Fail2banClientServerBase): if th: th.join() - @withtmpdir + @with_tmpdir def testServerFailStart(self, tmp): try: # started directly here, so prevent overwrite test cases logger with "INHERITED" diff --git a/fail2ban/tests/utils.py b/fail2ban/tests/utils.py index bfbef4de..be8ce015 100644 --- a/fail2ban/tests/utils.py +++ b/fail2ban/tests/utils.py @@ -79,7 +79,12 @@ class F2B(optparse.Values): return wtime -def withtmpdir(f): +def with_tmpdir(f): + """Helper decorator to create a temporary directory + + Directory gets removed after function returns, regardless + if exception was thrown of not + """ @wraps(f) def wrapper(self, *args, **kwargs): tmp = tempfile.mkdtemp(prefix="f2b-temp") @@ -90,6 +95,7 @@ def withtmpdir(f): shutil.rmtree(tmp) return wrapper + def initTests(opts): unittest.F2B = F2B(opts) # --fast :