several amend fixes after cherry pick from 10th branch

pull/1486/merge
sebres 8 years ago
parent 3e330604c7
commit 8b1225f177

@ -293,7 +293,11 @@ class Fail2BanDb(object):
Jail to be added to the database. Jail to be added to the database.
""" """
cur.execute( cur.execute(
"INSERT OR REPLACE INTO jails(name, enabled) VALUES(?, 1)", "INSERT OR IGNORE INTO jails(name, enabled) VALUES(?, 1)",
(jail.name,))
if cur.rowcount <= 0:
cur.execute(
"UPDATE jails SET enabled = 1 WHERE name = ? AND enabled != 1",
(jail.name,)) (jail.name,))
@commitandrollback @commitandrollback
@ -317,7 +321,7 @@ class Fail2BanDb(object):
cur.execute("UPDATE jails SET enabled=0") cur.execute("UPDATE jails SET enabled=0")
@commitandrollback @commitandrollback
def getJailNames(self, cur): def getJailNames(self, cur, enabled=None):
"""Get name of jails in database. """Get name of jails in database.
Currently only used for testing purposes. Currently only used for testing purposes.
@ -327,7 +331,11 @@ class Fail2BanDb(object):
set set
Set of jail names. Set of jail names.
""" """
if enabled is None:
cur.execute("SELECT name FROM jails") cur.execute("SELECT name FROM jails")
else:
cur.execute("SELECT name FROM jails WHERE enabled=%s" %
(int(enabled),))
return set(row[0] for row in cur.fetchmany()) return set(row[0] for row in cur.fetchmany())
@commitandrollback @commitandrollback

@ -271,6 +271,7 @@ class LogCaptureTestCase(unittest.TestCase):
def tearDown(self): def tearDown(self):
"""Call after every test case.""" """Call after every test case."""
# print "O: >>%s<<" % self._log.getvalue() # print "O: >>%s<<" % self._log.getvalue()
self.pruneLog()
logSys = getLogger("fail2ban") logSys = getLogger("fail2ban")
logSys.handlers = self._old_handlers logSys.handlers = self._old_handlers
logSys.level = self._old_level logSys.level = self._old_level
@ -278,7 +279,7 @@ class LogCaptureTestCase(unittest.TestCase):
def _is_logged(self, s): def _is_logged(self, s):
return s in self._log.getvalue() return s in self._log.getvalue()
def assertLogged(self, *s): def assertLogged(self, *s, **kwargs):
"""Assert that one of the strings was logged """Assert that one of the strings was logged
Preferable to assertTrue(self._is_logged(..))) Preferable to assertTrue(self._is_logged(..)))
@ -288,14 +289,23 @@ class LogCaptureTestCase(unittest.TestCase):
---------- ----------
s : string or list/set/tuple of strings s : string or list/set/tuple of strings
Test should succeed if string (or any of the listed) is present in the log Test should succeed if string (or any of the listed) is present in the log
all : boolean (default False) if True should fail if any of s not logged
""" """
logged = self._log.getvalue() logged = self._log.getvalue()
if not kwargs.get('all', False):
# at least one entry should be found:
for s_ in s: for s_ in s:
if s_ in logged: if s_ in logged:
return return
raise AssertionError("None among %r was found in the log: %r" % (s, logged)) if True: # pragma: no cover
self.fail("None among %r was found in the log: ===\n%s===" % (s, logged))
else:
# each entry should be found:
for s_ in s:
if s_ not in logged: # pragma: no cover
self.fail("%r was not found in the log: ===\n%s===" % (s_, logged))
def assertNotLogged(self, *s): def assertNotLogged(self, *s, **kwargs):
"""Assert that strings were not logged """Assert that strings were not logged
Parameters Parameters
@ -303,13 +313,22 @@ class LogCaptureTestCase(unittest.TestCase):
s : string or list/set/tuple of strings s : string or list/set/tuple of strings
Test should succeed if the string (or at least one of the listed) is not Test should succeed if the string (or at least one of the listed) is not
present in the log present in the log
all : boolean (default False) if True should fail if any of s logged
""" """
logged = self._log.getvalue() logged = self._log.getvalue()
if not kwargs.get('all', False):
for s_ in s: for s_ in s:
if s_ not in logged: if s_ not in logged:
return return
raise AssertionError("All of the %r were found present in the log: %r" % (s, logged)) if True: # pragma: no cover
self.fail("All of the %r were found present in the log: ===\n%s===" % (s, logged))
else:
for s_ in s:
if s_ in logged: # pragma: no cover
self.fail("%r was found in the log: ===\n%s===" % (s_, logged))
def pruneLog(self):
self._log.truncate(0)
def getLog(self): def getLog(self):
return self._log.getvalue() return self._log.getvalue()

Loading…
Cancel
Save