Merge "Provide file list about the difference between parser and upstreams."
[parser.git] / tosca2heat / heat-translator / translator / hot / tosca / tests / test_tosca_compute.py
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
4 #
5 #         http://www.apache.org/licenses/LICENSE-2.0
6 #
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
11 #    under the License.
12
13 import json
14 import mock
15 from mock import patch
16
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
22
23
24 class ToscaComputeTest(TestCase):
25
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]
30         try:
31             nodetemplate = NodeTemplate(name, nodetemplates)
32             nodetemplate.validate()
33             toscacompute = ToscaCompute(nodetemplate)
34             toscacompute.handle_properties()
35             if not self._compare_properties(toscacompute.properties,
36                                             expectedprops):
37                 raise Exception(_("Hot Properties are not"
38                                   " same as expected properties"))
39         except Exception:
40             # for time being rethrowing. Will be handled future based
41             # on new development in Glance and Graffiti
42             raise
43
44     def _compare_properties(self, hotprops, expectedprops):
45         return all(item in hotprops.items() for item in expectedprops.items())
46
47     def test_node_compute_with_host_and_os_capabilities(self):
48         tpl_snippet = '''
49         node_templates:
50           server:
51             type: tosca.nodes.Compute
52             capabilities:
53               host:
54                 properties:
55                   disk_size: 10 GB
56                   num_cpus: 4
57                   mem_size: 4 GB
58               os:
59                 properties:
60                   architecture: x86_64
61                   type: Linux
62                   distribution: Fedora
63                   version: 18.0
64         '''
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(
70             tpl_snippet,
71             expectedprops)
72
73     def test_node_compute_without_os_capabilities(self):
74         tpl_snippet = '''
75         node_templates:
76           server:
77             type: tosca.nodes.Compute
78             capabilities:
79               host:
80                 properties:
81                   disk_size: 10 GB
82                   num_cpus: 4
83                   mem_size: 4 GB
84               #left intentionally
85         '''
86         expectedprops = {'flavor': 'm1.large',
87                          'image': None,
88                          'user_data_format': 'SOFTWARE_CONFIG',
89                          'software_config_transport': 'POLL_SERVER_HEAT'}
90         self._tosca_compute_test(
91             tpl_snippet,
92             expectedprops)
93
94     def test_node_compute_without_host_capabilities(self):
95         tpl_snippet = '''
96         node_templates:
97           server:
98             type: tosca.nodes.Compute
99             capabilities:
100               os:
101                 properties:
102                   architecture: x86_64
103                   type: Linux
104                   distribution: Fedora
105                   version: 18.0
106         '''
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(
112             tpl_snippet,
113             expectedprops)
114
115     def test_node_compute_without_properties_and_os_capabilities(self):
116         tpl_snippet = '''
117         node_templates:
118           server:
119             type: tosca.nodes.Compute
120             properties:
121               #left intentionally
122             capabilities:
123               #left intentionally
124         '''
125         expectedprops = {'flavor': None,
126                          'image': None,
127                          'user_data_format': 'SOFTWARE_CONFIG',
128                          'software_config_transport': 'POLL_SERVER_HEAT'}
129         self._tosca_compute_test(
130             tpl_snippet,
131             expectedprops)
132
133     def test_node_compute_with_only_type(self):
134         tpl_snippet = '''
135         node_templates:
136           server:
137             type: tosca.nodes.Compute
138         '''
139         expectedprops = {'flavor': None,
140                          'image': None,
141                          'user_data_format': 'SOFTWARE_CONFIG',
142                          'software_config_transport': 'POLL_SERVER_HEAT'}
143         self._tosca_compute_test(
144             tpl_snippet,
145             expectedprops)
146
147     def test_node_compute_host_capabilities_without_properties(self):
148         tpl_snippet = '''
149         node_templates:
150           server:
151             type: tosca.nodes.Compute
152             capabilities:
153               host:
154                 properties:
155                 #left intentionally
156         '''
157         expectedprops = {'flavor': None,
158                          'image': None,
159                          'user_data_format': 'SOFTWARE_CONFIG',
160                          'software_config_transport': 'POLL_SERVER_HEAT'}
161         self._tosca_compute_test(
162             tpl_snippet,
163             expectedprops)
164
165     def test_node_compute_host_capabilities_without_disk_size(self):
166         tpl_snippet = '''
167         node_templates:
168           server:
169             type: tosca.nodes.Compute
170             capabilities:
171               host:
172                 properties:
173                   num_cpus: 4
174                   mem_size: 4 GB
175         '''
176         expectedprops = {'flavor': 'm1.large',
177                          'image': None,
178                          'user_data_format': 'SOFTWARE_CONFIG',
179                          'software_config_transport': 'POLL_SERVER_HEAT'}
180         self._tosca_compute_test(
181             tpl_snippet,
182             expectedprops)
183
184     def test_node_compute_host_capabilities_without_mem_size(self):
185         tpl_snippet = '''
186         node_templates:
187           server:
188             type: tosca.nodes.Compute
189             capabilities:
190               host:
191                 properties:
192                   num_cpus: 4
193                   disk_size: 10 GB
194         '''
195         expectedprops = {'flavor': 'm1.large',
196                          'image': None,
197                          'user_data_format': 'SOFTWARE_CONFIG',
198                          'software_config_transport': 'POLL_SERVER_HEAT'}
199         self._tosca_compute_test(
200             tpl_snippet,
201             expectedprops)
202
203     def test_node_compute_host_capabilities_without_mem_size_disk_size(self):
204         tpl_snippet = '''
205         node_templates:
206           server:
207             type: tosca.nodes.Compute
208             capabilities:
209               host:
210                 properties:
211                   num_cpus: 4
212         '''
213         expectedprops = {'flavor': 'm1.large',
214                          'image': None,
215                          'user_data_format': 'SOFTWARE_CONFIG',
216                          'software_config_transport': 'POLL_SERVER_HEAT'}
217         self._tosca_compute_test(
218             tpl_snippet,
219             expectedprops)
220
221     @patch('requests.post')
222     @patch('requests.get')
223     @patch('os.getenv')
224     def test_node_compute_with_nova_flavor(self, mock_os_getenv,
225                                            mock_get, mock_post):
226         tpl_snippet = '''
227         node_templates:
228           server:
229             type: tosca.nodes.Compute
230             capabilities:
231               host:
232                 properties:
233                   num_cpus: 1
234                   disk_size: 1 GB
235                   mem_size: 1 GB
236         '''
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/',
242                                           'demo', 'demo',
243                                           'demo', 'http://abc.com/5000/']
244             mock_ks_response = mock.MagicMock()
245             mock_ks_response.status_code = 200
246             mock_ks_content = {
247                 'access': {
248                     'token': {
249                         'id': 'd1dfa603-3662-47e0-b0b6-3ae7914bdf76'
250                     },
251                     'serviceCatalog': [{
252                         'type': 'compute',
253                         'endpoints': [{
254                             'publicURL': 'http://abc.com'
255                         }]
256                     }]
257                 }
258             }
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 = {
263                 'flavors': [{
264                     'name': 'm1.mock_flavor',
265                     'ram': 1024,
266                     'disk': 1,
267                     'vcpus': 1
268                 }]
269             }
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',
275                              'image': None,
276                              'user_data_format': 'SOFTWARE_CONFIG',
277                              'software_config_transport': 'POLL_SERVER_HEAT'}
278             self._tosca_compute_test(
279                 tpl_snippet,
280                 expectedprops)
281
282     @patch('requests.post')
283     @patch('requests.get')
284     @patch('os.getenv')
285     def test_node_compute_without_nova_flavor(self, mock_os_getenv,
286                                               mock_get, mock_post):
287         tpl_snippet = '''
288         node_templates:
289           server:
290             type: tosca.nodes.Compute
291             capabilities:
292               host:
293                 properties:
294                   num_cpus: 1
295                   disk_size: 1 GB
296                   mem_size: 1 GB
297         '''
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()
304             mock_ks_content = {}
305             mock_ks_response.content = json.dumps(mock_ks_content)
306             expectedprops = {'flavor': 'm1.small',
307                              'image': None,
308                              'user_data_format': 'SOFTWARE_CONFIG',
309                              'software_config_transport': 'POLL_SERVER_HEAT'}
310             self._tosca_compute_test(
311                 tpl_snippet,
312                 expectedprops)