Synchronise the openstack bugs
[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 import toscaparser.utils.yamlparser
20 from translator.hot.tosca.tosca_compute import ToscaCompute
21
22
23 class ToscaComputeTest(TestCase):
24
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()
33
34         self.assertDictEqual(expectedprops, toscacompute.properties)
35
36     def test_node_compute_with_host_and_os_capabilities(self):
37         tpl_snippet = '''
38         node_templates:
39           server:
40             type: tosca.nodes.Compute
41             capabilities:
42               host:
43                 properties:
44                   disk_size: 10 GB
45                   num_cpus: 4
46                   mem_size: 4 GB
47               os:
48                 properties:
49                   architecture: x86_64
50                   type: Linux
51                   distribution: Fedora
52                   version: 18.0
53         '''
54         expectedprops = {'flavor': 'm1.large',
55                          'image': 'fedora-amd64-heat-config',
56                          'user_data_format': 'SOFTWARE_CONFIG'}
57         self._tosca_compute_test(
58             tpl_snippet,
59             expectedprops)
60
61     def test_node_compute_without_os_capabilities(self):
62         tpl_snippet = '''
63         node_templates:
64           server:
65             type: tosca.nodes.Compute
66             capabilities:
67               host:
68                 properties:
69                   disk_size: 10 GB
70                   num_cpus: 4
71                   mem_size: 4 GB
72               #left intentionally
73         '''
74         expectedprops = {'flavor': 'm1.large',
75                          'image': None,
76                          'user_data_format': 'SOFTWARE_CONFIG'}
77         self._tosca_compute_test(
78             tpl_snippet,
79             expectedprops)
80
81     def test_node_compute_without_host_capabilities(self):
82         tpl_snippet = '''
83         node_templates:
84           server:
85             type: tosca.nodes.Compute
86             capabilities:
87               os:
88                 properties:
89                   architecture: x86_64
90                   type: Linux
91                   distribution: Fedora
92                   version: 18.0
93         '''
94         expectedprops = {'flavor': None,
95                          'image': 'fedora-amd64-heat-config',
96                          'user_data_format': 'SOFTWARE_CONFIG'}
97         self._tosca_compute_test(
98             tpl_snippet,
99             expectedprops)
100
101     def test_node_compute_without_properties_and_os_capabilities(self):
102         tpl_snippet = '''
103         node_templates:
104           server:
105             type: tosca.nodes.Compute
106             properties:
107               #left intentionally
108             capabilities:
109               #left intentionally
110         '''
111         expectedprops = {'flavor': None,
112                          'image': None,
113                          'user_data_format': 'SOFTWARE_CONFIG'}
114         self._tosca_compute_test(
115             tpl_snippet,
116             expectedprops)
117
118     def test_node_compute_with_only_type(self):
119         tpl_snippet = '''
120         node_templates:
121           server:
122             type: tosca.nodes.Compute
123         '''
124         expectedprops = {'flavor': None,
125                          'image': None,
126                          'user_data_format': 'SOFTWARE_CONFIG'}
127         self._tosca_compute_test(
128             tpl_snippet,
129             expectedprops)
130
131     def test_node_compute_host_capabilities_without_properties(self):
132         tpl_snippet = '''
133         node_templates:
134           server:
135             type: tosca.nodes.Compute
136             capabilities:
137               host:
138                 properties:
139                 #left intentionally
140         '''
141         expectedprops = {'flavor': None,
142                          'image': None,
143                          'user_data_format': 'SOFTWARE_CONFIG'}
144         self._tosca_compute_test(
145             tpl_snippet,
146             expectedprops)
147
148     def test_node_compute_host_capabilities_without_disk_size(self):
149         tpl_snippet = '''
150         node_templates:
151           server:
152             type: tosca.nodes.Compute
153             capabilities:
154               host:
155                 properties:
156                   num_cpus: 4
157                   mem_size: 4 GB
158         '''
159         expectedprops = {'flavor': 'm1.large',
160                          'image': None,
161                          'user_data_format': 'SOFTWARE_CONFIG'}
162         self._tosca_compute_test(
163             tpl_snippet,
164             expectedprops)
165
166     def test_node_compute_host_capabilities_without_mem_size(self):
167         tpl_snippet = '''
168         node_templates:
169           server:
170             type: tosca.nodes.Compute
171             capabilities:
172               host:
173                 properties:
174                   num_cpus: 4
175                   disk_size: 10 GB
176         '''
177         expectedprops = {'flavor': 'm1.large',
178                          'image': None,
179                          'user_data_format': 'SOFTWARE_CONFIG'}
180         self._tosca_compute_test(
181             tpl_snippet,
182             expectedprops)
183
184     def test_node_compute_host_capabilities_without_mem_size_disk_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         '''
194         expectedprops = {'flavor': 'm1.large',
195                          'image': None,
196                          'user_data_format': 'SOFTWARE_CONFIG'}
197         self._tosca_compute_test(
198             tpl_snippet,
199             expectedprops)
200
201     @patch('requests.post')
202     @patch('requests.get')
203     @patch('os.getenv')
204     def test_node_compute_with_nova_flavor(self, mock_os_getenv,
205                                            mock_get, mock_post):
206         tpl_snippet = '''
207         node_templates:
208           server:
209             type: tosca.nodes.Compute
210             capabilities:
211               host:
212                 properties:
213                   num_cpus: 1
214                   disk_size: 1 GB
215                   mem_size: 1 GB
216         '''
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/',
222                                           'demo', 'demo',
223                                           'demo', 'http://abc.com/5000/']
224             mock_ks_response = mock.MagicMock()
225             mock_ks_response.status_code = 200
226             mock_ks_content = {
227                 'access': {
228                     'token': {
229                         'id': 'd1dfa603-3662-47e0-b0b6-3ae7914bdf76'
230                     },
231                     'serviceCatalog': [{
232                         'type': 'compute',
233                         'endpoints': [{
234                             'publicURL': 'http://abc.com'
235                         }]
236                     }]
237                 }
238             }
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 = {
243                 'flavors': [{
244                     'name': 'm1.mock_flavor',
245                     'ram': 1024,
246                     'disk': 1,
247                     'vcpus': 1
248                 }]
249             }
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',
255                              'image': None,
256                              'user_data_format': 'SOFTWARE_CONFIG'}
257             self._tosca_compute_test(
258                 tpl_snippet,
259                 expectedprops)
260
261     @patch('requests.post')
262     @patch('requests.get')
263     @patch('os.getenv')
264     def test_node_compute_without_nova_flavor(self, mock_os_getenv,
265                                               mock_get, mock_post):
266         tpl_snippet = '''
267         node_templates:
268           server:
269             type: tosca.nodes.Compute
270             capabilities:
271               host:
272                 properties:
273                   num_cpus: 1
274                   disk_size: 1 GB
275                   mem_size: 1 GB
276         '''
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()
283             mock_ks_content = {}
284             mock_ks_response.content = json.dumps(mock_ks_content)
285             expectedprops = {'flavor': 'm1.small',
286                              'image': None,
287                              'user_data_format': 'SOFTWARE_CONFIG'}
288             self._tosca_compute_test(
289                 tpl_snippet,
290                 expectedprops)