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
5 # http://www.apache.org/licenses/LICENSE-2.0
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
16 from toscaparser.common.exception import ExceptionCollector
17 from toscaparser.common.exception import URLException
18 from toscaparser.common.exception import ValidationError
19 from toscaparser.tosca_template import ToscaTemplate
20 from toscaparser.utils.gettextutils import _
21 from translator.common.exception import UnsupportedTypeError
22 from translator.common.utils import TranslationUtils
23 from translator.hot.tosca_translator import TOSCATranslator
24 from translator.tests.base import TestCase
27 class ToscaHotTranslationTest(TestCase):
29 def _test_successful_translation(self, tosca_file, hot_files, params=None,
30 nested_resources=False):
33 if not isinstance(hot_files, list):
34 hot_files = [hot_files]
36 TranslationUtils.compare_tosca_translation_with_hot(
37 tosca_file, hot_files, params, nested_resources)
38 self.assertEqual({}, diff, '<difference> : ' +
39 json.dumps(diff, indent=4, separators=(', ', ': ')))
41 def _test_failed_translation(self, tosca_file, hot_file, params, msg,
42 msg_path, error_raise, error_collect):
44 path = os.path.normpath(os.path.join(
45 os.path.dirname(os.path.realpath(__file__)), tosca_file))
49 TranslationUtils.compare_tosca_translation_with_hot,
50 tosca_file, hot_file, params)
51 ExceptionCollector.assertExceptionMessage(error_collect, msg)
53 def test_hot_translate_single_server(self):
54 tosca_file = '../tests/data/tosca_single_server.yaml'
55 hot_file = '../tests/data/hot_output/hot_single_server.yaml'
57 self._test_successful_translation(tosca_file, hot_file, params)
59 def test_hot_translate_single_server_with_defaults(self):
61 '../tests/data/tosca_single_server_with_defaults.yaml'
63 hot_file_with_input = '../tests/data/hot_output/' \
64 'hot_single_server_with_defaults_with_input.yaml'
65 params1 = {'cpus': '1'}
66 self._test_successful_translation(tosca_file, hot_file_with_input,
69 hot_file_without_input = '../tests/data/hot_output/' \
70 'hot_single_server_with_defaults_without_input.yaml'
72 self._test_successful_translation(tosca_file, hot_file_without_input,
75 def test_hot_translate_wordpress_single_instance(self):
76 tosca_file = '../tests/data/tosca_single_instance_wordpress.yaml'
77 hot_file = '../tests/data/hot_output/' \
78 'hot_single_instance_wordpress.yaml'
79 params = {'db_name': 'wordpress',
82 'db_root_pwd': 'passw0rd',
85 self._test_successful_translation(tosca_file, hot_file, params)
87 def test_hot_translate_helloworld(self):
88 tosca_file = '../tests/data/tosca_helloworld.yaml'
89 hot_file = '../tests/data/hot_output/hot_hello_world.yaml'
90 self._test_successful_translation(tosca_file, hot_file)
92 def test_hot_translate_host_assignment(self):
93 tosca_file = '../tests/data/test_host_assignment.yaml'
94 hot_file = '../tests/data/hot_output/hot_host_assignment.yaml'
95 self._test_successful_translation(tosca_file, hot_file)
97 def test_hot_translate_elk(self):
98 tosca_file = '../tests/data/tosca_elk.yaml'
99 hot_file = '../tests/data/hot_output/hot_elk.yaml'
100 params = {'github_url':
101 'http://github.com/paypal/rest-api-sample-app-nodejs.git',
103 self._test_successful_translation(tosca_file, hot_file, params)
105 def test_hot_translate_nodejs_mongodb_two_instances(self):
106 tosca_file = '../tests/data/tosca_nodejs_mongodb_two_instances.yaml'
107 hot_file = '../tests/data/hot_output/' \
108 'hot_nodejs_mongodb_two_instances.yaml'
109 params = {'github_url':
110 'http://github.com/paypal/rest-api-sample-app-nodejs.git',
112 self._test_successful_translation(tosca_file, hot_file, params)
114 def test_hot_translate_blockstorage_with_attachment(self):
115 tosca_file = '../tests/data/storage/' \
116 'tosca_blockstorage_with_attachment.yaml'
117 hot_file = '../tests/data/hot_output/storage/' \
118 'hot_blockstorage_with_attachment.yaml'
120 'storage_location': '/dev/vdc',
121 'storage_size': '2000 MB',
122 'storage_snapshot_id': 'ssid'}
123 self._test_successful_translation(tosca_file, hot_file, params)
125 def test_hot_translate_blockstorage_with_custom_relationship_type(self):
126 tosca_file = '../tests/data/storage/' \
127 'tosca_blockstorage_with_custom_relationship_type.yaml'
128 hot_file = '../tests/data/hot_output/storage/' \
129 'hot_blockstorage_with_custom_relationship_type.yaml'
131 'storage_location': '/dev/vdc',
132 'storage_size': '1 GB',
133 'storage_snapshot_id': 'ssid'}
134 self._test_successful_translation(tosca_file, hot_file, params)
136 def test_hot_translate_blockstorage_with_relationship_template(self):
137 tosca_file = '../tests/data/storage/' \
138 'tosca_blockstorage_with_relationship_template.yaml'
139 hot_file = '../tests/data/hot_output/storage/' \
140 'hot_blockstorage_with_relationship_template.yaml'
142 'storage_location': '/dev/vdc',
143 'storage_size': '1 GB'}
144 self._test_successful_translation(tosca_file, hot_file, params)
146 def test_hot_translate_blockstorage_with_attachment_notation1(self):
147 tosca_file = '../tests/data/storage/' \
148 'tosca_blockstorage_with_attachment_notation1.yaml'
149 hot_file1 = '../tests/data/hot_output/storage/' \
150 'hot_blockstorage_with_attachment_notation1_alt1.yaml'
151 hot_file2 = '../tests/data/hot_output/storage/' \
152 'hot_blockstorage_with_attachment_notation1_alt2.yaml'
154 'storage_location': 'some_folder',
155 'storage_size': '1 GB',
156 'storage_snapshot_id': 'ssid'}
159 self._test_successful_translation(tosca_file, hot_file1, params)
161 self._test_successful_translation(tosca_file, hot_file2, params)
163 def test_hot_translate_blockstorage_with_attachment_notation2(self):
164 tosca_file = '../tests/data/storage/' \
165 'tosca_blockstorage_with_attachment_notation2.yaml'
166 hot_file1 = '../tests/data/hot_output/storage/' \
167 'hot_blockstorage_with_attachment_notation2_alt1.yaml'
168 hot_file2 = '../tests/data/hot_output/storage/' \
169 'hot_blockstorage_with_attachment_notation2_alt2.yaml'
171 'storage_location': '/dev/vdc',
172 'storage_size': '1 GB',
173 'storage_snapshot_id': 'ssid'}
175 self._test_successful_translation(tosca_file, hot_file1, params)
177 self._test_successful_translation(tosca_file, hot_file2, params)
179 def test_hot_translate_multiple_blockstorage_with_attachment(self):
180 tosca_file = '../tests/data/storage/' \
181 'tosca_multiple_blockstorage_with_attachment.yaml'
182 hot_file1 = '../tests/data/hot_output/storage/' \
183 'hot_multiple_blockstorage_with_attachment_alt1.yaml'
184 hot_file2 = '../tests/data/hot_output/storage/' \
185 'hot_multiple_blockstorage_with_attachment_alt2.yaml'
187 'storage_location': '/dev/vdc',
188 'storage_size': '1 GB',
189 'storage_snapshot_id': 'ssid'}
191 self._test_successful_translation(tosca_file, hot_file1, params)
193 self._test_successful_translation(tosca_file, hot_file2, params)
195 def test_hot_translate_multiple_blockstorage_w_multiple_attachment(self):
196 tosca_file = '../tests/data/storage/' \
197 'tosca_multiple_blockstorage_w_multiple_attachment.yaml'
198 hot_file = '../tests/data/hot_output/storage/' \
199 'hot_multiple_blockstorage_w_multiple_attachment.yaml'
201 'storage_location1': '/dev/vdb',
202 'storage_location2': '/dev/vdc',
203 'storage_size': '1 GB',
204 'storage_snapshot_id': 'ssid'}
205 self._test_successful_translation(tosca_file, hot_file, params)
207 def test_hot_translate_single_object_store(self):
208 tosca_file = '../tests/data/storage/tosca_single_object_store.yaml'
209 hot_file = '../tests/data/hot_output/hot_single_object_store.yaml'
210 params = {'objectstore_name': 'myobjstore'}
211 self._test_successful_translation(tosca_file, hot_file, params)
213 def test_hot_translate_one_server_one_network(self):
214 tosca_file = '../tests/data/network/tosca_one_server_one_network.yaml'
215 hot_file = '../tests/data/hot_output/network/' \
216 'hot_one_server_one_network.yaml'
217 params = {'network_name': 'private_net'}
218 self._test_successful_translation(tosca_file, hot_file, params)
220 def test_hot_translate_server_on_existing_network(self):
221 tosca_file = '../tests/data/network/' \
222 'tosca_server_on_existing_network.yaml'
223 hot_file = '../tests/data/hot_output/network/' \
224 'hot_server_on_existing_network.yaml'
225 params = {'network_name': 'private_net'}
226 self._test_successful_translation(tosca_file, hot_file, params)
228 def test_hot_translate_two_servers_one_network(self):
229 tosca_file = '../tests/data/network/tosca_two_servers_one_network.yaml'
230 hot_file = '../tests/data/hot_output/network/' \
231 'hot_two_servers_one_network.yaml'
232 params = {'network_name': 'my_private_net',
233 'network_cidr': '10.0.0.0/24',
234 'network_start_ip': '10.0.0.100',
235 'network_end_ip': '10.0.0.150'}
236 self._test_successful_translation(tosca_file, hot_file, params)
238 def test_hot_translate_one_server_three_networks(self):
239 tosca_file = '../tests/data/network/' \
240 'tosca_one_server_three_networks.yaml'
241 hot_file = '../tests/data/hot_output/network/' \
242 'hot_one_server_three_networks.yaml'
244 self._test_successful_translation(tosca_file, hot_file, params)
246 def test_hot_translate_software_component(self):
247 tosca_file = '../tests/data/tosca_software_component.yaml'
248 hot_file = '../tests/data/hot_output/hot_software_component.yaml'
249 params = {'cpus': '1',
250 'download_url': 'http://www.software.com/download'}
251 self._test_successful_translation(tosca_file, hot_file, params)
253 def test_hot_translate_software_component_multiple_hosts(self):
254 tosca_file = '../tests/data/tosca_software_component'\
255 '_multiple_hosts.yaml'
256 hot_file = '../tests/data/hot_output/hot_software_component'\
257 '_multiple_hosts.yaml'
258 params = {'cpus': '1',
259 'download_url': 'http://www.software.com/download'}
260 self._test_successful_translation(tosca_file, hot_file, params)
262 def test_hot_translate_web_application(self):
263 tosca_file = '../tests/data/tosca_web_application.yaml'
264 hot_file = '../tests/data/hot_output/hot_web_application.yaml'
265 params = {'cpus': '2', 'context_root': 'my_web_app'}
266 self._test_successful_translation(tosca_file, hot_file, params)
268 def test_hot_translate_template_with_url_import(self):
269 tosca_file = '../tests/data/' \
270 'tosca_single_instance_wordpress_with_url_import.yaml'
271 hot_file = '../tests/data/hot_output/' \
272 'hot_single_instance_wordpress.yaml'
273 params = {'db_name': 'wordpress',
274 'db_user': 'wp_user',
276 'db_root_pwd': 'passw0rd',
279 self._test_successful_translation(tosca_file, hot_file, params)
281 def test_hot_translate_template_by_url_with_local_import(self):
282 tosca_file = 'https://raw.githubusercontent.com/openstack/' \
283 'heat-translator/master/translator/tests/data/' \
284 'tosca_single_instance_wordpress.yaml'
285 hot_file = '../tests/data/hot_output/' \
286 'hot_single_instance_wordpress.yaml'
287 params = {'db_name': 'wordpress',
288 'db_user': 'wp_user',
290 'db_root_pwd': 'passw0rd',
293 self._test_successful_translation(tosca_file, hot_file, params)
295 def test_hot_translate_template_by_url_with_local_abspath_import(self):
296 tosca_file = 'https://raw.githubusercontent.com/openstack/' \
297 'heat-translator/master/translator/tests/data/' \
298 'tosca_single_instance_wordpress_with_local_abspath' \
300 hot_file = '../tests/data/hot_output/' \
301 'hot_single_instance_wordpress.yaml'
302 params = {'db_name': 'wordpress',
303 'db_user': 'wp_user',
305 'db_root_pwd': 'passw0rd',
308 expected_msg = _('Absolute file name "/tmp/wordpress.yaml" cannot be '
309 'used in a URL-based input template "https://raw.'
310 'githubusercontent.com/openstack/heat-translator/'
311 'master/translator/tests/data/tosca_single_instance_'
312 'wordpress_with_local_abspath_import.yaml".')
314 self._test_failed_translation(tosca_file, hot_file, params,
315 expected_msg, msg_path, ValidationError,
318 def test_hot_translate_template_by_url_with_url_import(self):
319 tosca_url = 'https://raw.githubusercontent.com/openstack/' \
320 'heat-translator/master/translator/tests/data/' \
321 'tosca_single_instance_wordpress_with_url_import.yaml'
322 hot_file = '../tests/data/hot_output/' \
323 'hot_single_instance_wordpress.yaml'
324 params = {'db_name': 'wordpress',
325 'db_user': 'wp_user',
327 'db_root_pwd': 'passw0rd',
330 self._test_successful_translation(tosca_url, hot_file, params)
332 def test_translate_hello_world_csar(self):
333 tosca_file = '../tests/data/csar_hello_world.zip'
334 hot_file = '../tests/data/hot_output/hot_hello_world.yaml'
335 self._test_successful_translation(tosca_file, hot_file)
337 def test_translate_single_instance_wordpress_csar(self):
338 tosca_file = '../tests/data/csar_single_instance_wordpress.zip'
339 hot_file = '../tests/data/hot_output/' \
340 'hot_single_instance_wordpress_from_csar.yaml'
341 params = {'db_name': 'wordpress',
342 'db_user': 'wp_user',
344 'db_root_pwd': 'passw0rd',
347 self._test_successful_translation(tosca_file, hot_file, params)
349 def test_translate_elk_csar_from_url(self):
350 tosca_file = 'https://github.com/openstack/heat-translator/raw/' \
351 'master/translator/tests/data/csar_elk.zip'
352 hot_file = '../tests/data/hot_output/hot_elk_from_csar.yaml'
353 params = {'github_url':
354 'http://github.com/paypal/rest-api-sample-app-nodejs.git',
356 self._test_successful_translation(tosca_file, hot_file, params)
358 def test_translate_csar_not_zip(self):
359 tosca_file = '../tests/data/csar_not_zip.zip'
362 expected_msg = _('"%s" is not a valid zip file.')
364 self._test_failed_translation(tosca_file, hot_file, params,
365 expected_msg, msg_path, ValidationError,
368 def test_translate_csar_metadata_not_yaml(self):
369 tosca_file = '../tests/data/csar_metadata_not_yaml.zip'
372 expected_msg = _('The file "TOSCA-Metadata/TOSCA.meta" in the CSAR '
373 '"%s" does not contain valid YAML content.')
375 self._test_failed_translation(tosca_file, hot_file, params,
376 expected_msg, msg_path, ValidationError,
379 def test_translate_csar_wrong_metadata_file(self):
380 tosca_file = '../tests/data/csar_wrong_metadata_file.zip'
383 expected_msg = _('"%s" is not a valid CSAR as it does not contain the '
384 'required file "TOSCA.meta" in the folder '
387 self._test_failed_translation(tosca_file, hot_file, params,
388 expected_msg, msg_path, ValidationError,
391 def test_translate_csar_wordpress_invalid_import_path(self):
392 tosca_file = '../tests/data/csar_wordpress_invalid_import_path.zip'
395 expected_msg = _('Import '
396 '"Invalid_import_path/wordpress.yaml" is not valid.')
398 self._test_failed_translation(tosca_file, hot_file, params,
399 expected_msg, msg_path, ValidationError,
402 def test_translate_csar_wordpress_invalid_script_url(self):
403 tosca_file = '../tests/data/csar_wordpress_invalid_script_url.zip'
406 expected_msg = _('The resource at '
407 '"https://raw.githubusercontent.com/openstack/'
408 'heat-translator/master/translator/tests/data/'
409 'custom_types/wordpress1.yaml" cannot be accessed.')
411 self._test_failed_translation(tosca_file, hot_file, params,
412 expected_msg, msg_path, ValidationError,
415 def test_hot_translate_flavor_image(self):
416 tosca_file = '../tests/data/test_tosca_flavor_and_image.yaml'
417 hot_file = '../tests/data/hot_output/hot_flavor_and_image.yaml'
418 self._test_successful_translation(tosca_file, hot_file)
420 def test_hot_translate_flavor_image_params(self):
421 tosca_file = '../tests/data/test_tosca_flavor_and_image.yaml'
422 hot_file = '../tests/data/hot_output/hot_flavor_and_image_params.yaml'
423 params = {'key_name': 'paramkey'}
424 self._test_successful_translation(tosca_file, hot_file, params)
426 def test_hot_translate_custom_type(self):
427 tosca_file = '../tests/data/test_tosca_custom_type.yaml'
428 hot_file = '../tests/data/hot_output/' \
429 'hot_custom_type.yaml'
431 self._test_successful_translation(tosca_file, hot_file, params)
433 def test_hot_translate_custom_type_with_override(self):
434 tosca_file = '../tests/data/test_tosca_custom_type_with_override.yaml'
435 hot_file = '../tests/data/hot_output/' \
436 'hot_custom_type_with_override.yaml'
438 self._test_successful_translation(tosca_file, hot_file, params)
440 def test_hot_translate_custom_type_with_param_override(self):
441 tosca_file = '../tests/data/test_tosca_custom_type_with_override.yaml'
442 hot_file = '../tests/data/hot_output/' \
443 'hot_custom_type_with_param_override.yaml'
444 params = {'install_path': '/home/custom/from/cli'}
445 self._test_successful_translation(tosca_file, hot_file, params)
447 def test_hot_translate_artifact(self):
448 tosca_file = '../tests/data/test_tosca_artifact.yaml'
449 hot_file = '../tests/data/hot_output/' \
452 self._test_successful_translation(tosca_file, hot_file, params)
454 def test_hot_translate_without_tosca_os_version(self):
455 tosca_file = '../tests/data/' \
456 'test_single_server_without_optional_version_prop.yaml'
457 hot_file = '../tests/data/hot_output/' \
458 'hot_single_server_without_tosca_os_version.yaml'
460 self._test_successful_translation(tosca_file, hot_file, params)
462 def test_hot_translate_helloworld_with_userkey(self):
463 tosca_file = '../tests/data/tosca_helloworld.yaml'
464 hot_file = '../tests/data/hot_output/hot_hello_world_userkey.yaml'
465 params = {'key_name': 'userkey'}
466 self._test_successful_translation(tosca_file, hot_file, params)
468 def test_hot_translate_custom_networks_nodes_inline(self):
469 tosca_file = '../tests/data/network/' \
470 'test_tosca_custom_network_nodes_inline.yaml'
471 hot_file = '../tests/data/hot_output/network/' \
472 'hot_custom_network_nodes.yaml'
474 self._test_successful_translation(tosca_file, hot_file, params)
476 def test_hot_translate_custom_networks_nodes_imports(self):
477 tosca_file = '../tests/data/network/' \
478 'test_tosca_custom_network_nodes_imports.yaml'
479 hot_file = '../tests/data/hot_output/network/' \
480 'hot_custom_network_nodes.yaml'
482 self._test_successful_translation(tosca_file, hot_file, params)
484 def test_hot_translate_nfv_sample(self):
485 tosca_file = '../tests/data/nfv/test_tosca_nfv_sample.yaml'
486 hot_file = '../tests/data/hot_output/nfv/hot_nfv_sample.yaml'
488 self._test_successful_translation(tosca_file, hot_file, params)
490 def test_hot_translate_policy(self):
491 tosca_file = '../tests/data/policies/tosca_policies.yaml'
492 hot_file = '../tests/data/hot_output/policies/hot_policies.yaml'
494 self._test_successful_translation(tosca_file, hot_file, params)
496 def test_hot_script_types(self):
497 tosca_file = '../tests/data/interfaces/test_tosca_script_types.yaml'
498 hot_file = '../tests/data/hot_output/hot_script_types.yaml'
500 self._test_successful_translation(tosca_file, hot_file, params)
502 def test_hot_interface_on_compute(self):
503 tosca_file = '../tests/data/interfaces/' \
504 'test_tosca_interface_on_compute.yaml'
505 hot_file = '../tests/data/hot_output/interfaces/' \
506 'hot_interface_on_compute.yaml'
508 self._test_successful_translation(tosca_file, hot_file, params)
510 def test_hot_get_functions_semantic(self):
511 tosca_file = '../tests/data/test_tosca_get_functions_semantic.yaml'
512 hot_file = '../tests/data/hot_output/hot_get_functions_semantic.yaml'
514 self._test_successful_translation(tosca_file, hot_file, params)
516 def test_hot_exchange_public_ssh_key(self):
517 tosca_file = '../tests/data/tosca_exchange_public_ssh_key.yaml'
518 hot_file = '../tests/data/hot_output/hot_exchange_public_ssh_key.yaml'
520 self._test_successful_translation(tosca_file, hot_file, params)
522 def test_hot_translate_scaling_policy(self):
523 tosca_file = '../tests/data/autoscaling/tosca_autoscaling.yaml'
525 '../tests/data/hot_output/autoscaling/hot_autoscaling.yaml',
526 '../tests/data/hot_output/autoscaling/asg_res.yaml',
529 self._test_successful_translation(tosca_file, hot_files, params)
531 def test_hot_translate_scaling_nested_plate(self):
532 tosca_file = '../tests/data/autoscaling/tosca_autoscaling.yaml'
534 '../tests/data/hot_output/autoscaling/asg_res.yaml'
537 self._test_successful_translation(tosca_file, hot_files, params,
538 nested_resources=True)
540 def test_translate_unsupported_tosca_type(self):
541 tosca_file = '../tests/data/test_tosca_unsupported_type.yaml'
542 tosca_tpl = os.path.normpath(os.path.join(
543 os.path.dirname(os.path.abspath(__file__)), tosca_file))
545 expected_msg = _('Type "tosca.nodes.LoadBalancer" is valid TOSCA '
546 'type but translation support is not yet available.')
547 tosca = ToscaTemplate(tosca_tpl, params, True)
548 err = self.assertRaises(UnsupportedTypeError,
549 TOSCATranslator(tosca, params)
551 self.assertEqual(expected_msg, err.__str__())
553 def test_hot_translate_cluster_scaling_policy(self):
554 tosca_file = '../tests/data/autoscaling/tosca_cluster_autoscaling.yaml'
555 hot_file = '../tests/data/hot_output/autoscaling/' \
556 'hot_cluster_autoscaling.yaml'
558 self._test_successful_translation(tosca_file, hot_file, params)
560 def test_hot_translate_nfv_scaling(self):
561 tosca_file = '../tests/data/nfv/test_tosca_nfv_autoscaling.yaml'
563 '../tests/data/hot_output/nfv/hot_tosca_nfv_autoscaling.yaml',
564 '../tests/data/hot_output/nfv/SP1_res.yaml',
567 self._test_successful_translation(tosca_file, hot_files, params)
569 def test_hot_translate_mon_scaling_policy(self):
570 tosca_file = '../tests/data/monitoring/tosca_monitoring_scaling.yaml'
572 '../tests/data/hot_output/monitoring/hot_monitoring_scaling.yaml',
573 '../tests/data/hot_output/monitoring/asg_res.yaml',
576 self._test_successful_translation(tosca_file, hot_files, params)