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 import toscaparser.utils.yamlparser
20 from translator.hot.tosca.tosca_compute import ToscaCompute
23 class ToscaComputeTest(TestCase):
25 def _tosca_compute_test(self, tpl_snippet, expectedprops):
26 nodetemplates = (toscaparser.utils.yamlparser.
27 simple_parse(tpl_snippet)['node_templates'])
28 name = list(nodetemplates.keys())[0]
29 nodetemplate = NodeTemplate(name, nodetemplates)
30 nodetemplate.validate()
31 toscacompute = ToscaCompute(nodetemplate)
32 toscacompute.handle_properties()
34 self.assertDictEqual(expectedprops, toscacompute.properties)
36 def test_node_compute_with_host_and_os_capabilities(self):
40 type: tosca.nodes.Compute
54 expectedprops = {'flavor': 'm1.large',
55 'image': 'fedora-amd64-heat-config',
56 'user_data_format': 'SOFTWARE_CONFIG'}
57 self._tosca_compute_test(
61 def test_node_compute_without_os_capabilities(self):
65 type: tosca.nodes.Compute
74 expectedprops = {'flavor': 'm1.large',
76 'user_data_format': 'SOFTWARE_CONFIG'}
77 self._tosca_compute_test(
81 def test_node_compute_without_host_capabilities(self):
85 type: tosca.nodes.Compute
94 expectedprops = {'flavor': None,
95 'image': 'fedora-amd64-heat-config',
96 'user_data_format': 'SOFTWARE_CONFIG'}
97 self._tosca_compute_test(
101 def test_node_compute_without_properties_and_os_capabilities(self):
105 type: tosca.nodes.Compute
111 expectedprops = {'flavor': None,
113 'user_data_format': 'SOFTWARE_CONFIG'}
114 self._tosca_compute_test(
118 def test_node_compute_with_only_type(self):
122 type: tosca.nodes.Compute
124 expectedprops = {'flavor': None,
126 'user_data_format': 'SOFTWARE_CONFIG'}
127 self._tosca_compute_test(
131 def test_node_compute_host_capabilities_without_properties(self):
135 type: tosca.nodes.Compute
141 expectedprops = {'flavor': None,
143 'user_data_format': 'SOFTWARE_CONFIG'}
144 self._tosca_compute_test(
148 def test_node_compute_host_capabilities_without_disk_size(self):
152 type: tosca.nodes.Compute
159 expectedprops = {'flavor': 'm1.large',
161 'user_data_format': 'SOFTWARE_CONFIG'}
162 self._tosca_compute_test(
166 def test_node_compute_host_capabilities_without_mem_size(self):
170 type: tosca.nodes.Compute
177 expectedprops = {'flavor': 'm1.large',
179 'user_data_format': 'SOFTWARE_CONFIG'}
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',
196 'user_data_format': 'SOFTWARE_CONFIG'}
197 self._tosca_compute_test(
201 @patch('requests.post')
202 @patch('requests.get')
204 def test_node_compute_with_nova_flavor(self, mock_os_getenv,
205 mock_get, mock_post):
209 type: tosca.nodes.Compute
217 with patch('translator.common.utils.'
218 'check_for_env_variables') as mock_check_env:
219 mock_check_env.return_value = True
220 mock_os_getenv.side_effect = ['demo', 'demo',
221 'demo', 'http://abc.com/5000/',
223 'demo', 'http://abc.com/5000/']
224 mock_ks_response = mock.MagicMock()
225 mock_ks_response.status_code = 200
229 'id': 'd1dfa603-3662-47e0-b0b6-3ae7914bdf76'
234 'publicURL': 'http://abc.com'
239 mock_ks_response.content = json.dumps(mock_ks_content)
240 mock_nova_response = mock.MagicMock()
241 mock_nova_response.status_code = 200
242 mock_flavor_content = {
244 'name': 'm1.mock_flavor',
250 mock_nova_response.content = \
251 json.dumps(mock_flavor_content)
252 mock_post.return_value = mock_ks_response
253 mock_get.return_value = mock_nova_response
254 expectedprops = {'flavor': 'm1.mock_flavor',
256 'user_data_format': 'SOFTWARE_CONFIG'}
257 self._tosca_compute_test(
261 @patch('requests.post')
262 @patch('requests.get')
264 def test_node_compute_without_nova_flavor(self, mock_os_getenv,
265 mock_get, mock_post):
269 type: tosca.nodes.Compute
277 with patch('translator.common.utils.'
278 'check_for_env_variables') as mock_check_env:
279 mock_check_env.return_value = True
280 mock_os_getenv.side_effect = ['demo', 'demo',
281 'demo', 'http://abc.com/5000/']
282 mock_ks_response = mock.MagicMock()
284 mock_ks_response.content = json.dumps(mock_ks_content)
285 expectedprops = {'flavor': 'm1.small',
287 'user_data_format': 'SOFTWARE_CONFIG'}
288 self._tosca_compute_test(