docs: fix issues
[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         self._tosca_compute_test(
68             tpl_snippet,
69             expectedprops)
70
71     def test_node_compute_without_os_capabilities(self):
72         tpl_snippet = '''
73         node_templates:
74           server:
75             type: tosca.nodes.Compute
76             capabilities:
77               host:
78                 properties:
79                   disk_size: 10 GB
80                   num_cpus: 4
81                   mem_size: 4 GB
82               #left intentionally
83         '''
84         expectedprops = {'flavor': 'm1.large',
85                          'image': None}
86         self._tosca_compute_test(
87             tpl_snippet,
88             expectedprops)
89
90     def test_node_compute_without_host_capabilities(self):
91         tpl_snippet = '''
92         node_templates:
93           server:
94             type: tosca.nodes.Compute
95             capabilities:
96               os:
97                 properties:
98                   architecture: x86_64
99                   type: Linux
100                   distribution: Fedora
101                   version: 18.0
102         '''
103         expectedprops = {'flavor': None,
104                          'image': 'fedora-amd64-heat-config'}
105         self._tosca_compute_test(
106             tpl_snippet,
107             expectedprops)
108
109     def test_node_compute_without_properties_and_os_capabilities(self):
110         tpl_snippet = '''
111         node_templates:
112           server:
113             type: tosca.nodes.Compute
114             properties:
115               #left intentionally
116             capabilities:
117               #left intentionally
118         '''
119         expectedprops = {'flavor': None,
120                          'image': None}
121         self._tosca_compute_test(
122             tpl_snippet,
123             expectedprops)
124
125     def test_node_compute_with_only_type(self):
126         tpl_snippet = '''
127         node_templates:
128           server:
129             type: tosca.nodes.Compute
130         '''
131         expectedprops = {'flavor': None,
132                          'image': None}
133         self._tosca_compute_test(
134             tpl_snippet,
135             expectedprops)
136
137     def test_node_compute_host_capabilities_without_properties(self):
138         tpl_snippet = '''
139         node_templates:
140           server:
141             type: tosca.nodes.Compute
142             capabilities:
143               host:
144                 properties:
145                 #left intentionally
146         '''
147         expectedprops = {'flavor': 'm1.nano'}
148         self._tosca_compute_test(
149             tpl_snippet,
150             expectedprops)
151
152     def test_node_compute_host_capabilities_without_disk_size(self):
153         tpl_snippet = '''
154         node_templates:
155           server:
156             type: tosca.nodes.Compute
157             capabilities:
158               host:
159                 properties:
160                   num_cpus: 4
161                   mem_size: 4 GB
162         '''
163         expectedprops = {'flavor': 'm1.large'}
164         self._tosca_compute_test(
165             tpl_snippet,
166             expectedprops)
167
168     def test_node_compute_host_capabilities_without_mem_size(self):
169         tpl_snippet = '''
170         node_templates:
171           server:
172             type: tosca.nodes.Compute
173             capabilities:
174               host:
175                 properties:
176                   num_cpus: 4
177                   disk_size: 10 GB
178         '''
179         expectedprops = {'flavor': 'm1.large'}
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         self._tosca_compute_test(
196             tpl_snippet,
197             expectedprops)
198
199     @patch('requests.post')
200     @patch('requests.get')
201     @patch('os.getenv')
202     def test_node_compute_with_nova_flavor(self, mock_os_getenv,
203                                            mock_get, mock_post):
204         tpl_snippet = '''
205         node_templates:
206           server:
207             type: tosca.nodes.Compute
208             capabilities:
209               host:
210                 properties:
211                   num_cpus: 1
212                   disk_size: 1 GB
213                   mem_size: 1 GB
214         '''
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/',
220                                           'demo', 'demo',
221                                           'demo', 'http://abc.com/5000/']
222             mock_ks_response = mock.MagicMock()
223             mock_ks_response.status_code = 200
224             mock_ks_content = {
225                 'access': {
226                     'token': {
227                         'id': 'd1dfa603-3662-47e0-b0b6-3ae7914bdf76'
228                     },
229                     'serviceCatalog': [{
230                         'type': 'compute',
231                         'endpoints': [{
232                             'publicURL': 'http://abc.com'
233                         }]
234                     }]
235                 }
236             }
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 = {
241                 'flavors': [{
242                     'name': 'm1.mock_flavor',
243                     'ram': 1024,
244                     'disk': 1,
245                     'vcpus': 1
246                 }]
247             }
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(
254                 tpl_snippet,
255                 expectedprops)
256
257     @patch('requests.post')
258     @patch('requests.get')
259     @patch('os.getenv')
260     def test_node_compute_without_nova_flavor(self, mock_os_getenv,
261                                               mock_get, mock_post):
262         tpl_snippet = '''
263         node_templates:
264           server:
265             type: tosca.nodes.Compute
266             capabilities:
267               host:
268                 properties:
269                   num_cpus: 1
270                   disk_size: 1 GB
271                   mem_size: 1 GB
272         '''
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()
279             mock_ks_content = {}
280             mock_ks_response.content = json.dumps(mock_ks_content)
281             expectedprops = {'flavor': 'm1.small',
282                              'user_data_format': 'SOFTWARE_CONFIG',
283                              'image': None}
284             self._tosca_compute_test(
285                 tpl_snippet,
286                 expectedprops)