/usr/lib/python2.7/dist-packages/toscaparser/tests/test_toscatpl.py is in python-tosca-parser 0.1.0-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 | # 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.
import os
import six
from toscaparser.common import exception
import toscaparser.elements.interfaces as ifaces
from toscaparser.elements.nodetype import NodeType
from toscaparser.functions import GetInput
from toscaparser.functions import GetProperty
from toscaparser.nodetemplate import NodeTemplate
from toscaparser.tests.base import TestCase
from toscaparser.tosca_template import ToscaTemplate
import toscaparser.utils.yamlparser
class ToscaTemplateTest(TestCase):
'''TOSCA template.'''
tosca_tpl = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"data/tosca_single_instance_wordpress.yaml")
tosca = ToscaTemplate(tosca_tpl)
tosca_elk_tpl = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"data/tosca_elk.yaml")
def test_version(self):
self.assertEqual(self.tosca.version, "tosca_simple_yaml_1_0")
def test_description(self):
expected_description = "TOSCA simple profile with wordpress, " \
"web server and mysql on the same server."
self.assertEqual(self.tosca.description, expected_description)
def test_inputs(self):
self.assertEqual(
['cpus', 'db_name', 'db_port',
'db_pwd', 'db_root_pwd', 'db_user'],
sorted([input.name for input in self.tosca.inputs]))
input_name = "db_port"
expected_description = "Port for the MySQL database."
for input in self.tosca.inputs:
if input.name == input_name:
self.assertEqual(input.description, expected_description)
def test_node_tpls(self):
'''Test nodetemplate names.'''
self.assertEqual(
['mysql_database', 'mysql_dbms', 'server',
'webserver', 'wordpress'],
sorted([tpl.name for tpl in self.tosca.nodetemplates]))
tpl_name = "mysql_database"
expected_type = "tosca.nodes.Database"
expected_properties = ['name', 'password', 'user']
expected_capabilities = ['database_endpoint']
expected_requirements = [{'host': {'node': 'mysql_dbms'}}]
''' TODO: needs enhancement in tosca_elk.yaml..
expected_relationshp = ['tosca.relationships.HostedOn']
expected_host = ['mysql_dbms']
'''
expected_interface = [ifaces.LIFECYCLE_SHORTNAME]
for tpl in self.tosca.nodetemplates:
if tpl_name == tpl.name:
'''Test node type.'''
self.assertEqual(tpl.type, expected_type)
'''Test properties.'''
self.assertEqual(
expected_properties,
sorted(tpl.get_properties().keys()))
'''Test capabilities.'''
self.assertEqual(
expected_capabilities,
sorted(tpl.get_capabilities().keys()))
'''Test requirements.'''
self.assertEqual(
expected_requirements, tpl.requirements)
'''Test relationship.'''
''' needs enhancements in tosca_elk.yaml
self.assertEqual(
expected_relationshp,
[x.type for x in tpl.relationships.keys()])
self.assertEqual(
expected_host,
[y.name for y in tpl.relationships.values()])
'''
'''Test interfaces.'''
self.assertEqual(
expected_interface,
[x.type for x in tpl.interfaces])
if tpl.name == 'server':
'''Test property value'''
props = tpl.get_properties()
if props and 'mem_size' in props.keys():
self.assertEqual(props['mem_size'].value, '4096 MB')
'''Test capability'''
caps = tpl.get_capabilities()
self.assertIn('os', caps.keys())
os_props_objs = None
os_props = None
os_type_prop = None
if caps and 'os' in caps.keys():
capability = caps['os']
os_props_objs = capability.get_properties_objects()
os_props = capability.get_properties()
os_type_prop = capability.get_property_value('type')
break
self.assertEqual(
['Linux'],
[p.value for p in os_props_objs if p.name == 'type'])
self.assertEqual(
'Linux',
os_props['type'].value if 'type' in os_props else '')
self.assertEqual('Linux', os_props['type'].value)
self.assertEqual('Linux', os_type_prop)
def test_outputs(self):
self.assertEqual(
['website_url'],
sorted([output.name for output in self.tosca.outputs]))
def test_interfaces(self):
wordpress_node = [
node for node in self.tosca.nodetemplates
if node.name == 'wordpress'][0]
interfaces = wordpress_node.interfaces
self.assertEqual(2, len(interfaces))
for interface in interfaces:
if interface.name == 'create':
self.assertEqual(ifaces.LIFECYCLE_SHORTNAME,
interface.type)
self.assertEqual('wordpress/wordpress_install.sh',
interface.implementation)
self.assertIsNone(interface.inputs)
elif interface.name == 'configure':
self.assertEqual(ifaces.LIFECYCLE_SHORTNAME,
interface.type)
self.assertEqual('wordpress/wordpress_configure.sh',
interface.implementation)
self.assertEqual(3, len(interface.inputs))
TestCase.skip(self, 'bug #1440247')
wp_db_port = interface.inputs['wp_db_port']
self.assertTrue(isinstance(wp_db_port, GetProperty))
self.assertEqual('get_property', wp_db_port.name)
self.assertEqual(['SELF',
'database_endpoint',
'port'],
wp_db_port.args)
result = wp_db_port.result()
self.assertTrue(isinstance(result, GetInput))
else:
raise AssertionError(
'Unexpected interface: {0}'.format(interface.name))
def test_normative_type_by_short_name(self):
# test template with a short name Compute
template = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"data/test_tosca_normative_type_by_shortname.yaml")
tosca_tpl = ToscaTemplate(template)
expected_type = "tosca.nodes.Compute"
for tpl in tosca_tpl.nodetemplates:
self.assertEqual(tpl.type, expected_type)
for tpl in tosca_tpl.nodetemplates:
compute_type = NodeType(tpl.type)
self.assertEqual(
sorted(['tosca.capabilities.Container',
'tosca.capabilities.OperatingSystem',
'tosca.capabilities.network.Bindable',
'tosca.capabilities.Scalable']),
sorted([c.type
for c in compute_type.get_capabilities_objects()]))
def test_template_with_no_inputs(self):
tosca_tpl = self._load_template('test_no_inputs_in_template.yaml')
self.assertEqual(0, len(tosca_tpl.inputs))
def test_template_with_no_outputs(self):
tosca_tpl = self._load_template('test_no_outputs_in_template.yaml')
self.assertEqual(0, len(tosca_tpl.outputs))
def test_relationship_interface(self):
template = ToscaTemplate(self.tosca_elk_tpl)
for node_tpl in template.nodetemplates:
if node_tpl.name == 'logstash':
config_interface = 'Configure'
artifact = 'logstash/configure_elasticsearch.py'
relation = node_tpl.relationships
for key in relation.keys():
rel_tpl = relation.get(key).get_relationship_template()
if rel_tpl:
interfaces = rel_tpl[0].interfaces
for interface in interfaces:
self.assertEqual(config_interface,
interface.type)
self.assertEqual('pre_configure_source',
interface.name)
self.assertEqual(artifact,
interface.implementation)
def test_template_macro(self):
template = ToscaTemplate(self.tosca_elk_tpl)
for node_tpl in template.nodetemplates:
if node_tpl.name == 'mongo_server':
self.assertEqual(
['disk_size', 'mem_size', 'num_cpus'],
sorted(node_tpl.get_capability('host').
get_properties().keys()))
def test_template_requirements(self):
"""Test different formats of requirements
The requirements can be defined in few different ways,
1. Requirement expressed as a capability with an implicit relationship.
2. Requirement expressed with explicit relationship.
3. Requirement expressed with a relationship template.
4. Requirement expressed via TOSCA types to provision a node
with explicit relationship.
5. Requirement expressed via TOSCA types with a filter.
"""
tosca_tpl = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"data/test_requirements.yaml")
tosca = ToscaTemplate(tosca_tpl)
for node_tpl in tosca.nodetemplates:
if node_tpl.name == 'my_app':
expected_relationship = [
('tosca.relationships.ConnectsTo', 'mysql_database'),
('tosca.relationships.HostedOn', 'my_webserver')]
actual_relationship = sorted([
(relation.type, node.name) for
relation, node in node_tpl.relationships.items()])
self.assertEqual(expected_relationship, actual_relationship)
if node_tpl.name == 'mysql_database':
self.assertEqual(
[('tosca.relationships.HostedOn', 'my_dbms')],
[(relation.type, node.name) for
relation,
node in node_tpl.relationships.items()])
if node_tpl.name == 'my_server':
self.assertEqual(
[('tosca.relationships.AttachesTo', 'my_storage')],
[(relation.type, node.name) for
relation,
node in node_tpl.relationships.items()])
def test_template_requirements_not_implemented(self):
# TODO(spzala): replace this test with new one once TOSCA types look up
# support is implemented.
"""Requirements that yet need to be implemented
The following requirement formats are not yet implemented,
due to look up dependency:
1. Requirement expressed via TOSCA types to provision a node
with explicit relationship.
2. Requirement expressed via TOSCA types with a filter.
"""
tpl_snippet_1 = '''
node_templates:
mysql_database:
type: tosca.nodes.Database
description: Requires a particular node type and relationship.
To be full-filled via lookup into node repository.
requirements:
- req1:
node: tosca.nodes.DBMS
relationship: tosca.relationships.HostedOn
'''
tpl_snippet_2 = '''
node_templates:
my_webserver:
type: tosca.nodes.WebServer
description: Requires a particular node type with a filter.
To be full-filled via lookup into node repository.
requirements:
- req1:
node: tosca.nodes.Compute
target_filter:
properties:
num_cpus: { in_range: [ 1, 4 ] }
mem_size: { greater_or_equal: 2 }
capabilities:
- tosca.capabilities.OS:
properties:
architecture: x86_64
type: linux
'''
tpl_snippet_3 = '''
node_templates:
my_webserver2:
type: tosca.nodes.WebServer
description: Requires a node type with a particular capability.
To be full-filled via lookup into node repository.
requirements:
- req1:
node: tosca.nodes.Compute
relationship: tosca.relationships.HostedOn
capability: tosca.capabilities.Container
'''
self._requirements_not_implemented(tpl_snippet_1, 'mysql_database')
self._requirements_not_implemented(tpl_snippet_2, 'my_webserver')
self._requirements_not_implemented(tpl_snippet_3, 'my_webserver2')
def _requirements_not_implemented(self, tpl_snippet, tpl_name):
nodetemplates = (toscaparser.utils.yamlparser.
simple_parse(tpl_snippet))['node_templates']
self.assertRaises(
NotImplementedError,
lambda: NodeTemplate(tpl_name, nodetemplates).relationships)
def test_custom_capability_type_definition(self):
tpl_snippet = '''
node_templates:
test_app:
type: tosca.nodes.WebApplication.TestApp
capabilities:
test_cap:
properties:
test: 1
'''
# custom definition with capability type definition
custom_def = '''
tosca.nodes.WebApplication.TestApp:
derived_from: tosca.nodes.WebApplication
capabilities:
test_cap:
type: tosca.capabilities.TestCapability
tosca.capabilities.TestCapability:
derived_from: tosca.capabilities.Root
properties:
test:
type: integer
required: no
'''
expected_capabilities = ['test_cap']
nodetemplates = (toscaparser.utils.yamlparser.
simple_parse(tpl_snippet))['node_templates']
custom_def = (toscaparser.utils.yamlparser.
simple_parse(custom_def))
name = list(nodetemplates.keys())[0]
tpl = NodeTemplate(name, nodetemplates, custom_def)
self.assertEqual(
expected_capabilities,
sorted(tpl.get_capabilities().keys()))
# custom definition without capability type definition
custom_def = '''
tosca.nodes.WebApplication.TestApp:
derived_from: tosca.nodes.WebApplication
capabilities:
test_cap:
type: tosca.capabilities.TestCapability
'''
custom_def = (toscaparser.utils.yamlparser.
simple_parse(custom_def))
tpl = NodeTemplate(name, nodetemplates, custom_def)
err = self.assertRaises(
exception.InvalidTypeError,
lambda: NodeTemplate(name, nodetemplates,
custom_def).get_capabilities_objects())
self.assertEqual('Type "tosca.capabilities.TestCapability" is not '
'a valid type.', six.text_type(err))
|