From a8dc3e7d7a27d791e30b7796a0c613b41264b8cb Mon Sep 17 00:00:00 2001 From: Rabi Mishra Date: Wed, 11 Mar 2020 14:02:00 +0530 Subject: [PATCH] Move UploadTemplatesAction functionality to utils Change-Id: I007473967b57cda40bb1c9d7a2546a71a0c02db5 --- tripleo_common/actions/templates.py | 13 +- .../tests/actions/test_templates.py | 116 ------------------ tripleo_common/tests/utils/test_template.py | 19 +++ tripleo_common/utils/template.py | 13 ++ 4 files changed, 36 insertions(+), 125 deletions(-) delete mode 100644 tripleo_common/tests/actions/test_templates.py diff --git a/tripleo_common/actions/templates.py b/tripleo_common/actions/templates.py index 9b01d7011..a902809a1 100644 --- a/tripleo_common/actions/templates.py +++ b/tripleo_common/actions/templates.py @@ -13,12 +13,10 @@ # License for the specific language governing permissions and limitations # under the License. -import tempfile - from tripleo_common.actions import base from tripleo_common import constants from tripleo_common.utils import plan as plan_utils -from tripleo_common.utils import tarball +from tripleo_common.utils import template as template_utils class UploadTemplatesAction(base.TripleOAction): @@ -31,12 +29,9 @@ class UploadTemplatesAction(base.TripleOAction): self.dir_to_upload = dir_to_upload def run(self, context): - with tempfile.NamedTemporaryFile() as tmp_tarball: - tarball.create_tarball(self.dir_to_upload, tmp_tarball.name) - tarball.tarball_extract_to_swift_container( - self.get_object_client(context), - tmp_tarball.name, - self.container) + swift = self.get_object_client(context) + template_utils.upload_templates_as_tarball( + swift, self.dir_to_upload, self.container) class UploadPlanEnvironmentAction(base.TripleOAction): diff --git a/tripleo_common/tests/actions/test_templates.py b/tripleo_common/tests/actions/test_templates.py deleted file mode 100644 index d59cb5c2c..000000000 --- a/tripleo_common/tests/actions/test_templates.py +++ /dev/null @@ -1,116 +0,0 @@ -# Copyright 2016 Red Hat, Inc. -# All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. -import mock - -from tripleo_common.actions import templates -from tripleo_common import constants -from tripleo_common.tests import base - -JINJA_SNIPPET = r""" -# Jinja loop for Role in role_data.yaml -{% for role in roles %} - # Resources generated for {{role.name}} Role - {{role.name}}ServiceChain: - type: OS::TripleO::Services - properties: - Services: - get_param: {{role.name}}Services - ServiceNetMap: {get_attr: [ServiceNetMap, service_net_map]} - EndpointMap: {get_attr: [EndpointMap, endpoint_map]} - DefaultPasswords: {get_attr: [DefaultPasswords, passwords]} -{% endfor %}""" - -ROLE_DATA_YAML = r""" -- - name: CustomRole -""" - -NETWORK_DATA_YAML = r""" -- - name: InternalApi -""" - -EXPECTED_JINJA_RESULT = r""" -# Jinja loop for Role in role_data.yaml - - # Resources generated for CustomRole Role - CustomRoleServiceChain: - type: OS::TripleO::Services - properties: - Services: - get_param: CustomRoleServices - ServiceNetMap: {get_attr: [ServiceNetMap, service_net_map]} - EndpointMap: {get_attr: [EndpointMap, endpoint_map]} - DefaultPasswords: {get_attr: [DefaultPasswords, passwords]} -""" - -JINJA_SNIPPET_CONFIG = r""" -outputs: - OS::stack_id: - description: The software config which runs puppet on the {{role}} role - value: {get_resource: {{role}}PuppetConfigImpl}""" - -J2_EXCLUDES = r""" -name: - - puppet/controller-role.yaml -""" - -J2_EXCLUDES_EMPTY_LIST = r""" -name: -""" - -J2_EXCLUDES_EMPTY_FILE = r""" -""" - -ROLE_DATA_ENABLE_NETWORKS = r""" -- name: RoleWithNetworks - networks: - - InternalApi -""" - -JINJA_SNIPPET_ROLE_NETWORKS = r""" -{%- for network in networks %} - {%- if network.name in role.networks%} - {{network.name}}Port: - type: {{role.name}}::{{network.name}}::Port - {%- endif %} -{% endfor %} -""" - -EXPECTED_JINJA_RESULT_ROLE_NETWORKS = r""" - InternalApiPort: - type: RoleWithNetworks::InternalApi::Port -""" - - -class UploadTemplatesActionTest(base.TestCase): - - @mock.patch('tempfile.NamedTemporaryFile') - @mock.patch('tripleo_common.actions.base.TripleOAction.get_object_client') - @mock.patch('tripleo_common.utils.tarball.' - 'tarball_extract_to_swift_container') - @mock.patch('tripleo_common.utils.tarball.create_tarball') - def test_run(self, mock_create_tar, mock_extract_tar, mock_get_swift, - tempfile): - mock_ctx = mock.MagicMock() - tempfile.return_value.__enter__.return_value.name = "test" - - action = templates.UploadTemplatesAction(container='tar-container') - action.run(mock_ctx) - - mock_create_tar.assert_called_once_with( - constants.DEFAULT_TEMPLATES_PATH, 'test') - mock_extract_tar.assert_called_once_with( - mock_get_swift.return_value, 'test', 'tar-container') diff --git a/tripleo_common/tests/utils/test_template.py b/tripleo_common/tests/utils/test_template.py index 653f20fd1..d9896c443 100644 --- a/tripleo_common/tests/utils/test_template.py +++ b/tripleo_common/tests/utils/test_template.py @@ -505,3 +505,22 @@ class ProcessTemplatesTest(base.TestCase): template_utils.prune_unused_services(swift, test_role_data, resource_registry, 'overcloud') mock_put.assert_not_called() + + +class UploadTemplatesTest(base.TestCase): + + @mock.patch('tempfile.NamedTemporaryFile') + @mock.patch('tripleo_common.utils.tarball.' + 'tarball_extract_to_swift_container') + @mock.patch('tripleo_common.utils.tarball.create_tarball') + def test_upload_templates(self, mock_create_tar, + mock_extract_tar, tempfile): + tempfile.return_value.__enter__.return_value.name = "test" + + swift = mock.MagicMock() + template_utils.upload_templates_as_tarball( + swift, container='tar-container') + mock_create_tar.assert_called_once_with( + constants.DEFAULT_TEMPLATES_PATH, 'test') + mock_extract_tar.assert_called_once_with( + swift, 'test', 'tar-container') diff --git a/tripleo_common/utils/template.py b/tripleo_common/utils/template.py index 5a29c34c4..016bc9d05 100644 --- a/tripleo_common/utils/template.py +++ b/tripleo_common/utils/template.py @@ -16,6 +16,7 @@ import jinja2 import logging import os import six +import tempfile import yaml from heatclient import exc as heat_exc @@ -25,6 +26,7 @@ from tripleo_common import constants from tripleo_common.utils import parameters from tripleo_common.utils import plan as plan_utils from tripleo_common.utils import swift as swiftutils +from tripleo_common.utils import tarball as tarball LOG = logging.getLogger(__name__) @@ -413,3 +415,14 @@ def process_templates(swift, heat, container=constants.DEFAULT_CONTAINER_NAME, 'environment': heat_args['env'], 'files': files } + + +def upload_templates_as_tarball( + swift, dir_to_upload=constants.DEFAULT_TEMPLATES_PATH, + container=constants.DEFAULT_CONTAINER_NAME): + with tempfile.NamedTemporaryFile() as tmp_tarball: + tarball.create_tarball(dir_to_upload, tmp_tarball.name) + tarball.tarball_extract_to_swift_container( + swift, + tmp_tarball.name, + container)