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 .. import protocol
from ..server import server
from ..server.mytime import MyTime
from ..server.utils import Utils
from .utils import LogCaptureTestCase, with_tmpdir, shutil, logging
@ -303,14 +304,20 @@ class Fail2banClientServerBase(LogCaptureTestCase):
phase['end'] = True
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
def testStartForeground(self, tmp):
# intended to be ran only in subclasses
@wraps(f)
def wrapper(self, tmp, *args, **kwargs):
th = None
phase = dict()
try:
# 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:
th = Thread(
name="_TestCaseWorker",
@ -326,10 +333,8 @@ class Fail2banClientServerBase(LogCaptureTestCase):
# wait for server (socket and ready):
self._wait_for_srv(tmp, True, startparams=startparams)
self.pruneLog()
# several commands to server:
self.execSuccess(startparams, "ping")
self.execFailed(startparams, "~~unknown~cmd~failed~~")
self.execSuccess(startparams, "echo", "TEST-ECHO")
# several commands to server in body of decorated function:
return f(self, tmp, startparams, *args, **kwargs)
finally:
self.pruneLog()
# stop:
@ -344,6 +349,15 @@ class Fail2banClientServerBase(LogCaptureTestCase):
# so don't kill (same process) - if success, just wait for end of worker:
if phase.get('end', None):
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):