Merge "Represent memory unit in string"

This commit is contained in:
Jenkins 2014-10-21 08:05:49 +00:00 committed by Gerrit Code Review
commit f28903447a
8 changed files with 223 additions and 12 deletions

View File

@ -81,3 +81,7 @@ class ValidationError(TOSCAException):
class UnknownInputError(TOSCAException): class UnknownInputError(TOSCAException):
msg_fmt = _('Unknown input: %(input_name)s') msg_fmt = _('Unknown input: %(input_name)s')
class InvalidPropertyValueError(TOSCAException):
msg_fmt = _('Value of property "%(what)s" is invalid.')

View File

@ -54,6 +54,7 @@ tosca.nodes.Compute:
description: > description: >
Size of memory, in Megabytes (MB), available to applications running Size of memory, in Megabytes (MB), available to applications running
on the Compute node. on the Compute node.
default: 1024
os_arch: os_arch:
required: no required: no
default: x86_64 default: x86_64

View File

@ -26,3 +26,11 @@ class PropertyDef(object):
if prop_key == 'required' and prop_vale: if prop_key == 'required' and prop_vale:
return True return True
return False return False
@property
def default(self):
if self.schema:
for prop_key, prop_value in self.schema.items():
if prop_key == 'default':
return prop_value
return None

View File

@ -78,7 +78,7 @@ class EntityTemplate(object):
self.entity_tpl) self.entity_tpl)
if caps: if caps:
for name, value in caps.items(): for name, value in caps.items():
for prop, val in value.items(): for val in value.values():
properties = val properties = val
for c in self.type_definition.capabilities: for c in self.type_definition.capabilities:
if c.name == name: if c.name == name:
@ -147,13 +147,16 @@ class EntityTemplate(object):
def _create_properties(self): def _create_properties(self):
props = [] props = []
properties = self.type_definition.get_value(self.PROPERTIES, properties = self.type_definition.get_value(self.PROPERTIES,
self.entity_tpl) self.entity_tpl) or {}
if properties:
for name, value in properties.items(): for name, value in properties.items():
for p in self.type_definition.properties_def: for p in self.type_definition.properties_def:
if p.name == name: if p.name == name:
prop = Property(name, value, p.schema) prop = Property(name, value, p.schema)
props.append(prop) props.append(prop)
for p in self.type_definition.properties_def:
if p.default is not None and p.name not in properties.keys():
prop = Property(p.name, p.default, p.schema)
props.append(prop)
return props return props
def _create_interfaces(self): def _create_interfaces(self):

View File

@ -10,6 +10,9 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import re
from translator.toscalib.common.exception import InvalidPropertyValueError
from translator.toscalib.elements.constraints import Constraint from translator.toscalib.elements.constraints import Constraint
from translator.toscalib.elements.constraints import Schema from translator.toscalib.elements.constraints import Schema
from translator.toscalib.functions import Function from translator.toscalib.functions import Function
@ -21,12 +24,12 @@ class Property(object):
PROPERTY_KEYS = ( PROPERTY_KEYS = (
TYPE, REQUIRED, DESCRIPTION, DEFAULT, CONSTRAINTS, TYPE, REQUIRED, DESCRIPTION, DEFAULT, CONSTRAINTS,
) = ( ) = (
'type', 'required', 'description', 'default', 'constraints' 'type', 'required', 'description', 'default', 'constraints',
) )
def __init__(self, property_name, value, schema_dict): def __init__(self, property_name, value, schema_dict):
self.name = property_name self.name = property_name
self.value = value self.value = self._convert_value(value)
self.schema = Schema(property_name, schema_dict) self.schema = Schema(property_name, schema_dict)
@property @property
@ -73,3 +76,20 @@ class Property(object):
if self.constraints: if self.constraints:
for constraint in self.constraints: for constraint in self.constraints:
constraint.validate(self.value) constraint.validate(self.value)
def _convert_value(self, value):
if self.name == 'mem_size':
mem_reader = re.compile('(\d*)\s*(\w*)')
matcher = str(value)
result = mem_reader.match(matcher).groups()
r = []
if (result[0] != '') and (result[1] == ''):
r = int(result[0])
elif (result[0] != '') and (result[1] == 'MB'):
r = int(result[0])
elif (result[0] != '') and (result[1] == 'GB'):
r = int(result[0]) * 1024
else:
raise InvalidPropertyValueError(what=self.name)
return r
return value

View File

@ -18,6 +18,7 @@
import os import os
import fixtures import fixtures
import testscenarios
import testtools import testtools
from translator.toscalib.tosca_template import ToscaTemplate from translator.toscalib.tosca_template import ToscaTemplate
@ -25,7 +26,7 @@ from translator.toscalib.tosca_template import ToscaTemplate
_TRUE_VALUES = ('True', 'true', '1', 'yes') _TRUE_VALUES = ('True', 'true', '1', 'yes')
class TestCase(testtools.TestCase): class TestCase(testscenarios.TestWithScenarios, testtools.TestCase):
"""Test case base class for all unit tests.""" """Test case base class for all unit tests."""

View File

@ -0,0 +1,167 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from translator.toscalib.common.exception import InvalidPropertyValueError
from translator.toscalib.nodetemplate import NodeTemplate
from translator.toscalib.tests.base import TestCase
import translator.toscalib.utils.yamlparser
class ToscaTemplateMemorySizeOutputTest(TestCase):
scenarios = [
(
# tpl_snippet with mem_size given as number
'mem_size_is_number',
dict(tpl_snippet='''
node_templates:
server:
type: tosca.nodes.Compute
properties:
# compute properties (flavor)
mem_size: 1024
# host image properties
os_type: Linux
''',
expected=1024)
),
(
# tpl_snippet with mem_size given as number+space+MB
'mem_size_is_number_Space_MB',
dict(tpl_snippet='''
node_templates:
server:
type: tosca.nodes.Compute
properties:
# compute properties (flavor)
mem_size: 1024 MB
# host image properties
os_type: Linux
''',
expected=1024)
),
(
# tpl_snippet with mem_size given as number+space+GB
'mem_size_is_number_Space_GB',
dict(tpl_snippet='''
node_templates:
server:
type: tosca.nodes.Compute
properties:
# compute properties (flavor)
mem_size: 1 GB
# host image properties
os_type: Linux
''',
expected=1024)
),
(
# tpl_snippet with mem_size given as number+GB
'mem_size_is_number_NoSpace_GB',
dict(tpl_snippet='''
node_templates:
server:
type: tosca.nodes.Compute
properties:
# compute properties (flavor)
mem_size: 1GB
# host image properties
os_type: Linux
''',
expected=1024)
),
(
# tpl_snippet with mem_size given as number+Spaces+GB
'mem_size_is_number_Spaces_GB',
dict(tpl_snippet='''
node_templates:
server:
type: tosca.nodes.Compute
properties:
# compute properties (flavor)
mem_size: 1 GB
# host image properties
os_type: Linux
''',
expected=1024)
),
(
# tpl_snippet with no mem_size given
'mem_size_is_absent',
dict(tpl_snippet='''
node_templates:
server:
type: tosca.nodes.Compute
properties:
# compute properties (flavor)
# host image properties
os_type: Linux
''',
expected=1024)
),
]
def test_scenario_mem_size(self):
tpl = self.tpl_snippet
nodetemplates = (translator.toscalib.utils.yamlparser.
simple_parse(tpl))['node_templates']
name = list(nodetemplates.keys())[0]
nodetemplate = NodeTemplate(name, nodetemplates)
for p in nodetemplate.properties:
if p.name == 'mem_size':
resolved = p.value
self.assertEqual(resolved, self.expected)
class ToscaTemplateMemorySizeErrorTest(TestCase):
scenarios = [
(
# tpl_snippet with mem_size given as empty (error)
'mem_size_is_empty',
dict(tpl_snippet='''
node_templates:
server:
type: tosca.nodes.Compute
properties:
# compute properties (flavor)
mem_size:
# host image properties
os_type: Linux
''',
err=InvalidPropertyValueError)
),
(
# tpl_snippet with mem_size given as number+InvalidUnit (error)
'mem_size_unit_is_invalid',
dict(tpl_snippet='''
node_templates:
server:
type: tosca.nodes.Compute
properties:
# compute properties (flavor)
mem_size: 1 QB
# host image properties
os_type: Linux
''',
err=InvalidPropertyValueError)
),
]
def test_scenario_mem_size_error(self):
tpl = self.tpl_snippet
nodetemplates = (translator.toscalib.utils.yamlparser.
simple_parse(tpl))['node_templates']
name = list(nodetemplates.keys())[0]
nodetemplate = NodeTemplate(name, nodetemplates)
self.assertRaises(self.err,
nodetemplate._create_properties)

View File

@ -64,3 +64,10 @@ class ToscaDefTest(TestCase):
self.assertEqual(compute_type.interfaces, None) self.assertEqual(compute_type.interfaces, None)
root_node = NodeType('tosca.nodes.Root') root_node = NodeType('tosca.nodes.Root')
self.assertIn('tosca.interfaces.node.Lifecycle', root_node.interfaces) self.assertIn('tosca.interfaces.node.Lifecycle', root_node.interfaces)
def test_default_mem_size(self):
test_value = 0
for p_def in compute_type.properties_def:
if p_def.name == 'mem_size':
test_value = p_def.default
self.assertEqual(test_value, 1024)