From e00be5f308c98a53bde277baa24d20b6712b9644 Mon Sep 17 00:00:00 2001 From: sebres Date: Thu, 22 Sep 2016 22:09:08 +0200 Subject: [PATCH] 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); --- fail2ban/tests/banmanagertestcase.py | 13 ++++++------- fail2ban/tests/misctestcase.py | 4 ++++ fail2ban/tests/utils.py | 20 ++++++++++++++------ 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/fail2ban/tests/banmanagertestcase.py b/fail2ban/tests/banmanagertestcase.py index 4a86102c..de128b70 100644 --- a/fail2ban/tests/banmanagertestcase.py +++ b/fail2ban/tests/banmanagertestcase.py @@ -28,7 +28,6 @@ import unittest from ..server.banmanager import BanManager from ..server.ticket import BanTicket -from .utils import assert_dict_equal class AddFailure(unittest.TestCase): def setUp(self): @@ -131,7 +130,7 @@ class StatusExtendedCymruInfo(unittest.TestCase): def testCymruInfo(self): cymru_info = self.__banManager.getBanListExtendedCymruInfo() - assert_dict_equal(cymru_info, + self.assertDictEqual(cymru_info, {"asn": [self.__asn], "country": [self.__country], "rir": [self.__rir]}) @@ -158,7 +157,7 @@ class StatusExtendedCymruInfo(unittest.TestCase): ticket = BanTicket("0.0.0.0", 1167605999.0) self.assertTrue(self.__banManager.addBanTicket(ticket)) cymru_info = self.__banManager.getBanListExtendedCymruInfo() - assert_dict_equal(cymru_info, + self.assertDictEqual(cymru_info, {"asn": ["nxdomain"], "country": ["nxdomain"], "rir": ["nxdomain"]}) @@ -169,7 +168,7 @@ class StatusExtendedCymruInfo(unittest.TestCase): ticket = BanTicket("10.0.0.0", 1167606000.0) self.assertTrue(self.__banManager.addBanTicket(ticket)) cymru_info = self.__banManager.getBanListExtendedCymruInfo() - assert_dict_equal(cymru_info, - {"asn": ["nxdomain", "4565",], - "country": ["nxdomain", "unknown"], - "rir": ["nxdomain", "other"]}) + self.assertDictEqual(dict((k, sorted(v)) for k, v in cymru_info.iteritems()), + {"asn": sorted(["nxdomain", "4565",]), + "country": sorted(["nxdomain", "unknown"]), + "rir": sorted(["nxdomain", "other"])}) diff --git a/fail2ban/tests/misctestcase.py b/fail2ban/tests/misctestcase.py index 8fb393ea..450904d5 100644 --- a/fail2ban/tests/misctestcase.py +++ b/fail2ban/tests/misctestcase.py @@ -284,6 +284,10 @@ class TestsUtilsTest(LogCaptureTestCase): self.assertLogged, 'test_zyx', 'zyx', all=False) self._testAssertionErrorRE(r"All of the .* were found present in the log", 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): strout = StringIO() diff --git a/fail2ban/tests/utils.py b/fail2ban/tests/utils.py index 91350b1d..eecb94b8 100644 --- a/fail2ban/tests/utils.py +++ b/fail2ban/tests/utils.py @@ -466,6 +466,20 @@ def gatherTests(regexps=None, opts=None): # 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'): def assertRaisesRegexp(self, exccls, regexp, fun, *args, **kwargs): try: @@ -673,9 +687,3 @@ class LogCaptureTestCase(unittest.TestCase): 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)