7a89827169e0f85fbbca0cf149c28c7f8e92c9c3
[parser.git] / tosca2heat / heat-translator / translator / tests / test_tosca_hot_translation.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 os
15
16 from toscaparser.common.exception import ExceptionCollector
17 from toscaparser.common.exception import URLException
18 from toscaparser.common.exception import ValidationError
19 from toscaparser.utils.gettextutils import _
20 from translator.common.utils import TranslationUtils
21 from translator.tests.base import TestCase
22
23
24 class ToscaHotTranslationTest(TestCase):
25
26     def _test_successful_translation(self, tosca_file, hot_file, params=None):
27         if not params:
28             params = {}
29         diff = TranslationUtils.compare_tosca_translation_with_hot(tosca_file,
30                                                                    hot_file,
31                                                                    params)
32         self.assertEqual({}, diff, '<difference> : ' +
33                          json.dumps(diff, indent=4, separators=(', ', ': ')))
34
35     def _test_failed_translation(self, tosca_file, hot_file, params, msg,
36                                  msg_path, error_raise, error_collect):
37         if msg_path:
38             path = os.path.normpath(os.path.join(
39                 os.path.dirname(os.path.realpath(__file__)), tosca_file))
40             msg = msg % path
41         self.assertRaises(
42             error_raise,
43             TranslationUtils.compare_tosca_translation_with_hot,
44             tosca_file, hot_file, params)
45         ExceptionCollector.assertExceptionMessage(error_collect, msg)
46
47     def test_hot_translate_single_server(self):
48         tosca_file = '../tests/data/tosca_single_server.yaml'
49         hot_file = '../tests/data/hot_output/hot_single_server.yaml'
50         params = {'cpus': 1}
51         self._test_successful_translation(tosca_file, hot_file, params)
52
53     def test_hot_translate_single_server_with_defaults(self):
54         tosca_file = \
55             '../tests/data/tosca_single_server_with_defaults.yaml'
56
57         hot_file_with_input = '../tests/data/hot_output/' \
58             'hot_single_server_with_defaults_with_input.yaml'
59         params1 = {'cpus': '1'}
60         self._test_successful_translation(tosca_file, hot_file_with_input,
61                                           params1)
62
63         hot_file_without_input = '../tests/data/hot_output/' \
64             'hot_single_server_with_defaults_without_input.yaml'
65         params2 = {}
66         self._test_successful_translation(tosca_file, hot_file_without_input,
67                                           params2)
68
69     def test_hot_translate_wordpress_single_instance(self):
70         tosca_file = '../tests/data/tosca_single_instance_wordpress.yaml'
71         hot_file = '../tests/data/hot_output/' \
72             'hot_single_instance_wordpress.yaml'
73         params = {'db_name': 'wordpress',
74                   'db_user': 'wp_user',
75                   'db_pwd': 'wp_pass',
76                   'db_root_pwd': 'passw0rd',
77                   'db_port': 3366,
78                   'cpus': 8}
79         self._test_successful_translation(tosca_file, hot_file, params)
80
81     def test_hot_translate_helloworld(self):
82         tosca_file = '../tests/data/tosca_helloworld.yaml'
83         hot_file = '../tests/data/hot_output/hot_hello_world.yaml'
84         self._test_successful_translation(tosca_file, hot_file)
85
86     def test_hot_translate_host_assignment(self):
87         tosca_file = '../tests/data/test_host_assignment.yaml'
88         hot_file = '../tests/data/hot_output/hot_host_assignment.yaml'
89         self._test_successful_translation(tosca_file, hot_file)
90
91     def test_hot_translate_elk(self):
92         tosca_file = '../tests/data/tosca_elk.yaml'
93         hot_file = '../tests/data/hot_output/hot_elk.yaml'
94         params = {'github_url':
95                   'http://github.com/paypal/rest-api-sample-app-nodejs.git',
96                   'my_cpus': 4}
97         self._test_successful_translation(tosca_file, hot_file, params)
98
99     def test_hot_translate_nodejs_mongodb_two_instances(self):
100         tosca_file = '../tests/data/tosca_nodejs_mongodb_two_instances.yaml'
101         hot_file = '../tests/data/hot_output/' \
102                    'hot_nodejs_mongodb_two_instances.yaml'
103         params = {'github_url':
104                   'http://github.com/paypal/rest-api-sample-app-nodejs.git',
105                   'my_cpus': 4}
106         self._test_successful_translation(tosca_file, hot_file, params)
107
108     def test_hot_translate_blockstorage_with_attachment(self):
109         tosca_file = '../tests/data/storage/' \
110                      'tosca_blockstorage_with_attachment.yaml'
111         hot_file = '../tests/data/hot_output/storage/' \
112                    'hot_blockstorage_with_attachment.yaml'
113         params = {'cpus': 1,
114                   'storage_location': '/dev/vdc',
115                   'storage_size': '2000 MB',
116                   'storage_snapshot_id': 'ssid'}
117         self._test_successful_translation(tosca_file, hot_file, params)
118
119     def test_hot_translate_blockstorage_with_custom_relationship_type(self):
120         tosca_file = '../tests/data/storage/' \
121                      'tosca_blockstorage_with_custom_relationship_type.yaml'
122         hot_file = '../tests/data/hot_output/storage/' \
123                    'hot_blockstorage_with_custom_relationship_type.yaml'
124         params = {'cpus': 1,
125                   'storage_location': '/dev/vdc',
126                   'storage_size': '1 GB',
127                   'storage_snapshot_id': 'ssid'}
128         self._test_successful_translation(tosca_file, hot_file, params)
129
130     def test_hot_translate_blockstorage_with_relationship_template(self):
131         tosca_file = '../tests/data/storage/' \
132                      'tosca_blockstorage_with_relationship_template.yaml'
133         hot_file = '../tests/data/hot_output/storage/' \
134                    'hot_blockstorage_with_relationship_template.yaml'
135         params = {'cpus': 1,
136                   'storage_location': '/dev/vdc',
137                   'storage_size': '1 GB'}
138         self._test_successful_translation(tosca_file, hot_file, params)
139
140     def test_hot_translate_blockstorage_with_attachment_notation1(self):
141         tosca_file = '../tests/data/storage/' \
142                      'tosca_blockstorage_with_attachment_notation1.yaml'
143         hot_file1 = '../tests/data/hot_output/storage/' \
144                     'hot_blockstorage_with_attachment_notation1_alt1.yaml'
145         hot_file2 = '../tests/data/hot_output/storage/' \
146                     'hot_blockstorage_with_attachment_notation1_alt2.yaml'
147         params = {'cpus': 1,
148                   'storage_location': 'some_folder',
149                   'storage_size': '1 GB',
150                   'storage_snapshot_id': 'ssid'}
151
152         try:
153             self._test_successful_translation(tosca_file, hot_file1, params)
154         except Exception:
155             self._test_successful_translation(tosca_file, hot_file2, params)
156
157     def test_hot_translate_blockstorage_with_attachment_notation2(self):
158         tosca_file = '../tests/data/storage/' \
159                      'tosca_blockstorage_with_attachment_notation2.yaml'
160         hot_file1 = '../tests/data/hot_output/storage/' \
161                     'hot_blockstorage_with_attachment_notation2_alt1.yaml'
162         hot_file2 = '../tests/data/hot_output/storage/' \
163                     'hot_blockstorage_with_attachment_notation2_alt2.yaml'
164         params = {'cpus': 1,
165                   'storage_location': '/dev/vdc',
166                   'storage_size': '1 GB',
167                   'storage_snapshot_id': 'ssid'}
168         try:
169             self._test_successful_translation(tosca_file, hot_file1, params)
170         except Exception:
171             self._test_successful_translation(tosca_file, hot_file2, params)
172
173     def test_hot_translate_multiple_blockstorage_with_attachment(self):
174         tosca_file = '../tests/data/storage/' \
175                      'tosca_multiple_blockstorage_with_attachment.yaml'
176         hot_file1 = '../tests/data/hot_output/storage/' \
177                     'hot_multiple_blockstorage_with_attachment_alt1.yaml'
178         hot_file2 = '../tests/data/hot_output/storage/' \
179                     'hot_multiple_blockstorage_with_attachment_alt2.yaml'
180         params = {'cpus': 1,
181                   'storage_location': '/dev/vdc',
182                   'storage_size': '1 GB',
183                   'storage_snapshot_id': 'ssid'}
184         try:
185             self._test_successful_translation(tosca_file, hot_file1, params)
186         except Exception:
187             self._test_successful_translation(tosca_file, hot_file2, params)
188
189     def test_hot_translate_single_object_store(self):
190         tosca_file = '../tests/data/storage/tosca_single_object_store.yaml'
191         hot_file = '../tests/data/hot_output/hot_single_object_store.yaml'
192         params = {'objectstore_name': 'myobjstore'}
193         self._test_successful_translation(tosca_file, hot_file, params)
194
195     def test_hot_translate_one_server_one_network(self):
196         tosca_file = '../tests/data/network/tosca_one_server_one_network.yaml'
197         hot_file = '../tests/data/hot_output/network/' \
198                    'hot_one_server_one_network.yaml'
199         params = {'network_name': 'private_net'}
200         self._test_successful_translation(tosca_file, hot_file, params)
201
202     def test_hot_translate_server_on_existing_network(self):
203         tosca_file = '../tests/data/network/' \
204                      'tosca_server_on_existing_network.yaml'
205         hot_file = '../tests/data/hot_output/network/' \
206                    'hot_server_on_existing_network.yaml'
207         params = {'network_name': 'private_net'}
208         self._test_successful_translation(tosca_file, hot_file, params)
209
210     def test_hot_translate_two_servers_one_network(self):
211         tosca_file = '../tests/data/network/tosca_two_servers_one_network.yaml'
212         hot_file = '../tests/data/hot_output/network/' \
213                    'hot_two_servers_one_network.yaml'
214         params = {'network_name': 'my_private_net',
215                   'network_cidr': '10.0.0.0/24',
216                   'network_start_ip': '10.0.0.100',
217                   'network_end_ip': '10.0.0.150'}
218         self._test_successful_translation(tosca_file, hot_file, params)
219
220     def test_hot_translate_one_server_three_networks(self):
221         tosca_file = '../tests/data/network/' \
222                      'tosca_one_server_three_networks.yaml'
223         hot_file = '../tests/data/hot_output/network/' \
224                    'hot_one_server_three_networks.yaml'
225         params = {}
226         self._test_successful_translation(tosca_file, hot_file, params)
227
228     def test_hot_translate_software_component(self):
229         tosca_file = '../tests/data/tosca_software_component.yaml'
230         hot_file = '../tests/data/hot_output/hot_software_component.yaml'
231         params = {'cpus': '1',
232                   'download_url': 'http://www.software.com/download'}
233         self._test_successful_translation(tosca_file, hot_file, params)
234
235     def test_hot_translate_web_application(self):
236         tosca_file = '../tests/data/tosca_web_application.yaml'
237         hot_file = '../tests/data/hot_output/hot_web_application.yaml'
238         params = {'cpus': '2', 'context_root': 'my_web_app'}
239         self._test_successful_translation(tosca_file, hot_file, params)
240
241     def test_hot_translate_template_with_url_import(self):
242         tosca_file = '../tests/data/' \
243                      'tosca_single_instance_wordpress_with_url_import.yaml'
244         hot_file = '../tests/data/hot_output/' \
245                    'hot_single_instance_wordpress.yaml'
246         params = {'db_name': 'wordpress',
247                   'db_user': 'wp_user',
248                   'db_pwd': 'wp_pass',
249                   'db_root_pwd': 'passw0rd',
250                   'db_port': 3366,
251                   'cpus': 8}
252         self._test_successful_translation(tosca_file, hot_file, params)
253
254     def test_hot_translate_template_by_url_with_local_import(self):
255         tosca_file = 'https://raw.githubusercontent.com/openstack/' \
256                      'heat-translator/master/translator/tests/data/' \
257                      'tosca_single_instance_wordpress.yaml'
258         hot_file = '../tests/data/hot_output/' \
259                    'hot_single_instance_wordpress.yaml'
260         params = {'db_name': 'wordpress',
261                   'db_user': 'wp_user',
262                   'db_pwd': 'wp_pass',
263                   'db_root_pwd': 'passw0rd',
264                   'db_port': 3366,
265                   'cpus': 8}
266         self._test_successful_translation(tosca_file, hot_file, params)
267
268     def test_hot_translate_template_by_url_with_local_abspath_import(self):
269         tosca_file = 'https://raw.githubusercontent.com/openstack/' \
270                      'heat-translator/master/translator/tests/data/' \
271                      'tosca_single_instance_wordpress_with_local_abspath' \
272                      '_import.yaml'
273         hot_file = '../tests/data/hot_output/' \
274                    'hot_single_instance_wordpress.yaml'
275         params = {'db_name': 'wordpress',
276                   'db_user': 'wp_user',
277                   'db_pwd': 'wp_pass',
278                   'db_root_pwd': 'passw0rd',
279                   'db_port': 3366,
280                   'cpus': 8}
281         expected_msg = _('Absolute file name "/tmp/wordpress.yaml" cannot be '
282                          'used in a URL-based input template "https://raw.'
283                          'githubusercontent.com/openstack/heat-translator/'
284                          'master/translator/tests/data/tosca_single_instance_'
285                          'wordpress_with_local_abspath_import.yaml".')
286         msg_path = False
287         self._test_failed_translation(tosca_file, hot_file, params,
288                                       expected_msg, msg_path, ValidationError,
289                                       ImportError)
290
291     def test_hot_translate_template_by_url_with_url_import(self):
292         tosca_url = 'https://raw.githubusercontent.com/openstack/' \
293                     'heat-translator/master/translator/tests/data/' \
294                     'tosca_single_instance_wordpress_with_url_import.yaml'
295         hot_file = '../tests/data/hot_output/' \
296                    'hot_single_instance_wordpress.yaml'
297         params = {'db_name': 'wordpress',
298                   'db_user': 'wp_user',
299                   'db_pwd': 'wp_pass',
300                   'db_root_pwd': 'passw0rd',
301                   'db_port': 3366,
302                   'cpus': 8}
303         self._test_successful_translation(tosca_url, hot_file, params)
304
305     def test_translate_hello_world_csar(self):
306         tosca_file = '../tests/data/csar_hello_world.zip'
307         hot_file = '../tests/data/hot_output/hot_hello_world.yaml'
308         self._test_successful_translation(tosca_file, hot_file)
309
310     def test_translate_single_instance_wordpress_csar(self):
311         tosca_file = '../tests/data/csar_single_instance_wordpress.zip'
312         hot_file = '../tests/data/hot_output/' \
313                    'hot_single_instance_wordpress_from_csar.yaml'
314         params = {'db_name': 'wordpress',
315                   'db_user': 'wp_user',
316                   'db_pwd': 'wp_pass',
317                   'db_root_pwd': 'passw0rd',
318                   'db_port': 3366,
319                   'cpus': 8}
320         self._test_successful_translation(tosca_file, hot_file, params)
321
322     def test_translate_elk_csar_from_url(self):
323         tosca_file = 'https://github.com/openstack/heat-translator/raw/' \
324                      'master/translator/tests/data/csar_elk.zip'
325         hot_file = '../tests/data/hot_output/hot_elk_from_csar.yaml'
326         params = {'github_url':
327                   'http://github.com/paypal/rest-api-sample-app-nodejs.git',
328                   'my_cpus': 4}
329         self._test_successful_translation(tosca_file, hot_file, params)
330
331     def test_translate_csar_not_zip(self):
332         tosca_file = '../tests/data/csar_not_zip.zip'
333         hot_file = ''
334         params = {}
335         expected_msg = _('"%s" is not a valid zip file.')
336         msg_path = True
337         self._test_failed_translation(tosca_file, hot_file, params,
338                                       expected_msg, msg_path, ValidationError,
339                                       ValidationError)
340
341     def test_translate_csar_metadata_not_yaml(self):
342         tosca_file = '../tests/data/csar_metadata_not_yaml.zip'
343         hot_file = ''
344         params = {}
345         expected_msg = _('The file "TOSCA-Metadata/TOSCA.meta" in the CSAR '
346                          '"%s" does not contain valid YAML content.')
347         msg_path = True
348         self._test_failed_translation(tosca_file, hot_file, params,
349                                       expected_msg, msg_path, ValidationError,
350                                       ValidationError)
351
352     def test_translate_csar_wrong_metadata_file(self):
353         tosca_file = '../tests/data/csar_wrong_metadata_file.zip'
354         hot_file = ''
355         params = {}
356         expected_msg = _('"%s" is not a valid CSAR as it does not contain the '
357                          'required file "TOSCA.meta" in the folder '
358                          '"TOSCA-Metadata".')
359         msg_path = True
360         self._test_failed_translation(tosca_file, hot_file, params,
361                                       expected_msg, msg_path, ValidationError,
362                                       ValidationError)
363
364     def test_translate_csar_wordpress_invalid_import_path(self):
365         tosca_file = '../tests/data/csar_wordpress_invalid_import_path.zip'
366         hot_file = ''
367         params = {}
368         expected_msg = _('Import '
369                          '"Invalid_import_path/wordpress.yaml" is not valid.')
370         msg_path = False
371         self._test_failed_translation(tosca_file, hot_file, params,
372                                       expected_msg, msg_path, ValidationError,
373                                       ImportError)
374
375     def test_translate_csar_wordpress_invalid_script_url(self):
376         tosca_file = '../tests/data/csar_wordpress_invalid_script_url.zip'
377         hot_file = ''
378         params = {}
379         expected_msg = _('The resource at '
380                          '"https://raw.githubusercontent.com/openstack/'
381                          'heat-translator/master/translator/tests/data/'
382                          'custom_types/wordpress1.yaml" cannot be accessed.')
383         msg_path = False
384         self._test_failed_translation(tosca_file, hot_file, params,
385                                       expected_msg, msg_path, ValidationError,
386                                       URLException)
387
388     def test_hot_translate_flavor_image(self):
389         tosca_file = '../tests/data/test_tosca_flavor_and_image.yaml'
390         hot_file = '../tests/data/hot_output/hot_flavor_and_image.yaml'
391         self._test_successful_translation(tosca_file, hot_file)
392
393     def test_hot_translate_flavor_image_params(self):
394         tosca_file = '../tests/data/test_tosca_flavor_and_image.yaml'
395         hot_file = '../tests/data/hot_output/hot_flavor_and_image_params.yaml'
396         params = {'key_name': 'paramkey'}
397         self._test_successful_translation(tosca_file, hot_file, params)
398
399     def test_hot_translate_custom_type(self):
400         tosca_file = '../tests/data/test_tosca_custom_type.yaml'
401         hot_file = '../tests/data/hot_output/' \
402             'hot_custom_type.yaml'
403         params = {}
404         self._test_successful_translation(tosca_file, hot_file, params)
405
406     def test_hot_translate_custom_type_with_override(self):
407         tosca_file = '../tests/data/test_tosca_custom_type_with_override.yaml'
408         hot_file = '../tests/data/hot_output/' \
409             'hot_custom_type_with_override.yaml'
410         params = {}
411         self._test_successful_translation(tosca_file, hot_file, params)
412
413     def test_hot_translate_custom_type_with_param_override(self):
414         tosca_file = '../tests/data/test_tosca_custom_type_with_override.yaml'
415         hot_file = '../tests/data/hot_output/' \
416             'hot_custom_type_with_param_override.yaml'
417         params = {'install_path': '/home/custom/from/cli'}
418         self._test_successful_translation(tosca_file, hot_file, params)
419
420     def test_hot_translate_artifact(self):
421         tosca_file = '../tests/data/test_tosca_artifact.yaml'
422         hot_file = '../tests/data/hot_output/' \
423             'hot_artifact.yaml'
424         params = {}
425         self._test_successful_translation(tosca_file, hot_file, params)
426
427     def test_hot_translate_without_tosca_os_version(self):
428         tosca_file = '../tests/data/' \
429             'test_single_server_without_optional_version_prop.yaml'
430         hot_file = '../tests/data/hot_output/' \
431             'hot_single_server_without_tosca_os_version.yaml'
432         params = {}
433         self._test_successful_translation(tosca_file, hot_file, params)
434
435     def test_hot_translate_helloworld_with_userkey(self):
436         tosca_file = '../tests/data/tosca_helloworld.yaml'
437         hot_file = '../tests/data/hot_output/hot_hello_world_userkey.yaml'
438         params = {'key_name': 'userkey'}
439         self._test_successful_translation(tosca_file, hot_file, params)
440
441     def test_hot_translate_custom_networks_nodes_inline(self):
442         tosca_file = '../tests/data/network/' \
443                      'test_tosca_custom_network_nodes_inline.yaml'
444         hot_file = '../tests/data/hot_output/network/' \
445                    'hot_custom_network_nodes.yaml'
446         params = {}
447         self._test_successful_translation(tosca_file, hot_file, params)
448
449     def test_hot_translate_custom_networks_nodes_imports(self):
450         tosca_file = '../tests/data/network/' \
451                      'test_tosca_custom_network_nodes_imports.yaml'
452         hot_file = '../tests/data/hot_output/network/' \
453                    'hot_custom_network_nodes.yaml'
454         params = {}
455         self._test_successful_translation(tosca_file, hot_file, params)
456
457     def test_hot_translate_nfv_sample(self):
458         tosca_file = '../tests/data/test_tosca_nfv_sample.yaml'
459         hot_file = '../tests/data/hot_output/hot_nfv_sample.yaml'
460         params = {}
461         self._test_successful_translation(tosca_file, hot_file, params)
462
463     def test_hot_translate_policy(self):
464         tosca_file = '../tests/data/tosca_policies.yaml'
465         hot_file = '../tests/data/hot_output/hot_policies.yaml'
466         params = {}
467         self._test_successful_translation(tosca_file, hot_file, params)
468
469     def test_hot_script_types(self):
470         tosca_file = '../tests/data/test_tosca_script_types.yaml'
471         hot_file = '../tests/data/hot_output/hot_script_types.yaml'
472         params = {}
473         self._test_successful_translation(tosca_file, hot_file, params)
474
475     def test_hot_interface_on_compute(self):
476         tosca_file = '../tests/data/test_tosca_interface_on_compute.yaml'
477         hot_file = '../tests/data/hot_output/hot_interface_on_compute.yaml'
478         params = {}
479         self._test_successful_translation(tosca_file, hot_file, params)
480
481     def test_hot_get_functions_semantic(self):
482         tosca_file = '../tests/data/test_tosca_get_functions_semantic.yaml'
483         hot_file = '../tests/data/hot_output/hot_get_functions_semantic.yaml'
484         params = {}
485         self._test_successful_translation(tosca_file, hot_file, params)
486
487     def test_hot_exchange_public_ssh_key(self):
488         tosca_file = '../tests/data/tosca_exchange_public_ssh_key.yaml'
489         hot_file = '../tests/data/hot_output/hot_exchange_public_ssh_key.yaml'
490         params = {}
491         self._test_successful_translation(tosca_file, hot_file, params)