Fix guardmap as a function object issue in service_guard()
The issue was the original commit had a bug (using the wrong variable), and also that there were no tests to verify the functionality. This patchset fixes the bug and adds tests to verify the contract. Change-Id: Ia8b5580132c5a592461a57fb514994ab64a9ed42 Closes-Bug: #1805128
This commit is contained in:
parent
c88155d17a
commit
50a7253b80
@ -1256,7 +1256,7 @@ def service_guard(guard_map, contexts, active=False):
|
|||||||
_contexts = contexts
|
_contexts = contexts
|
||||||
incomplete_services = []
|
incomplete_services = []
|
||||||
for svc in _guard_map:
|
for svc in _guard_map:
|
||||||
for interface in guard_map[svc]:
|
for interface in _guard_map[svc]:
|
||||||
if interface not in _contexts.complete_contexts():
|
if interface not in _contexts.complete_contexts():
|
||||||
incomplete_services.append(svc)
|
incomplete_services.append(svc)
|
||||||
ret = f(*args, **kwargs)
|
ret = f(*args, **kwargs)
|
||||||
|
@ -1062,6 +1062,69 @@ class NovaCCUtilsTests(CharmTestCase):
|
|||||||
self.assertFalse(self.service_stop.called)
|
self.assertFalse(self.service_stop.called)
|
||||||
self.assertTrue(_mc.called)
|
self.assertTrue(_mc.called)
|
||||||
|
|
||||||
|
def test_service_guard_active_with_guardmap_function_object(self):
|
||||||
|
class MockContext(object):
|
||||||
|
called = False
|
||||||
|
|
||||||
|
def complete_contexts(self):
|
||||||
|
self.called = True
|
||||||
|
return ['interfacea', 'interfaceb']
|
||||||
|
|
||||||
|
_mc = MockContext()
|
||||||
|
|
||||||
|
def guard_map():
|
||||||
|
return {'test': ['interfacea', 'interfaceb']}
|
||||||
|
|
||||||
|
@utils.service_guard(guard_map, _mc, True)
|
||||||
|
def dummy_func():
|
||||||
|
pass
|
||||||
|
|
||||||
|
dummy_func()
|
||||||
|
self.assertFalse(self.service_running.called)
|
||||||
|
self.assertFalse(self.service_stop.called)
|
||||||
|
self.assertTrue(_mc.called)
|
||||||
|
|
||||||
|
def test_service_guard_active_with_contexts_function_object(self):
|
||||||
|
class MockContext(object):
|
||||||
|
called = False
|
||||||
|
|
||||||
|
def complete_contexts(self):
|
||||||
|
self.called = True
|
||||||
|
return ['interfacea', 'interfaceb']
|
||||||
|
|
||||||
|
_mc = MockContext()
|
||||||
|
|
||||||
|
def lmc():
|
||||||
|
return _mc
|
||||||
|
|
||||||
|
@utils.service_guard({'test': ['interfacea', 'interfaceb']}, lmc, True)
|
||||||
|
def dummy_func():
|
||||||
|
pass
|
||||||
|
|
||||||
|
dummy_func()
|
||||||
|
self.assertFalse(self.service_running.called)
|
||||||
|
self.assertFalse(self.service_stop.called)
|
||||||
|
self.assertTrue(_mc.called)
|
||||||
|
|
||||||
|
def test_service_guard_active_with_active_function_object(self):
|
||||||
|
class MockContext(object):
|
||||||
|
called = False
|
||||||
|
|
||||||
|
def complete_contexts(self):
|
||||||
|
self.called = True
|
||||||
|
return ['interfacea', 'interfaceb']
|
||||||
|
|
||||||
|
_mc = MockContext()
|
||||||
|
|
||||||
|
@utils.service_guard({'test': ['interfacea', 'interfaceb']},
|
||||||
|
_mc, lambda: False)
|
||||||
|
def dummy_func():
|
||||||
|
pass
|
||||||
|
|
||||||
|
dummy_func()
|
||||||
|
self.assertFalse(self.service_running.called)
|
||||||
|
self.assertFalse(_mc.called)
|
||||||
|
|
||||||
def helper_test_is_api_ready(self, tgt):
|
def helper_test_is_api_ready(self, tgt):
|
||||||
fake_config = MagicMock()
|
fake_config = MagicMock()
|
||||||
with patch('charmhelpers.contrib.openstack.utils.'
|
with patch('charmhelpers.contrib.openstack.utils.'
|
||||||
|
Loading…
Reference in New Issue
Block a user