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 import exception
17 import toscaparser.elements.interfaces as ifaces
18 from toscaparser.elements.nodetype import NodeType
19 from toscaparser.functions import GetInput
20 from toscaparser.functions import GetProperty
21 from toscaparser.nodetemplate import NodeTemplate
22 from toscaparser.tests.base import TestCase
23 from toscaparser.tosca_template import ToscaTemplate
24 from toscaparser.utils.gettextutils import _
25 import toscaparser.utils.yamlparser
28 class ToscaTemplateTest(TestCase):
31 tosca_tpl = os.path.join(
32 os.path.dirname(os.path.abspath(__file__)),
33 "data/tosca_single_instance_wordpress.yaml")
34 tosca = ToscaTemplate(tosca_tpl)
36 tosca_elk_tpl = os.path.join(
37 os.path.dirname(os.path.abspath(__file__)),
38 "data/tosca_elk.yaml")
40 def test_version(self):
41 self.assertEqual(self.tosca.version, "tosca_simple_yaml_1_0")
43 def test_description(self):
44 expected_description = "TOSCA simple profile with wordpress, " \
45 "web server and mysql on the same server."
46 self.assertEqual(self.tosca.description, expected_description)
48 def test_inputs(self):
50 ['cpus', 'db_name', 'db_port',
51 'db_pwd', 'db_root_pwd', 'db_user'],
52 sorted([input.name for input in self.tosca.inputs]))
54 input_name = "db_port"
55 expected_description = "Port for the MySQL database."
56 for input in self.tosca.inputs:
57 if input.name == input_name:
58 self.assertEqual(input.description, expected_description)
60 def test_node_tpls(self):
61 '''Test nodetemplate names.'''
63 ['mysql_database', 'mysql_dbms', 'server',
64 'webserver', 'wordpress'],
65 sorted([tpl.name for tpl in self.tosca.nodetemplates]))
67 tpl_name = "mysql_database"
68 expected_type = "tosca.nodes.Database"
69 expected_properties = ['name', 'password', 'user']
70 expected_capabilities = ['database_endpoint', 'feature']
71 expected_requirements = [{'host': 'mysql_dbms'}]
72 ''' TODO: needs enhancement in tosca_elk.yaml..
73 expected_relationshp = ['tosca.relationships.HostedOn']
74 expected_host = ['mysql_dbms']
76 expected_interface = [ifaces.LIFECYCLE_SHORTNAME]
78 for tpl in self.tosca.nodetemplates:
79 if tpl_name == tpl.name:
81 self.assertEqual(tpl.type, expected_type)
83 '''Test properties.'''
86 sorted(tpl.get_properties().keys()))
88 '''Test capabilities.'''
90 expected_capabilities,
91 sorted(tpl.get_capabilities().keys()))
93 '''Test requirements.'''
95 expected_requirements, tpl.requirements)
97 '''Test relationship.'''
98 ''' needs enhancements in tosca_elk.yaml
100 expected_relationshp,
101 [x.type for x in tpl.relationships.keys()])
104 [y.name for y in tpl.relationships.values()])
106 '''Test interfaces.'''
109 [x.type for x in tpl.interfaces])
111 if tpl.name == 'server':
112 '''Test property value'''
113 props = tpl.get_properties()
114 if props and 'mem_size' in props.keys():
115 self.assertEqual(props['mem_size'].value, '4096 MB')
116 '''Test capability'''
117 caps = tpl.get_capabilities()
118 self.assertIn('os', caps.keys())
122 if caps and 'os' in caps.keys():
123 capability = caps['os']
124 os_props_objs = capability.get_properties_objects()
125 os_props = capability.get_properties()
126 os_type_prop = capability.get_property_value('type')
130 [p.value for p in os_props_objs if p.name == 'type'])
133 os_props['type'].value if 'type' in os_props else '')
134 self.assertEqual('Linux', os_props['type'].value)
135 self.assertEqual('Linux', os_type_prop)
137 def test_node_inheritance_type(self):
139 node for node in self.tosca.nodetemplates
140 if node.name == 'wordpress'][0]
142 wordpress_node.is_derived_from("tosca.nodes.WebApplication"))
144 wordpress_node.is_derived_from("tosca.nodes.Root"))
146 wordpress_node.is_derived_from("tosca.policies.Root"))
148 wordpress_node.is_derived_from("tosca.groups.Root"))
150 def test_outputs(self):
153 sorted([output.name for output in self.tosca.outputs]))
155 def test_interfaces(self):
157 node for node in self.tosca.nodetemplates
158 if node.name == 'wordpress'][0]
159 interfaces = wordpress_node.interfaces
160 self.assertEqual(2, len(interfaces))
161 for interface in interfaces:
162 if interface.name == 'create':
163 self.assertEqual(ifaces.LIFECYCLE_SHORTNAME,
165 self.assertEqual('wordpress/wordpress_install.sh',
166 interface.implementation)
167 self.assertIsNone(interface.inputs)
168 elif interface.name == 'configure':
169 self.assertEqual(ifaces.LIFECYCLE_SHORTNAME,
171 self.assertEqual('wordpress/wordpress_configure.sh',
172 interface.implementation)
173 self.assertEqual(3, len(interface.inputs))
174 TestCase.skip(self, 'bug #1440247')
175 wp_db_port = interface.inputs['wp_db_port']
176 self.assertTrue(isinstance(wp_db_port, GetProperty))
177 self.assertEqual('get_property', wp_db_port.name)
178 self.assertEqual(['SELF',
182 result = wp_db_port.result()
183 self.assertTrue(isinstance(result, GetInput))
185 raise AssertionError(
186 'Unexpected interface: {0}'.format(interface.name))
188 def test_normative_type_by_short_name(self):
189 # test template with a short name Compute
190 template = os.path.join(
191 os.path.dirname(os.path.abspath(__file__)),
192 "data/test_tosca_normative_type_by_shortname.yaml")
194 tosca_tpl = ToscaTemplate(template)
195 expected_type = "tosca.nodes.Compute"
196 for tpl in tosca_tpl.nodetemplates:
197 self.assertEqual(tpl.type, expected_type)
198 for tpl in tosca_tpl.nodetemplates:
199 compute_type = NodeType(tpl.type)
201 sorted(['tosca.capabilities.Container',
202 'tosca.capabilities.Node',
203 'tosca.capabilities.OperatingSystem',
204 'tosca.capabilities.network.Bindable',
205 'tosca.capabilities.Scalable']),
207 for c in compute_type.get_capabilities_objects()]))
209 def test_template_with_no_inputs(self):
210 tosca_tpl = self._load_template('test_no_inputs_in_template.yaml')
211 self.assertEqual(0, len(tosca_tpl.inputs))
213 def test_template_with_no_outputs(self):
214 tosca_tpl = self._load_template('test_no_outputs_in_template.yaml')
215 self.assertEqual(0, len(tosca_tpl.outputs))
217 def test_relationship_interface(self):
218 template = ToscaTemplate(self.tosca_elk_tpl)
219 for node_tpl in template.nodetemplates:
220 if node_tpl.name == 'logstash':
221 config_interface = 'Configure'
222 artifact = 'logstash/configure_elasticsearch.py'
223 relation = node_tpl.relationships
224 for key in relation.keys():
225 rel_tpl = relation.get(key).get_relationship_template()
227 self.assertTrue(rel_tpl[0].is_derived_from(
228 "tosca.relationships.Root"))
229 interfaces = rel_tpl[0].interfaces
230 for interface in interfaces:
231 self.assertEqual(config_interface,
233 self.assertEqual('pre_configure_source',
235 self.assertEqual(artifact,
236 interface.implementation)
238 def test_relationship(self):
239 template = ToscaTemplate(self.tosca_elk_tpl)
240 for node_tpl in template.nodetemplates:
241 if node_tpl.name == 'paypal_pizzastore':
242 expected_relationships = ['tosca.relationships.ConnectsTo',
243 'tosca.relationships.HostedOn']
244 expected_hosts = ['tosca.nodes.Database',
245 'tosca.nodes.WebServer']
246 self.assertEqual(len(node_tpl.relationships), 2)
248 expected_relationships,
249 sorted([k.type for k in node_tpl.relationships.keys()]))
252 sorted([v.type for v in node_tpl.relationships.values()]))
254 def test_template_macro(self):
255 template = ToscaTemplate(self.tosca_elk_tpl)
256 for node_tpl in template.nodetemplates:
257 if node_tpl.name == 'mongo_server':
259 ['disk_size', 'mem_size', 'num_cpus'],
260 sorted(node_tpl.get_capability('host').
261 get_properties().keys()))
263 def test_template_requirements(self):
264 """Test different formats of requirements
266 The requirements can be defined in few different ways,
267 1. Requirement expressed as a capability with an implicit relationship.
268 2. Requirement expressed with explicit relationship.
269 3. Requirement expressed with a relationship template.
270 4. Requirement expressed via TOSCA types to provision a node
271 with explicit relationship.
272 5. Requirement expressed via TOSCA types with a filter.
274 tosca_tpl = os.path.join(
275 os.path.dirname(os.path.abspath(__file__)),
276 "data/test_requirements.yaml")
277 tosca = ToscaTemplate(tosca_tpl)
278 for node_tpl in tosca.nodetemplates:
279 if node_tpl.name == 'my_app':
280 expected_relationship = [
281 ('tosca.relationships.ConnectsTo', 'mysql_database'),
282 ('tosca.relationships.HostedOn', 'my_webserver')]
283 actual_relationship = sorted([
284 (relation.type, node.name) for
285 relation, node in node_tpl.relationships.items()])
286 self.assertEqual(expected_relationship, actual_relationship)
287 if node_tpl.name == 'mysql_database':
289 [('tosca.relationships.HostedOn', 'my_dbms')],
290 [(relation.type, node.name) for
292 node in node_tpl.relationships.items()])
293 if node_tpl.name == 'my_server':
295 [('tosca.relationships.AttachesTo', 'my_storage')],
296 [(relation.type, node.name) for
298 node in node_tpl.relationships.items()])
300 def test_template_requirements_not_implemented(self):
301 # TODO(spzala): replace this test with new one once TOSCA types look up
302 # support is implemented.
303 """Requirements that yet need to be implemented
305 The following requirement formats are not yet implemented,
306 due to look up dependency:
307 1. Requirement expressed via TOSCA types to provision a node
308 with explicit relationship.
309 2. Requirement expressed via TOSCA types with a filter.
314 type: tosca.nodes.Database
315 description: Requires a particular node type and relationship.
316 To be full-filled via lookup into node repository.
319 node: tosca.nodes.DBMS
320 relationship: tosca.relationships.HostedOn
326 type: tosca.nodes.WebServer
327 description: Requires a particular node type with a filter.
328 To be full-filled via lookup into node repository.
331 node: tosca.nodes.Compute
334 num_cpus: { in_range: [ 1, 4 ] }
335 mem_size: { greater_or_equal: 2 }
337 - tosca.capabilities.OS:
346 type: tosca.nodes.WebServer
347 description: Requires a node type with a particular capability.
348 To be full-filled via lookup into node repository.
351 node: tosca.nodes.Compute
352 relationship: tosca.relationships.HostedOn
353 capability: tosca.capabilities.Container
355 self._requirements_not_implemented(tpl_snippet_1, 'mysql_database')
356 self._requirements_not_implemented(tpl_snippet_2, 'my_webserver')
357 self._requirements_not_implemented(tpl_snippet_3, 'my_webserver2')
359 def _requirements_not_implemented(self, tpl_snippet, tpl_name):
360 nodetemplates = (toscaparser.utils.yamlparser.
361 simple_parse(tpl_snippet))['node_templates']
364 lambda: NodeTemplate(tpl_name, nodetemplates).relationships)
366 # Test the following:
367 # 1. Custom node type derived from 'WebApplication' named 'TestApp'
368 # with a custom Capability Type 'TestCapability'
369 # 2. Same as #1, but referencing a custom 'TestCapability' Capability Type
370 # that is not defined
371 def test_custom_capability_type_definition(self):
375 type: tosca.nodes.WebApplication.TestApp
381 # custom node type definition with custom capability type definition
383 tosca.nodes.WebApplication.TestApp:
384 derived_from: tosca.nodes.WebApplication
387 type: tosca.capabilities.TestCapability
388 tosca.capabilities.TestCapability:
389 derived_from: tosca.capabilities.Root
395 expected_capabilities = ['app_endpoint', 'feature', 'test_cap']
396 nodetemplates = (toscaparser.utils.yamlparser.
397 simple_parse(tpl_snippet))['node_templates']
398 custom_def = (toscaparser.utils.yamlparser.
399 simple_parse(custom_def))
400 name = list(nodetemplates.keys())[0]
401 tpl = NodeTemplate(name, nodetemplates, custom_def)
403 expected_capabilities,
404 sorted(tpl.get_capabilities().keys()))
406 # custom definition without valid capability type definition
408 tosca.nodes.WebApplication.TestApp:
409 derived_from: tosca.nodes.WebApplication
412 type: tosca.capabilities.TestCapability
414 custom_def = (toscaparser.utils.yamlparser.
415 simple_parse(custom_def))
416 tpl = NodeTemplate(name, nodetemplates, custom_def)
417 err = self.assertRaises(
418 exception.InvalidTypeError,
419 lambda: NodeTemplate(name, nodetemplates,
420 custom_def).get_capabilities_objects())
421 self.assertEqual('Type "tosca.capabilities.TestCapability" is not '
422 'a valid type.', six.text_type(err))
424 def test_local_template_with_local_relpath_import(self):
425 tosca_tpl = os.path.join(
426 os.path.dirname(os.path.abspath(__file__)),
427 "data/tosca_single_instance_wordpress.yaml")
428 tosca = ToscaTemplate(tosca_tpl)
429 self.assertTrue(tosca.topology_template.custom_defs)
431 def test_local_template_with_url_import(self):
432 tosca_tpl = os.path.join(
433 os.path.dirname(os.path.abspath(__file__)),
434 "data/tosca_single_instance_wordpress_with_url_import.yaml")
435 tosca = ToscaTemplate(tosca_tpl)
436 self.assertTrue(tosca.topology_template.custom_defs)
438 def test_url_template_with_local_relpath_import(self):
439 tosca_tpl = ('https://raw.githubusercontent.com/openstack/'
440 'tosca-parser/master/toscaparser/tests/data/'
441 'tosca_single_instance_wordpress.yaml')
442 tosca = ToscaTemplate(tosca_tpl, None, False)
443 self.assertTrue(tosca.topology_template.custom_defs)
445 def test_url_template_with_local_abspath_import(self):
446 tosca_tpl = ('https://raw.githubusercontent.com/openstack/'
447 'tosca-parser/master/toscaparser/tests/data/'
448 'tosca_single_instance_wordpress_with_local_abspath_'
450 self.assertRaises(exception.ValidationError, ToscaTemplate, tosca_tpl,
452 err_msg = (_('Absolute file name "/tmp/tosca-parser/toscaparser/tests'
453 '/data/custom_types/wordpress.yaml" cannot be used in a '
454 'URL-based input template "%(tpl)s".')
455 % {'tpl': tosca_tpl})
456 exception.ExceptionCollector.assertExceptionMessage(ImportError,
459 def test_url_template_with_url_import(self):
460 tosca_tpl = ('https://raw.githubusercontent.com/openstack/'
461 'tosca-parser/master/toscaparser/tests/data/'
462 'tosca_single_instance_wordpress_with_url_import.yaml')
463 tosca = ToscaTemplate(tosca_tpl, None, False)
464 self.assertTrue(tosca.topology_template.custom_defs)
466 def test_csar_parsing_wordpress(self):
467 csar_archive = os.path.join(
468 os.path.dirname(os.path.abspath(__file__)),
469 'data/CSAR/csar_wordpress.zip')
470 self.assertTrue(ToscaTemplate(csar_archive))
472 def test_csar_parsing_elk_url_based(self):
473 csar_archive = ('https://github.com/openstack/tosca-parser/raw/master/'
474 'toscaparser/tests/data/CSAR/csar_elk.zip')
475 self.assertTrue(ToscaTemplate(csar_archive, None, False))
477 def test_nested_imports_in_templates(self):
478 tosca_tpl = os.path.join(
479 os.path.dirname(os.path.abspath(__file__)),
480 "data/test_instance_nested_imports.yaml")
481 tosca = ToscaTemplate(tosca_tpl)
482 expected_custom_types = ['tosca.nodes.WebApplication.WordPress',
483 'test_namespace_prefix.Rsyslog',
484 'Test2ndRsyslogType',
485 'test_2nd_namespace_prefix.Rsyslog',
486 'tosca.nodes.SoftwareComponent.Logstash',
487 'tosca.nodes.SoftwareComponent.Rsyslog.'
489 self.assertItemsEqual(tosca.topology_template.custom_defs.keys(),
490 expected_custom_types)
492 def test_invalid_template_file(self):
493 template_file = 'invalid template file'
494 expected_msg = (_('"%s" is not a valid file.') % template_file)
496 exception.ValidationError,
497 ToscaTemplate, template_file, None, False)
498 exception.ExceptionCollector.assertExceptionMessage(ValueError,
501 def test_multiple_validation_errors(self):
502 tosca_tpl = os.path.join(
503 os.path.dirname(os.path.abspath(__file__)),
504 "data/test_multiple_validation_errors.yaml")
505 self.assertRaises(exception.ValidationError, ToscaTemplate, tosca_tpl,
507 valid_versions = ', '.join(ToscaTemplate.VALID_TEMPLATE_VERSIONS)
508 err1_msg = (_('The template version "tosca_simple_yaml_1" is invalid. '
509 'Valid versions are "%s".') % valid_versions)
510 exception.ExceptionCollector.assertExceptionMessage(
511 exception.InvalidTemplateVersion, err1_msg)
513 err2_msg = _('Import "custom_types/not_there.yaml" is not valid.')
514 exception.ExceptionCollector.assertExceptionMessage(
515 ImportError, err2_msg)
517 err3_msg = _('Type "tosca.nodes.WebApplication.WordPress" is not a '
519 exception.ExceptionCollector.assertExceptionMessage(
520 exception.InvalidTypeError, err3_msg)
522 err4_msg = _('Node template "wordpress" contains unknown field '
523 '"requirement". Refer to the definition to verify valid '
525 exception.ExceptionCollector.assertExceptionMessage(
526 exception.UnknownFieldError, err4_msg)
528 err5_msg = _('\'Property "passwords" was not found in node template '
529 '"mysql_database".\'')
530 exception.ExceptionCollector.assertExceptionMessage(
533 err6_msg = _('Template "mysql_dbms" is missing required field "type".')
534 exception.ExceptionCollector.assertExceptionMessage(
535 exception.MissingRequiredFieldError, err6_msg)
537 err7_msg = _('Node template "mysql_dbms" contains unknown field '
538 '"type1". Refer to the definition to verify valid '
540 exception.ExceptionCollector.assertExceptionMessage(
541 exception.UnknownFieldError, err7_msg)
543 err8_msg = _('\'Node template "server1" was not found.\'')
544 exception.ExceptionCollector.assertExceptionMessage(
547 err9_msg = _('"relationship" used in template "webserver" is missing '
548 'required field "type".')
549 exception.ExceptionCollector.assertExceptionMessage(
550 exception.MissingRequiredFieldError, err9_msg)
552 def test_invalid_section_names(self):
553 tosca_tpl = os.path.join(
554 os.path.dirname(os.path.abspath(__file__)),
555 "data/test_invalid_section_names.yaml")
556 self.assertRaises(exception.ValidationError, ToscaTemplate, tosca_tpl,
558 err1_msg = _('Template contains unknown field '
559 '"tosca_definitions_versions". Refer to the definition '
560 'to verify valid values.')
561 exception.ExceptionCollector.assertExceptionMessage(
562 exception.UnknownFieldError, err1_msg)
564 err2_msg = _('Template contains unknown field "descriptions". '
565 'Refer to the definition to verify valid values.')
566 exception.ExceptionCollector.assertExceptionMessage(
567 exception.UnknownFieldError, err2_msg)
569 err3_msg = _('Template contains unknown field "import". Refer to '
570 'the definition to verify valid values.')
571 exception.ExceptionCollector.assertExceptionMessage(
572 exception.UnknownFieldError, err3_msg)
574 err4_msg = _('Template contains unknown field "topology_templates". '
575 'Refer to the definition to verify valid values.')
576 exception.ExceptionCollector.assertExceptionMessage(
577 exception.UnknownFieldError, err4_msg)
579 def test_csar_with_alternate_extenstion(self):
580 tosca_tpl = os.path.join(
581 os.path.dirname(os.path.abspath(__file__)),
582 "data/CSAR/csar_elk.csar")
583 tosca = ToscaTemplate(tosca_tpl)
584 self.assertTrue(tosca.topology_template.custom_defs)
586 def test_available_rel_tpls(self):
587 tosca_tpl = os.path.join(
588 os.path.dirname(os.path.abspath(__file__)),
589 "data/test_available_rel_tpls.yaml")
590 tosca = ToscaTemplate(tosca_tpl)
591 for node in tosca.nodetemplates:
592 for relationship, target in node.relationships.items():
595 except TypeError as error:
598 def test_no_input(self):
599 self.assertRaises(exception.ValidationError, ToscaTemplate, None,
601 err_msg = (('No path or yaml_dict_tpl was provided. '
602 'There is nothing to parse.'))
603 exception.ExceptionCollector.assertExceptionMessage(ValueError,
606 def test_path_and_yaml_dict_tpl_input(self):
607 test_tpl = os.path.join(
608 os.path.dirname(os.path.abspath(__file__)),
609 "data/tosca_helloworld.yaml")
611 yaml_dict_tpl = toscaparser.utils.yamlparser.load_yaml(test_tpl)
613 tosca = ToscaTemplate(test_tpl, yaml_dict_tpl=yaml_dict_tpl)
615 self.assertEqual(tosca.version, "tosca_simple_yaml_1_0")
617 def test_yaml_dict_tpl_input(self):
618 test_tpl = os.path.join(
619 os.path.dirname(os.path.abspath(__file__)),
620 "data/tosca_helloworld.yaml")
622 yaml_dict_tpl = toscaparser.utils.yamlparser.load_yaml(test_tpl)
624 tosca = ToscaTemplate(yaml_dict_tpl=yaml_dict_tpl)
626 self.assertEqual(tosca.version, "tosca_simple_yaml_1_0")
628 def test_yaml_dict_tpl_with_params_and_url_import(self):
629 test_tpl = os.path.join(
630 os.path.dirname(os.path.abspath(__file__)),
631 "data/tosca_single_instance_wordpress_with_url_import.yaml")
633 yaml_dict_tpl = toscaparser.utils.yamlparser.load_yaml(test_tpl)
635 params = {'db_name': 'my_wordpress', 'db_user': 'my_db_user',
636 'db_root_pwd': 'mypasswd'}
638 tosca = ToscaTemplate(parsed_params=params,
639 yaml_dict_tpl=yaml_dict_tpl)
641 self.assertEqual(tosca.version, "tosca_simple_yaml_1_0")
643 def test_yaml_dict_tpl_with_rel_import(self):
644 test_tpl = os.path.join(
645 os.path.dirname(os.path.abspath(__file__)),
646 "data/tosca_single_instance_wordpress.yaml")
648 yaml_dict_tpl = toscaparser.utils.yamlparser.load_yaml(test_tpl)
650 self.assertRaises(exception.ValidationError, ToscaTemplate, None,
651 None, False, yaml_dict_tpl)
652 err_msg = (_('Relative file name "custom_types/wordpress.yaml" '
653 'cannot be used in a pre-parsed input template.'))
654 exception.ExceptionCollector.assertExceptionMessage(ImportError,
657 def test_yaml_dict_tpl_with_fullpath_import(self):
658 test_tpl = os.path.join(
659 os.path.dirname(os.path.abspath(__file__)),
660 "data/tosca_single_instance_wordpress.yaml")
662 yaml_dict_tpl = toscaparser.utils.yamlparser.load_yaml(test_tpl)
664 yaml_dict_tpl['imports'] = [os.path.join(os.path.dirname(
665 os.path.abspath(__file__)), "data/custom_types/wordpress.yaml")]
667 params = {'db_name': 'my_wordpress', 'db_user': 'my_db_user',
668 'db_root_pwd': 'mypasswd'}
670 tosca = ToscaTemplate(parsed_params=params,
671 yaml_dict_tpl=yaml_dict_tpl)
673 self.assertEqual(tosca.version, "tosca_simple_yaml_1_0")
675 def test_policies_for_node_templates(self):
676 tosca_tpl = os.path.join(
677 os.path.dirname(os.path.abspath(__file__)),
678 "data/policies/tosca_policy_template.yaml")
679 tosca = ToscaTemplate(tosca_tpl)
681 for policy in tosca.topology_template.policies:
682 if policy.name == 'my_compute_placement_policy':
683 self.assertEqual('tosca.policies.Placement', policy.type)
684 self.assertEqual(['my_server_1', 'my_server_2'],
686 self.assertEqual('node_templates', policy.get_targets_type())
687 for node in policy.targets_list:
688 if node.name == 'my_server_1':
689 '''Test property value'''
690 props = node.get_properties()
691 if props and 'mem_size' in props.keys():
692 self.assertEqual(props['mem_size'].value,
695 def test_policies_for_groups(self):
696 tosca_tpl = os.path.join(
697 os.path.dirname(os.path.abspath(__file__)),
698 "data/policies/tosca_policy_template.yaml")
699 tosca = ToscaTemplate(tosca_tpl)
701 for policy in tosca.topology_template.policies:
702 if policy.name == 'my_groups_placement':
703 self.assertEqual('mycompany.mytypes.myScalingPolicy',
705 self.assertEqual(['webserver_group'], policy.targets)
706 self.assertEqual('groups', policy.get_targets_type())
707 group = policy.get_targets_list()[0]
708 for node in group.get_member_nodes():
709 if node.name == 'my_server_2':
710 '''Test property value'''
711 props = node.get_properties()
712 if props and 'mem_size' in props.keys():
713 self.assertEqual(props['mem_size'].value,
716 def test_node_filter(self):
717 tosca_tpl = os.path.join(
718 os.path.dirname(os.path.abspath(__file__)),
719 "data/test_node_filter.yaml")
720 ToscaTemplate(tosca_tpl)
722 def test_attributes_inheritance(self):
723 tosca_tpl = os.path.join(
724 os.path.dirname(os.path.abspath(__file__)),
725 "data/test_attributes_inheritance.yaml")
726 ToscaTemplate(tosca_tpl)
728 def test_repositories_definition(self):
729 tosca_tpl = os.path.join(
730 os.path.dirname(os.path.abspath(__file__)),
731 "data/test_repositories_definition.yaml")
732 ToscaTemplate(tosca_tpl)
734 def test_custom_caps_def(self):
735 tosca_tpl = os.path.join(
736 os.path.dirname(os.path.abspath(__file__)),
737 "data/test_custom_caps_def.yaml")
738 ToscaTemplate(tosca_tpl)
740 def test_custom_rel_with_script(self):
741 tosca_tpl = os.path.join(
742 os.path.dirname(os.path.abspath(__file__)),
743 "data/test_tosca_custom_rel_with_script.yaml")
744 tosca = ToscaTemplate(tosca_tpl)
745 rel = tosca.relationship_templates[0]
746 self.assertEqual(rel.type, "tosca.relationships.HostedOn")
747 self.assertTrue(rel.is_derived_from("tosca.relationships.Root"))
748 self.assertEqual(len(rel.interfaces), 1)
749 self.assertEqual(rel.interfaces[0].type, "Configure")