Vault charm action needs to be run on leader
The vault charm action to authorise the charm within vault needs to be run on the leader. This mp adds run_action_on_leader to support that and updates the tests.
This commit is contained in:
@@ -24,6 +24,11 @@ class TestModel(ut_utils.BaseTestCase):
|
||||
async def _wait():
|
||||
return
|
||||
|
||||
def _is_leader(leader):
|
||||
async def _inner_is_leader():
|
||||
return leader
|
||||
return _inner_is_leader
|
||||
|
||||
self.run_action = mock.MagicMock()
|
||||
self.run_action.wait.side_effect = _wait
|
||||
self.action = mock.MagicMock()
|
||||
@@ -58,6 +63,8 @@ class TestModel(ut_utils.BaseTestCase):
|
||||
self.unit2.scp_from.side_effect = _scp_from
|
||||
self.unit1.run_action.side_effect = _run_action
|
||||
self.unit2.run_action.side_effect = _run_action
|
||||
self.unit1.is_leader_from_status.side_effect = _is_leader(False)
|
||||
self.unit2.is_leader_from_status.side_effect = _is_leader(True)
|
||||
self.units = [self.unit1, self.unit2]
|
||||
_units = mock.MagicMock()
|
||||
_units.units = self.units
|
||||
@@ -175,3 +182,13 @@ class TestModel(ut_utils.BaseTestCase):
|
||||
{'action': "action desc"})
|
||||
self.check_output.assert_called_once_with(
|
||||
['juju', 'actions', '-m', 'mname', 'myapp', '--format', 'yaml'])
|
||||
|
||||
def test_run_action_on_leader(self):
|
||||
self.patch_object(model, 'Model')
|
||||
self.Model.return_value = self.Model_mock
|
||||
model.run_action_on_leader('modelname', 'app', 'backup',
|
||||
{'backup_dir': '/dev/null'})
|
||||
self.assertFalse(self.unit1.called)
|
||||
self.unit2.run_action.assert_called_once_with(
|
||||
'backup',
|
||||
backup_dir='/dev/null')
|
||||
|
||||
@@ -203,9 +203,8 @@ def auth_all(clients, token):
|
||||
|
||||
|
||||
def run_charm_authorize(token):
|
||||
unit = zaza.model.get_first_unit_name(utils.get_juju_model(), 'vault')
|
||||
return zaza.model.run_action(
|
||||
return zaza.model.run_action_on_leader(
|
||||
utils.get_juju_model(),
|
||||
unit,
|
||||
'vault',
|
||||
'authorize-charm',
|
||||
action_params={'token': token})
|
||||
|
||||
@@ -364,6 +364,33 @@ async def async_run_action(model_name, unit_name, action_name,
|
||||
run_action = sync_wrapper(async_run_action)
|
||||
|
||||
|
||||
async def async_run_action_on_leader(model_name, application_name, action_name,
|
||||
action_params=None):
|
||||
"""Run action on given unit
|
||||
|
||||
:param model_name: Name of model to query.
|
||||
:type model_name: str
|
||||
:param application_name: Name of application
|
||||
:type application_name: str
|
||||
:param action_name: Name of action to run
|
||||
:type action_name: str
|
||||
:param action_params: Dictionary of config options for action
|
||||
:type action_params: dict
|
||||
:returns: Action object
|
||||
:rtype: juju.action.Action
|
||||
"""
|
||||
async with run_in_model(model_name) as model:
|
||||
for unit in model.applications[application_name].units:
|
||||
is_leader = await unit.is_leader_from_status()
|
||||
if is_leader:
|
||||
action_obj = await unit.run_action(action_name,
|
||||
**action_params)
|
||||
await action_obj.wait()
|
||||
return action_obj
|
||||
|
||||
run_action_on_leader = sync_wrapper(async_run_action_on_leader)
|
||||
|
||||
|
||||
def get_actions(model_name, application_name):
|
||||
"""Get the actions an applications supports
|
||||
|
||||
|
||||
Reference in New Issue
Block a user