Sort _get_new/deleted_set_ips responses in unittests

This fixes the test_set_members_adding/deleting_less_than_5 unit test
that breaks with a randomized PYTHONHASHSEED (see the bug report).

The test assumed that the _get_new/deleted_set_ips from
neutron.agent.linux.ipset_manager return elements in a particular order.
Found with PYTHONHASHSEED=1.

The fix refactors the test case to force sorted responses from
_get_new/deleted_set_ips during unittests.

Partial-bug: #1348818

Note: There are several other unrelated unit tests that also break with
a randomized PYTHONHASHSEED, but they are not addressed here. They will
be addressed in separate patches.

Change-Id: I8408365825ec1e97a83c2181f38ec1f9468df91e
This commit is contained in:
Cedric Brandily 2015-05-26 14:38:26 +00:00
parent 2f8373aff6
commit 77303fbeaa
1 changed files with 26 additions and 2 deletions

View File

@ -31,6 +31,30 @@ class BaseIpsetManagerTest(base.BaseTestCase):
self.execute = mock.patch.object(self.ipset, "execute").start() self.execute = mock.patch.object(self.ipset, "execute").start()
self.expected_calls = [] self.expected_calls = []
self.expect_create() self.expect_create()
self.force_sorted_get_set_ips()
def force_sorted_get_set_ips(self):
"""Force sorted responses by self.ipset._get_new/deleted_set_ips.
_get_new/deleted_set_ips use internally sets and return randomly
ordered responses. This method ensures sorted responses from them
in order to guarantee call order in self.ipset.set_members.
"""
original_get_new_set_ips = self.ipset._get_new_set_ips
original_get_deleted_set_ips = self.ipset._get_deleted_set_ips
def sorted_get_new_set_ips(set_name, expected_ips):
unsorted = original_get_new_set_ips(set_name, expected_ips)
return sorted(unsorted)
def sorted_get_deleted_set_ips(set_name, expected_ips):
unsorted = original_get_deleted_set_ips(set_name, expected_ips)
return sorted(unsorted)
mock.patch.object(self.ipset, '_get_new_set_ips',
side_effect=sorted_get_new_set_ips).start()
mock.patch.object(self.ipset, '_get_deleted_set_ips',
side_effect=sorted_get_deleted_set_ips).start()
def verify_mock_calls(self): def verify_mock_calls(self):
self.execute.assert_has_calls(self.expected_calls, any_order=False) self.execute.assert_has_calls(self.expected_calls, any_order=False)
@ -97,13 +121,13 @@ class IpsetManagerTestCase(BaseIpsetManagerTest):
def test_set_members_adding_less_than_5(self): def test_set_members_adding_less_than_5(self):
self.add_first_ip() self.add_first_ip()
self.expect_add(reversed(FAKE_IPS[1:5])) self.expect_add(FAKE_IPS[1:5])
self.ipset.set_members(TEST_SET_ID, ETHERTYPE, FAKE_IPS[0:5]) self.ipset.set_members(TEST_SET_ID, ETHERTYPE, FAKE_IPS[0:5])
self.verify_mock_calls() self.verify_mock_calls()
def test_set_members_deleting_less_than_5(self): def test_set_members_deleting_less_than_5(self):
self.add_all_ips() self.add_all_ips()
self.expect_del(reversed(FAKE_IPS[4:5])) self.expect_del(FAKE_IPS[3:4])
self.ipset.set_members(TEST_SET_ID, ETHERTYPE, FAKE_IPS[0:3]) self.ipset.set_members(TEST_SET_ID, ETHERTYPE, FAKE_IPS[0:3])
self.verify_mock_calls() self.verify_mock_calls()