1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
5 # http://www.apache.org/licenses/LICENSE-2.0
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
15 from toscaparser.tests.base import TestCase
16 from toscaparser.topology_template import TopologyTemplate
17 from toscaparser.tosca_template import ToscaTemplate
18 import toscaparser.utils.yamlparser
20 YAML_LOADER = toscaparser.utils.yamlparser.load_yaml
23 class TopologyTemplateTest(TestCase):
28 self.tosca_tpl_path = os.path.join(
29 os.path.dirname(os.path.abspath(__file__)),
30 "data/topology_template/subsystem.yaml")
31 self.tpl = YAML_LOADER(self.tosca_tpl_path)
32 self.topo_tpl = self.tpl.get('topology_template')
33 self.imports = self.tpl.get('imports')
34 self.topo = TopologyTemplate(self.topo_tpl,
35 self._get_all_custom_def())
37 self.tosca_system_tpl_path = os.path.join(
38 os.path.dirname(os.path.abspath(__file__)),
39 "data/topology_template/system.yaml")
40 self.system_template = ToscaTemplate(self.tosca_system_tpl_path)
42 def _get_custom_def(self, type_definition):
44 for definition in self.imports:
45 if os.path.isabs(definition):
48 tpl_dir = os.path.dirname(os.path.abspath(self.tosca_tpl_path))
49 def_file = os.path.join(tpl_dir, definition)
50 custom_type = YAML_LOADER(def_file)
51 custom_defs.update(custom_type.get(type_definition))
54 def _get_all_custom_def(self):
56 custom_defs.update(self._get_custom_def('node_types'))
57 custom_defs.update(self._get_custom_def('capability_types'))
60 def test_description(self):
61 expected_desc = 'Template of a database including its hosting stack.'
62 self.assertEqual(expected_desc, self.topo.description)
64 def test_inputs(self):
66 ['mq_server_ip', 'my_cpus', 'receiver_port'],
67 sorted([input.name for input in self.topo.inputs]))
69 input_name = "receiver_port"
70 expected_description = "Port to be used for receiving messages."
71 for input in self.topo.inputs:
72 if input.name == input_name:
73 self.assertEqual(expected_description, input.description)
75 def test_node_tpls(self):
76 '''Test nodetemplate names.'''
78 ['app', 'server', 'websrv'],
79 sorted([tpl.name for tpl in self.topo.nodetemplates]))
82 expected_type = "example.SomeApp"
83 expected_properties = ['admin_user', 'pool_size']
84 expected_capabilities = ['feature', 'message_receiver']
85 expected_requirements = [{'host': {'node': 'websrv'}}]
86 expected_relationshp = ['tosca.relationships.HostedOn']
87 expected_host = ['websrv']
88 for tpl in self.topo.nodetemplates:
89 if tpl_name == tpl.name:
91 self.assertEqual(tpl.type, expected_type)
93 '''Test properties.'''
96 sorted(tpl.get_properties().keys()))
98 '''Test capabilities.'''
100 expected_capabilities,
101 sorted(tpl.get_capabilities().keys()))
103 '''Test requirements.'''
105 expected_requirements, tpl.requirements)
107 '''Test relationship.'''
108 ''' TODO : skip tempororily. need to fix it
111 expected_relationshp,
112 [x.type for x in tpl.relationships.keys()])
115 [y.name for y in tpl.relationships.values()])
116 '''Test interfaces.'''
117 # TODO(hurf) add interface test when new template is available
119 if tpl.name == 'server':
120 '''Test property value'''
121 props = tpl.get_properties()
122 if props and 'mem_size' in props.keys():
123 self.assertEqual(props['mem_size'].value, '4096 MB')
124 '''Test capability'''
125 caps = tpl.get_capabilities()
126 self.assertIn('os', caps.keys())
130 if caps and 'os' in caps.keys():
131 capability = caps['os']
132 os_props_objs = capability.get_properties_objects()
133 os_props = capability.get_properties()
134 os_type_prop = capability.get_property_value('type')
138 [p.value for p in os_props_objs if p.name == 'type'])
141 os_props['type'].value if 'type' in os_props else '')
142 self.assertEqual('Linux', os_props['type'].value)
143 self.assertEqual('Linux', os_type_prop)
145 def test_outputs(self):
148 sorted([output.name for output in self.topo.outputs]))
150 def test_groups(self):
151 group = self.topo.groups[0]
152 self.assertEqual('webserver_group', group.name)
153 self.assertEqual(['websrv', 'server'], group.members)
154 for node in group.get_member_nodes():
155 if node.name == 'server':
156 '''Test property value'''
157 props = node.get_properties()
158 if props and 'mem_size' in props.keys():
159 self.assertEqual(props['mem_size'].value, '4096 MB')
161 def test_system_template(self):
162 tpl_path = os.path.join(
163 os.path.dirname(os.path.abspath(__file__)),
164 "data/topology_template/system.yaml")
165 self.assertIsNotNone(ToscaTemplate(tpl_path))