Support python3 uploaded to pypi websit
[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 mock
14
15 from toscaparser.nodetemplate import NodeTemplate
16 from toscaparser.tests.base import TestCase
17 import toscaparser.utils.yamlparser
18 from translator.hot.tosca.tosca_compute import ToscaCompute
19
20
21 class ToscaComputeTest(TestCase):
22
23     def _tosca_compute_test(self, tpl_snippet, expectedprops):
24         nodetemplates = (toscaparser.utils.yamlparser.
25                          simple_parse(tpl_snippet)['node_templates'])
26         name = list(nodetemplates.keys())[0]
27         nodetemplate = NodeTemplate(name, nodetemplates)
28         nodetemplate.validate()
29         toscacompute = ToscaCompute(nodetemplate)
30         toscacompute.handle_properties()
31
32         self.assertEqual(expectedprops, toscacompute.properties)
33
34     def test_node_compute_with_host_and_os_capabilities(self):
35         tpl_snippet = '''
36         node_templates:
37           server:
38             type: tosca.nodes.Compute
39             capabilities:
40               host:
41                 properties:
42                   disk_size: 10 GB
43                   num_cpus: 4
44                   mem_size: 4 GB
45               os:
46                 properties:
47                   architecture: x86_64
48                   type: Linux
49                   distribution: Fedora
50                   version: 18.0
51         '''
52         expectedprops = {'flavor': 'm1.large',
53                          'image': 'fedora-amd64-heat-config',
54                          'user_data_format': 'SOFTWARE_CONFIG',
55                          'software_config_transport': 'POLL_SERVER_HEAT'}
56         self._tosca_compute_test(
57             tpl_snippet,
58             expectedprops)
59
60     def test_node_compute_without_os_capabilities(self):
61         tpl_snippet = '''
62         node_templates:
63           server:
64             type: tosca.nodes.Compute
65             capabilities:
66               host:
67                 properties:
68                   disk_size: 10 GB
69                   num_cpus: 4
70                   mem_size: 4 GB
71               #left intentionally
72         '''
73         expectedprops = {'flavor': 'm1.large',
74                          'user_data_format': 'SOFTWARE_CONFIG',
75                          'software_config_transport': 'POLL_SERVER_HEAT'}
76         self._tosca_compute_test(
77             tpl_snippet,
78             expectedprops)
79
80     def test_node_compute_without_host_capabilities(self):
81         tpl_snippet = '''
82         node_templates:
83           server:
84             type: tosca.nodes.Compute
85             capabilities:
86               os:
87                 properties:
88                   architecture: x86_64
89                   type: Linux
90                   distribution: Fedora
91                   version: 18.0
92         '''
93         expectedprops = {'flavor': None,
94                          'image': 'fedora-amd64-heat-config',
95                          'user_data_format': 'SOFTWARE_CONFIG',
96                          'software_config_transport': 'POLL_SERVER_HEAT'}
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                          'user_data_format': 'SOFTWARE_CONFIG',
113                          'software_config_transport': 'POLL_SERVER_HEAT'}
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                          'user_data_format': 'SOFTWARE_CONFIG',
126                          'software_config_transport': 'POLL_SERVER_HEAT'}
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                          'user_data_format': 'SOFTWARE_CONFIG',
143                          'software_config_transport': 'POLL_SERVER_HEAT'}
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                          'user_data_format': 'SOFTWARE_CONFIG',
161                          'software_config_transport': 'POLL_SERVER_HEAT'}
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                          '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_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                          'user_data_format': 'SOFTWARE_CONFIG',
196                          'software_config_transport': 'POLL_SERVER_HEAT'}
197         self._tosca_compute_test(
198             tpl_snippet,
199             expectedprops)
200
201     @mock.patch('translator.common.flavors.get_flavors')
202     def test_node_compute_with_nova_flavor(self, mock_flavor):
203         tpl_snippet = '''
204         node_templates:
205           server:
206             type: tosca.nodes.Compute
207             capabilities:
208               host:
209                 properties:
210                   num_cpus: 1
211                   disk_size: 1 GB
212                   mem_size: 1 GB
213         '''
214         mock_flavor.return_value = {
215             'm1.mock_flavor': {
216                 'mem_size': 1024,
217                 'disk_size': 1,
218                 'num_cpus': 1}
219         }
220         expectedprops = {'flavor': 'm1.mock_flavor',
221                          'user_data_format': 'SOFTWARE_CONFIG',
222                          'software_config_transport': 'POLL_SERVER_HEAT'}
223         self._tosca_compute_test(tpl_snippet, expectedprops)
224
225     @mock.patch('translator.common.images.get_images')
226     def test_node_compute_with_glance_image(self, mock_images):
227         tpl_snippet = '''
228         node_templates:
229           server:
230             type: tosca.nodes.Compute
231             capabilities:
232               host:
233                 properties:
234                   num_cpus: 1
235                   disk_size: 1 GB
236                   mem_size: 1 GB
237               os:
238                 properties:
239                   architecture: x86_64
240                   type: Linux
241                   distribution: Fake Distribution
242                   version: 19.0
243         '''
244         mock_images.return_value = {
245             'fake-image-foobar': {'architecture': 'x86_64',
246                                   'type': 'Linux',
247                                   'distribution': 'Fake Distribution',
248                                   'version': '19.0'},
249             'fake-image-foobar-old': {'architecture': 'x86_64',
250                                       'type': 'Linux',
251                                       'distribution': 'Fake Distribution',
252                                       'version': '18.0'}
253         }
254         expectedprops = {'flavor': 'm1.small',
255                          'image': 'fake-image-foobar',
256                          'user_data_format': 'SOFTWARE_CONFIG',
257                          'software_config_transport': 'POLL_SERVER_HEAT'}
258         self._tosca_compute_test(tpl_snippet, expectedprops)