Fix _goal_state_achieved_for_relid() with unsorted lists

Essentially, the functions returning the related units and expected
units (for goal state) might not be sorted, something the author of
the code (me) hadn't taken into account.  This fixes that by comparing
sorted lists.

Change-Id: I5c7bfe39b80f103e95fd5105d2185a89975ec23c
Closes-bug: #1859050
This commit is contained in:
Alex Kavanagh 2020-01-09 18:42:57 +00:00
parent fae011df00
commit 0db6d59353
2 changed files with 5 additions and 2 deletions

View File

@ -738,7 +738,9 @@ def _goal_state_achieved_for_relid(reltype, rid=None):
hookenv.expected_related_units(reltype=reltype))
prefix = units_so_far[0].split('/')[0]
target_units = [u for u in all_units if u.split('/')[0] == prefix]
return units_so_far == target_units
# BUG: #1859050 -- the units may not necessarily be sorted, so we have
# to compare sorted lists
return sorted(units_so_far) == sorted(target_units)
except (KeyError, IndexError):
# expected_related_units() can raise a KeyError in the case there are
# no units - in that case assume that the goal wasn't met

View File

@ -422,7 +422,8 @@ class NovaCCHooksTests(CharmTestCase):
@patch('charmhelpers.core.hookenv.expected_related_units')
def test__goal_state_achieved_for_relid__goal_state_sufficient_units(
self, mock_expected_related_units, mock_related_units):
mock_related_units.return_value = ['service/0', 'service/1']
# BUG: #1859050 -- units out of order should still match
mock_related_units.return_value = ['service/1', 'service/0']
mock_expected_related_units.return_value = ['service/0', 'service/1']
self.assertTrue(hooks._goal_state_achieved_for_relid('aservice', None))