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 self._tosca_compute_test(
71 def test_node_compute_without_os_capabilities(self):
75 type: tosca.nodes.Compute
84 expectedprops = {'flavor': 'm1.large',
86 self._tosca_compute_test(
90 def test_node_compute_without_host_capabilities(self):
94 type: tosca.nodes.Compute
103 expectedprops = {'flavor': None,
104 'image': 'fedora-amd64-heat-config'}
105 self._tosca_compute_test(
109 def test_node_compute_without_properties_and_os_capabilities(self):
113 type: tosca.nodes.Compute
119 expectedprops = {'flavor': None,
121 self._tosca_compute_test(
125 def test_node_compute_with_only_type(self):
129 type: tosca.nodes.Compute
131 expectedprops = {'flavor': None,
133 self._tosca_compute_test(
137 def test_node_compute_host_capabilities_without_properties(self):
141 type: tosca.nodes.Compute
147 expectedprops = {'flavor': 'm1.nano'}
148 self._tosca_compute_test(
152 def test_node_compute_host_capabilities_without_disk_size(self):
156 type: tosca.nodes.Compute
163 expectedprops = {'flavor': 'm1.large'}
164 self._tosca_compute_test(
168 def test_node_compute_host_capabilities_without_mem_size(self):
172 type: tosca.nodes.Compute
179 expectedprops = {'flavor': 'm1.large'}
180 self._tosca_compute_test(
184 def test_node_compute_host_capabilities_without_mem_size_disk_size(self):
188 type: tosca.nodes.Compute
194 expectedprops = {'flavor': 'm1.large'}
195 self._tosca_compute_test(
199 @patch('requests.post')
200 @patch('requests.get')
202 def test_node_compute_with_nova_flavor(self, mock_os_getenv,
203 mock_get, mock_post):
207 type: tosca.nodes.Compute
215 with patch('translator.common.utils.'
216 'check_for_env_variables') as mock_check_env:
217 mock_check_env.return_value = True
218 mock_os_getenv.side_effect = ['demo', 'demo',
219 'demo', 'http://abc.com/5000/',
221 'demo', 'http://abc.com/5000/']
222 mock_ks_response = mock.MagicMock()
223 mock_ks_response.status_code = 200
227 'id': 'd1dfa603-3662-47e0-b0b6-3ae7914bdf76'
232 'publicURL': 'http://abc.com'
237 mock_ks_response.content = json.dumps(mock_ks_content)
238 mock_nova_response = mock.MagicMock()
239 mock_nova_response.status_code = 200
240 mock_flavor_content = {
242 'name': 'm1.mock_flavor',
248 mock_nova_response.content = \
249 json.dumps(mock_flavor_content)
250 mock_post.return_value = mock_ks_response
251 mock_get.return_value = mock_nova_response
252 expectedprops = {'flavor': 'm1.mock_flavor'}
253 self._tosca_compute_test(
257 @patch('requests.post')
258 @patch('requests.get')
260 def test_node_compute_without_nova_flavor(self, mock_os_getenv,
261 mock_get, mock_post):
265 type: tosca.nodes.Compute
273 with patch('translator.common.utils.'
274 'check_for_env_variables') as mock_check_env:
275 mock_check_env.return_value = True
276 mock_os_getenv.side_effect = ['demo', 'demo',
277 'demo', 'http://abc.com/5000/']
278 mock_ks_response = mock.MagicMock()
280 mock_ks_response.content = json.dumps(mock_ks_content)
281 expectedprops = {'flavor': 'm1.small',
282 'user_data_format': 'SOFTWARE_CONFIG',
284 self._tosca_compute_test(