mirror of https://github.com/fail2ban/fail2ban
Fixed sporadically error in testCymruInfoNxdomain, because of unsorted values:
``` AssertionError: Dictionaries differ: {'country': ['unknown', 'nxdomain'], 'asn': ['4565', 'nxdomain'], 'rir': ['other', 'nxdomain']} != {'country': ['nxdomain', 'unknown'], 'asn': ['nxdomain', '4565'], 'rir': ['nxdomain', 'other']} ``` Added assertDictEqual for compatibility to early python versions (< 2.7);pull/1557/head
parent
e7fa74b989
commit
e00be5f308
|
@ -28,7 +28,6 @@ import unittest
|
||||||
|
|
||||||
from ..server.banmanager import BanManager
|
from ..server.banmanager import BanManager
|
||||||
from ..server.ticket import BanTicket
|
from ..server.ticket import BanTicket
|
||||||
from .utils import assert_dict_equal
|
|
||||||
|
|
||||||
class AddFailure(unittest.TestCase):
|
class AddFailure(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
@ -131,7 +130,7 @@ class StatusExtendedCymruInfo(unittest.TestCase):
|
||||||
|
|
||||||
def testCymruInfo(self):
|
def testCymruInfo(self):
|
||||||
cymru_info = self.__banManager.getBanListExtendedCymruInfo()
|
cymru_info = self.__banManager.getBanListExtendedCymruInfo()
|
||||||
assert_dict_equal(cymru_info,
|
self.assertDictEqual(cymru_info,
|
||||||
{"asn": [self.__asn],
|
{"asn": [self.__asn],
|
||||||
"country": [self.__country],
|
"country": [self.__country],
|
||||||
"rir": [self.__rir]})
|
"rir": [self.__rir]})
|
||||||
|
@ -158,7 +157,7 @@ class StatusExtendedCymruInfo(unittest.TestCase):
|
||||||
ticket = BanTicket("0.0.0.0", 1167605999.0)
|
ticket = BanTicket("0.0.0.0", 1167605999.0)
|
||||||
self.assertTrue(self.__banManager.addBanTicket(ticket))
|
self.assertTrue(self.__banManager.addBanTicket(ticket))
|
||||||
cymru_info = self.__banManager.getBanListExtendedCymruInfo()
|
cymru_info = self.__banManager.getBanListExtendedCymruInfo()
|
||||||
assert_dict_equal(cymru_info,
|
self.assertDictEqual(cymru_info,
|
||||||
{"asn": ["nxdomain"],
|
{"asn": ["nxdomain"],
|
||||||
"country": ["nxdomain"],
|
"country": ["nxdomain"],
|
||||||
"rir": ["nxdomain"]})
|
"rir": ["nxdomain"]})
|
||||||
|
@ -169,7 +168,7 @@ class StatusExtendedCymruInfo(unittest.TestCase):
|
||||||
ticket = BanTicket("10.0.0.0", 1167606000.0)
|
ticket = BanTicket("10.0.0.0", 1167606000.0)
|
||||||
self.assertTrue(self.__banManager.addBanTicket(ticket))
|
self.assertTrue(self.__banManager.addBanTicket(ticket))
|
||||||
cymru_info = self.__banManager.getBanListExtendedCymruInfo()
|
cymru_info = self.__banManager.getBanListExtendedCymruInfo()
|
||||||
assert_dict_equal(cymru_info,
|
self.assertDictEqual(dict((k, sorted(v)) for k, v in cymru_info.iteritems()),
|
||||||
{"asn": ["nxdomain", "4565",],
|
{"asn": sorted(["nxdomain", "4565",]),
|
||||||
"country": ["nxdomain", "unknown"],
|
"country": sorted(["nxdomain", "unknown"]),
|
||||||
"rir": ["nxdomain", "other"]})
|
"rir": sorted(["nxdomain", "other"])})
|
||||||
|
|
|
@ -284,6 +284,10 @@ class TestsUtilsTest(LogCaptureTestCase):
|
||||||
self.assertLogged, 'test_zyx', 'zyx', all=False)
|
self.assertLogged, 'test_zyx', 'zyx', all=False)
|
||||||
self._testAssertionErrorRE(r"All of the .* were found present in the log",
|
self._testAssertionErrorRE(r"All of the .* were found present in the log",
|
||||||
self.assertNotLogged, 'test', 'xyz', all=False)
|
self.assertNotLogged, 'test', 'xyz', all=False)
|
||||||
|
## assertDictEqual:
|
||||||
|
self.assertDictEqual({'A': [1, 2]}, {'A': [1, 2]})
|
||||||
|
self.assertRaises(AssertionError, self.assertDictEqual,
|
||||||
|
{'A': [1, 2]}, {'A': [2, 1]})
|
||||||
|
|
||||||
def testFormatterWithTraceBack(self):
|
def testFormatterWithTraceBack(self):
|
||||||
strout = StringIO()
|
strout = StringIO()
|
||||||
|
|
|
@ -466,6 +466,20 @@ def gatherTests(regexps=None, opts=None):
|
||||||
# Forwards compatibility of unittest.TestCase for some early python versions
|
# Forwards compatibility of unittest.TestCase for some early python versions
|
||||||
#
|
#
|
||||||
|
|
||||||
|
if not hasattr(unittest.TestCase, 'assertDictEqual'):
|
||||||
|
import difflib, pprint
|
||||||
|
def assertDictEqual(self, d1, d2, msg=None):
|
||||||
|
self.assert_(isinstance(d1, dict), 'First argument is not a dictionary')
|
||||||
|
self.assert_(isinstance(d2, dict), 'Second argument is not a dictionary')
|
||||||
|
if d1 != d2:
|
||||||
|
standardMsg = '%r != %r' % (d1, d2)
|
||||||
|
diff = ('\n' + '\n'.join(difflib.ndiff(
|
||||||
|
pprint.pformat(d1).splitlines(),
|
||||||
|
pprint.pformat(d2).splitlines())))
|
||||||
|
msg = msg or (standardMsg + diff)
|
||||||
|
self.fail(msg)
|
||||||
|
unittest.TestCase.assertDictEqual = assertDictEqual
|
||||||
|
|
||||||
if not hasattr(unittest.TestCase, 'assertRaisesRegexp'):
|
if not hasattr(unittest.TestCase, 'assertRaisesRegexp'):
|
||||||
def assertRaisesRegexp(self, exccls, regexp, fun, *args, **kwargs):
|
def assertRaisesRegexp(self, exccls, regexp, fun, *args, **kwargs):
|
||||||
try:
|
try:
|
||||||
|
@ -673,9 +687,3 @@ class LogCaptureTestCase(unittest.TestCase):
|
||||||
|
|
||||||
|
|
||||||
pid_exists = Utils.pid_exists
|
pid_exists = Utils.pid_exists
|
||||||
|
|
||||||
# Python 2.6 compatibility. in 2.7 assertDictEqual
|
|
||||||
def assert_dict_equal(a, b):
|
|
||||||
assert isinstance(a, dict), "Object is not dictionary: %r" % a
|
|
||||||
assert isinstance(b, dict), "Object is not dictionary: %r" % b
|
|
||||||
assert a==b, "Dictionaries differ:\n%r !=\n%r" % (a, b)
|
|
||||||
|
|
Loading…
Reference in New Issue