From e7aec22e5499cb9742e79aaa1c04b04f097e59a9 Mon Sep 17 00:00:00 2001 From: Vahid Hashemian Date: Wed, 2 Sep 2015 12:15:57 -0700 Subject: [PATCH] Fix property inheritance for node templates Fix issues with property inheritance that cause a derived node not inherit all its parent(s) property definitions. Add proper unit tests as well. Change-Id: Ia27af7e3fef742ccf71beecfc47e56483e9ebc37 Closes-Bug: #1483896 --- toscaparser/elements/entitytype.py | 17 ++++++++++ toscaparser/elements/statefulentitytype.py | 2 +- ...storage_with_custom_relationship_type.yaml | 2 ++ toscaparser/tests/test_properties.py | 33 +++++++++++++++++++ 4 files changed, 53 insertions(+), 1 deletion(-) diff --git a/toscaparser/elements/entitytype.py b/toscaparser/elements/entitytype.py index ea4c987..d4fada3 100644 --- a/toscaparser/elements/entitytype.py +++ b/toscaparser/elements/entitytype.py @@ -93,3 +93,20 @@ class EntityType(object): value = p.get_value(ndtype) p = p.parent_type return value + + def get_definition(self, ndtype): + value = None + defs = self.defs + if ndtype in defs: + value = defs[ndtype] + p = self.parent_type + if p: + inherited = p.get_definition(ndtype) + if inherited: + inherited = dict(inherited) + if not value: + value = inherited + else: + inherited.update(value) + value.update(inherited) + return value diff --git a/toscaparser/elements/statefulentitytype.py b/toscaparser/elements/statefulentitytype.py index 0425b6d..a5cdc18 100644 --- a/toscaparser/elements/statefulentitytype.py +++ b/toscaparser/elements/statefulentitytype.py @@ -44,7 +44,7 @@ class StatefulEntityType(EntityType): def get_properties_def_objects(self): '''Return a list of property definition objects.''' properties = [] - props = self.get_value(self.PROPERTIES) + props = self.get_definition(self.PROPERTIES) if props: for prop, schema in props.items(): properties.append(PropertyDef(prop, None, schema)) diff --git a/toscaparser/tests/data/hot_output/storage/hot_blockstorage_with_custom_relationship_type.yaml b/toscaparser/tests/data/hot_output/storage/hot_blockstorage_with_custom_relationship_type.yaml index bc1bcc1..9c92770 100644 --- a/toscaparser/tests/data/hot_output/storage/hot_blockstorage_with_custom_relationship_type.yaml +++ b/toscaparser/tests/data/hot_output/storage/hot_blockstorage_with_custom_relationship_type.yaml @@ -54,6 +54,8 @@ resources: get_resource: my_server volume_id: get_resource: my_storage + mountpoint: + get_param: storage_location outputs: private_ip: diff --git a/toscaparser/tests/test_properties.py b/toscaparser/tests/test_properties.py index ec7dfcb..8e3ba9d 100644 --- a/toscaparser/tests/test_properties.py +++ b/toscaparser/tests/test_properties.py @@ -188,3 +188,36 @@ class PropertyTest(TestCase): propertyInstance = Property('test_property', 'Foo', test_property_schema) self.assertEqual(True, propertyInstance.required) + + def test_proprety_inheritance(self): + from toscaparser.nodetemplate import NodeTemplate + + tosca_custom_def = ''' + tosca.nodes.SoftwareComponent.MySoftware: + derived_from: SoftwareComponent + properties: + install_path: + required: false + type: string + default: /opt/mysoftware + ''' + + tosca_node_template = ''' + node_templates: + mysoftware_instance: + type: tosca.nodes.SoftwareComponent.MySoftware + properties: + component_version: 3.1 + ''' + + expected_properties = ['component_version', + 'install_path'] + + nodetemplates = yamlparser.\ + simple_parse(tosca_node_template)['node_templates'] + custom_def = yamlparser.simple_parse(tosca_custom_def) + name = list(nodetemplates.keys())[0] + tpl = NodeTemplate(name, nodetemplates, custom_def) + self.assertIsNone(tpl.validate()) + self.assertEqual(expected_properties, + sorted(tpl.get_properties().keys()))