diff --git a/toscaparser/elements/policytype.py b/toscaparser/elements/policytype.py index 82aed0aa..0061e0bf 100644 --- a/toscaparser/elements/policytype.py +++ b/toscaparser/elements/policytype.py @@ -60,8 +60,12 @@ class PolicyType(StatefulEntityType): parent_policy = self.parent_type.type if self.parent_type else None if parent_policy: while parent_policy != 'tosca.policies.Root': - policies[parent_policy] = self.TOSCA_DEF[parent_policy] - parent_policy = policies[parent_policy]['derived_from'] + if parent_policy in self.TOSCA_DEF: + policies[parent_policy] = self.TOSCA_DEF[parent_policy] + parent_policy = policies[parent_policy]['derived_from'] + elif self.custom_def and parent_policy in self.custom_def: + policies[parent_policy] = self.custom_def[parent_policy] + parent_policy = policies[parent_policy]['derived_from'] return policies @property diff --git a/toscaparser/elements/tosca_type_validation.py b/toscaparser/elements/tosca_type_validation.py index fe5b4442..2ae14baa 100644 --- a/toscaparser/elements/tosca_type_validation.py +++ b/toscaparser/elements/tosca_type_validation.py @@ -57,4 +57,4 @@ class TypeValidation(object): ExceptionCollector.appendException( InvalidTemplateVersion( what=version + ' in ' + str(self.import_def), - valid_versions=', '. join(self.VALID_TEMPLATE_VERSIONS))) + valid_versions='", "'. join(self.VALID_TEMPLATE_VERSIONS))) diff --git a/toscaparser/extensions/exttools.py b/toscaparser/extensions/exttools.py index 2efed353..96da4b22 100644 --- a/toscaparser/extensions/exttools.py +++ b/toscaparser/extensions/exttools.py @@ -71,7 +71,7 @@ class ExtTools(object): return extensions def get_versions(self): - return self.EXTENSION_INFO.keys() + return sorted(self.EXTENSION_INFO.keys()) def get_sections(self): sections = {} diff --git a/toscaparser/tests/data/policies/custom_definitions.yaml b/toscaparser/tests/data/policies/custom_definitions.yaml index 7f15ade5..b5b0c336 100644 --- a/toscaparser/tests/data/policies/custom_definitions.yaml +++ b/toscaparser/tests/data/policies/custom_definitions.yaml @@ -8,3 +8,9 @@ policy_types: entry_schema: type: string + tosca.policies.Adva.Failure: + derived_from: tosca.policies.Root + + tosca.policies.Adva.Failure.Restart: + derived_from: tosca.policies.Adva.Failure + diff --git a/toscaparser/tests/data/policies/tosca_custom_policy_template.yaml b/toscaparser/tests/data/policies/tosca_custom_policy_template.yaml new file mode 100644 index 00000000..fb315367 --- /dev/null +++ b/toscaparser/tests/data/policies/tosca_custom_policy_template.yaml @@ -0,0 +1,51 @@ +tosca_definitions_version: tosca_simple_yaml_1_0 + +description: > + Template for deploying servers based on policies. + +imports: + - custom_definitions.yaml + +topology_template: + node_templates: + my_server_1: + 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 + + my_server_2: + type: tosca.nodes.Compute + capabilities: + host: + properties: + disk_size: 10 GB + num_cpus: 2 + mem_size: 4096 MB + os: + properties: + architecture: x86_64 + type: Linux + distribution: Ubuntu + version: 14.04 + + + policies: + - My_failure_policy_restart: + type: tosca.policies.Adva.Failure.Restart + description: respawn the VDU + targets: [my_server_1] + + diff --git a/toscaparser/tests/test_toscatpl.py b/toscaparser/tests/test_toscatpl.py index ea444e9d..512f3355 100644 --- a/toscaparser/tests/test_toscatpl.py +++ b/toscaparser/tests/test_toscatpl.py @@ -540,7 +540,7 @@ class ToscaTemplateTest(TestCase): "data/test_multiple_validation_errors.yaml") self.assertRaises(exception.ValidationError, ToscaTemplate, tosca_tpl, None) - valid_versions = ', '.join(ToscaTemplate.VALID_TEMPLATE_VERSIONS) + valid_versions = '", "'.join(ToscaTemplate.VALID_TEMPLATE_VERSIONS) err1_msg = (_('The template version "tosca_simple_yaml_1" is invalid. ' 'Valid versions are "%s".') % valid_versions) exception.ExceptionCollector.assertExceptionMessage( @@ -758,6 +758,48 @@ class ToscaTemplateTest(TestCase): if props and 'mem_size' in props.keys(): self.assertEqual(props['mem_size'].value, '4096 MB') + # Test the following: + # check the inheritance between custom policies. + # It will first parse the tosca template located at + # data/policies/tosca_custom_policy_template.yaml where + # two empty customs policies have been created. The child + # empty custom policy tosca.policies.Adva.Failure.Restart + # is derived from its parent empty custom policy + # tosca.policies.Adva.Failure which is also derived + # from its parent empty policy tosca.policies.Root. + + def test_policies_for_custom(self): + host_prop = {} + tosca_tpl = os.path.join( + os.path.dirname(os.path.abspath(__file__)), + "data/policies/tosca_custom_policy_template.yaml") + tosca = ToscaTemplate(tosca_tpl) + + for policy in tosca.topology_template.policies: + self.assertTrue( + policy.is_derived_from("tosca.policies.Root")) + if policy.name == 'My_failure_policy_restart': + self.assertEqual('tosca.policies.Adva.Failure.Restart', + policy.type) + targets = policy.targets + for target in targets: + if ('my_server_1' == target): + '''Test property value''' + for nodetemplate in tosca.nodetemplates: + if nodetemplate.name == target: + caps = nodetemplate.get_capabilities() + for cap in caps.keys(): + generic_cap = \ + nodetemplate.get_capability(cap) + if generic_cap: + for prop in \ + generic_cap.\ + get_properties_objects(): + host_prop[prop.name] = prop.value + if cap == 'host': + self.assertEqual(host_prop + ['mem_size'], + '512 MB') def test_node_filter(self): tosca_tpl = os.path.join( diff --git a/toscaparser/tests/test_toscatplvalidation.py b/toscaparser/tests/test_toscatplvalidation.py index 288a84e8..d4bf4f0f 100644 --- a/toscaparser/tests/test_toscatplvalidation.py +++ b/toscaparser/tests/test_toscatplvalidation.py @@ -96,13 +96,12 @@ class ToscaTemplateValidationTest(TestCase): _('Template custom_types/imported_sample.yaml contains unknown ' 'field "tosca1_definitions_version". Refer to the definition' ' to verify valid values.')) + versions = '", "'.join(ToscaTemplate.VALID_TEMPLATE_VERSIONS) exception.ExceptionCollector.assertExceptionMessage( exception.InvalidTemplateVersion, _('The template version "tosca_simple_yaml_1_10 in ' 'custom_types/imported_sample.yaml" is invalid. ' - 'Valid versions are "tosca_simple_yaml_1_0, ' - 'tosca_simple_profile_for_nfv_1_0_0, ' - 'tosca_simple_profile_for_mec_1_0_0".')) + 'Valid versions are "%s".') % versions) exception.ExceptionCollector.assertExceptionMessage( exception.UnknownFieldError, _('Template custom_types/imported_sample.yaml contains unknown ' @@ -1436,7 +1435,7 @@ heat-translator/master/translator/tests/data/custom_types/wordpress.yaml os.path.dirname(os.path.abspath(__file__)), "data/test_invalid_template_version.yaml") self.assertRaises(exception.ValidationError, ToscaTemplate, tosca_tpl) - valid_versions = ', '.join(ToscaTemplate.VALID_TEMPLATE_VERSIONS) + valid_versions = '", "'.join(ToscaTemplate.VALID_TEMPLATE_VERSIONS) exception.ExceptionCollector.assertExceptionMessage( exception.InvalidTemplateVersion, (_('The template version "tosca_xyz" is invalid. Valid versions ' @@ -1447,7 +1446,7 @@ heat-translator/master/translator/tests/data/custom_types/wordpress.yaml os.path.dirname(os.path.abspath(__file__)), "data/test_import_invalid_template_version.yaml") self.assertRaises(exception.ValidationError, ToscaTemplate, tosca_tpl) - valid_versions = ', '.join(ToscaTemplate.VALID_TEMPLATE_VERSIONS) + valid_versions = '", "'.join(ToscaTemplate.VALID_TEMPLATE_VERSIONS) exception.ExceptionCollector.assertExceptionMessage( exception.InvalidTemplateVersion, (_('The template version "tosca_simple_yaml_XXX in ' diff --git a/toscaparser/tosca_template.py b/toscaparser/tosca_template.py index a1e338a9..4f4dc013 100644 --- a/toscaparser/tosca_template.py +++ b/toscaparser/tosca_template.py @@ -267,7 +267,7 @@ class ToscaTemplate(object): ExceptionCollector.appendException( InvalidTemplateVersion( what=version, - valid_versions=', '. join(self.VALID_TEMPLATE_VERSIONS))) + valid_versions='", "'. join(self.VALID_TEMPLATE_VERSIONS))) else: if version != 'tosca_simple_yaml_1_0': update_definitions(version)