ARIA-210 Handle relative paths in CLI service-templates This was a rather simple change, mainly involving adding absolute path references.
The problems were only in `service-templates store` and in `service-templates create-archive`. `service-templates validate` was not affected. Project: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/commit/d0411d3d Tree: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/tree/d0411d3d Diff: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/diff/d0411d3d Branch: refs/heads/ARIA-148-extra-cli-commands Commit: d0411d3de37bb31073fda605cd9b73431b685d92 Parents: 16fcca4 Author: Avia Efrat <a...@gigaspaces.com> Authored: Mon May 8 17:45:23 2017 +0300 Committer: Avia Efrat <a...@gigaspaces.com> Committed: Tue May 9 17:30:22 2017 +0300 ---------------------------------------------------------------------- aria/cli/commands/service_templates.py | 4 +++- aria/cli/csar.py | 18 ++++++++++-------- aria/cli/service_template_utils.py | 2 +- tests/cli/test_service_templates.py | 22 ++++++++++++++++++++++ 4 files changed, 36 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/d0411d3d/aria/cli/commands/service_templates.py ---------------------------------------------------------------------- diff --git a/aria/cli/commands/service_templates.py b/aria/cli/commands/service_templates.py index 2537012..e459871 100644 --- a/aria/cli/commands/service_templates.py +++ b/aria/cli/commands/service_templates.py @@ -195,7 +195,9 @@ def create_archive(service_template_path, destination, logger): `destination` is the path of the output CSAR archive file """ logger.info('Creating a CSAR archive') - csar.write(os.path.dirname(service_template_path), service_template_path, destination, logger) + if not destination.endswith(csar.CSAR_FILE_EXTENSION): + destination += csar.CSAR_FILE_EXTENSION + csar.write(service_template_path, destination, logger) logger.info('CSAR archive created at {0}'.format(destination)) http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/d0411d3d/aria/cli/csar.py ---------------------------------------------------------------------- diff --git a/aria/cli/csar.py b/aria/cli/csar.py index 5bc35ac..8f44557 100644 --- a/aria/cli/csar.py +++ b/aria/cli/csar.py @@ -22,7 +22,7 @@ import zipfile import requests from ruamel import yaml - +CSAR_FILE_EXTENSION = '.csar' META_FILE = 'TOSCA-Metadata/TOSCA.meta' META_FILE_VERSION_KEY = 'TOSCA-Meta-File-Version' META_FILE_VERSION_VALUE = '1.0' @@ -38,17 +38,19 @@ BASE_METADATA = { } -def write(source, entry, destination, logger): - source = os.path.expanduser(source) - destination = os.path.expanduser(destination) - entry_definitions = os.path.join(source, entry) +def write(service_template_path, destination, logger): + + service_template_path = os.path.abspath(os.path.expanduser(service_template_path)) + source = os.path.dirname(service_template_path) + entry = os.path.basename(service_template_path) + meta_file = os.path.join(source, META_FILE) if not os.path.isdir(source): raise ValueError('{0} is not a directory. Please specify the service template ' 'directory.'.format(source)) - if not os.path.isfile(entry_definitions): + if not os.path.isfile(service_template_path): raise ValueError('{0} does not exists. Please specify a valid entry point.' - .format(entry_definitions)) + .format(service_template_path)) if os.path.exists(destination): raise ValueError('{0} already exists. Please provide a path to where the CSAR should be ' 'created.'.format(destination)) @@ -175,4 +177,4 @@ def read(source, destination=None, logger=None): def is_csar_archive(source): - return source.endswith('.csar') + return source.endswith(CSAR_FILE_EXTENSION) http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/d0411d3d/aria/cli/service_template_utils.py ---------------------------------------------------------------------- diff --git a/aria/cli/service_template_utils.py b/aria/cli/service_template_utils.py index 382cce1..c953c02 100644 --- a/aria/cli/service_template_utils.py +++ b/aria/cli/service_template_utils.py @@ -53,7 +53,7 @@ def get(source, service_template_filename): return _get_service_template_file_from_archive(source, service_template_filename) else: # Maybe check if yaml. - return source + return os.path.abspath(source) elif len(source.split('/')) == 2: url = _map_to_github_url(source) downloaded_file = utils.download_file(url) http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/d0411d3d/tests/cli/test_service_templates.py ---------------------------------------------------------------------- diff --git a/tests/cli/test_service_templates.py b/tests/cli/test_service_templates.py index dd9eedd..22a8fc8 100644 --- a/tests/cli/test_service_templates.py +++ b/tests/cli/test_service_templates.py @@ -12,6 +12,8 @@ # 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 os +import zipfile import pytest import mock @@ -131,6 +133,18 @@ class TestServiceTemplatesStore(TestCliBase): assert 'Service template {name} stored'.format( name=mock_models.SERVICE_TEMPLATE_NAME) in self.logger_output_string + def test_store_relative_path_single_yaml_file(self, monkeypatch, mock_object): + monkeypatch.setattr(Core, 'create_service_template', mock_object) + monkeypatch.setattr(os.path, 'isfile', lambda x: True) + monkeypatch.setattr(service_template_utils, '_is_archive', lambda x: False) + + self.invoke('service_templates store service_template.yaml {name}'.format( + name=mock_models.SERVICE_TEMPLATE_NAME)) + + mock_object.assert_called_with(os.path.join(os.getcwd(), 'service_template.yaml'), + mock.ANY, + mock.ANY) + def test_store_raises_exception_resulting_from_name_uniqueness(self, monkeypatch, mock_object): monkeypatch.setattr(service_template_utils, 'get', mock_object) @@ -244,3 +258,11 @@ class TestServiceTemplatesCreateArchive(TestCliBase): monkeypatch.setattr(csar, 'write', mock_object) self.invoke('service_templates create_archive stubpath stubdest') assert 'CSAR archive created at stubdest' in self.logger_output_string + + def test_create_archive_from_relative_path(self, monkeypatch, mock_object): + + monkeypatch.setattr(os.path, 'isfile', mock_object) + monkeypatch.setattr(zipfile, 'ZipFile', mock.MagicMock) + + self.invoke('service_templates create_archive archive stubdest') + mock_object.assert_called_with(os.path.join(os.getcwd(), 'archive'))