vFW and vDNS support added to azure-plugin
[multicloud/azure.git] / azure / aria / aria-extension-cloudify / src / aria / tests / cli / test_service_templates.py
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
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
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.
15 import os
16 import zipfile
17
18 import pytest
19 import mock
20
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
26
27 from .base_test import (  # pylint: disable=unused-import
28     TestCliBase,
29     assert_exception_raised,
30     raise_exception,
31     mock_storage
32 )
33 from ..mock import models as mock_models
34
35
36 class TestServiceTemplatesShow(TestCliBase):
37
38     def test_header_string(self, monkeypatch, mock_storage):
39
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
43
44     def test_no_services_no_description(self, monkeypatch, mock_storage):
45
46         monkeypatch.setattr(_Environment, 'model_storage', mock_storage)
47         self.invoke('service_templates show test_st')
48
49         assert 'Description:' not in self.logger_output_string
50         assert 'Existing services:' not in self.logger_output_string
51
52     def test_no_services_yes_description(self, monkeypatch, mock_storage):
53
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))
58
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
63
64     def test_one_service_no_description(self, monkeypatch, mock_storage):
65
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))
72
73         self.invoke('service_templates show test_st')
74
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
78
79     def test_one_service_yes_description(self, monkeypatch, mock_storage):
80
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))
87
88         self.invoke('service_templates show test_st')
89
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
94
95
96 class TestServiceTemplatesList(TestCliBase):
97
98     def test_header_string(self, monkeypatch, mock_storage):
99
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
103
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')
109     ])
110     def test_all_sorting_combinations(self, monkeypatch, mock_storage, sort_by, order,
111                                       sort_by_in_output, order_in_output):
112
113         monkeypatch.setattr(_Environment, 'model_storage', mock_storage)
114         self.invoke('service_templates list{sort_by}{order}'.format(sort_by=sort_by, order=order))
115
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
119
120
121 class TestServiceTemplatesStore(TestCliBase):
122
123     def test_header_string(self, monkeypatch, mock_storage):
124
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
128
129     def test_store_no_exception(self, monkeypatch, mock_object):
130
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
138
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)
143
144         self.invoke('service_templates store service_template.yaml {name}'.format(
145             name=mock_models.SERVICE_TEMPLATE_NAME))
146
147         mock_object.assert_called_with(os.path.join(os.getcwd(), 'service_template.yaml'),
148                                        mock.ANY,
149                                        mock.ANY)
150
151     def test_store_raises_exception_resulting_from_name_uniqueness(self, monkeypatch, mock_object):
152
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)
159
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')
164
165     def test_store_raises_exception(self, monkeypatch, mock_object):
166
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)
172
173         assert_exception_raised(
174             self.invoke('service_templates store stubpath test_st'),
175             expected_exception=storage_exceptions.StorageError)
176
177
178 class TestServiceTemplatesDelete(TestCliBase):
179
180     def test_header_string(self, monkeypatch, mock_storage):
181
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
185
186     def test_delete_no_exception(self, monkeypatch, mock_object):
187
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
194
195     def test_delete_raises_exception(self, monkeypatch, mock_object):
196
197         monkeypatch.setattr(_Environment, 'model_storage', mock_object)
198         monkeypatch.setattr(Core,
199                             'delete_service_template',
200                             raise_exception(storage_exceptions.StorageError))
201
202         assert_exception_raised(
203             self.invoke('service_templates delete test_st'),
204             expected_exception=storage_exceptions.StorageError,
205             expected_msg='')
206
207
208 class TestServiceTemplatesInputs(TestCliBase):
209
210     def test_header_string(self, monkeypatch, mock_storage):
211
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
215
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))
222
223         self.invoke('service_templates inputs with_inputs')
224         assert 'input1' in self.logger_output_string and 'value1' in self.logger_output_string
225
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
230
231
232 class TestServiceTemplatesValidate(TestCliBase):
233
234     def test_header_string(self, monkeypatch, mock_storage):
235
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
239
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
245
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)
252
253
254 class TestServiceTemplatesCreateArchive(TestCliBase):
255
256     def test_header_string(self, monkeypatch, mock_storage):
257
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
261
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
266
267     def test_create_archive_from_relative_path(self, monkeypatch, mock_object):
268
269         monkeypatch.setattr(os.path, 'isfile', mock_object)
270         monkeypatch.setattr(zipfile, 'ZipFile', mock.MagicMock)
271
272         self.invoke('service_templates create_archive archive stubdest')
273         mock_object.assert_called_with(os.path.join(os.getcwd(), 'archive'))