1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
5 # http://www.apache.org/licenses/LICENSE-2.0
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
15 from mock import patch
17 from toscaparser.nodetemplate import NodeTemplate
18 from toscaparser.tests.base import TestCase
19 from toscaparser.utils.gettextutils import _
20 import toscaparser.utils.yamlparser
21 from translator.hot.tosca.tosca_compute import ToscaCompute
24 class ToscaComputeTest(TestCase):
26 def _tosca_compute_test(self, tpl_snippet, expectedprops):
27 nodetemplates = (toscaparser.utils.yamlparser.
28 simple_parse(tpl_snippet)['node_templates'])
29 name = list(nodetemplates.keys())[0]
31 nodetemplate = NodeTemplate(name, nodetemplates)
32 nodetemplate.validate()
33 toscacompute = ToscaCompute(nodetemplate)
34 toscacompute.handle_properties()
35 if not self._compare_properties(toscacompute.properties,
37 raise Exception(_("Hot Properties are not"
38 " same as expected properties"))
40 # for time being rethrowing. Will be handled future based
41 # on new development in Glance and Graffiti
44 def _compare_properties(self, hotprops, expectedprops):
45 return all(item in hotprops.items() for item in expectedprops.items())
47 def test_node_compute_with_host_and_os_capabilities(self):
51 type: tosca.nodes.Compute
65 expectedprops = {'flavor': 'm1.large',
66 'image': 'fedora-amd64-heat-config',
67 'user_data_format': 'SOFTWARE_CONFIG',
68 'software_config_transport': 'POLL_SERVER_HEAT'}
69 self._tosca_compute_test(
73 def test_node_compute_without_os_capabilities(self):
77 type: tosca.nodes.Compute
86 expectedprops = {'flavor': 'm1.large',
88 'user_data_format': 'SOFTWARE_CONFIG',
89 'software_config_transport': 'POLL_SERVER_HEAT'}
90 self._tosca_compute_test(
94 def test_node_compute_without_host_capabilities(self):
98 type: tosca.nodes.Compute
107 expectedprops = {'flavor': None,
108 'image': 'fedora-amd64-heat-config',
109 'user_data_format': 'SOFTWARE_CONFIG',
110 'software_config_transport': 'POLL_SERVER_HEAT'}
111 self._tosca_compute_test(
115 def test_node_compute_without_properties_and_os_capabilities(self):
119 type: tosca.nodes.Compute
125 expectedprops = {'flavor': None,
127 'user_data_format': 'SOFTWARE_CONFIG',
128 'software_config_transport': 'POLL_SERVER_HEAT'}
129 self._tosca_compute_test(
133 def test_node_compute_with_only_type(self):
137 type: tosca.nodes.Compute
139 expectedprops = {'flavor': None,
141 'user_data_format': 'SOFTWARE_CONFIG',
142 'software_config_transport': 'POLL_SERVER_HEAT'}
143 self._tosca_compute_test(
147 def test_node_compute_host_capabilities_without_properties(self):
151 type: tosca.nodes.Compute
157 expectedprops = {'flavor': None,
159 'user_data_format': 'SOFTWARE_CONFIG',
160 'software_config_transport': 'POLL_SERVER_HEAT'}
161 self._tosca_compute_test(
165 def test_node_compute_host_capabilities_without_disk_size(self):
169 type: tosca.nodes.Compute
176 expectedprops = {'flavor': 'm1.large',
178 'user_data_format': 'SOFTWARE_CONFIG',
179 'software_config_transport': 'POLL_SERVER_HEAT'}
180 self._tosca_compute_test(
184 def test_node_compute_host_capabilities_without_mem_size(self):
188 type: tosca.nodes.Compute
195 expectedprops = {'flavor': 'm1.large',
197 'user_data_format': 'SOFTWARE_CONFIG',
198 'software_config_transport': 'POLL_SERVER_HEAT'}
199 self._tosca_compute_test(
203 def test_node_compute_host_capabilities_without_mem_size_disk_size(self):
207 type: tosca.nodes.Compute
213 expectedprops = {'flavor': 'm1.large',
215 'user_data_format': 'SOFTWARE_CONFIG',
216 'software_config_transport': 'POLL_SERVER_HEAT'}
217 self._tosca_compute_test(
221 @patch('requests.post')
222 @patch('requests.get')
224 def test_node_compute_with_nova_flavor(self, mock_os_getenv,
225 mock_get, mock_post):
229 type: tosca.nodes.Compute
237 with patch('translator.common.utils.'
238 'check_for_env_variables') as mock_check_env:
239 mock_check_env.return_value = True
240 mock_os_getenv.side_effect = ['demo', 'demo',
241 'demo', 'http://abc.com/5000/',
243 'demo', 'http://abc.com/5000/']
244 mock_ks_response = mock.MagicMock()
245 mock_ks_response.status_code = 200
249 'id': 'd1dfa603-3662-47e0-b0b6-3ae7914bdf76'
254 'publicURL': 'http://abc.com'
259 mock_ks_response.content = json.dumps(mock_ks_content)
260 mock_nova_response = mock.MagicMock()
261 mock_nova_response.status_code = 200
262 mock_flavor_content = {
264 'name': 'm1.mock_flavor',
270 mock_nova_response.content = \
271 json.dumps(mock_flavor_content)
272 mock_post.return_value = mock_ks_response
273 mock_get.return_value = mock_nova_response
274 expectedprops = {'flavor': 'm1.mock_flavor',
276 'user_data_format': 'SOFTWARE_CONFIG',
277 'software_config_transport': 'POLL_SERVER_HEAT'}
278 self._tosca_compute_test(
282 @patch('requests.post')
283 @patch('requests.get')
285 def test_node_compute_without_nova_flavor(self, mock_os_getenv,
286 mock_get, mock_post):
290 type: tosca.nodes.Compute
298 with patch('translator.common.utils.'
299 'check_for_env_variables') as mock_check_env:
300 mock_check_env.return_value = True
301 mock_os_getenv.side_effect = ['demo', 'demo',
302 'demo', 'http://abc.com/5000/']
303 mock_ks_response = mock.MagicMock()
305 mock_ks_response.content = json.dumps(mock_ks_content)
306 expectedprops = {'flavor': 'm1.small',
308 'user_data_format': 'SOFTWARE_CONFIG',
309 'software_config_transport': 'POLL_SERVER_HEAT'}
310 self._tosca_compute_test(