From a2cee0260d459e80e2d5ac456439fda0ff06e23d Mon Sep 17 00:00:00 2001 From: Meena Date: Tue, 1 Sep 2015 15:40:07 +0530 Subject: [PATCH] Added occurrences of requirements validation Added validation check for occurrences property value of requirements. Included unit testcases Change-Id: I6e4fc6769464a3973db5b778d0d318ab7525510e Partially Implements: blueprint tosca-requirement-changes --- toscaparser/nodetemplate.py | 21 ++++ toscaparser/tests/test_toscatplvalidation.py | 119 +++++++++++++++++++ 2 files changed, 140 insertions(+) diff --git a/toscaparser/nodetemplate.py b/toscaparser/nodetemplate.py index 8d34473..c8625cc 100644 --- a/toscaparser/nodetemplate.py +++ b/toscaparser/nodetemplate.py @@ -13,8 +13,10 @@ import logging +from toscaparser.common.exception import InvalidPropertyValueError from toscaparser.common.exception import TypeMismatchError from toscaparser.common.exception import UnknownFieldError +from toscaparser.dataentity import DataEntity from toscaparser.elements.interfaces import CONFIGURE from toscaparser.elements.interfaces import CONFIGURE_SHORTNAME from toscaparser.elements.interfaces import InterfacesDef @@ -180,9 +182,28 @@ class NodeTemplate(EntityTemplate): for r1, value in req.items(): if isinstance(value, dict): self._validate_requirements_keys(value) + self._validate_requirements_properties(value) allowed_reqs.append(r1) self._common_validate_field(req, allowed_reqs, 'Requirements') + def _validate_requirements_properties(self, requirements): + # TODO(anyone): Only occurences property of the requirements is + # validated here. Validation of other requirement properties are being + # validated in different files. Better to keep all the requirements + # properties validation here. + for key, value in requirements.items(): + if key == 'occurrences': + self._validate_occurrences(value) + break + + def _validate_occurrences(self, occurrences): + DataEntity.validate_datatype('list', occurrences) + for value in occurrences: + DataEntity.validate_datatype('integer', value) + if len(occurrences) != 2 or not (0 <= occurrences[0] <= occurrences[1]) \ + or occurrences[1] == 0: + raise InvalidPropertyValueError(what=(occurrences)) + def _validate_requirements_keys(self, requirement): for key in requirement.keys(): if key not in self.REQUIREMENTS_SECTION: diff --git a/toscaparser/tests/test_toscatplvalidation.py b/toscaparser/tests/test_toscatplvalidation.py index 5a5be94..e9a5a91 100644 --- a/toscaparser/tests/test_toscatplvalidation.py +++ b/toscaparser/tests/test_toscatplvalidation.py @@ -450,6 +450,33 @@ class ToscaTemplateValidationTest(TestCase): exception.UnknownFieldError, expectedmessage) + def test_node_template_requirements_with_wrong_occurrences_keyname(self): + """Incorrect node template requirements keyname + + Node template requirements keyname 'occurrences' given as + 'occurences'. + """ + tpl_snippet = ''' + node_templates: + mysql_database: + type: tosca.nodes.Database + requirements: + - host: + node: mysql_dbms + - log_endpoint: + node: logstash + capability: log_endpoint + relationship: + type: tosca.relationships.ConnectsTo + occurences: [0, UNBOUNDED] + ''' + expectedmessage = ('Requirements of template mysql_database ' + 'contain(s) unknown field: "occurences", refer' + ' to the definition to verify valid values.') + self._single_node_template_content_test(tpl_snippet, + exception.UnknownFieldError, + expectedmessage) + def test_node_template_requirements_with_multiple_wrong_keynames(self): """Node templates given with multiple wrong requirements keynames.""" tpl_snippet = ''' @@ -494,6 +521,98 @@ class ToscaTemplateValidationTest(TestCase): exception.UnknownFieldError, expectedmessage) + def test_node_template_requirements_invalid_occurrences(self): + tpl_snippet = ''' + node_templates: + server: + type: tosca.nodes.Compute + requirements: + - log_endpoint: + capability: log_endpoint + occurrences: [0, -1] + ''' + expectedmessage = ('Value of property "[0, -1]" is invalid.') + self._single_node_template_content_test( + tpl_snippet, + exception.InvalidPropertyValueError, + expectedmessage) + + tpl_snippet = ''' + node_templates: + server: + type: tosca.nodes.Compute + requirements: + - log_endpoint: + capability: log_endpoint + occurrences: [a, w] + ''' + expectedmessage = ('"a" is not an integer') + self._single_node_template_content_test( + tpl_snippet, + ValueError, + expectedmessage) + + tpl_snippet = ''' + node_templates: + server: + type: tosca.nodes.Compute + requirements: + - log_endpoint: + capability: log_endpoint + occurrences: -1 + ''' + expectedmessage = ('"-1" is not a list') + self._single_node_template_content_test( + tpl_snippet, + ValueError, + expectedmessage) + + tpl_snippet = ''' + node_templates: + server: + type: tosca.nodes.Compute + requirements: + - log_endpoint: + capability: log_endpoint + occurrences: [5, 1] + ''' + expectedmessage = ('Value of property "[5, 1]" is invalid.') + self._single_node_template_content_test( + tpl_snippet, + exception.InvalidPropertyValueError, + expectedmessage) + + tpl_snippet = ''' + node_templates: + server: + type: tosca.nodes.Compute + requirements: + - log_endpoint: + capability: log_endpoint + occurrences: [0, 0] + ''' + expectedmessage = ('Value of property "[0, 0]" is invalid.') + self._single_node_template_content_test( + tpl_snippet, + exception.InvalidPropertyValueError, + expectedmessage) + + def test_node_template_requirements_valid_occurrences(self): + tpl_snippet = ''' + node_templates: + server: + type: tosca.nodes.Compute + requirements: + - log_endpoint: + capability: log_endpoint + occurrences: [2, 2] + ''' + expectedmessage = '' + self._single_node_template_content_test( + tpl_snippet, + None, + expectedmessage) + def test_node_template_capabilities(self): tpl_snippet = ''' node_templates: