1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
|
# 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 testtools import matchers
from toscaparser.common import exception
from toscaparser.elements.property_definition import PropertyDef
from toscaparser.nodetemplate import NodeTemplate
from toscaparser.properties import Property
from toscaparser.tests.base import TestCase
from toscaparser.utils.gettextutils import _
from toscaparser.utils import yamlparser
class PropertyTest(TestCase):
def test_type(self):
test_property_schema = {'type': 'string'}
propertyInstance = Property('test_property', 'Hughes',
test_property_schema)
self.assertEqual('string', propertyInstance.type)
def test_type_invalid(self):
test_property_schema = {'type': 'Fish'}
propertyInstance = Property('test_property', 'Hughes',
test_property_schema)
error = self.assertRaises(exception.InvalidTypeError,
propertyInstance.validate)
self.assertEqual(_('Type "Fish" is not a valid type.'), str(error))
def test_list(self):
test_property_schema = {'type': 'list'}
propertyInstance = Property('test_property', ['a', 'b'],
test_property_schema)
self.assertIsNone(propertyInstance.validate())
self.assertEqual(['a', 'b'], propertyInstance.value)
def test_list_invalid(self):
test_property_schema = {'type': 'list'}
propertyInstance = Property('test_property', 'a',
test_property_schema)
error = self.assertRaises(ValueError, propertyInstance.validate)
self.assertEqual(_('"a" is not a list.'), str(error))
def test_list_entry_schema(self):
test_property_schema = {'type': 'list',
'entry_schema': {'type': 'string'}}
propertyInstance = Property('test_property', ['a', 'b'],
test_property_schema)
self.assertIsNone(propertyInstance.validate())
self.assertEqual(['a', 'b'], propertyInstance.value)
schema_snippet = '''
type: list
entry_schema:
type: string
constraints:
- min_length: 2
'''
test_property_schema = yamlparser.simple_parse(schema_snippet)
propertyInstance = Property('test_property', ['ab', 'cd'],
test_property_schema)
self.assertIsNone(propertyInstance.validate())
self.assertEqual(['ab', 'cd'], propertyInstance.value)
def test_list_entry_schema_invalid(self):
test_property_schema = {'type': 'list',
'entry_schema': {'type': 'integer'}}
propertyInstance = Property('test_property', [1, 'b'],
test_property_schema)
error = self.assertRaises(ValueError, propertyInstance.validate)
self.assertEqual(_('"b" is not an integer.'), str(error))
def test_map(self):
test_property_schema = {'type': 'map'}
propertyInstance = Property('test_property', {'a': 'b'},
test_property_schema)
self.assertIsNone(propertyInstance.validate())
self.assertEqual({'a': 'b'}, propertyInstance.value)
def test_map_invalid(self):
test_property_schema = {'type': 'map'}
propertyInstance = Property('test_property', 12,
test_property_schema)
error = self.assertRaises(ValueError, propertyInstance.validate)
self.assertEqual(_('"12" is not a map.'), str(error))
def test_map_entry_schema(self):
test_property_schema = {'type': 'map',
'entry_schema': {'type': 'boolean'}}
propertyInstance = Property('test_property',
{'valid': True, 'required': True},
test_property_schema)
self.assertIsNone(propertyInstance.validate())
self.assertEqual({'valid': True, 'required': True},
propertyInstance.value)
def test_map_entry_schema_invalid(self):
test_property_schema = {'type': 'map',
'entry_schema': {'type': 'boolean'}}
propertyInstance = Property('test_property',
{'valid': True, 'contact_name': 123},
test_property_schema)
error = self.assertRaises(ValueError, propertyInstance.validate)
self.assertEqual(_('"123" is not a boolean.'), str(error))
def test_boolean(self):
test_property_schema = {'type': 'boolean'}
propertyInstance = Property('test_property', 'true',
test_property_schema)
self.assertIsNone(propertyInstance.validate())
propertyInstance = Property('test_property', True,
test_property_schema)
self.assertIsNone(propertyInstance.validate())
self.assertEqual(True, propertyInstance.value)
def test_boolean_invalid(self):
test_property_schema = {'type': 'boolean'}
propertyInstance = Property('test_property', 12,
test_property_schema)
error = self.assertRaises(ValueError, propertyInstance.validate)
self.assertEqual(_('"12" is not a boolean.'), str(error))
def test_float(self):
test_property_schema = {'type': 'float'}
propertyInstance = Property('test_property', 0.1,
test_property_schema)
self.assertIsNone(propertyInstance.validate())
self.assertEqual(0.1, propertyInstance.value)
def test_float_invalid(self):
test_property_schema = {'type': 'float'}
propertyInstance = Property('test_property', 12,
test_property_schema)
error = self.assertRaises(ValueError, propertyInstance.validate)
self.assertEqual(_('"12" is not a float.'), str(error))
def test_timestamp(self):
test_property_schema = {'type': 'timestamp'}
# canonical timestamp
propertyInstance = Property('test_property', '2015-04-01T02:59:43.1Z',
test_property_schema)
self.assertIsNone(propertyInstance.validate())
self.assertEqual("2015-04-01T02:59:43.1Z", propertyInstance.value)
# iso8601 timestamp
propertyInstance = Property('test_property',
'2015-04-01t21:59:43.10-05:00',
test_property_schema)
self.assertIsNone(propertyInstance.validate())
self.assertEqual("2015-04-01t21:59:43.10-05:00",
propertyInstance.value)
# space separated timestamp
propertyInstance = Property('test_property',
'2015-04-01 21:59:43.10 -5',
test_property_schema)
self.assertIsNone(propertyInstance.validate())
self.assertEqual("2015-04-01 21:59:43.10 -5", propertyInstance.value)
# no time zone timestamp
propertyInstance = Property('test_property', '2015-04-01 21:59:43.10',
test_property_schema)
self.assertIsNone(propertyInstance.validate())
self.assertEqual("2015-04-01 21:59:43.10", propertyInstance.value)
# date (00:00:00Z)
propertyInstance = Property('test_property', '2015-04-01',
test_property_schema)
self.assertIsNone(propertyInstance.validate())
self.assertEqual("2015-04-01", propertyInstance.value)
def test_timestamp_invalid(self):
test_property_schema = {'type': 'timestamp'}
# invalid timestamp - day out of range
value = '2015-04-115T02:59:43.1Z'
propertyInstance = Property('test_property', value,
test_property_schema)
error = self.assertRaises(ValueError, propertyInstance.validate)
expected_message = (_('"%s" is not a valid timestamp.') % value)
self.assertThat(str(error), matchers.StartsWith(expected_message))
def test_required(self):
test_property_schema = {'type': 'string'}
propertyInstance = Property('test_property', 'Foo',
test_property_schema)
self.assertEqual(True, propertyInstance.required)
def test_proprety_inheritance(self):
tosca_custom_def = '''
tosca.nodes.SoftwareComponent.MySoftware:
derived_from: SoftwareComponent
properties:
install_path:
required: false
type: string
default: /opt/mysoftware
'''
tosca_node_template = '''
node_templates:
mysoftware_instance:
type: tosca.nodes.SoftwareComponent.MySoftware
properties:
component_version: 3.1
'''
expected_properties = ['component_version',
'install_path']
tpl = self._get_nodetemplate(tosca_node_template, tosca_custom_def)
self.assertIsNone(tpl.validate())
self.assertEqual(expected_properties,
sorted(tpl.get_properties().keys()))
def test_missing_property_type(self):
tpl_snippet = '''
properties:
prop:
typo: tosca.mytesttype.Test
'''
schema = yamlparser.simple_parse(tpl_snippet)
error = self.assertRaises(exception.InvalidSchemaError, PropertyDef,
'prop', None, schema['properties']['prop'])
self.assertEqual(_('Schema definition of "prop" must have a "type" '
'attribute.'), str(error))
def test_invalid_required_value(self):
tpl_snippet = '''
properties:
prop:
type: tosca.mytesttype.Test
required: dunno
'''
schema = yamlparser.simple_parse(tpl_snippet)
error = self.assertRaises(exception.InvalidSchemaError, PropertyDef,
'prop', None, schema['properties']['prop'])
valid_values = ', '.join(PropertyDef.VALID_REQUIRED_VALUES)
expected_message = (_('Schema definition of "prop" has "required" '
'attribute with invalid value "dunno". The '
'value must be one of "%s".') % valid_values)
self.assertEqual(expected_message, str(error))
def test_invalid_property_status(self):
tpl_snippet = '''
properties:
prop:
type: string
status: unknown
'''
schema = yamlparser.simple_parse(tpl_snippet)
error = self.assertRaises(exception.InvalidSchemaError, PropertyDef,
'prop', None, schema['properties']['prop'])
valid_values = ', '.join(PropertyDef.VALID_STATUS_VALUES)
expected_message = (_('Schema definition of "prop" has "status" '
'attribute with invalid value "unknown". The '
'value must be one of "%s".') % valid_values)
self.assertEqual(expected_message, str(error))
def test_capability_proprety_inheritance(self):
tosca_custom_def_example1 = '''
tosca.capabilities.ScalableNew:
derived_from: tosca.capabilities.Scalable
properties:
max_instances:
type: integer
default: 0
required: no
tosca.nodes.ComputeNew:
derived_from: tosca.nodes.Compute
capabilities:
scalable:
type: tosca.capabilities.ScalableNew
'''
tosca_node_template_example1 = '''
node_templates:
compute_instance:
type: tosca.nodes.ComputeNew
capabilities:
scalable:
properties:
min_instances: 1
'''
tosca_custom_def_example2 = '''
tosca.nodes.ComputeNew:
derived_from: tosca.nodes.Compute
capabilities:
new_cap:
type: tosca.capabilities.Scalable
'''
tosca_node_template_example2 = '''
node_templates:
db_server:
type: tosca.nodes.ComputeNew
capabilities:
host:
properties:
num_cpus: 1
'''
tpl1 = self._get_nodetemplate(tosca_node_template_example1,
tosca_custom_def_example1)
self.assertIsNone(tpl1.validate())
tpl2 = self._get_nodetemplate(tosca_node_template_example2,
tosca_custom_def_example2)
self.assertIsNone(tpl2.validate())
def _get_nodetemplate(self, tpl_snippet,
custom_def_snippet=None):
nodetemplates = yamlparser.\
simple_parse(tpl_snippet)['node_templates']
custom_def = []
if custom_def_snippet:
custom_def = yamlparser.simple_parse(custom_def_snippet)
name = list(nodetemplates.keys())[0]
tpl = NodeTemplate(name, nodetemplates, custom_def)
return tpl
def test_explicit_relationship_proprety(self):
tosca_node_template = '''
node_templates:
client_node:
type: tosca.nodes.Compute
requirements:
- local_storage:
node: my_storage
relationship:
type: AttachesTo
properties:
location: /mnt/disk
my_storage:
type: tosca.nodes.BlockStorage
properties:
size: 1 GB
'''
expected_properties = ['location']
nodetemplates = yamlparser.\
simple_parse(tosca_node_template)['node_templates']
tpl = NodeTemplate('client_node', nodetemplates, [])
self.assertIsNone(tpl.validate())
rel_tpls = []
for relationship, trgt in tpl.relationships.items():
rel_tpls.extend(trgt.get_relationship_template())
self.assertEqual(expected_properties,
sorted(rel_tpls[0].get_properties().keys()))
|