Matteo Scandolo | 920e8fd | 2017-08-08 13:05:24 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
Matteo Scandolo | 5a07a2c | 2018-06-05 18:04:00 -0700 | [diff] [blame] | 17 | from helpers import * |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 18 | import unittest |
Matteo Scandolo | df2600b | 2017-07-05 17:01:29 -0700 | [diff] [blame] | 19 | import os |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 20 | from tosca.parser import TOSCA_Parser |
| 21 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 22 | class TOSCA_Parser_Test(unittest.TestCase): |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 23 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 24 | def test_get_tosca_models_by_name(self): |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 25 | """ |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 26 | [TOSCA_Parser] get_tosca_models_by_name: should extract models from the TOSCA recipe and store them in a dict |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 27 | """ |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 28 | class FakeNode: |
| 29 | def __init__(self, name): |
| 30 | self.name = name |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 31 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 32 | class FakeTemplate: |
| 33 | nodetemplates = [ |
| 34 | FakeNode('model1'), |
| 35 | FakeNode('model2') |
| 36 | ] |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 37 | |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 38 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 39 | res = TOSCA_Parser.get_tosca_models_by_name(FakeTemplate) |
| 40 | self.assertIsInstance(res['model1'], FakeNode) |
Matteo Scandolo | 8bbb03a | 2017-07-05 14:03:33 -0700 | [diff] [blame] | 41 | self.assertIsInstance(res['model2'], FakeNode) |
| 42 | |
Matteo Scandolo | df2600b | 2017-07-05 17:01:29 -0700 | [diff] [blame] | 43 | self.assertEqual(res['model1'].name, 'model1') |
| 44 | self.assertEqual(res['model2'].name, 'model2') |
| 45 | |
Matteo Scandolo | 8bbb03a | 2017-07-05 14:03:33 -0700 | [diff] [blame] | 46 | def test_populate_dependencies(self): |
| 47 | """ |
| 48 | [TOSCA_Parser] populate_dependencies: if a recipe has dependencies, it should find the ID of the requirements and add it to the model |
| 49 | """ |
| 50 | class FakeRecipe: |
| 51 | requirements = [ |
| 52 | { |
| 53 | 'site': { |
| 54 | 'node': 'site_onlab', |
| 55 | 'relationship': 'tosca.relationship.BelongsToOne' |
| 56 | } |
| 57 | } |
| 58 | ] |
| 59 | |
| 60 | class FakeSite: |
| 61 | id = 1 |
| 62 | name = 'onlab' |
| 63 | |
| 64 | class FakeModel: |
| 65 | name = 'test@opencord.org' |
| 66 | |
| 67 | saved_models = { |
| 68 | 'site_onlab': FakeSite |
| 69 | } |
| 70 | |
| 71 | model = TOSCA_Parser.populate_dependencies(FakeModel, FakeRecipe.requirements, saved_models) |
Matteo Scandolo | df2600b | 2017-07-05 17:01:29 -0700 | [diff] [blame] | 72 | self.assertEqual(model.site_id, 1) |
| 73 | |
| 74 | def test_get_ordered_models_template(self): |
| 75 | """ |
| 76 | [TOSCA_Parser] get_ordered_models_template: Create a list of templates based on topsorted models |
| 77 | """ |
| 78 | ordered_models = ['foo', 'bar'] |
| 79 | |
| 80 | templates = { |
| 81 | 'foo': 'foo_template', |
| 82 | 'bar': 'bar_template' |
| 83 | } |
| 84 | |
| 85 | ordered_templates = TOSCA_Parser.get_ordered_models_template(ordered_models, templates) |
| 86 | |
| 87 | self.assertEqual(ordered_templates[0], 'foo_template') |
| 88 | self.assertEqual(ordered_templates[1], 'bar_template') |
| 89 | |
| 90 | def test_topsort_dependencies(self): |
| 91 | """ |
| 92 | [TOSCA_Parser] topsort_dependencies: Create a list of models based on dependencies |
| 93 | """ |
| 94 | class FakeTemplate: |
| 95 | def __init__(self, name, deps): |
| 96 | self.name = name |
| 97 | self.dependencies_names = deps |
| 98 | |
| 99 | |
| 100 | templates = { |
| 101 | 'deps': FakeTemplate('deps', ['main']), |
| 102 | 'main': FakeTemplate('main', []), |
| 103 | } |
| 104 | |
| 105 | sorted = TOSCA_Parser.topsort_dependencies(templates) |
| 106 | |
| 107 | self.assertEqual(sorted[0], 'main') |
| 108 | self.assertEqual(sorted[1], 'deps') |
| 109 | |
| 110 | def test_compute_dependencies(self): |
| 111 | """ |
| 112 | [TOSCA_Parser] compute_dependencies: augment the TOSCA nodetemplate with information on requirements (aka related models) |
| 113 | """ |
| 114 | |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame] | 115 | parser = TOSCA_Parser('', 'user', 'pass') |
Matteo Scandolo | df2600b | 2017-07-05 17:01:29 -0700 | [diff] [blame] | 116 | |
| 117 | class FakeNode: |
| 118 | def __init__(self, name, requirements): |
| 119 | self.name = name |
| 120 | self.requirements = requirements |
| 121 | |
| 122 | main = FakeNode('main', []) |
| 123 | dep = FakeNode('dep', [{'relation': {'node': 'main'}}]) |
| 124 | |
| 125 | models_by_name = { |
| 126 | 'main': main, |
| 127 | 'dep': dep |
| 128 | } |
| 129 | |
| 130 | class FakeTemplate: |
| 131 | nodetemplates = [dep, main] |
| 132 | |
| 133 | parser.compute_dependencies(FakeTemplate, models_by_name) |
| 134 | |
| 135 | templates = FakeTemplate.nodetemplates |
| 136 | augmented_dep = templates[0] |
| 137 | augmented_main = templates[1] |
| 138 | |
| 139 | self.assertIsInstance(augmented_dep.dependencies[0], FakeNode) |
| 140 | self.assertEqual(augmented_dep.dependencies[0].name, 'main') |
| 141 | self.assertEqual(augmented_dep.dependencies_names[0], 'main') |
| 142 | |
| 143 | self.assertEqual(len(augmented_main.dependencies), 0) |
| 144 | self.assertEqual(len(augmented_main.dependencies_names), 0) |
| 145 | |
| 146 | def test_populate_model(self): |
| 147 | """ |
| 148 | [TOSCA_Parser] populate_model: augment the GRPC model with data from TOSCA |
| 149 | """ |
| 150 | class FakeModel: |
| 151 | pass |
| 152 | |
| 153 | data = { |
| 154 | 'name': 'test', |
| 155 | 'foo': 'bar', |
| 156 | 'number': 1 |
| 157 | } |
| 158 | |
| 159 | model = TOSCA_Parser.populate_model(FakeModel, data) |
| 160 | |
| 161 | self.assertEqual(model.name, 'test') |
| 162 | self.assertEqual(model.foo, 'bar') |
| 163 | self.assertEqual(model.number, 1) |
| 164 | |
Matteo Scandolo | 5ccdb13 | 2018-01-17 14:02:12 -0800 | [diff] [blame] | 165 | def test_populate_model_error(self): |
| 166 | """ |
| 167 | [TOSCA_Parser] populate_model: should print a meaningful error message |
| 168 | """ |
| 169 | |
| 170 | class FakeModel: |
| 171 | |
| 172 | model_name = "FakeModel" |
| 173 | |
| 174 | def __setattr__(self, name, value): |
| 175 | if name == 'foo': |
| 176 | raise TypeError('reported exception') |
| 177 | else: |
| 178 | super(FakeModel, self).__setattr__(name, value) |
| 179 | |
| 180 | data = { |
| 181 | 'name': 'test', |
| 182 | 'foo': None, |
| 183 | 'number': 1 |
| 184 | } |
| 185 | |
| 186 | |
| 187 | model = FakeModel() |
| 188 | |
| 189 | with self.assertRaises(Exception) as e: |
| 190 | model = TOSCA_Parser.populate_model(model, data) |
| 191 | self.assertEqual(e.exception.message, 'Failed to set None on field foo for class FakeModel, Exception was: "reported exception"') |
| 192 | |
Matteo Scandolo | df2600b | 2017-07-05 17:01:29 -0700 | [diff] [blame] | 193 | def test_translate_exception(self): |
| 194 | """ |
| 195 | [TOSCA_Parser] translate_exception: convert a TOSCA Parser exception in a user readable string |
| 196 | """ |
| 197 | e = TOSCA_Parser._translate_exception("Non tosca exception") |
| 198 | self.assertEqual(e, "Non tosca exception") |
| 199 | |
| 200 | e = TOSCA_Parser._translate_exception(""" |
| 201 | MissingRequiredFieldError: some message |
| 202 | followed by unreadable |
| 203 | and mystic |
| 204 | python error |
| 205 | starting at line |
| 206 | 38209834 of some file |
Matteo Scandolo | 1fedfae | 2017-10-09 13:57:00 -0700 | [diff] [blame] | 207 | UnknownFieldError: with some message |
| 208 | followed by useless things |
| 209 | ImportError: with some message |
| 210 | followed by useless things |
| 211 | InvalidTypeError: with some message |
| 212 | followed by useless things |
| 213 | TypeMismatchError: with some message |
| 214 | followed by useless things |
Matteo Scandolo | df2600b | 2017-07-05 17:01:29 -0700 | [diff] [blame] | 215 | """) |
Matteo Scandolo | 1fedfae | 2017-10-09 13:57:00 -0700 | [diff] [blame] | 216 | self.assertEqual(e, """MissingRequiredFieldError: some message |
| 217 | UnknownFieldError: with some message |
| 218 | ImportError: with some message |
| 219 | InvalidTypeError: with some message |
| 220 | TypeMismatchError: with some message |
| 221 | """) |