From c71c7d37b2a49597dd4a1ec6d9812ed6be642d67 Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Wed, 15 Mar 2017 09:50:34 +0100 Subject: [PATCH] Fix error not considering inheritance in valid_target_types checking in functions Change-Id: Ifcf0ce478450340597fc112a87c367f5b832faca Related-Bug: #1672989 --- toscaparser/elements/capabilitytype.py | 13 ++++++++ toscaparser/functions.py | 7 ++-- .../custom_types/container_cap_child.yaml | 33 +++++++++++++++++++ .../functions/test_container_cap_child.yaml | 28 ++++++++++++++++ toscaparser/tests/test_functions.py | 4 +++ 5 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 toscaparser/tests/data/custom_types/container_cap_child.yaml create mode 100644 toscaparser/tests/data/functions/test_container_cap_child.yaml diff --git a/toscaparser/elements/capabilitytype.py b/toscaparser/elements/capabilitytype.py index 54cd9fed..5fa96615 100644 --- a/toscaparser/elements/capabilitytype.py +++ b/toscaparser/elements/capabilitytype.py @@ -81,3 +81,16 @@ class CapabilityTypeDef(StatefulEntityType): if pnode: return CapabilityTypeDef(self.name, pnode, self.nodetype, self.custom_def) + + def inherits_from(self, type_names): + '''Check this capability is in type_names + + Check if this capability or some of its parent types + are in the list of types: type_names + ''' + if self.type in type_names: + return True + elif self.parent_type: + return self.parent_type.inherits_from(type_names) + else: + return False diff --git a/toscaparser/functions.py b/toscaparser/functions.py index d4982297..777b2cdb 100644 --- a/toscaparser/functions.py +++ b/toscaparser/functions.py @@ -236,8 +236,8 @@ class GetAttribute(Function): target_node = self._find_node_template(target_name) target_type = target_node.type_definition for capability in target_type.get_capabilities_objects(): - if capability.type in \ - hosted_on_rel['valid_target_types']: + if capability.inherits_from( + hosted_on_rel['valid_target_types']): if self._attribute_exists_in_type(target_type): return target_node return self._find_host_containing_attribute( @@ -555,7 +555,8 @@ class GetProperty(Function): target_node = self._find_node_template(target_name) target_type = target_node.type_definition for capability in target_type.get_capabilities_objects(): - if capability.type in hosted_on_rel['valid_target_types']: + if capability.inherits_from( + hosted_on_rel['valid_target_types']): if self._property_exists_in_type(target_type): return target_node return self._find_host_containing_property( diff --git a/toscaparser/tests/data/custom_types/container_cap_child.yaml b/toscaparser/tests/data/custom_types/container_cap_child.yaml new file mode 100644 index 00000000..1df09dd1 --- /dev/null +++ b/toscaparser/tests/data/custom_types/container_cap_child.yaml @@ -0,0 +1,33 @@ +tosca_definitions_version: tosca_simple_yaml_1_0 + +description: > + Define a capability class that inherits from tosca.capabilities.Container + +capability_types: + + tosca.capabilities.ContainerChild: + derived_from: tosca.capabilities.Container + +node_types: + + tosca.nodes.SomeNode: + derived_from: tosca.nodes.Root + properties: + some_prop: + type: string + requirements: + - host_child: + capability: tosca.capabilities.ContainerChild + node: tosca.nodes.SomeNode2 + relationship: tosca.relationships.HostedOn + + tosca.nodes.SomeNode2: + derived_from: tosca.nodes.Root + capabilities: + host_child: + type: tosca.capabilities.ContainerChild + requirements: + - host: + capability: tosca.capabilities.Container + node: tosca.nodes.Compute + relationship: tosca.relationships.HostedOn diff --git a/toscaparser/tests/data/functions/test_container_cap_child.yaml b/toscaparser/tests/data/functions/test_container_cap_child.yaml new file mode 100644 index 00000000..84118c8f --- /dev/null +++ b/toscaparser/tests/data/functions/test_container_cap_child.yaml @@ -0,0 +1,28 @@ +tosca_definitions_version: tosca_simple_yaml_1_0 + +description: > + TOSCA simple profile to test the get attribute function with HOST parameter + using a node that has as capability a child class of Container + +imports: + - ../custom_types/container_cap_child.yaml + +topology_template: + + node_templates: + + test_node: + type: tosca.nodes.SomeNode + properties: + some_prop: { get_attribute: [ HOST, public_address ] } + requirements: + - host_child: test_node2 + + test_node2: + type: tosca.nodes.SomeNode2 + requirements: + - host: server + + server: + type: tosca.nodes.Compute + diff --git a/toscaparser/tests/test_functions.py b/toscaparser/tests/test_functions.py index fa601406..70b374c0 100644 --- a/toscaparser/tests/test_functions.py +++ b/toscaparser/tests/test_functions.py @@ -318,6 +318,10 @@ class GetAttributeTest(TestCase): self.assertIsNotNone(self._load_template( 'functions/test_get_implicit_attribute.yaml')) + def test_get_attribute_capability_inheritance(self): + self.assertIsNotNone(self._load_template( + 'functions/test_container_cap_child.yaml')) + class ConcatTest(TestCase):