1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements. See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License. You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
21 from aria.cli import service_template_utils, csar
22 from aria.cli.env import _Environment
23 from aria.core import Core
24 from aria.exceptions import AriaException
25 from aria.storage import exceptions as storage_exceptions
27 from .base_test import ( # pylint: disable=unused-import
29 assert_exception_raised,
33 from ..mock import models as mock_models
36 class TestServiceTemplatesShow(TestCliBase):
38 def test_header_string(self, monkeypatch, mock_storage):
40 monkeypatch.setattr(_Environment, 'model_storage', mock_storage)
41 self.invoke('service_templates show test_st')
42 assert 'Showing service template test_st...' in self.logger_output_string
44 def test_no_services_no_description(self, monkeypatch, mock_storage):
46 monkeypatch.setattr(_Environment, 'model_storage', mock_storage)
47 self.invoke('service_templates show test_st')
49 assert 'Description:' not in self.logger_output_string
50 assert 'Existing services:' not in self.logger_output_string
52 def test_no_services_yes_description(self, monkeypatch, mock_storage):
54 monkeypatch.setattr(_Environment, 'model_storage', mock_storage)
55 st = mock_models.create_service_template(description='test_description')
56 monkeypatch.setattr(mock_storage.service_template, 'get_by_name',
57 mock.MagicMock(return_value=st))
59 self.invoke('service_templates show test_st')
60 assert 'Description:' in self.logger_output_string
61 assert 'test_description' in self.logger_output_string
62 assert 'Existing services:' not in self.logger_output_string
64 def test_one_service_no_description(self, monkeypatch, mock_storage):
66 monkeypatch.setattr(_Environment, 'model_storage', mock_storage)
67 st = mock_models.create_service_template()
68 s = mock_models.create_service(st)
69 st.services = {s.name: s}
70 monkeypatch.setattr(mock_storage.service_template, 'get_by_name',
71 mock.MagicMock(return_value=st))
73 self.invoke('service_templates show test_st')
75 assert 'Description:' not in self.logger_output_string
76 assert 'Existing services:' in self.logger_output_string
77 assert mock_models.SERVICE_NAME in self.logger_output_string
79 def test_one_service_yes_description(self, monkeypatch, mock_storage):
81 monkeypatch.setattr(_Environment, 'model_storage', mock_storage)
82 st = mock_models.create_service_template(description='test_description')
83 s = mock_models.create_service(st)
84 st.services = {s.name: s}
85 monkeypatch.setattr(mock_storage.service_template, 'get_by_name',
86 mock.MagicMock(return_value=st))
88 self.invoke('service_templates show test_st')
90 assert 'Description:' in self.logger_output_string
91 assert 'test_description' in self.logger_output_string
92 assert 'Existing services:' in self.logger_output_string
93 assert 'test_s' in self.logger_output_string
96 class TestServiceTemplatesList(TestCliBase):
98 def test_header_string(self, monkeypatch, mock_storage):
100 monkeypatch.setattr(_Environment, 'model_storage', mock_storage)
101 self.invoke('service_templates list')
102 assert 'Listing all service templates...' in self.logger_output_string
104 @pytest.mark.parametrize('sort_by, order, sort_by_in_output, order_in_output', [
105 ('', '', 'created_at', 'asc'),
106 ('', ' --descending', 'created_at', 'desc'),
107 (' --sort-by name', '', 'name', 'asc'),
108 (' --sort-by name', ' --descending', 'name', 'desc')
110 def test_all_sorting_combinations(self, monkeypatch, mock_storage, sort_by, order,
111 sort_by_in_output, order_in_output):
113 monkeypatch.setattr(_Environment, 'model_storage', mock_storage)
114 self.invoke('service_templates list{sort_by}{order}'.format(sort_by=sort_by, order=order))
116 mock_storage.service_template.list.assert_called_with(
117 sort={sort_by_in_output: order_in_output})
118 assert mock_models.SERVICE_TEMPLATE_NAME in self.logger_output_string
121 class TestServiceTemplatesStore(TestCliBase):
123 def test_header_string(self, monkeypatch, mock_storage):
125 monkeypatch.setattr(_Environment, 'model_storage', mock_storage)
126 self.invoke('service_templates store stubpath test_st')
127 assert 'Storing service template test_st...' in self.logger_output_string
129 def test_store_no_exception(self, monkeypatch, mock_object):
131 monkeypatch.setattr(Core, 'create_service_template', mock_object)
132 monkeypatch.setattr(service_template_utils, 'get', mock_object)
133 monkeypatch.setattr(os.path, 'dirname', mock_object)
134 self.invoke('service_templates store stubpath {name}'.format(
135 name=mock_models.SERVICE_TEMPLATE_NAME))
136 assert 'Service template {name} stored'.format(
137 name=mock_models.SERVICE_TEMPLATE_NAME) in self.logger_output_string
139 def test_store_relative_path_single_yaml_file(self, monkeypatch, mock_object):
140 monkeypatch.setattr(Core, 'create_service_template', mock_object)
141 monkeypatch.setattr(os.path, 'isfile', lambda x: True)
142 monkeypatch.setattr(service_template_utils, '_is_archive', lambda x: False)
144 self.invoke('service_templates store service_template.yaml {name}'.format(
145 name=mock_models.SERVICE_TEMPLATE_NAME))
147 mock_object.assert_called_with(os.path.join(os.getcwd(), 'service_template.yaml'),
151 def test_store_raises_exception_resulting_from_name_uniqueness(self, monkeypatch, mock_object):
153 monkeypatch.setattr(service_template_utils, 'get', mock_object)
154 monkeypatch.setattr(Core,
155 'create_service_template',
156 raise_exception(storage_exceptions.NotFoundError,
157 msg='UNIQUE constraint failed'))
158 monkeypatch.setattr(os.path, 'dirname', mock_object)
160 assert_exception_raised(
161 self.invoke('service_templates store stubpath test_st'),
162 expected_exception=storage_exceptions.NotFoundError,
163 expected_msg='There already a exists a service template with the same name')
165 def test_store_raises_exception(self, monkeypatch, mock_object):
167 monkeypatch.setattr(service_template_utils, 'get', mock_object)
168 monkeypatch.setattr(Core,
169 'create_service_template',
170 raise_exception(storage_exceptions.NotFoundError))
171 monkeypatch.setattr(os.path, 'dirname', mock_object)
173 assert_exception_raised(
174 self.invoke('service_templates store stubpath test_st'),
175 expected_exception=storage_exceptions.StorageError)
178 class TestServiceTemplatesDelete(TestCliBase):
180 def test_header_string(self, monkeypatch, mock_storage):
182 monkeypatch.setattr(_Environment, 'model_storage', mock_storage)
183 self.invoke('service_templates delete test_st')
184 assert 'Deleting service template test_st...' in self.logger_output_string
186 def test_delete_no_exception(self, monkeypatch, mock_object):
188 monkeypatch.setattr(_Environment, 'model_storage', mock_object)
189 monkeypatch.setattr(Core, 'delete_service_template', mock_object)
190 self.invoke('service_templates delete {name}'.format(
191 name=mock_models.SERVICE_TEMPLATE_NAME))
192 assert 'Service template {name} deleted'.format(
193 name=mock_models.SERVICE_TEMPLATE_NAME) in self.logger_output_string
195 def test_delete_raises_exception(self, monkeypatch, mock_object):
197 monkeypatch.setattr(_Environment, 'model_storage', mock_object)
198 monkeypatch.setattr(Core,
199 'delete_service_template',
200 raise_exception(storage_exceptions.StorageError))
202 assert_exception_raised(
203 self.invoke('service_templates delete test_st'),
204 expected_exception=storage_exceptions.StorageError,
208 class TestServiceTemplatesInputs(TestCliBase):
210 def test_header_string(self, monkeypatch, mock_storage):
212 monkeypatch.setattr(_Environment, 'model_storage', mock_storage)
213 self.invoke('service_templates inputs test_st')
214 assert 'Showing inputs for service template test_st...' in self.logger_output_string
216 def test_inputs_existing_inputs(self, monkeypatch, mock_storage):
217 monkeypatch.setattr(_Environment, 'model_storage', mock_storage)
218 input = mock_models.create_input(name='input1', value='value1')
219 st = mock_models.create_service_template(inputs={'input1': input})
220 monkeypatch.setattr(mock_storage.service_template, 'get_by_name',
221 mock.MagicMock(return_value=st))
223 self.invoke('service_templates inputs with_inputs')
224 assert 'input1' in self.logger_output_string and 'value1' in self.logger_output_string
226 def test_inputs_no_inputs(self, monkeypatch, mock_storage):
227 monkeypatch.setattr(_Environment, 'model_storage', mock_storage)
228 self.invoke('service_templates inputs without_inputs')
229 assert 'No inputs' in self.logger_output_string
232 class TestServiceTemplatesValidate(TestCliBase):
234 def test_header_string(self, monkeypatch, mock_storage):
236 monkeypatch.setattr(_Environment, 'model_storage', mock_storage)
237 self.invoke('service_templates validate stubpath')
238 assert 'Validating service template: stubpath' in self.logger_output_string
240 def test_validate_no_exception(self, monkeypatch, mock_object):
241 monkeypatch.setattr(Core, 'validate_service_template', mock_object)
242 monkeypatch.setattr(service_template_utils, 'get', mock_object)
243 self.invoke('service_templates validate stubpath')
244 assert 'Service template validated successfully' in self.logger_output_string
246 def test_validate_raises_exception(self, monkeypatch, mock_object):
247 monkeypatch.setattr(Core, 'validate_service_template', raise_exception(AriaException))
248 monkeypatch.setattr(service_template_utils, 'get', mock_object)
249 assert_exception_raised(
250 self.invoke('service_templates validate stubpath'),
251 expected_exception=AriaException)
254 class TestServiceTemplatesCreateArchive(TestCliBase):
256 def test_header_string(self, monkeypatch, mock_storage):
258 monkeypatch.setattr(_Environment, 'model_storage', mock_storage)
259 self.invoke('service_templates create_archive stubpath stubdest')
260 assert 'Creating a CSAR archive' in self.logger_output_string
262 def test_create_archive_successful(self, monkeypatch, mock_object):
263 monkeypatch.setattr(csar, 'write', mock_object)
264 self.invoke('service_templates create_archive stubpath stubdest')
265 assert 'CSAR archive created at stubdest' in self.logger_output_string
267 def test_create_archive_from_relative_path(self, monkeypatch, mock_object):
269 monkeypatch.setattr(os.path, 'isfile', mock_object)
270 monkeypatch.setattr(zipfile, 'ZipFile', mock.MagicMock)
272 self.invoke('service_templates create_archive archive stubdest')
273 mock_object.assert_called_with(os.path.join(os.getcwd(), 'archive'))