BF+ENH: import signal used in the code, withtmpdir -> with_tmpdir (more readable)

case shows how evil except Exception can be ;)
pull/1483/head
Yaroslav Halchenko 2016-05-12 09:37:30 -04:00 committed by sebres
parent 6353de8b0f
commit e57321ab1e
2 changed files with 17 additions and 10 deletions

View File

@ -28,6 +28,7 @@ import os
import re import re
import sys import sys
import time import time
import signal
import unittest import unittest
from threading import Thread from threading import Thread
@ -38,7 +39,7 @@ from ..client.fail2banserver import Fail2banServer, exec_command_line as _exec_s
from .. import protocol from .. import protocol
from ..server import server from ..server import server
from ..server.utils import Utils 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" STOCK_CONF_DIR = "config"
@ -252,7 +253,7 @@ class Fail2banClientTest(Fail2banClientServerBase):
(CLIENT, "-vq", "-V",)) (CLIENT, "-vq", "-V",))
self.assertLogged("Fail2Ban v" + fail2bancmdline.version) self.assertLogged("Fail2Ban v" + fail2bancmdline.version)
@withtmpdir @with_tmpdir
def testClientDump(self, tmp): def testClientDump(self, tmp):
# use here the stock configuration (if possible) # use here the stock configuration (if possible)
startparams = _start_params(tmp, True) startparams = _start_params(tmp, True)
@ -261,7 +262,7 @@ class Fail2banClientTest(Fail2banClientServerBase):
self.assertLogged("Loading files") self.assertLogged("Loading files")
self.assertLogged("logtarget") self.assertLogged("logtarget")
@withtmpdir @with_tmpdir
def testClientStartBackgroundInside(self, tmp): def testClientStartBackgroundInside(self, tmp):
try: try:
# use once the stock configuration (to test starting also) # use once the stock configuration (to test starting also)
@ -300,7 +301,7 @@ class Fail2banClientTest(Fail2banClientServerBase):
finally: finally:
_kill_srv(tmp) _kill_srv(tmp)
@withtmpdir @with_tmpdir
def testClientStartBackgroundCall(self, tmp): def testClientStartBackgroundCall(self, tmp):
try: try:
global INTERACT global INTERACT
@ -382,7 +383,7 @@ class Fail2banClientTest(Fail2banClientServerBase):
phase['end'] = True phase['end'] = True
logSys.debug("-- end of test worker") logSys.debug("-- end of test worker")
@withtmpdir @with_tmpdir
def testClientStartForeground(self, tmp): def testClientStartForeground(self, tmp):
th = None th = None
try: try:
@ -422,7 +423,7 @@ class Fail2banClientTest(Fail2banClientServerBase):
if th: if th:
th.join() th.join()
@withtmpdir @with_tmpdir
def testClientFailStart(self, tmp): def testClientFailStart(self, tmp):
try: try:
# started directly here, so prevent overwrite test cases logger with "INHERITED" # started directly here, so prevent overwrite test cases logger with "INHERITED"
@ -483,7 +484,7 @@ class Fail2banServerTest(Fail2banClientServerBase):
self.assertLogged("Usage: " + SERVER) self.assertLogged("Usage: " + SERVER)
self.assertLogged("Report bugs to ") self.assertLogged("Report bugs to ")
@withtmpdir @with_tmpdir
def testServerStartBackground(self, tmp): def testServerStartBackground(self, tmp):
try: try:
# to prevent fork of test-cases process, start server in background via command: # to prevent fork of test-cases process, start server in background via command:
@ -523,7 +524,7 @@ class Fail2banServerTest(Fail2banClientServerBase):
phase['end'] = True phase['end'] = True
logSys.debug("-- end of test worker") logSys.debug("-- end of test worker")
@withtmpdir @with_tmpdir
def testServerStartForeground(self, tmp): def testServerStartForeground(self, tmp):
th = None th = None
try: try:
@ -563,7 +564,7 @@ class Fail2banServerTest(Fail2banClientServerBase):
if th: if th:
th.join() th.join()
@withtmpdir @with_tmpdir
def testServerFailStart(self, tmp): def testServerFailStart(self, tmp):
try: try:
# started directly here, so prevent overwrite test cases logger with "INHERITED" # started directly here, so prevent overwrite test cases logger with "INHERITED"

View File

@ -79,7 +79,12 @@ class F2B(optparse.Values):
return wtime 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) @wraps(f)
def wrapper(self, *args, **kwargs): def wrapper(self, *args, **kwargs):
tmp = tempfile.mkdtemp(prefix="f2b-temp") tmp = tempfile.mkdtemp(prefix="f2b-temp")
@ -90,6 +95,7 @@ def withtmpdir(f):
shutil.rmtree(tmp) shutil.rmtree(tmp)
return wrapper return wrapper
def initTests(opts): def initTests(opts):
unittest.F2B = F2B(opts) unittest.F2B = F2B(opts)
# --fast : # --fast :