Add tests for vault authorize-charm action

Add tests for the authorize-charm action on the vault app. To
support this add get_action method to return an applications
actions. However, this is not implemented in libjuju yet so
fallback to subprocess
This commit is contained in:
Liam Young
2018-04-19 10:28:35 +00:00
parent 128652b43c
commit bcfd6aaf30
4 changed files with 56 additions and 3 deletions

View File

@@ -166,3 +166,12 @@ class TestModel(ut_utils.BaseTestCase):
self.unit1.run_action.assert_called_once_with(
'backup',
backup_dir='/dev/null')
def test_get_actions(self):
self.patch_object(model.subprocess, 'check_output')
self.check_output.return_value = 'action: "action desc"'
self.assertEqual(
model.get_actions('mname', 'myapp'),
{'action': "action desc"})
self.check_output.assert_called_once_with(
['juju', 'actions', '-m', 'mname', 'myapp', '--format', 'yaml'])

View File

@@ -5,8 +5,10 @@ import time
import unittest
import uuid
import zaza.charm_lifecycle.utils as lifecycle_utils
import zaza.charm_tests.test_utils as test_utils
import zaza.charm_tests.vault.utils as vault_utils
import zaza.model
class VaultTest(unittest.TestCase):
@@ -17,9 +19,9 @@ class VaultTest(unittest.TestCase):
cls.vip_client = vault_utils.get_vip_client()
if cls.vip_client:
cls.clients.append(cls.vip_client)
vault_creds = vault_utils.get_credentails()
vault_utils.unseal_all(cls.clients, vault_creds['keys'][0])
vault_utils.auth_all(cls.clients, vault_creds['root_token'])
cls.vault_creds = vault_utils.get_credentails()
vault_utils.unseal_all(cls.clients, cls.vault_creds['keys'][0])
vault_utils.auth_all(cls.clients, cls.vault_creds['root_token'])
def test_all_clients_authenticated(self):
for client in self.clients:
@@ -72,6 +74,20 @@ class VaultTest(unittest.TestCase):
self.assertFalse(client.hvac_client.seal_status['sealed'])
self.assertTrue(client.hvac_client.seal_status['cluster_name'])
def test_vault_authorize_charm_action(self):
vault_actions = zaza.model.get_actions(
lifecycle_utils.get_juju_model(),
'vault')
if 'authorize-charm' not in vault_actions:
raise unittest.SkipTest('Action not defined')
action = vault_utils.run_charm_authorize(
self.vault_creds['root_token'])
self.assertEqual(action.status, 'completed')
client = self.clients[0]
self.assertIn(
'local-charm-policy',
client.hvac_client.list_policies())
if __name__ == '__main__':
unittest.main()

View File

@@ -200,3 +200,12 @@ def auth_all(clients, token):
"""
for client in clients:
client.hvac_client.token = token
def run_charm_authorize(token):
unit = zaza.model.get_first_unit_name(utils.get_juju_model(), 'vault')
return zaza.model.run_action(
utils.get_juju_model(),
unit,
'authorize-charm',
action_params={'token': token})

View File

@@ -1,5 +1,7 @@
import asyncio
from async_generator import async_generator, yield_, asynccontextmanager
import subprocess
import yaml
from juju import loop
from juju.model import Model
@@ -362,6 +364,23 @@ async def async_run_action(model_name, unit_name, action_name,
run_action = sync_wrapper(async_run_action)
def get_actions(model_name, application_name):
"""Get the actions an applications supports
:param model_name: Name of model to query.
:type model_name: str
:param application_name: Name of application
:type application_name: str
:returns: Dictionary of actions and their descriptions
:rtype: dict
"""
# libjuju has not implemented get_actions yet
# https://github.com/juju/python-libjuju/issues/226
cmd = ['juju', 'actions', '-m', model_name, application_name,
'--format', 'yaml']
return yaml.load(subprocess.check_output(cmd))
def main():
# Run the deploy coroutine in an asyncio event loop, using a helper
# that abstracts loop creation and teardown.