From 27c7ee28c2c13ff520b4cd4d993beb4c307bcea0 Mon Sep 17 00:00:00 2001 From: Ryan Brady Date: Thu, 12 Jul 2018 10:35:57 -0400 Subject: [PATCH] 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 --- tripleo_common/actions/heat_capabilities.py | 36 +++++++++------ .../tests/actions/test_heat_capabilities.py | 46 +++++++++++++++++-- 2 files changed, 63 insertions(+), 19 deletions(-) diff --git a/tripleo_common/actions/heat_capabilities.py b/tripleo_common/actions/heat_capabilities.py index 71d1bfff9..c3fd17bc2 100644 --- a/tripleo_common/actions/heat_capabilities.py +++ b/tripleo_common/actions/heat_capabilities.py @@ -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) diff --git a/tripleo_common/tests/actions/test_heat_capabilities.py b/tripleo_common/tests/actions/test_heat_capabilities.py index 87d1b2b00..32002ffc8 100644 --- a/tripleo_common/tests/actions/test_heat_capabilities.py +++ b/tripleo_common/tests/actions/test_heat_capabilities.py @@ -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 = {