mirror of https://github.com/fail2ban/fail2ban
TST: Fix issues in tests which assumed dictionary's order
parent
fa0f8f9e6d
commit
d061b2b549
|
@ -146,7 +146,9 @@ class FilterReaderTest(unittest.TestCase):
|
|||
#filterReader.getOptions(["failregex", "ignoreregex"])
|
||||
filterReader.getOptions(None)
|
||||
|
||||
self.assertEquals(filterReader.convert(), output)
|
||||
# Add sort as configreader uses dictionary and therefore order
|
||||
# is unreliable
|
||||
self.assertEquals(sorted(filterReader.convert()), sorted(output))
|
||||
|
||||
class JailsReaderTest(unittest.TestCase):
|
||||
|
||||
|
|
|
@ -94,22 +94,25 @@ def _assert_equal_entries(utest, found, output, count=None):
|
|||
# do not check if custom count (e.g. going through them twice)
|
||||
utest.assertEqual(repr(found[3]), repr(output[3]))
|
||||
|
||||
def _ticket_tuple(ticket):
|
||||
"""Create a tuple for easy comparison from fail ticket
|
||||
"""
|
||||
attempts = ticket.getAttempt()
|
||||
date = ticket.getTime()
|
||||
ip = ticket.getIP()
|
||||
matches = ticket.getMatches()
|
||||
return (ip, attempts, date, matches)
|
||||
|
||||
def _assert_correct_last_attempt(utest, filter_, output, count=None):
|
||||
"""Additional helper to wrap most common test case
|
||||
|
||||
Test filter to contain target ticket
|
||||
"""
|
||||
if isinstance(filter_, DummyJail):
|
||||
ticket = filter_.getFailTicket()
|
||||
found = _ticket_tuple(filter_.getFailTicket())
|
||||
else:
|
||||
# when we are testing without jails
|
||||
ticket = filter_.failManager.toBan()
|
||||
|
||||
attempts = ticket.getAttempt()
|
||||
date = ticket.getTime()
|
||||
ip = ticket.getIP()
|
||||
matches = ticket.getMatches()
|
||||
found = (ip, attempts, date, matches)
|
||||
found = _ticket_tuple(filter_.failManager.toBan())
|
||||
|
||||
_assert_equal_entries(utest, found, output, count)
|
||||
|
||||
|
@ -642,10 +645,14 @@ class GetFailures(unittest.TestCase):
|
|||
|
||||
self.filter.getFailures(GetFailures.FILENAME_MULTILINE)
|
||||
|
||||
_assert_correct_last_attempt(self, self.filter, output.pop())
|
||||
_assert_correct_last_attempt(self, self.filter, output.pop())
|
||||
|
||||
self.assertRaises(FailManagerEmpty, self.filter.failManager.toBan)
|
||||
foundList = []
|
||||
while True:
|
||||
try:
|
||||
foundList.append(
|
||||
_ticket_tuple(self.filter.failManager.toBan())[0:3])
|
||||
except FailManagerEmpty:
|
||||
break
|
||||
self.assertEqual(sorted(foundList), sorted(output))
|
||||
|
||||
def testGetFailuresMultiLineIgnoreRegex(self):
|
||||
output = [("192.0.43.10", 2, 1124013599.0)]
|
||||
|
@ -673,11 +680,14 @@ class GetFailures(unittest.TestCase):
|
|||
|
||||
self.filter.getFailures(GetFailures.FILENAME_MULTILINE)
|
||||
|
||||
_assert_correct_last_attempt(self, self.filter, output.pop())
|
||||
_assert_correct_last_attempt(self, self.filter, output.pop())
|
||||
_assert_correct_last_attempt(self, self.filter, output.pop())
|
||||
|
||||
self.assertRaises(FailManagerEmpty, self.filter.failManager.toBan)
|
||||
foundList = []
|
||||
while True:
|
||||
try:
|
||||
foundList.append(
|
||||
_ticket_tuple(self.filter.failManager.toBan())[0:3])
|
||||
except FailManagerEmpty:
|
||||
break
|
||||
self.assertEqual(sorted(foundList), sorted(output))
|
||||
|
||||
class DNSUtilsTests(unittest.TestCase):
|
||||
|
||||
|
|
Loading…
Reference in New Issue