Merge "Validation for TOSCA second to below levels in template"
This commit is contained in:
@@ -21,11 +21,9 @@ class EntityType(object):
|
|||||||
'''Base class for TOSCA elements.'''
|
'''Base class for TOSCA elements.'''
|
||||||
|
|
||||||
SECTIONS = (DERIVED_FROM, PROPERTIES, ATTRIBUTES, REQUIREMENTS,
|
SECTIONS = (DERIVED_FROM, PROPERTIES, ATTRIBUTES, REQUIREMENTS,
|
||||||
INTERFACES, CAPABILITIES, RELATIONSHIP, CAPABILITY, TYPE,
|
INTERFACES, CAPABILITIES, TYPE, ARTIFACTS) = \
|
||||||
NODE, OCCURRENCES, ARTIFACTS) = \
|
|
||||||
('derived_from', 'properties', 'attributes', 'requirements',
|
('derived_from', 'properties', 'attributes', 'requirements',
|
||||||
'interfaces', 'capabilities', 'relationship', 'capability',
|
'interfaces', 'capabilities', 'type', 'artifacts')
|
||||||
'type', 'node', 'occurrences', 'artifacts')
|
|
||||||
|
|
||||||
'''TOSCA definition file.'''
|
'''TOSCA definition file.'''
|
||||||
TOSCA_DEF_FILE = os.path.join(
|
TOSCA_DEF_FILE = os.path.join(
|
||||||
|
|||||||
@@ -24,9 +24,14 @@ class EntityTemplate(object):
|
|||||||
'''Base class for TOSCA templates.'''
|
'''Base class for TOSCA templates.'''
|
||||||
|
|
||||||
SECTIONS = (DERIVED_FROM, PROPERTIES, REQUIREMENTS,
|
SECTIONS = (DERIVED_FROM, PROPERTIES, REQUIREMENTS,
|
||||||
INTERFACES, CAPABILITIES, TYPE) = \
|
INTERFACES, CAPABILITIES, TYPE, DESCRIPTION, DIRECTIVES,
|
||||||
|
ATTRIBUTES, ARTIFACTS, NODE_FILTER, COPY) = \
|
||||||
('derived_from', 'properties', 'requirements', 'interfaces',
|
('derived_from', 'properties', 'requirements', 'interfaces',
|
||||||
'capabilities', 'type')
|
'capabilities', 'type', 'description', 'directives',
|
||||||
|
'attributes', 'artifacts', 'node_filter', 'copy')
|
||||||
|
REQUIREMENTS_SECTION = (NODE, CAPABILITY, RELATIONSHIP, OCCURRENCES) = \
|
||||||
|
('node', 'capability', 'relationship',
|
||||||
|
'occurrences')
|
||||||
|
|
||||||
def __init__(self, name, template, entity_name, custom_def=None):
|
def __init__(self, name, template, entity_name, custom_def=None):
|
||||||
self.name = name
|
self.name = name
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ class NodeTemplate(EntityTemplate):
|
|||||||
'node_type',
|
'node_type',
|
||||||
custom_def)
|
custom_def)
|
||||||
self.templates = node_templates
|
self.templates = node_templates
|
||||||
|
self._validate_fields(node_templates[name])
|
||||||
self.custom_def = custom_def
|
self.custom_def = custom_def
|
||||||
self.related = {}
|
self.related = {}
|
||||||
self.relationship_tpl = []
|
self.relationship_tpl = []
|
||||||
@@ -178,9 +179,17 @@ class NodeTemplate(EntityTemplate):
|
|||||||
for req in requires:
|
for req in requires:
|
||||||
for r1, value in req.items():
|
for r1, value in req.items():
|
||||||
if isinstance(value, dict):
|
if isinstance(value, dict):
|
||||||
|
self._validate_requirements_keys(value)
|
||||||
allowed_reqs.append(r1)
|
allowed_reqs.append(r1)
|
||||||
self._common_validate_field(req, allowed_reqs, 'Requirements')
|
self._common_validate_field(req, allowed_reqs, 'Requirements')
|
||||||
|
|
||||||
|
def _validate_requirements_keys(self, requirement):
|
||||||
|
for key in requirement.keys():
|
||||||
|
if key not in self.REQUIREMENTS_SECTION:
|
||||||
|
raise UnknownFieldError(
|
||||||
|
what='Requirements of template %s' % self.name,
|
||||||
|
field=key)
|
||||||
|
|
||||||
def _validate_interfaces(self):
|
def _validate_interfaces(self):
|
||||||
ifaces = self.type_definition.get_value(self.INTERFACES,
|
ifaces = self.type_definition.get_value(self.INTERFACES,
|
||||||
self.entity_tpl)
|
self.entity_tpl)
|
||||||
@@ -201,3 +210,9 @@ class NodeTemplate(EntityTemplate):
|
|||||||
raise UnknownFieldError(
|
raise UnknownFieldError(
|
||||||
what='Interfaces of template %s' % self.name,
|
what='Interfaces of template %s' % self.name,
|
||||||
field=name)
|
field=name)
|
||||||
|
|
||||||
|
def _validate_fields(self, nodetemplate):
|
||||||
|
for name in nodetemplate.keys():
|
||||||
|
if name not in self.SECTIONS:
|
||||||
|
raise UnknownFieldError(what='Node template %s'
|
||||||
|
% self.name, field=name)
|
||||||
|
|||||||
@@ -123,7 +123,7 @@ class ToscaTemplateValidationTest(TestCase):
|
|||||||
def _single_node_template_content_test(self, tpl_snippet, expectederror,
|
def _single_node_template_content_test(self, tpl_snippet, expectederror,
|
||||||
expectedmessage):
|
expectedmessage):
|
||||||
nodetemplates = (translator.toscalib.utils.yamlparser.
|
nodetemplates = (translator.toscalib.utils.yamlparser.
|
||||||
simple_parse(tpl_snippet))['node_templates']
|
simple_ordered_parse(tpl_snippet))['node_templates']
|
||||||
name = list(nodetemplates.keys())[0]
|
name = list(nodetemplates.keys())[0]
|
||||||
try:
|
try:
|
||||||
nodetemplate = NodeTemplate(name, nodetemplates,
|
nodetemplate = NodeTemplate(name, nodetemplates,
|
||||||
@@ -162,6 +162,25 @@ class ToscaTemplateValidationTest(TestCase):
|
|||||||
exception.MissingRequiredFieldError,
|
exception.MissingRequiredFieldError,
|
||||||
expectedmessage)
|
expectedmessage)
|
||||||
|
|
||||||
|
def test_node_template_with_wrong_properties_keyname(self):
|
||||||
|
"""Node template keyname 'properties' given as 'propertiessss'."""
|
||||||
|
tpl_snippet = '''
|
||||||
|
node_templates:
|
||||||
|
mysql_dbms:
|
||||||
|
type: tosca.nodes.DBMS
|
||||||
|
propertiessss:
|
||||||
|
root_password: aaa
|
||||||
|
port: 3376
|
||||||
|
'''
|
||||||
|
expectedmessage = ('Node template mysql_dbms '
|
||||||
|
'contain(s) unknown field: "propertiessss", '
|
||||||
|
'refer to the definition to verify valid values.')
|
||||||
|
self._single_node_template_content_test(tpl_snippet,
|
||||||
|
exception.UnknownFieldError,
|
||||||
|
expectedmessage)
|
||||||
|
|
||||||
|
def test_node_template_with_wrong_requirements_keyname(self):
|
||||||
|
"""Node template keyname 'requirements' given as 'requirement'."""
|
||||||
tpl_snippet = '''
|
tpl_snippet = '''
|
||||||
node_templates:
|
node_templates:
|
||||||
mysql_dbms:
|
mysql_dbms:
|
||||||
@@ -171,23 +190,125 @@ class ToscaTemplateValidationTest(TestCase):
|
|||||||
port: 3376
|
port: 3376
|
||||||
requirement:
|
requirement:
|
||||||
- host: server
|
- host: server
|
||||||
interfaces:
|
|
||||||
Standard:
|
|
||||||
create: mysql_dbms_install.sh
|
|
||||||
start: mysql_dbms_start.sh
|
|
||||||
configure:
|
|
||||||
implementation: mysql_dbms_configure.sh
|
|
||||||
inputs:
|
|
||||||
db_root_password: { get_property: [ mysql_dbms, \
|
|
||||||
dbms_root_password ] }
|
|
||||||
'''
|
'''
|
||||||
expectedmessage = ('Second level of template mysql_dbms '
|
expectedmessage = ('Node template mysql_dbms '
|
||||||
'contain(s) unknown field: "requirement", '
|
'contain(s) unknown field: "requirement", '
|
||||||
'refer to the definition to verify valid values.')
|
'refer to the definition to verify valid values.')
|
||||||
self._single_node_template_content_test(tpl_snippet,
|
self._single_node_template_content_test(tpl_snippet,
|
||||||
exception.UnknownFieldError,
|
exception.UnknownFieldError,
|
||||||
expectedmessage)
|
expectedmessage)
|
||||||
|
|
||||||
|
def test_node_template_with_wrong_interfaces_keyname(self):
|
||||||
|
"""Node template keyname 'interfaces' given as 'interfac'."""
|
||||||
|
tpl_snippet = '''
|
||||||
|
node_templates:
|
||||||
|
mysql_dbms:
|
||||||
|
type: tosca.nodes.DBMS
|
||||||
|
properties:
|
||||||
|
root_password: aaa
|
||||||
|
port: 3376
|
||||||
|
requirements:
|
||||||
|
- host: server
|
||||||
|
interfac:
|
||||||
|
Standard:
|
||||||
|
configure: mysql_database_configure.sh
|
||||||
|
'''
|
||||||
|
expectedmessage = ('Node template mysql_dbms '
|
||||||
|
'contain(s) unknown field: "interfac", '
|
||||||
|
'refer to the definition to verify valid values.')
|
||||||
|
self._single_node_template_content_test(tpl_snippet,
|
||||||
|
exception.UnknownFieldError,
|
||||||
|
expectedmessage)
|
||||||
|
|
||||||
|
def test_node_template_with_wrong_capabilities_keyname(self):
|
||||||
|
"""Node template keyname 'capabilities' given as 'capabilitiis'."""
|
||||||
|
tpl_snippet = '''
|
||||||
|
node_templates:
|
||||||
|
mysql_database:
|
||||||
|
type: tosca.nodes.Database
|
||||||
|
properties:
|
||||||
|
db_name: { get_input: db_name }
|
||||||
|
db_user: { get_input: db_user }
|
||||||
|
db_password: { get_input: db_pwd }
|
||||||
|
capabilitiis:
|
||||||
|
database_endpoint:
|
||||||
|
properties:
|
||||||
|
port: { get_input: db_port }
|
||||||
|
'''
|
||||||
|
expectedmessage = ('Node template mysql_database '
|
||||||
|
'contain(s) unknown field: "capabilitiis", '
|
||||||
|
'refer to the definition to verify valid values.')
|
||||||
|
self._single_node_template_content_test(tpl_snippet,
|
||||||
|
exception.UnknownFieldError,
|
||||||
|
expectedmessage)
|
||||||
|
|
||||||
|
def test_node_template_with_wrong_artifacts_keyname(self):
|
||||||
|
"""Node template keyname 'artifacts' given as 'artifactsss'."""
|
||||||
|
tpl_snippet = '''
|
||||||
|
node_templates:
|
||||||
|
mysql_database:
|
||||||
|
type: tosca.nodes.Database
|
||||||
|
artifactsss:
|
||||||
|
db_content:
|
||||||
|
implementation: files/my_db_content.txt
|
||||||
|
type: tosca.artifacts.File
|
||||||
|
'''
|
||||||
|
expectedmessage = ('Node template mysql_database '
|
||||||
|
'contain(s) unknown field: "artifactsss", '
|
||||||
|
'refer to the definition to verify valid values.')
|
||||||
|
self._single_node_template_content_test(tpl_snippet,
|
||||||
|
exception.UnknownFieldError,
|
||||||
|
expectedmessage)
|
||||||
|
|
||||||
|
def test_node_template_with_multiple_wrong_keynames(self):
|
||||||
|
"""Node templates given with multiple wrong keynames."""
|
||||||
|
tpl_snippet = '''
|
||||||
|
node_templates:
|
||||||
|
mysql_dbms:
|
||||||
|
type: tosca.nodes.DBMS
|
||||||
|
propertieees:
|
||||||
|
root_password: aaa
|
||||||
|
port: 3376
|
||||||
|
requirements:
|
||||||
|
- host: server
|
||||||
|
interfacs:
|
||||||
|
Standard:
|
||||||
|
configure: mysql_database_configure.sh
|
||||||
|
'''
|
||||||
|
expectedmessage = ('Node template mysql_dbms '
|
||||||
|
'contain(s) unknown field: "propertieees", '
|
||||||
|
'refer to the definition to verify valid values.')
|
||||||
|
self._single_node_template_content_test(tpl_snippet,
|
||||||
|
exception.UnknownFieldError,
|
||||||
|
expectedmessage)
|
||||||
|
|
||||||
|
tpl_snippet = '''
|
||||||
|
node_templates:
|
||||||
|
mysql_database:
|
||||||
|
type: tosca.nodes.Database
|
||||||
|
properties:
|
||||||
|
name: { get_input: db_name }
|
||||||
|
user: { get_input: db_user }
|
||||||
|
password: { get_input: db_pwd }
|
||||||
|
capabilitiiiies:
|
||||||
|
database_endpoint:
|
||||||
|
properties:
|
||||||
|
port: { get_input: db_port }
|
||||||
|
requirementsss:
|
||||||
|
- host:
|
||||||
|
node: mysql_dbms
|
||||||
|
interfac:
|
||||||
|
Standard:
|
||||||
|
configure: mysql_database_configure.sh
|
||||||
|
|
||||||
|
'''
|
||||||
|
expectedmessage = ('Node template mysql_database '
|
||||||
|
'contain(s) unknown field: "capabilitiiiies", '
|
||||||
|
'refer to the definition to verify valid values.')
|
||||||
|
self._single_node_template_content_test(tpl_snippet,
|
||||||
|
exception.UnknownFieldError,
|
||||||
|
expectedmessage)
|
||||||
|
|
||||||
def test_node_template_type(self):
|
def test_node_template_type(self):
|
||||||
tpl_snippet = '''
|
tpl_snippet = '''
|
||||||
node_templates:
|
node_templates:
|
||||||
@@ -257,6 +378,122 @@ class ToscaTemplateValidationTest(TestCase):
|
|||||||
exception.UnknownFieldError,
|
exception.UnknownFieldError,
|
||||||
expectedmessage)
|
expectedmessage)
|
||||||
|
|
||||||
|
def test_node_template_requirements_with_wrong_node_keyname(self):
|
||||||
|
"""Node template requirements keyname 'node' given as 'nodes'."""
|
||||||
|
tpl_snippet = '''
|
||||||
|
node_templates:
|
||||||
|
mysql_database:
|
||||||
|
type: tosca.nodes.Database
|
||||||
|
requirements:
|
||||||
|
- host:
|
||||||
|
nodes: mysql_dbms
|
||||||
|
|
||||||
|
'''
|
||||||
|
expectedmessage = ('Requirements of template mysql_database '
|
||||||
|
'contain(s) unknown field: "nodes", 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_wrong_capability_keyname(self):
|
||||||
|
"""Incorrect node template requirements keyname
|
||||||
|
|
||||||
|
Node template requirements keyname 'capability' given as
|
||||||
|
'capabilityy'.
|
||||||
|
"""
|
||||||
|
tpl_snippet = '''
|
||||||
|
node_templates:
|
||||||
|
mysql_database:
|
||||||
|
type: tosca.nodes.Database
|
||||||
|
requirements:
|
||||||
|
- host:
|
||||||
|
node: mysql_dbms
|
||||||
|
- log_endpoint:
|
||||||
|
node: logstash
|
||||||
|
capabilityy: log_endpoint
|
||||||
|
relationship:
|
||||||
|
type: tosca.relationships.ConnectsTo
|
||||||
|
|
||||||
|
'''
|
||||||
|
expectedmessage = ('Requirements of template mysql_database '
|
||||||
|
'contain(s) unknown field: "capabilityy", 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_wrong_relationship_keyname(self):
|
||||||
|
"""Incorrect node template requirements keyname
|
||||||
|
|
||||||
|
Node template requirements keyname 'relationship' given as
|
||||||
|
'relationshipppp'.
|
||||||
|
"""
|
||||||
|
tpl_snippet = '''
|
||||||
|
node_templates:
|
||||||
|
mysql_database:
|
||||||
|
type: tosca.nodes.Database
|
||||||
|
requirements:
|
||||||
|
- host:
|
||||||
|
node: mysql_dbms
|
||||||
|
- log_endpoint:
|
||||||
|
node: logstash
|
||||||
|
capability: log_endpoint
|
||||||
|
relationshipppp:
|
||||||
|
type: tosca.relationships.ConnectsTo
|
||||||
|
|
||||||
|
'''
|
||||||
|
expectedmessage = ('Requirements of template mysql_database '
|
||||||
|
'contain(s) unknown field: "relationshipppp", 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 = '''
|
||||||
|
node_templates:
|
||||||
|
mysql_database:
|
||||||
|
type: tosca.nodes.Database
|
||||||
|
requirements:
|
||||||
|
- host:
|
||||||
|
node: mysql_dbms
|
||||||
|
- log_endpoint:
|
||||||
|
nod: logstash
|
||||||
|
capabilit: log_endpoint
|
||||||
|
relationshipppp:
|
||||||
|
type: tosca.relationships.ConnectsTo
|
||||||
|
|
||||||
|
'''
|
||||||
|
expectedmessage = ('Requirements of template mysql_database '
|
||||||
|
'contain(s) unknown field: "nod", refer'
|
||||||
|
' to the definition to verify valid values.')
|
||||||
|
self._single_node_template_content_test(tpl_snippet,
|
||||||
|
exception.UnknownFieldError,
|
||||||
|
expectedmessage)
|
||||||
|
|
||||||
|
tpl_snippet = '''
|
||||||
|
node_templates:
|
||||||
|
mysql_database:
|
||||||
|
type: tosca.nodes.Database
|
||||||
|
requirements:
|
||||||
|
- host:
|
||||||
|
node: mysql_dbms
|
||||||
|
- log_endpoint:
|
||||||
|
node: logstash
|
||||||
|
capabilit: log_endpoint
|
||||||
|
relationshipppp:
|
||||||
|
type: tosca.relationships.ConnectsTo
|
||||||
|
|
||||||
|
'''
|
||||||
|
expectedmessage = ('Requirements of template mysql_database '
|
||||||
|
'contain(s) unknown field: "capabilit", refer'
|
||||||
|
' to the definition to verify valid values.')
|
||||||
|
self._single_node_template_content_test(tpl_snippet,
|
||||||
|
exception.UnknownFieldError,
|
||||||
|
expectedmessage)
|
||||||
|
|
||||||
def test_node_template_capabilities(self):
|
def test_node_template_capabilities(self):
|
||||||
tpl_snippet = '''
|
tpl_snippet = '''
|
||||||
node_templates:
|
node_templates:
|
||||||
|
|||||||
Reference in New Issue
Block a user