File: test_topology_template.py

package info (click to toggle)
python-tosca-parser 2.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,920 kB
  • sloc: python: 11,185; sh: 144; makefile: 26
file content (467 lines) | stat: -rw-r--r-- 20,661 bytes parent folder | download
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#    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

from toscaparser.common import exception
from toscaparser.substitution_mappings import SubstitutionMappings
from toscaparser.tests.base import TestCase
from toscaparser.tests import utils
from toscaparser.topology_template import TopologyTemplate
from toscaparser.tosca_template import ToscaTemplate
from toscaparser.utils.gettextutils import _
import toscaparser.utils.yamlparser

YAML_LOADER = toscaparser.utils.yamlparser.load_yaml


class TopologyTemplateTest(TestCase):

    def setUp(self):
        TestCase.setUp(self)
        '''TOSCA template.'''
        self.tosca_tpl_path = utils.get_sample_test_path(
            "data/topology_template/transactionsubsystem.yaml")
        self.tpl = YAML_LOADER(self.tosca_tpl_path)
        self.topo_tpl = self.tpl.get('topology_template')
        self.imports = self.tpl.get('imports')
        self.topo = TopologyTemplate(self.topo_tpl,
                                     self._get_all_custom_def())

    def _get_custom_def(self, type_definition):
        custom_defs = {}
        for definition in self.imports:
            if os.path.isabs(definition):
                def_file = definition
            else:
                tpl_dir = os.path.dirname(os.path.abspath(self.tosca_tpl_path))
                def_file = os.path.join(tpl_dir, definition)
            custom_type = YAML_LOADER(def_file)
            custom_defs.update(custom_type.get(type_definition))
        return custom_defs

    def _get_all_custom_def(self):
        custom_defs = {}
        custom_defs.update(self._get_custom_def('node_types'))
        custom_defs.update(self._get_custom_def('capability_types'))
        return custom_defs

    def _get_custom_types(self):
        custom_types = {}
        def_file = utils.get_sample_test_path(
            "data/topology_template/definitions.yaml")
        custom_type = YAML_LOADER(def_file)
        node_types = custom_type['node_types']
        for name in node_types:
            defintion = node_types[name]
            custom_types[name] = defintion
        return custom_types

    def test_description(self):
        expected_desc = 'Template of a database including its hosting stack.'
        self.assertEqual(expected_desc, self.topo.description)

    def test_inputs(self):
        self.assertEqual(
            ['mq_server_ip', 'my_cpus', 'receiver_port'],
            sorted([input.name for input in self.topo.inputs]))

        input_name = "receiver_port"
        expected_description = "Port to be used for receiving messages."
        for input in self.topo.inputs:
            if input.name == input_name:
                self.assertEqual(expected_description, input.description)

    def test_node_tpls(self):
        '''Test nodetemplate names.'''
        self.assertEqual(
            ['app', 'db', 'server', 'websrv'],
            sorted([tpl.name for tpl in self.topo.nodetemplates]))

        tpl_name = "app"
        expected_type = "example.SomeApp"
        expected_properties = ['admin_user', 'pool_size']
        expected_capabilities = ['app_endpoint', 'feature', 'message_receiver']
        expected_requirements = [{'host': {'node': 'websrv'}},
                                 {'database': {'node': 'db'}}]
        expected_relationshp = ['tosca.relationships.HostedOn',
                                'tosca.relationships.ConnectsTo']
        expected_host = ['websrv', 'db']
        for tpl in self.topo.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.'''
                ''' TODO : skip tempororily. need to fix it
                '''
                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.'''
                # TODO(hurf) add interface test when new template is available

            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(
            sorted(['receiver_ip', 'receiver_port']),
            sorted([output.name for output in self.topo.outputs]))

    def test_groups(self):
        group = self.topo.groups[0]
        self.assertEqual('webserver_group', group.name)
        self.assertEqual(['websrv', 'server'], group.members)
        for node in group.get_member_nodes():
            if node.name == 'server':
                '''Test property value'''
                props = node.get_properties()
                if props and 'mem_size' in props.keys():
                    self.assertEqual(props['mem_size'].value, '4096 MB')

    def test_system_template(self):
        tpl_path = utils.get_sample_test_path(
            "data/topology_template/system.yaml")
        system_tosca_template = ToscaTemplate(tpl_path)
        self.assertIsNotNone(system_tosca_template)
        self.assertEqual(
            len(system_tosca_template.
                nested_tosca_templates_with_topology), 4)
        self.assertTrue(system_tosca_template.has_nested_templates())

    def test_invalid_keyname(self):
        tpl_snippet = '''
        substitution_mappings:
          node_type: example.DatabaseSubsystem
          capabilities:
            database_endpoint: [ db_app, database_endpoint ]
          requirements:
            receiver1: [ tran_app, receiver1 ]
          invalid_key: 123
        '''
        sub_mappings = (toscaparser.utils.yamlparser.
                        simple_parse(tpl_snippet))['substitution_mappings']
        expected_message = _(
            'SubstitutionMappings contains unknown field '
            '"invalid_key". Refer to the definition '
            'to verify valid values.')
        err = self.assertRaises(
            exception.UnknownFieldError,
            lambda: SubstitutionMappings(sub_mappings, None, None,
                                         None, None, None))
        self.assertEqual(expected_message, err.__str__())

    def test_missing_required_keyname(self):
        tpl_snippet = '''
        substitution_mappings:
          capabilities:
            database_endpoint: [ db_app, database_endpoint ]
          requirements:
            receiver1: [ tran_app, receiver1 ]
        '''
        sub_mappings = (toscaparser.utils.yamlparser.
                        simple_parse(tpl_snippet))['substitution_mappings']
        expected_message = _('SubstitutionMappings used in topology_template '
                             'is missing required field "node_type".')
        err = self.assertRaises(
            exception.MissingRequiredFieldError,
            lambda: SubstitutionMappings(sub_mappings, None, None,
                                         None, None, None))
        self.assertEqual(expected_message, err.__str__())

    def test_invalid_nodetype(self):
        tpl_snippet = '''
        substitution_mappings:
          node_type: example.DatabaseSubsystem1
          capabilities:
            database_endpoint: [ db_app, database_endpoint ]
          requirements:
            receiver1: [ tran_app, receiver1 ]
        '''
        sub_mappings = (toscaparser.utils.yamlparser.
                        simple_parse(tpl_snippet))['substitution_mappings']
        custom_defs = self._get_custom_types()
        expected_message = _('Node type "example.DatabaseSubsystem1" '
                             'is not a valid type.')
        err = self.assertRaises(
            exception.InvalidNodeTypeError,
            lambda: SubstitutionMappings(sub_mappings, None, None,
                                         None, None, custom_defs))
        self.assertEqual(expected_message, err.__str__())

    def test_system_with_input_validation(self):
        tpl_path0 = utils.get_sample_test_path(
            "data/topology_template/validate/system_invalid_input.yaml")
        tpl_path1 = utils.get_sample_test_path(
            "data/topology_template/validate/"
            "queuingsubsystem_invalid_input.yaml")
        errormsg = _('SubstitutionMappings with node_type '
                     'example.QueuingSubsystem is missing '
                     'required input definition of input "server_port".')

        # It's invalid in nested template.
        self.assertRaises(exception.ValidationError,
                          lambda: ToscaTemplate(tpl_path0))
        exception.ExceptionCollector.assertExceptionMessage(
            exception.MissingRequiredInputError, errormsg)

        # Subtemplate deploy standaolone is also invalid.
        self.assertRaises(exception.ValidationError,
                          lambda: ToscaTemplate(tpl_path1))
        exception.ExceptionCollector.assertExceptionMessage(
            exception.MissingRequiredInputError, errormsg)

    def test_substitution_mappings_valid_output(self):
        tpl_path = utils.get_sample_test_path(
            "data/topology_template/validate/"
            "test_substitution_mappings_valid_output.yaml")
        self.assertIsNotNone(ToscaTemplate(tpl_path))

    def test_system_with_unknown_output_validation(self):
        tpl_path = utils.get_sample_test_path(
            "data/topology_template/validate/"
            "test_substitution_mappings_invalid_output.yaml")
        errormsg = _('\'Attribute "my_cpu_output" was not found in node '
                     'template "substitute_app".\'')
        self.assertRaises(exception.ValidationError,
                          lambda: ToscaTemplate(tpl_path))
        exception.ExceptionCollector.assertExceptionMessage(
            KeyError, errormsg)

    def test_substitution_mappings_with_substitution_filter(self):
        tpl_path = utils.get_sample_test_path(
            "data/topology_template/validate/"
            "test_substitution_mappings_section.yaml")
        tosca = ToscaTemplate(tpl_path)
        filter_name = tosca.tpl['topology_template'][
            'substitution_mappings']['substitution_filter'].keys()
        self.assertEqual('node_filter', list(filter_name)[0])
        n_f = tosca.topology_template.substitution_mappings.sub_mapping_def[
            'substitution_filter'].keys()
        self.assertEqual('node_filter', list(n_f)[0])
        p_v = tosca.topology_template.substitution_mappings.sub_mapping_def[
            'substitution_filter'].values()
        self.assertEqual('property_value',
                         list(p_v)[0]['properties'][0]['my_property'])

    def test_substitution_mappings_error_substitution_filter(self):
        tpl_path = utils.get_sample_test_path(
            "data/topology_template/validate/"
            "test_substitution_mappings_error_node_filter.yaml")
        errormsg = _('SubstitutionMappings contains unknown field '
                     '"substitution_filter". Refer to the definition '
                     'to verify valid values.')
        err = self.assertRaises(
            exception.ValidationError, ToscaTemplate, tpl_path)
        self.assertIn(errormsg, str(err))

    def test_substitution_mappings_error_node_filter(self):
        tpl_path = utils.get_sample_test_path(
            "data/topology_template/validate/"
            "test_substitution_mappings_error_node_filter_key.yaml")
        errormsg = _('SubstitutionMappings contains unknown field '
                     '"node_filter". Refer to the definition '
                     'to verify valid values.')
        err = self.assertRaises(
            exception.ValidationError, ToscaTemplate, tpl_path)
        self.assertIn(errormsg, str(err))

    def test_substitution_mappings_error_properties(self):
        tpl_path = utils.get_sample_test_path(
            "data/topology_template/validate/"
            "test_substitution_mappings_error_property_type.yaml")
        errormsg = _('properties of the Node Filter definition '
                     'Keyname must be of type "list".')
        err = self.assertRaises(
            exception.ValidationError, ToscaTemplate, tpl_path)
        self.assertIn(errormsg, str(err))

    def test_substitution_mappings_error_capabilities(self):
        tpl_path = utils.get_sample_test_path(
            "data/topology_template/validate/"
            "test_substitution_mappings_error_capability_type.yaml")
        errormsg = _('capabilities of the Node Filter definition '
                     'Keyname must be of type "list".')
        err = self.assertRaises(
            exception.ValidationError, ToscaTemplate, tpl_path)
        self.assertIn(errormsg, str(err))

    def test_substitution_mappings_with_attributes(self):
        tpl_path = utils.get_sample_test_path(
            "data/topology_template/validate/"
            "test_substitution_mappings_section.yaml")
        tosca = ToscaTemplate(tpl_path)
        attribute_name = tosca.tpl['topology_template'][
            'substitution_mappings']['attributes'].keys()
        self.assertEqual('receiver_ip', list(attribute_name)[0])
        a_n = tosca.topology_template.substitution_mappings.sub_mapping_def[
            'attributes'].keys()
        self.assertEqual('receiver_ip', list(a_n)[0])
        a_v = tosca.topology_template.substitution_mappings.sub_mapping_def[
            'attributes'].values()
        self.assertEqual('my_property', list(a_v)[0])

    def test_substitution_mappings_with_interfaces(self):
        tpl_path = utils.get_sample_test_path(
            "data/topology_template/validate/"
            "test_substitution_mappings_section.yaml")
        tosca = ToscaTemplate(tpl_path)
        interface_name = tosca.tpl['topology_template'][
            'substitution_mappings']['interfaces'].keys()
        self.assertEqual('Standard', list(interface_name)[0])
        i_n = tosca.topology_template.substitution_mappings.sub_mapping_def[
            'interfaces'].keys()
        self.assertEqual('Standard', list(i_n)[0])
        i_v = tosca.topology_template.substitution_mappings.sub_mapping_def[
            'interfaces'].values()
        self.assertEqual('my_start_node.start', list(i_v)[0]['start'])

    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__())