blob: 4fb2f2bab2f93dd2bf4e2fc92bb01ed6220514be [file] [log] [blame]
Matteo Scandolo9ce18252017-06-22 10:48:25 -07001import unittest
Matteo Scandolodf2600b2017-07-05 17:01:29 -07002import os
Matteo Scandolo9ce18252017-06-22 10:48:25 -07003from tosca.parser import TOSCA_Parser
4
Matteo Scandolo485b7132017-06-30 11:46:47 -07005class TOSCA_Parser_Test(unittest.TestCase):
Matteo Scandolo9ce18252017-06-22 10:48:25 -07006
Matteo Scandolo485b7132017-06-30 11:46:47 -07007 def test_get_tosca_models_by_name(self):
Matteo Scandolo9ce18252017-06-22 10:48:25 -07008 """
Matteo Scandolo485b7132017-06-30 11:46:47 -07009 [TOSCA_Parser] get_tosca_models_by_name: should extract models from the TOSCA recipe and store them in a dict
Matteo Scandolo9ce18252017-06-22 10:48:25 -070010 """
Matteo Scandolo485b7132017-06-30 11:46:47 -070011 class FakeNode:
12 def __init__(self, name):
13 self.name = name
Matteo Scandolo9ce18252017-06-22 10:48:25 -070014
Matteo Scandolo485b7132017-06-30 11:46:47 -070015 class FakeTemplate:
16 nodetemplates = [
17 FakeNode('model1'),
18 FakeNode('model2')
19 ]
Matteo Scandolo9ce18252017-06-22 10:48:25 -070020
Matteo Scandolo9ce18252017-06-22 10:48:25 -070021
Matteo Scandolo485b7132017-06-30 11:46:47 -070022 res = TOSCA_Parser.get_tosca_models_by_name(FakeTemplate)
23 self.assertIsInstance(res['model1'], FakeNode)
Matteo Scandolo8bbb03a2017-07-05 14:03:33 -070024 self.assertIsInstance(res['model2'], FakeNode)
25
Matteo Scandolodf2600b2017-07-05 17:01:29 -070026 self.assertEqual(res['model1'].name, 'model1')
27 self.assertEqual(res['model2'].name, 'model2')
28
Matteo Scandolo8bbb03a2017-07-05 14:03:33 -070029 def test_populate_dependencies(self):
30 """
31 [TOSCA_Parser] populate_dependencies: if a recipe has dependencies, it should find the ID of the requirements and add it to the model
32 """
33 class FakeRecipe:
34 requirements = [
35 {
36 'site': {
37 'node': 'site_onlab',
38 'relationship': 'tosca.relationship.BelongsToOne'
39 }
40 }
41 ]
42
43 class FakeSite:
44 id = 1
45 name = 'onlab'
46
47 class FakeModel:
48 name = 'test@opencord.org'
49
50 saved_models = {
51 'site_onlab': FakeSite
52 }
53
54 model = TOSCA_Parser.populate_dependencies(FakeModel, FakeRecipe.requirements, saved_models)
Matteo Scandolodf2600b2017-07-05 17:01:29 -070055 self.assertEqual(model.site_id, 1)
56
57 def test_get_ordered_models_template(self):
58 """
59 [TOSCA_Parser] get_ordered_models_template: Create a list of templates based on topsorted models
60 """
61 ordered_models = ['foo', 'bar']
62
63 templates = {
64 'foo': 'foo_template',
65 'bar': 'bar_template'
66 }
67
68 ordered_templates = TOSCA_Parser.get_ordered_models_template(ordered_models, templates)
69
70 self.assertEqual(ordered_templates[0], 'foo_template')
71 self.assertEqual(ordered_templates[1], 'bar_template')
72
73 def test_topsort_dependencies(self):
74 """
75 [TOSCA_Parser] topsort_dependencies: Create a list of models based on dependencies
76 """
77 class FakeTemplate:
78 def __init__(self, name, deps):
79 self.name = name
80 self.dependencies_names = deps
81
82
83 templates = {
84 'deps': FakeTemplate('deps', ['main']),
85 'main': FakeTemplate('main', []),
86 }
87
88 sorted = TOSCA_Parser.topsort_dependencies(templates)
89
90 self.assertEqual(sorted[0], 'main')
91 self.assertEqual(sorted[1], 'deps')
92
93 def test_compute_dependencies(self):
94 """
95 [TOSCA_Parser] compute_dependencies: augment the TOSCA nodetemplate with information on requirements (aka related models)
96 """
97
98 parser = TOSCA_Parser('')
99
100 class FakeNode:
101 def __init__(self, name, requirements):
102 self.name = name
103 self.requirements = requirements
104
105 main = FakeNode('main', [])
106 dep = FakeNode('dep', [{'relation': {'node': 'main'}}])
107
108 models_by_name = {
109 'main': main,
110 'dep': dep
111 }
112
113 class FakeTemplate:
114 nodetemplates = [dep, main]
115
116 parser.compute_dependencies(FakeTemplate, models_by_name)
117
118 templates = FakeTemplate.nodetemplates
119 augmented_dep = templates[0]
120 augmented_main = templates[1]
121
122 self.assertIsInstance(augmented_dep.dependencies[0], FakeNode)
123 self.assertEqual(augmented_dep.dependencies[0].name, 'main')
124 self.assertEqual(augmented_dep.dependencies_names[0], 'main')
125
126 self.assertEqual(len(augmented_main.dependencies), 0)
127 self.assertEqual(len(augmented_main.dependencies_names), 0)
128
129 def test_populate_model(self):
130 """
131 [TOSCA_Parser] populate_model: augment the GRPC model with data from TOSCA
132 """
133 class FakeModel:
134 pass
135
136 data = {
137 'name': 'test',
138 'foo': 'bar',
139 'number': 1
140 }
141
142 model = TOSCA_Parser.populate_model(FakeModel, data)
143
144 self.assertEqual(model.name, 'test')
145 self.assertEqual(model.foo, 'bar')
146 self.assertEqual(model.number, 1)
147
148 def test_translate_exception(self):
149 """
150 [TOSCA_Parser] translate_exception: convert a TOSCA Parser exception in a user readable string
151 """
152 e = TOSCA_Parser._translate_exception("Non tosca exception")
153 self.assertEqual(e, "Non tosca exception")
154
155 e = TOSCA_Parser._translate_exception("""
156MissingRequiredFieldError: some message
157 followed by unreadable
158 and mystic
159 python error
160 starting at line
161 38209834 of some file
162 """)
163 self.assertEqual(e, "MissingRequiredFieldError: some message")
164
165 def test_save_recipe_to_tmp_file(self):
166 """
167 [TOSCA_Parser] save_recipe_to_tmp_file: should save a TOSCA recipe to a tmp file
168 """
169 parser = TOSCA_Parser('')
170 parser.recipe_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'test_tmp.yaml')
171
172 parser.save_recipe_to_tmp_file('my tosca')
173
174 self.assertTrue(os.path.exists(parser.recipe_file))
175
176 content = open(parser.recipe_file).read()
177
178 self.assertEqual(content, 'my tosca')
179
180 os.remove(parser.recipe_file)