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
This commit is contained in:
Vahid Hashemian 2015-09-02 12:15:57 -07:00
parent bc5705240e
commit e7aec22e54
4 changed files with 53 additions and 1 deletions

@ -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

@ -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))

@ -54,6 +54,8 @@ resources:
get_resource: my_server
volume_id:
get_resource: my_storage
mountpoint:
get_param: storage_location
outputs:
private_ip:

@ -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()))