Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 1 | import unittest |
Matteo Scandolo | df2600b | 2017-07-05 17:01:29 -0700 | [diff] [blame] | 2 | import os |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 3 | from tosca.parser import TOSCA_Parser |
| 4 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 5 | class TOSCA_Parser_Test(unittest.TestCase): |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 6 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 7 | def test_get_tosca_models_by_name(self): |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 8 | """ |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 9 | [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] | 10 | """ |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 11 | class FakeNode: |
| 12 | def __init__(self, name): |
| 13 | self.name = name |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 14 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 15 | class FakeTemplate: |
| 16 | nodetemplates = [ |
| 17 | FakeNode('model1'), |
| 18 | FakeNode('model2') |
| 19 | ] |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 20 | |
Matteo Scandolo | 9ce1825 | 2017-06-22 10:48:25 -0700 | [diff] [blame] | 21 | |
Matteo Scandolo | 485b713 | 2017-06-30 11:46:47 -0700 | [diff] [blame] | 22 | res = TOSCA_Parser.get_tosca_models_by_name(FakeTemplate) |
| 23 | self.assertIsInstance(res['model1'], FakeNode) |
Matteo Scandolo | 8bbb03a | 2017-07-05 14:03:33 -0700 | [diff] [blame] | 24 | self.assertIsInstance(res['model2'], FakeNode) |
| 25 | |
Matteo Scandolo | df2600b | 2017-07-05 17:01:29 -0700 | [diff] [blame] | 26 | self.assertEqual(res['model1'].name, 'model1') |
| 27 | self.assertEqual(res['model2'].name, 'model2') |
| 28 | |
Matteo Scandolo | 8bbb03a | 2017-07-05 14:03:33 -0700 | [diff] [blame] | 29 | 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 Scandolo | df2600b | 2017-07-05 17:01:29 -0700 | [diff] [blame] | 55 | 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 | |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame^] | 98 | parser = TOSCA_Parser('', 'user', 'pass') |
Matteo Scandolo | df2600b | 2017-07-05 17:01:29 -0700 | [diff] [blame] | 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(""" |
| 156 | MissingRequiredFieldError: 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 | """ |
Matteo Scandolo | 21dde41 | 2017-07-11 18:54:12 -0700 | [diff] [blame^] | 169 | parser = TOSCA_Parser('', 'user', 'pass') |
Matteo Scandolo | df2600b | 2017-07-05 17:01:29 -0700 | [diff] [blame] | 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) |