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,47 +304,60 @@ class Fail2banClientServerBase(LogCaptureTestCase):
phase['end'] = True phase['end'] = True
logSys.debug("end of test worker") logSys.debug("end of test worker")
@with_tmpdir def with_foreground_server_thread(startextra={}):
def testStartForeground(self, tmp): """Helper to decorate tests uses foreground server (as thread), started directly in test-cases
# intended to be ran only in subclasses
th = None To be used only in subclasses
phase = dict() """
try: def _deco_wrapper(f):
# started directly here, so prevent overwrite test cases logger with "INHERITED" @with_tmpdir
startparams = _start_params(tmp, logtarget="INHERITED") @wraps(f)
# because foreground block execution - start it in thread: def wrapper(self, tmp, *args, **kwargs):
th = Thread( th = None
name="_TestCaseWorker", phase = dict()
target=self._testStartForeground, try:
args=(tmp, startparams, phase) # started directly here, so prevent overwrite test cases logger with "INHERITED"
) startparams = _start_params(tmp, logtarget="INHERITED", **startextra)
th.daemon = True # because foreground block execution - start it in thread:
th.start() th = Thread(
try: name="_TestCaseWorker",
# wait for start thread: target=self._testStartForeground,
Utils.wait_for(lambda: phase.get('start', None) is not None, MAX_WAITTIME) args=(tmp, startparams, phase)
self.assertTrue(phase.get('start', None)) )
# wait for server (socket and ready): th.daemon = True
self._wait_for_srv(tmp, True, startparams=startparams) th.start()
self.pruneLog() try:
# several commands to server: # wait for start thread:
self.execSuccess(startparams, "ping") Utils.wait_for(lambda: phase.get('start', None) is not None, MAX_WAITTIME)
self.execFailed(startparams, "~~unknown~cmd~failed~~") self.assertTrue(phase.get('start', None))
self.execSuccess(startparams, "echo", "TEST-ECHO") # wait for server (socket and ready):
finally: self._wait_for_srv(tmp, True, startparams=startparams)
self.pruneLog() self.pruneLog()
# stop: # several commands to server in body of decorated function:
self.execSuccess(startparams, "stop") return f(self, tmp, startparams, *args, **kwargs)
# wait for end: finally:
Utils.wait_for(lambda: phase.get('end', None) is not None, MAX_WAITTIME) self.pruneLog()
self.assertTrue(phase.get('end', None)) # stop:
self.assertLogged("Shutdown successful", "Exiting Fail2ban") self.execSuccess(startparams, "stop")
finally: # wait for end:
if th: Utils.wait_for(lambda: phase.get('end', None) is not None, MAX_WAITTIME)
# we start client/server directly in current process (new thread), self.assertTrue(phase.get('end', None))
# so don't kill (same process) - if success, just wait for end of worker: self.assertLogged("Shutdown successful", "Exiting Fail2ban")
if phase.get('end', None): finally:
th.join() if th:
# we start client/server directly in current process (new thread),
# 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): class Fail2banClientTest(Fail2banClientServerBase):