From 75a58a355616db591a62049d804134cc27ea3dab Mon Sep 17 00:00:00 2001 From: James Slagle Date: Thu, 22 Dec 2016 10:31:35 -0500 Subject: [PATCH] Sync template processing tripleo-heat-templates and tripleo-common now both have their own code for template processing as of Ie664c9c5f455b7320a58a26f35bc403355408d9b. As the above commit mentions, this duplication should be refactored in the future so these 2 processes can share the same code. In the short term however, we need to keep these 2 in sync. This patch adds handling of the disable_constraints logic to the tripleo-common code so that the 2 are in sync. Partially-implements: blueprint split-stack-software-configuration Change-Id: Id318d0bc3a6358749b7b83b16640012611f15c4f --- tripleo_common/actions/templates.py | 5 ++ .../tests/actions/test_templates.py | 59 +++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/tripleo_common/actions/templates.py b/tripleo_common/actions/templates.py index 6c6ad807f..96a2f66b7 100644 --- a/tripleo_common/actions/templates.py +++ b/tripleo_common/actions/templates.py @@ -134,6 +134,9 @@ class ProcessTemplatesAction(base.TripleOAction): raise Exception(error_msg) role_names = [r.get('name') for r in role_data] + r_map = {} + for r in role_data: + r_map[r.get('name')] = r excl_templates = j2_excl_data.get('name') for f in [f.get('name') for f in container_files[1]]: @@ -150,6 +153,8 @@ class ProcessTemplatesAction(base.TripleOAction): for role in role_names: j2_data = {'role': role} LOG.info("jinja2 rendering role %s" % role) + if r_map[role].get('disable_constraints', False): + j2_data['disable_constraints'] = True out_f = "-".join( [role.lower(), os.path.basename(f).replace('.role.j2.yaml', diff --git a/tripleo_common/tests/actions/test_templates.py b/tripleo_common/tests/actions/test_templates.py index a881e491d..04be40c8d 100644 --- a/tripleo_common/tests/actions/test_templates.py +++ b/tripleo_common/tests/actions/test_templates.py @@ -71,6 +71,27 @@ name: J2_EXCLUDES_EMPTY_FILE = """ """ +ROLE_DATA_DISABLE_CONSTRAINTS_YAML = """ +- name: RoleWithDisableConstraints + disable_constraints: True +""" + +JINJA_SNIPPET_DISABLE_CONSTRAINTS = """ + {{role}}Image: + type: string + default: overcloud-full +{% if disable_constraints is not defined %} + constraints: + - custom_constraint: glance.image +{% endif %} +""" + +EXPECTED_JINJA_RESULT_DISABLE_CONSTRAINTS = """ + RoleWithDisableConstraintsImage: + type: string + default: overcloud-full +""" + class UploadTemplatesActionTest(base.TestCase): @@ -190,6 +211,44 @@ class ProcessTemplatesActionTest(base.TestCase): swift.put_object.assert_has_calls( put_object_mock_calls, any_order=True) + @mock.patch('tripleo_common.actions.base.TripleOAction.get_object_client') + @mock.patch('mistral.context.ctx') + def test_process_custom_roles_disable_constraints( + self, ctx_mock, get_obj_client_mock): + + def return_multiple_files(*args): + if args[1] == constants.OVERCLOUD_J2_NAME: + return ['', JINJA_SNIPPET_DISABLE_CONSTRAINTS] + if args[1] == 'disable-constraints.role.j2.yaml': + return ['', JINJA_SNIPPET_DISABLE_CONSTRAINTS] + if args[1] == constants.OVERCLOUD_J2_EXCLUDES: + return ['', J2_EXCLUDES] + elif args[1] == constants.OVERCLOUD_J2_ROLES_NAME: + return ['', ROLE_DATA_DISABLE_CONSTRAINTS_YAML] + + def return_container_files(*args): + return ('headers', [{'name': constants.OVERCLOUD_J2_NAME}, + {'name': 'disable-constraints.role.j2.yaml'}, + {'name': constants.OVERCLOUD_J2_ROLES_NAME}]) + + # setup swift + swift = mock.MagicMock() + swift.get_object = mock.MagicMock(side_effect=return_multiple_files) + swift.get_container = mock.MagicMock( + side_effect=return_container_files) + get_obj_client_mock.return_value = swift + + # Test + action = templates.ProcessTemplatesAction() + action._process_custom_roles() + + put_object_mock_call = mock.call( + constants.DEFAULT_CONTAINER_NAME, + "rolewithdisableconstraints-disable-constraints.yaml", + EXPECTED_JINJA_RESULT_DISABLE_CONSTRAINTS) + self.assertEqual(swift.put_object.call_args_list[1], + put_object_mock_call) + @mock.patch('tripleo_common.actions.base.TripleOAction.get_object_client') @mock.patch('mistral.context.ctx') def test_j2_render_and_put(self, ctx_mock, get_obj_client_mock):