mirror of https://github.com/fail2ban/fail2ban
RF+ENH: @with_kill_srv fixture to kill_srv in the tests
parent
e57321ab1e
commit
d7ff7d18cd
|
@ -31,8 +31,10 @@ import time
|
|||
import signal
|
||||
import unittest
|
||||
|
||||
from functools import wraps
|
||||
from threading import Thread
|
||||
|
||||
|
||||
from ..client import fail2banclient, fail2banserver, fail2bancmdline
|
||||
from ..client.fail2banclient import Fail2banClient, exec_command_line as _exec_client, VisualWait
|
||||
from ..client.fail2banserver import Fail2banServer, exec_command_line as _exec_server
|
||||
|
@ -205,6 +207,20 @@ def _kill_srv(pidfile): # pragma: no cover
|
|||
f.close()
|
||||
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):
|
||||
|
||||
|
@ -263,8 +279,8 @@ class Fail2banClientTest(Fail2banClientServerBase):
|
|||
self.assertLogged("logtarget")
|
||||
|
||||
@with_tmpdir
|
||||
@with_kill_srv
|
||||
def testClientStartBackgroundInside(self, tmp):
|
||||
try:
|
||||
# use once the stock configuration (to test starting also)
|
||||
startparams = _start_params(tmp, True)
|
||||
# start:
|
||||
|
@ -298,12 +314,10 @@ class Fail2banClientTest(Fail2banClientServerBase):
|
|||
(CLIENT,) + startparams + ("stop",))
|
||||
self.assertLogged("Failed to access socket path")
|
||||
self.assertLogged("Is fail2ban running?")
|
||||
finally:
|
||||
_kill_srv(tmp)
|
||||
|
||||
@with_tmpdir
|
||||
@with_kill_srv
|
||||
def testClientStartBackgroundCall(self, tmp):
|
||||
try:
|
||||
global INTERACT
|
||||
startparams = _start_params(tmp, logtarget=tmp+"/f2b.log")
|
||||
# start (in new process, using the same python version):
|
||||
|
@ -370,8 +384,6 @@ class Fail2banClientTest(Fail2banClientServerBase):
|
|||
(CLIENT,) + startparams + ("stop",))
|
||||
self.assertLogged("Shutdown successful")
|
||||
self.assertLogged("Exit with code 0")
|
||||
finally:
|
||||
_kill_srv(tmp)
|
||||
|
||||
def _testClientStartForeground(self, tmp, startparams, phase):
|
||||
# start and wait to end (foreground):
|
||||
|
@ -424,8 +436,8 @@ class Fail2banClientTest(Fail2banClientServerBase):
|
|||
th.join()
|
||||
|
||||
@with_tmpdir
|
||||
@with_kill_srv
|
||||
def testClientFailStart(self, tmp):
|
||||
try:
|
||||
# started directly here, so prevent overwrite test cases logger with "INHERITED"
|
||||
startparams = _start_params(tmp, logtarget="INHERITED")
|
||||
|
||||
|
@ -461,9 +473,6 @@ class Fail2banClientTest(Fail2banClientServerBase):
|
|||
self.assertLogged("Usage: ")
|
||||
self.pruneLog()
|
||||
|
||||
finally:
|
||||
_kill_srv(tmp)
|
||||
|
||||
def testVisualWait(self):
|
||||
sleeptime = 0.035
|
||||
for verbose in (2, 0):
|
||||
|
@ -485,8 +494,8 @@ class Fail2banServerTest(Fail2banClientServerBase):
|
|||
self.assertLogged("Report bugs to ")
|
||||
|
||||
@with_tmpdir
|
||||
@with_kill_srv
|
||||
def testServerStartBackground(self, tmp):
|
||||
try:
|
||||
# to prevent fork of test-cases process, start server in background via command:
|
||||
startparams = _start_params(tmp, logtarget=tmp+"/f2b.log")
|
||||
# start (in new process, using the same python version):
|
||||
|
@ -511,8 +520,6 @@ class Fail2banServerTest(Fail2banClientServerBase):
|
|||
(SERVER,) + startparams + ("stop",))
|
||||
self.assertLogged("Shutdown successful")
|
||||
self.assertLogged("Exit with code 0")
|
||||
finally:
|
||||
_kill_srv(tmp)
|
||||
|
||||
def _testServerStartForeground(self, tmp, startparams, phase):
|
||||
# start and wait to end (foreground):
|
||||
|
@ -565,8 +572,8 @@ class Fail2banServerTest(Fail2banClientServerBase):
|
|||
th.join()
|
||||
|
||||
@with_tmpdir
|
||||
@with_kill_srv
|
||||
def testServerFailStart(self, tmp):
|
||||
try:
|
||||
# started directly here, so prevent overwrite test cases logger with "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.pruneLog()
|
||||
os.remove(tmp+"/f2b.sock")
|
||||
|
||||
finally:
|
||||
_kill_srv(tmp)
|
||||
|
|
Loading…
Reference in New Issue