RF+ENH: @with_kill_srv fixture to kill_srv in the tests

pull/1483/head
Yaroslav Halchenko 2016-05-12 09:47:01 -04:00 committed by sebres
parent e57321ab1e
commit d7ff7d18cd
1 changed files with 183 additions and 179 deletions

View File

@ -31,8 +31,10 @@ import time
import signal import signal
import unittest import unittest
from functools import wraps
from threading import Thread from threading import Thread
from ..client import fail2banclient, fail2banserver, fail2bancmdline from ..client import fail2banclient, fail2banserver, fail2bancmdline
from ..client.fail2banclient import Fail2banClient, exec_command_line as _exec_client, VisualWait from ..client.fail2banclient import Fail2banClient, exec_command_line as _exec_client, VisualWait
from ..client.fail2banserver import Fail2banServer, exec_command_line as _exec_server from ..client.fail2banserver import Fail2banServer, exec_command_line as _exec_server
@ -205,6 +207,20 @@ def _kill_srv(pidfile): # pragma: no cover
f.close() f.close()
return True return True
def with_kill_srv(f):
"""Helper to decorate tests which receive in the last argument tmpdir to pass to kill_srv
To be used in tandem with @with_tmpdir
"""
@wraps(f)
def wrapper(self, *args):
pidfile = args[-1]
try:
return f(self, *args)
finally:
_kill_srv(pidfile)
return wrapper
class Fail2banClientServerBase(LogCaptureTestCase): class Fail2banClientServerBase(LogCaptureTestCase):
@ -263,8 +279,8 @@ class Fail2banClientTest(Fail2banClientServerBase):
self.assertLogged("logtarget") self.assertLogged("logtarget")
@with_tmpdir @with_tmpdir
@with_kill_srv
def testClientStartBackgroundInside(self, tmp): def testClientStartBackgroundInside(self, tmp):
try:
# use once the stock configuration (to test starting also) # use once the stock configuration (to test starting also)
startparams = _start_params(tmp, True) startparams = _start_params(tmp, True)
# start: # start:
@ -298,12 +314,10 @@ class Fail2banClientTest(Fail2banClientServerBase):
(CLIENT,) + startparams + ("stop",)) (CLIENT,) + startparams + ("stop",))
self.assertLogged("Failed to access socket path") self.assertLogged("Failed to access socket path")
self.assertLogged("Is fail2ban running?") self.assertLogged("Is fail2ban running?")
finally:
_kill_srv(tmp)
@with_tmpdir @with_tmpdir
@with_kill_srv
def testClientStartBackgroundCall(self, tmp): def testClientStartBackgroundCall(self, tmp):
try:
global INTERACT global INTERACT
startparams = _start_params(tmp, logtarget=tmp+"/f2b.log") startparams = _start_params(tmp, logtarget=tmp+"/f2b.log")
# start (in new process, using the same python version): # start (in new process, using the same python version):
@ -370,8 +384,6 @@ class Fail2banClientTest(Fail2banClientServerBase):
(CLIENT,) + startparams + ("stop",)) (CLIENT,) + startparams + ("stop",))
self.assertLogged("Shutdown successful") self.assertLogged("Shutdown successful")
self.assertLogged("Exit with code 0") self.assertLogged("Exit with code 0")
finally:
_kill_srv(tmp)
def _testClientStartForeground(self, tmp, startparams, phase): def _testClientStartForeground(self, tmp, startparams, phase):
# start and wait to end (foreground): # start and wait to end (foreground):
@ -424,8 +436,8 @@ class Fail2banClientTest(Fail2banClientServerBase):
th.join() th.join()
@with_tmpdir @with_tmpdir
@with_kill_srv
def testClientFailStart(self, tmp): def testClientFailStart(self, tmp):
try:
# started directly here, so prevent overwrite test cases logger with "INHERITED" # started directly here, so prevent overwrite test cases logger with "INHERITED"
startparams = _start_params(tmp, logtarget="INHERITED") startparams = _start_params(tmp, logtarget="INHERITED")
@ -461,9 +473,6 @@ class Fail2banClientTest(Fail2banClientServerBase):
self.assertLogged("Usage: ") self.assertLogged("Usage: ")
self.pruneLog() self.pruneLog()
finally:
_kill_srv(tmp)
def testVisualWait(self): def testVisualWait(self):
sleeptime = 0.035 sleeptime = 0.035
for verbose in (2, 0): for verbose in (2, 0):
@ -485,8 +494,8 @@ class Fail2banServerTest(Fail2banClientServerBase):
self.assertLogged("Report bugs to ") self.assertLogged("Report bugs to ")
@with_tmpdir @with_tmpdir
@with_kill_srv
def testServerStartBackground(self, tmp): def testServerStartBackground(self, tmp):
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:
startparams = _start_params(tmp, logtarget=tmp+"/f2b.log") startparams = _start_params(tmp, logtarget=tmp+"/f2b.log")
# start (in new process, using the same python version): # start (in new process, using the same python version):
@ -511,8 +520,6 @@ class Fail2banServerTest(Fail2banClientServerBase):
(SERVER,) + startparams + ("stop",)) (SERVER,) + startparams + ("stop",))
self.assertLogged("Shutdown successful") self.assertLogged("Shutdown successful")
self.assertLogged("Exit with code 0") self.assertLogged("Exit with code 0")
finally:
_kill_srv(tmp)
def _testServerStartForeground(self, tmp, startparams, phase): def _testServerStartForeground(self, tmp, startparams, phase):
# start and wait to end (foreground): # start and wait to end (foreground):
@ -565,8 +572,8 @@ class Fail2banServerTest(Fail2banClientServerBase):
th.join() th.join()
@with_tmpdir @with_tmpdir
@with_kill_srv
def testServerFailStart(self, tmp): def testServerFailStart(self, tmp):
try:
# started directly here, so prevent overwrite test cases logger with "INHERITED" # started directly here, so prevent overwrite test cases logger with "INHERITED"
startparams = _start_params(tmp, logtarget="INHERITED") startparams = _start_params(tmp, logtarget="INHERITED")
@ -589,6 +596,3 @@ class Fail2banServerTest(Fail2banClientServerBase):
self.assertLogged("Fail2ban seems to be in unexpected state (not running but the socket exists)") self.assertLogged("Fail2ban seems to be in unexpected state (not running but the socket exists)")
self.pruneLog() self.pruneLog()
os.remove(tmp+"/f2b.sock") os.remove(tmp+"/f2b.sock")
finally:
_kill_srv(tmp)