From 46141f95d217a993b8836af2321adf9563e31a73 Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Wed, 16 Mar 2022 08:52:02 +0100 Subject: [PATCH] Fix KeyError in get_attribute function Fix KeyError is raised in get_attribute validation in case of nested data types. The data type schema was not correctly managed. Closes-Bug: 1965089 Change-Id: Ic87a3c429105d6108507de05a31b788538ee2259 --- toscaparser/functions.py | 6 ++-- .../custom_nested_data_types.yaml | 29 +++++++++++++++ .../test_get_attribute_nested_data_types.yaml | 36 +++++++++++++++++++ toscaparser/tests/test_functions.py | 4 +++ 4 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 toscaparser/tests/data/custom_types/custom_nested_data_types.yaml create mode 100644 toscaparser/tests/data/functions/test_get_attribute_nested_data_types.yaml diff --git a/toscaparser/functions.py b/toscaparser/functions.py index 1495e05d..4d1ba4b5 100644 --- a/toscaparser/functions.py +++ b/toscaparser/functions.py @@ -159,6 +159,7 @@ class GetAttribute(Function): return value_type = attr.schema['type'] + value_schema = attr.schema if len(self.args) > index: for elem in self.args[index:]: if value_type == "list": @@ -168,9 +169,9 @@ class GetAttribute(Function): ' "{0}". "{1}" Expected positive' ' integer argument' ).format(GET_ATTRIBUTE, elem))) - value_type = attr.schema['entry_schema']['type'] + value_type = value_schema['entry_schema']['type'] elif value_type == "map": - value_type = attr.schema['entry_schema']['type'] + value_type = value_schema['entry_schema']['type'] elif value_type in Schema.PROPERTY_TYPES: ExceptionCollector.appendException( ValueError(_('Illegal arguments for function' @@ -186,6 +187,7 @@ class GetAttribute(Function): if found: prop = found[0] value_type = prop.schema['type'] + value_schema = prop.schema else: ExceptionCollector.appendException( KeyError(_('Illegal arguments for function' diff --git a/toscaparser/tests/data/custom_types/custom_nested_data_types.yaml b/toscaparser/tests/data/custom_types/custom_nested_data_types.yaml new file mode 100644 index 00000000..fd06d720 --- /dev/null +++ b/toscaparser/tests/data/custom_types/custom_nested_data_types.yaml @@ -0,0 +1,29 @@ +tosca_definitions_version: tosca_simple_yaml_1_0 + +data_types: + + tosca.datatypes.SomeTask: + derived_from: tosca.datatypes.Root + properties: + state: + type: string + output: + type: string + + tosca.datatypes.OtherTask: + derived_from: tosca.datatypes.Root + properties: + tasks: + type: map + entry_schema: + type: tosca.datatypes.SomeTask + +node_types: + + tosca.nodes.SomeApp: + derived_from: tosca.nodes.SoftwareComponent + attributes: + some_new_att: + type: map + entry_schema: + type: tosca.datatypes.OtherTask diff --git a/toscaparser/tests/data/functions/test_get_attribute_nested_data_types.yaml b/toscaparser/tests/data/functions/test_get_attribute_nested_data_types.yaml new file mode 100644 index 00000000..9073c782 --- /dev/null +++ b/toscaparser/tests/data/functions/test_get_attribute_nested_data_types.yaml @@ -0,0 +1,36 @@ +tosca_definitions_version: tosca_simple_yaml_1_0 + +description: Template for testing get_attribute with a custom data type + +imports: + - ../custom_types/custom_nested_data_types.yaml + +topology_template: + + node_templates: + + some_app: + type: tosca.nodes.SomeApp + requirements: + - host: server + + server: + type: tosca.nodes.Compute + capabilities: + # Host container properties + host: + properties: + num_cpus: 2 + disk_size: 10 GB + mem_size: 512 MB + # Guest Operating System properties + os: + properties: + # host Operating System image properties + architecture: x86_64 + type: Linux + distribution: RHEL + version: 6.5 + outputs: + url: + value: { get_attribute: [ some_app, some_new_att, map_value, tasks, other_map, output ]} diff --git a/toscaparser/tests/test_functions.py b/toscaparser/tests/test_functions.py index e3bdab47..2a382705 100644 --- a/toscaparser/tests/test_functions.py +++ b/toscaparser/tests/test_functions.py @@ -346,6 +346,10 @@ class GetAttributeTest(TestCase): self.assertIsNotNone(self._load_template( 'functions/test_get_attribute_custom_data_type.yaml')) + def test_get_attribute_neste_data_types(self): + self.assertIsNotNone(self._load_template( + 'functions/test_get_attribute_nested_data_types.yaml')) + class ConcatTest(TestCase):