Makes sorting environments with capabilities-map optional

This patch adds an argument to the UpdateCapabilitesAction to
optionally enable the sorting of environments utilizing the
capabilites-map file in the plan.  This patch also restores
the unit tests before the introduction of the additional
sorting and adds a new test for the optional sorting.

Change-Id: I6944ba7f673344f0d40c0bdf8cfba78e2d758da5
This commit is contained in:
Ryan Brady 2018-07-12 10:35:57 -04:00
parent 84dcd3a9fc
commit 27c7ee28c2
2 changed files with 63 additions and 19 deletions

View File

@ -153,16 +153,20 @@ class UpdateCapabilitiesAction(base.TripleOAction):
:param purge_missing: remove any environments from the plan environment
that aren't included in the environments map
defaults to False
:param sort_environments: use the dependencies defined in the
capabilites-map.yaml file in the plan to order
the environments
:return: the updated plan environment
"""
def __init__(self, environments,
container=constants.DEFAULT_CONTAINER_NAME,
purge_missing=False):
purge_missing=False, sort_environments=False):
super(UpdateCapabilitiesAction, self).__init__()
self.container = container
self.environments = environments
self.purge_missing = purge_missing
self.sort_environments = sort_environments
def run(self, context):
swift = self.get_object_client(context)
@ -193,22 +197,24 @@ class UpdateCapabilitiesAction(base.TripleOAction):
self.cache_delete(context, self.container, "tripleo.parameters.get")
# get the capabilities-map content to perform the environment ordering
try:
swift = self.get_object_client(context)
map_file = swift.get_object(
self.container, 'capabilities-map.yaml')
capabilities = yaml.safe_load(map_file[1])
except swiftexceptions.ClientException as err:
err_msg = ("Error retrieving capabilities-map.yaml for "
"plan %s: %s" % (self.container, err))
LOG.exception(err_msg)
return actions.Result(error=err_msg)
if self.sort_environments:
# get the capabilities-map content to perform the environment
# ordering
try:
swift = self.get_object_client(context)
map_file = swift.get_object(
self.container, 'capabilities-map.yaml')
capabilities = yaml.safe_load(map_file[1])
except swiftexceptions.ClientException as err:
err_msg = ("Error retrieving capabilities-map.yaml for "
"plan %s: %s" % (self.container, err))
LOG.exception(err_msg)
return actions.Result(error=err_msg)
ordered_env = plan_utils.apply_environments_order(
capabilities, env.get('environments', []))
ordered_env = plan_utils.apply_environments_order(
capabilities, env.get('environments', []))
env['environments'] = ordered_env
env['environments'] = ordered_env
try:
plan_utils.put_env(swift, env)

View File

@ -246,6 +246,46 @@ class UpdateCapabilitiesActionTest(base.TestCase):
- path: /path/to/overcloud-default-env.yaml
- path: /path/to/ceph-storage-env.yaml
"""
swift.get_object.return_value = ({}, mocked_env)
get_object_client_mock.return_value = swift
environments = {
'/path/to/ceph-storage-env.yaml': False,
'/path/to/network-isolation.json': False,
'/path/to/poc-custom-env.yaml': True
}
action = heat_capabilities.UpdateCapabilitiesAction(
environments, self.container_name)
self.assertEqual({
'name': 'test-container',
'environments': [
{'path': '/path/to/overcloud-default-env.yaml'},
{'path': '/path/to/poc-custom-env.yaml'}
]},
action.run(mock_ctx))
mock_cache.assert_called_once_with(
mock_ctx,
self.container_name,
"tripleo.parameters.get"
)
@mock.patch('tripleo_common.actions.base.TripleOAction.'
'cache_delete')
@mock.patch('tripleo_common.actions.base.TripleOAction.get_object_client')
def test_run_with_sorting_environments(self, get_object_client_mock,
mock_cache):
mock_ctx = mock.MagicMock()
# setup swift
swift = mock.MagicMock()
mocked_env = """
name: test-container
environments:
- path: /path/to/overcloud-default-env.yaml
- path: /path/to/ceph-storage-env.yaml
"""
swift.get_object.side_effect = (
({}, mocked_env),
({}, MAPPING_YAML_CONTENTS))
@ -258,7 +298,7 @@ class UpdateCapabilitiesActionTest(base.TestCase):
}
action = heat_capabilities.UpdateCapabilitiesAction(
environments, self.container_name)
environments, self.container_name, sort_environments=True)
self.assertEqual({
'name': 'test-container',
'environments': [
@ -289,9 +329,7 @@ class UpdateCapabilitiesActionTest(base.TestCase):
- path: /path/to/overcloud-default-env.yaml
- path: /path/to/ceph-storage-env.yaml
"""
swift.get_object.side_effect = (
({}, mocked_env),
({}, MAPPING_YAML_CONTENTS))
swift.get_object.return_value = ({}, mocked_env)
get_object_client_mock.return_value = swift
environments = {