stability test-cases fix: avoid rare sporadic error on start of server (threaded in foreground);

additionally show the log output of the thread-server in case of any error there.
pull/2061/head
sebres 2018-03-02 17:00:01 +01:00
parent 8c291cad38
commit fa520f36c3
3 changed files with 37 additions and 11 deletions

View File

@ -266,7 +266,7 @@ class JailReaderTest(LogCaptureTestCase):
self.assertEqual(('mail--ho_is', {}), extractOptions("mail--ho_is"))
self.assertEqual(('mail--ho_is', {}), extractOptions("mail--ho_is['s']"))
#self.printLog()
#print(self.getLog())
#self.assertLogged("Invalid argument ['s'] in ''s''")
self.assertEqual(('mail', {'a': ','}), extractOptions("mail[a=',']"))
@ -528,7 +528,7 @@ class JailsReaderTestCache(LogCaptureTestCase):
# how many times jail.local was read:
cnt = self._getLoggedReadCount('jail.local')
# if cnt > 1:
# self.printLog()
# print(self.getLog())
self.assertTrue(cnt == 1, "Unexpected count by reading of jail files, cnt = %s" % cnt)
# read whole configuration like a file2ban-client, again ...
@ -648,7 +648,7 @@ class JailsReaderTest(LogCaptureTestCase):
## We should not "read" some bogus jail
#old_comm_commands = comm_commands[:] # make a copy
#self.assertRaises(ValueError, jails.getOptions, "BOGUS")
#self.printLog()
#print(self.getLog())
#self.assertLogged("No section: 'BOGUS'")
## and there should be no side-effects
#self.assertEqual(jails.convert(), old_comm_commands)

View File

@ -210,6 +210,12 @@ def _start_params(tmp, use_stock=False, use_stock_cfg=None,
"--timeout", str(fail2bancmdline.MAX_WAITTIME),
)
def _inherited_log(startparams):
try:
return startparams[startparams.index('--logtarget')+1] == 'INHERITED'
except ValueError:
return False
def _get_pid_from_file(pidfile):
pid = None
try:
@ -325,6 +331,13 @@ def with_foreground_server_thread(startextra={}):
self.pruneLog()
# several commands to server in body of decorated function:
return f(self, tmp, startparams, *args, **kwargs)
except Exception as e: # pragma: no cover
print('=== Catch an exception: %s' % e)
log = self.getLog()
if log:
print('=== Error of server, log: ===\n%s===' % log)
self.pruneLog()
raise
finally:
if th:
# wait for server end (if not yet already exited):
@ -369,7 +382,8 @@ class Fail2banClientServerBase(LogCaptureTestCase):
else:
raise FailExitException()
def _wait_for_srv(self, tmp, ready=True, startparams=None, phase={}):
def _wait_for_srv(self, tmp, ready=True, startparams=None, phase=None):
if not phase: phase = {}
try:
sock = pjoin(tmp, "f2b.sock")
# wait for server (socket):
@ -384,14 +398,17 @@ class Fail2banClientServerBase(LogCaptureTestCase):
ret = Utils.wait_for(lambda: "Server ready" in self.getLog(), MAX_WAITTIME)
if not ret: # pragma: no cover - test-failure case only
raise Exception(
'Unexpected: Server ready was not found.\nStart failed: %r'
% (startparams,)
'Unexpected: Server ready was not found, phase %r.\nStart failed: %r'
% (phase, startparams,)
)
except: # pragma: no cover
if _inherited_log(startparams):
print('=== Error by wait fot server, log: ===\n%s===' % self.getLog())
self.pruneLog()
log = pjoin(tmp, "f2b.log")
if isfile(log):
_out_file(log)
else:
elif not _inherited_log(startparams):
logSys.debug("No log file %s to examine details of error", log)
raise
@ -410,6 +427,7 @@ class Fail2banClientServerBase(LogCaptureTestCase):
self.execCmd(SUCCESS, ("-f",) + startparams, "start")
finally:
# end :
phase['start'] = False
phase['end'] = True
logSys.debug("end of test worker")
@ -1192,7 +1210,7 @@ class Fail2banServerTest(Fail2banClientServerBase):
'failregex = ^ failure "<F-ID>[^"]+</F-ID>" - <ADDR>',
'maxretry = 1', # ban by first failure
'enabled = true',
)
)
})
def testServerActions_NginxBlockMap(self, tmp, startparams):
cfg = pjoin(tmp, "config")
@ -1251,3 +1269,14 @@ class Fail2banServerTest(Fail2banClientServerBase):
_out_file(mpfn)
mp = _read_file(mpfn)
self.assertEqual(mp, '')
# test multiple start/stop of the server (threaded in foreground) --
if False: # pragma: no cover
@with_foreground_server_thread()
def _testServerStartStop(self, tmp, startparams):
# stop server and wait for end:
self.stopAndWaitForServerEnd(SUCCESS)
def testServerStartStop(self):
for i in xrange(2000):
self._testServerStartStop()

View File

@ -807,8 +807,5 @@ class LogCaptureTestCase(unittest.TestCase):
def getLog(self):
return self._log.getvalue()
def printLog(self):
print(self._log.getvalue())
pid_exists = Utils.pid_exists