Skip to content

Commit

Permalink
skip a testRepairDb if no sqlite3 command-helper available; code revi…
Browse files Browse the repository at this point in the history
…ew (removed unnecessary code-pieces resp. code-duplication)

closes #2026
  • Loading branch information
sebres committed Jan 22, 2018
1 parent 9d5f20a commit 8cfd97a
Showing 1 changed file with 7 additions and 31 deletions.
38 changes: 7 additions & 31 deletions fail2ban/tests/databasetestcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from ..server.filter import FileContainer
from ..server.mytime import MyTime
from ..server.ticket import FailTicket
from ..server.actions import Actions
from ..server.actions import Actions, Utils
from .dummyjail import DummyJail
try:
from ..server.database import Fail2BanDb as Fail2BanDb
Expand Down Expand Up @@ -85,30 +85,26 @@ def tearDown(self):
os.remove(self.dbFilename)

def testGetFilename(self):
if Fail2BanDb is None or self.db.filename == ':memory:': # pragma: no cover
return
if self.db.filename == ':memory:': # pragma: no cover
raise unittest.SkipTest("in :memory: database")
self.assertEqual(self.dbFilename, self.db.filename)

def testPurgeAge(self):
if Fail2BanDb is None: # pragma: no cover
return
self.assertEqual(self.db.purgeage, 86400)
self.db.purgeage = '1y6mon15d5h30m'
self.assertEqual(self.db.purgeage, 48652200)
self.db.purgeage = '2y 12mon 30d 10h 60m'
self.assertEqual(self.db.purgeage, 48652200*2)

def testCreateInvalidPath(self):
if Fail2BanDb is None: # pragma: no cover
return
self.assertRaises(
sqlite3.OperationalError,
Fail2BanDb,
"/this/path/should/not/exist")

def testCreateAndReconnect(self):
if Fail2BanDb is None or self.db.filename == ':memory:': # pragma: no cover
return
if self.db.filename == ':memory:': # pragma: no cover
raise unittest.SkipTest("in :memory: database")
self.testAddJail()
# Reconnect...
self.db = Fail2BanDb(self.dbFilename)
Expand All @@ -118,8 +114,8 @@ def testCreateAndReconnect(self):
"Jail not retained in Db after disconnect reconnect.")

def testRepairDb(self):
if Fail2BanDb is None: # pragma: no cover
return
if not Utils.executeCmd("sqlite3 --version"): # pragma: no cover
raise unittest.SkipTest("no sqlite3 command")
self.db = None
if self.dbFilename is None: # pragma: no cover
_, self.dbFilename = tempfile.mkstemp(".db", "fail2ban_")
Expand Down Expand Up @@ -153,8 +149,6 @@ def testRepairDb(self):
self.db = None

def testUpdateDb(self):
if Fail2BanDb is None: # pragma: no cover
return
self.db = None
try:
if self.dbFilename is None: # pragma: no cover
Expand All @@ -174,17 +168,13 @@ def testUpdateDb(self):
os.remove(self.db._dbBackupFilename)

def testAddJail(self):
if Fail2BanDb is None: # pragma: no cover
return
self.jail = DummyJail()
self.db.addJail(self.jail)
self.assertTrue(
self.jail.name in self.db.getJailNames(True),
"Jail not added to database")

def testAddLog(self):
if Fail2BanDb is None: # pragma: no cover
return
self.testAddJail() # Jail required

_, filename = tempfile.mkstemp(".log", "Fail2BanDb_")
Expand All @@ -196,8 +186,6 @@ def testAddLog(self):
os.remove(filename)

def testUpdateLog(self):
if Fail2BanDb is None: # pragma: no cover
return
self.testAddLog() # Add log file

# Write some text
Expand Down Expand Up @@ -237,8 +225,6 @@ def testUpdateLog(self):
os.remove(filename)

def testAddBan(self):
if Fail2BanDb is None: # pragma: no cover
return
self.testAddJail()
ticket = FailTicket("127.0.0.1", 0, ["abc\n"])
self.db.addBan(self.jail, ticket)
Expand All @@ -249,8 +235,6 @@ def testAddBan(self):
isinstance(tickets[0], FailTicket))

def testAddBanInvalidEncoded(self):
if Fail2BanDb is None: # pragma: no cover
return
self.testAddJail()
# invalid + valid, invalid + valid unicode, invalid + valid dual converted (like in filter:readline by fallback) ...
tickets = [
Expand Down Expand Up @@ -304,8 +288,6 @@ def testFlushBans(self):
self.assertEqual(len(self.db.getBans(jail=self.jail)), 0)

def testGetBansWithTime(self):
if Fail2BanDb is None: # pragma: no cover
return
self.testAddJail()
self.db.addBan(
self.jail, FailTicket("127.0.0.1", MyTime.time() - 60, ["abc\n"]))
Expand All @@ -318,8 +300,6 @@ def testGetBansWithTime(self):
self.assertEqual(len(self.db.getBans(jail=self.jail,bantime=-1)), 2)

def testGetBansMerged_MaxEntries(self):
if Fail2BanDb is None: # pragma: no cover
return
self.testAddJail()
maxEntries = 2
failures = ["abc\n", "123\n", "ABC\n", "1234\n"]
Expand Down Expand Up @@ -349,8 +329,6 @@ def testGetBansMerged_MaxEntries(self):
self.assertEqual(ticket.getMatches(), failures[len(failures) - maxEntries:])

def testGetBansMerged(self):
if Fail2BanDb is None: # pragma: no cover
return
self.testAddJail()

jail2 = DummyJail()
Expand Down Expand Up @@ -475,8 +453,6 @@ def testDelAndAddJail(self):
self.assertTrue(len(jails) == 0)

def testPurge(self):
if Fail2BanDb is None: # pragma: no cover
return
self.testAddJail() # Add jail

self.db.purge() # Jail enabled by default so shouldn't be purged
Expand Down

0 comments on commit 8cfd97a

Please sign in to comment.