Checks the type of the TopologyTemplate fields

Change-Id: Ia9140e45264e10dbd57759c6769ae41b2724d9ec
Related-Bug: #1835505
This commit is contained in:
Miguel Caballer 2019-07-05 13:53:22 +02:00
parent 048137e0b1
commit 076f09508d
2 changed files with 137 additions and 7 deletions

View File

@ -281,3 +281,103 @@ class TopologyTemplateTest(TestCase):
lambda: ToscaTemplate(tpl_path))
exception.ExceptionCollector.assertExceptionMessage(
KeyError, errormsg)
def test_invalid_type_policies(self):
tpl_snippet = '''
policies:
some_policy:
type: tosca.policies.Placement
'''
policies = (toscaparser.utils.yamlparser.simple_parse(tpl_snippet))
custom_defs = self._get_custom_types()
err = self.assertRaises(exception.TypeMismatchError,
lambda: TopologyTemplate(policies,
custom_defs))
errormsg = _('policies must be of type "list".')
self.assertEqual(errormsg, err.__str__())
def test_invalid_type_groups(self):
tpl_snippet = '''
groups:
- some_group:
type: tosca.groups.Root
'''
policies = (toscaparser.utils.yamlparser.simple_parse(tpl_snippet))
custom_defs = self._get_custom_types()
err = self.assertRaises(exception.TypeMismatchError,
lambda: TopologyTemplate(policies,
custom_defs))
errormsg = _('groups must be of type "dict".')
self.assertEqual(errormsg, err.__str__())
def test_invalid_type_substitution_mappings(self):
tpl_snippet = '''
substitution_mappings:
- node_type: MyService
properties:
num_cpus: cpus
'''
policies = (toscaparser.utils.yamlparser.simple_parse(tpl_snippet))
custom_defs = self._get_custom_types()
err = self.assertRaises(exception.TypeMismatchError,
lambda: TopologyTemplate(policies,
custom_defs))
errormsg = _('substitution_mappings must be of type "dict".')
self.assertEqual(errormsg, err.__str__())
def test_invalid_type_outputs(self):
tpl_snippet = '''
outputs:
- some_output:
value: some_value
'''
policies = (toscaparser.utils.yamlparser.simple_parse(tpl_snippet))
custom_defs = self._get_custom_types()
err = self.assertRaises(exception.TypeMismatchError,
lambda: TopologyTemplate(policies,
custom_defs))
errormsg = _('outputs must be of type "dict".')
self.assertEqual(errormsg, err.__str__())
def test_invalid_type_relationship_templates(self):
tpl_snippet = '''
relationship_templates:
- my_connection:
type: ConnectsTo
'''
policies = (toscaparser.utils.yamlparser.simple_parse(tpl_snippet))
custom_defs = self._get_custom_types()
err = self.assertRaises(exception.TypeMismatchError,
lambda: TopologyTemplate(policies,
custom_defs))
errormsg = _('relationship_templates must be of type "dict".')
self.assertEqual(errormsg, err.__str__())
def test_invalid_type_nodetemplates(self):
tpl_snippet = '''
node_templates:
- some_node:
type: tosca.nodes.Compute
'''
policies = (toscaparser.utils.yamlparser.simple_parse(tpl_snippet))
custom_defs = self._get_custom_types()
err = self.assertRaises(exception.TypeMismatchError,
lambda: TopologyTemplate(policies,
custom_defs))
errormsg = _('node_templates must be of type "dict".')
self.assertEqual(errormsg, err.__str__())
def test_invalid_type_inputs(self):
tpl_snippet = '''
inputs:
- some_input:
type: integer
value: 1
'''
policies = (toscaparser.utils.yamlparser.simple_parse(tpl_snippet))
custom_defs = self._get_custom_types()
err = self.assertRaises(exception.TypeMismatchError,
lambda: TopologyTemplate(policies,
custom_defs))
errormsg = _('inputs must be of type "dict".')
self.assertEqual(errormsg, err.__str__())

View File

@ -212,25 +212,55 @@ class TopologyTemplate(object):
return description.rstrip()
def _tpl_inputs(self):
return self.tpl.get(INPUTS) or {}
inputs = self.tpl.get(INPUTS) or {}
if not isinstance(inputs, dict):
exception.ExceptionCollector.appendException(
exception.TypeMismatchError(what=INPUTS, type="dict"))
return inputs
def _tpl_nodetemplates(self):
return self.tpl.get(NODE_TEMPLATES)
nodetemplates = self.tpl.get(NODE_TEMPLATES)
if nodetemplates and not isinstance(nodetemplates, dict):
exception.ExceptionCollector.appendException(
exception.TypeMismatchError(what=NODE_TEMPLATES, type="dict"))
return nodetemplates
def _tpl_relationship_templates(self):
return self.tpl.get(RELATIONSHIP_TEMPLATES) or {}
relationship_templates = self.tpl.get(RELATIONSHIP_TEMPLATES) or {}
if not isinstance(relationship_templates, dict):
exception.ExceptionCollector.appendException(
exception.TypeMismatchError(what=RELATIONSHIP_TEMPLATES,
type="dict"))
return relationship_templates
def _tpl_outputs(self):
return self.tpl.get(OUTPUTS) or {}
outputs = self.tpl.get(OUTPUTS) or {}
if not isinstance(outputs, dict):
exception.ExceptionCollector.appendException(
exception.TypeMismatchError(what=OUTPUTS, type="dict"))
return outputs
def _tpl_substitution_mappings(self):
return self.tpl.get(SUBSTITUION_MAPPINGS) or {}
substitution_mappings = self.tpl.get(SUBSTITUION_MAPPINGS) or {}
if not isinstance(substitution_mappings, dict):
exception.ExceptionCollector.appendException(
exception.TypeMismatchError(what=SUBSTITUION_MAPPINGS,
type="dict"))
return substitution_mappings
def _tpl_groups(self):
return self.tpl.get(GROUPS) or {}
groups = self.tpl.get(GROUPS) or {}
if not isinstance(groups, dict):
exception.ExceptionCollector.appendException(
exception.TypeMismatchError(what=GROUPS, type="dict"))
return groups
def _tpl_policies(self):
return self.tpl.get(POLICIES) or {}
policies = self.tpl.get(POLICIES) or []
if not isinstance(policies, list):
exception.ExceptionCollector.appendException(
exception.TypeMismatchError(what=POLICIES, type="list"))
return policies
def _validate_field(self):
for name in self.tpl: