Skip to content

Commit d061b2b

Browse files
committed
TST: Fix issues in tests which assumed dictionary's order
1 parent fa0f8f9 commit d061b2b

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

fail2ban/tests/clientreadertestcase.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,9 @@ def testConvert(self):
146146
#filterReader.getOptions(["failregex", "ignoreregex"])
147147
filterReader.getOptions(None)
148148

149-
self.assertEquals(filterReader.convert(), output)
149+
# Add sort as configreader uses dictionary and therefore order
150+
# is unreliable
151+
self.assertEquals(sorted(filterReader.convert()), sorted(output))
150152

151153
class JailsReaderTest(unittest.TestCase):
152154

fail2ban/tests/filtertestcase.py

+27-17
Original file line numberDiff line numberDiff line change
@@ -94,22 +94,25 @@ def _assert_equal_entries(utest, found, output, count=None):
9494
# do not check if custom count (e.g. going through them twice)
9595
utest.assertEqual(repr(found[3]), repr(output[3]))
9696

97+
def _ticket_tuple(ticket):
98+
"""Create a tuple for easy comparison from fail ticket
99+
"""
100+
attempts = ticket.getAttempt()
101+
date = ticket.getTime()
102+
ip = ticket.getIP()
103+
matches = ticket.getMatches()
104+
return (ip, attempts, date, matches)
105+
97106
def _assert_correct_last_attempt(utest, filter_, output, count=None):
98107
"""Additional helper to wrap most common test case
99108
100109
Test filter to contain target ticket
101110
"""
102111
if isinstance(filter_, DummyJail):
103-
ticket = filter_.getFailTicket()
112+
found = _ticket_tuple(filter_.getFailTicket())
104113
else:
105114
# when we are testing without jails
106-
ticket = filter_.failManager.toBan()
107-
108-
attempts = ticket.getAttempt()
109-
date = ticket.getTime()
110-
ip = ticket.getIP()
111-
matches = ticket.getMatches()
112-
found = (ip, attempts, date, matches)
115+
found = _ticket_tuple(filter_.failManager.toBan())
113116

114117
_assert_equal_entries(utest, found, output, count)
115118

@@ -642,10 +645,14 @@ def testGetFailuresMultiLine(self):
642645

643646
self.filter.getFailures(GetFailures.FILENAME_MULTILINE)
644647

645-
_assert_correct_last_attempt(self, self.filter, output.pop())
646-
_assert_correct_last_attempt(self, self.filter, output.pop())
647-
648-
self.assertRaises(FailManagerEmpty, self.filter.failManager.toBan)
648+
foundList = []
649+
while True:
650+
try:
651+
foundList.append(
652+
_ticket_tuple(self.filter.failManager.toBan())[0:3])
653+
except FailManagerEmpty:
654+
break
655+
self.assertEqual(sorted(foundList), sorted(output))
649656

650657
def testGetFailuresMultiLineIgnoreRegex(self):
651658
output = [("192.0.43.10", 2, 1124013599.0)]
@@ -673,11 +680,14 @@ def testGetFailuresMultiLineMultiRegex(self):
673680

674681
self.filter.getFailures(GetFailures.FILENAME_MULTILINE)
675682

676-
_assert_correct_last_attempt(self, self.filter, output.pop())
677-
_assert_correct_last_attempt(self, self.filter, output.pop())
678-
_assert_correct_last_attempt(self, self.filter, output.pop())
679-
680-
self.assertRaises(FailManagerEmpty, self.filter.failManager.toBan)
683+
foundList = []
684+
while True:
685+
try:
686+
foundList.append(
687+
_ticket_tuple(self.filter.failManager.toBan())[0:3])
688+
except FailManagerEmpty:
689+
break
690+
self.assertEqual(sorted(foundList), sorted(output))
681691

682692
class DNSUtilsTests(unittest.TestCase):
683693

0 commit comments

Comments
 (0)