Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
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.
  • Loading branch information
aviaefrat committed May 9, 2017
1 parent 16fcca4 commit d0411d3de37bb31073fda605cd9b73431b685d92
Showing 4 changed files with 36 additions and 10 deletions.
@@ -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))


@@ -22,7 +22,7 @@
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 @@
}


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)
@@ -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)
@@ -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 @@ def test_store_no_exception(self, monkeypatch, mock_object):
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 @@ def test_create_archive_successful(self, monkeypatch, mock_object):
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'))

0 comments on commit d0411d3

Please sign in to comment.