make with_foreground_server_thread decorator to test several client/server commands

pull/1557/head
sebres 2016-09-06 20:15:45 +02:00
parent 0a7374dec6
commit f512628af2
1 changed files with 55 additions and 41 deletions

View File

@ -41,6 +41,7 @@ from ..client.fail2banclient import exec_command_line as _exec_client, VisualWai
from ..client.fail2banserver import Fail2banServer, exec_command_line as _exec_server from ..client.fail2banserver import Fail2banServer, exec_command_line as _exec_server
from .. import protocol from .. import protocol
from ..server import server from ..server import server
from ..server.mytime import MyTime
from ..server.utils import Utils from ..server.utils import Utils
from .utils import LogCaptureTestCase, with_tmpdir, shutil, logging from .utils import LogCaptureTestCase, with_tmpdir, shutil, logging
@ -303,14 +304,20 @@ class Fail2banClientServerBase(LogCaptureTestCase):
phase['end'] = True phase['end'] = True
logSys.debug("end of test worker") logSys.debug("end of test worker")
def with_foreground_server_thread(startextra={}):
"""Helper to decorate tests uses foreground server (as thread), started directly in test-cases
To be used only in subclasses
"""
def _deco_wrapper(f):
@with_tmpdir @with_tmpdir
def testStartForeground(self, tmp): @wraps(f)
# intended to be ran only in subclasses def wrapper(self, tmp, *args, **kwargs):
th = None th = None
phase = dict() phase = dict()
try: 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", **startextra)
# because foreground block execution - start it in thread: # because foreground block execution - start it in thread:
th = Thread( th = Thread(
name="_TestCaseWorker", name="_TestCaseWorker",
@ -326,10 +333,8 @@ class Fail2banClientServerBase(LogCaptureTestCase):
# wait for server (socket and ready): # wait for server (socket and ready):
self._wait_for_srv(tmp, True, startparams=startparams) self._wait_for_srv(tmp, True, startparams=startparams)
self.pruneLog() self.pruneLog()
# several commands to server: # several commands to server in body of decorated function:
self.execSuccess(startparams, "ping") return f(self, tmp, startparams, *args, **kwargs)
self.execFailed(startparams, "~~unknown~cmd~failed~~")
self.execSuccess(startparams, "echo", "TEST-ECHO")
finally: finally:
self.pruneLog() self.pruneLog()
# stop: # stop:
@ -344,6 +349,15 @@ class Fail2banClientServerBase(LogCaptureTestCase):
# so don't kill (same process) - if success, just wait for end of worker: # so don't kill (same process) - if success, just wait for end of worker:
if phase.get('end', None): if phase.get('end', None):
th.join() th.join()
return wrapper
return _deco_wrapper
@with_foreground_server_thread()
def testStartForeground(self, tmp, startparams):
# several commands to server:
self.execSuccess(startparams, "ping")
self.execFailed(startparams, "~~unknown~cmd~failed~~")
self.execSuccess(startparams, "echo", "TEST-ECHO")
class Fail2banClientTest(Fail2banClientServerBase): class Fail2banClientTest(Fail2banClientServerBase):