| import unittest |
| import os |
| from tosca.parser import TOSCA_Parser |
| |
| class TOSCA_Parser_Test(unittest.TestCase): |
| |
| def test_get_tosca_models_by_name(self): |
| """ |
| [TOSCA_Parser] get_tosca_models_by_name: should extract models from the TOSCA recipe and store them in a dict |
| """ |
| class FakeNode: |
| def __init__(self, name): |
| self.name = name |
| |
| class FakeTemplate: |
| nodetemplates = [ |
| FakeNode('model1'), |
| FakeNode('model2') |
| ] |
| |
| |
| res = TOSCA_Parser.get_tosca_models_by_name(FakeTemplate) |
| self.assertIsInstance(res['model1'], FakeNode) |
| self.assertIsInstance(res['model2'], FakeNode) |
| |
| self.assertEqual(res['model1'].name, 'model1') |
| self.assertEqual(res['model2'].name, 'model2') |
| |
| def test_populate_dependencies(self): |
| """ |
| [TOSCA_Parser] populate_dependencies: if a recipe has dependencies, it should find the ID of the requirements and add it to the model |
| """ |
| class FakeRecipe: |
| requirements = [ |
| { |
| 'site': { |
| 'node': 'site_onlab', |
| 'relationship': 'tosca.relationship.BelongsToOne' |
| } |
| } |
| ] |
| |
| class FakeSite: |
| id = 1 |
| name = 'onlab' |
| |
| class FakeModel: |
| name = 'test@opencord.org' |
| |
| saved_models = { |
| 'site_onlab': FakeSite |
| } |
| |
| model = TOSCA_Parser.populate_dependencies(FakeModel, FakeRecipe.requirements, saved_models) |
| self.assertEqual(model.site_id, 1) |
| |
| def test_get_ordered_models_template(self): |
| """ |
| [TOSCA_Parser] get_ordered_models_template: Create a list of templates based on topsorted models |
| """ |
| ordered_models = ['foo', 'bar'] |
| |
| templates = { |
| 'foo': 'foo_template', |
| 'bar': 'bar_template' |
| } |
| |
| ordered_templates = TOSCA_Parser.get_ordered_models_template(ordered_models, templates) |
| |
| self.assertEqual(ordered_templates[0], 'foo_template') |
| self.assertEqual(ordered_templates[1], 'bar_template') |
| |
| def test_topsort_dependencies(self): |
| """ |
| [TOSCA_Parser] topsort_dependencies: Create a list of models based on dependencies |
| """ |
| class FakeTemplate: |
| def __init__(self, name, deps): |
| self.name = name |
| self.dependencies_names = deps |
| |
| |
| templates = { |
| 'deps': FakeTemplate('deps', ['main']), |
| 'main': FakeTemplate('main', []), |
| } |
| |
| sorted = TOSCA_Parser.topsort_dependencies(templates) |
| |
| self.assertEqual(sorted[0], 'main') |
| self.assertEqual(sorted[1], 'deps') |
| |
| def test_compute_dependencies(self): |
| """ |
| [TOSCA_Parser] compute_dependencies: augment the TOSCA nodetemplate with information on requirements (aka related models) |
| """ |
| |
| parser = TOSCA_Parser('') |
| |
| class FakeNode: |
| def __init__(self, name, requirements): |
| self.name = name |
| self.requirements = requirements |
| |
| main = FakeNode('main', []) |
| dep = FakeNode('dep', [{'relation': {'node': 'main'}}]) |
| |
| models_by_name = { |
| 'main': main, |
| 'dep': dep |
| } |
| |
| class FakeTemplate: |
| nodetemplates = [dep, main] |
| |
| parser.compute_dependencies(FakeTemplate, models_by_name) |
| |
| templates = FakeTemplate.nodetemplates |
| augmented_dep = templates[0] |
| augmented_main = templates[1] |
| |
| self.assertIsInstance(augmented_dep.dependencies[0], FakeNode) |
| self.assertEqual(augmented_dep.dependencies[0].name, 'main') |
| self.assertEqual(augmented_dep.dependencies_names[0], 'main') |
| |
| self.assertEqual(len(augmented_main.dependencies), 0) |
| self.assertEqual(len(augmented_main.dependencies_names), 0) |
| |
| def test_populate_model(self): |
| """ |
| [TOSCA_Parser] populate_model: augment the GRPC model with data from TOSCA |
| """ |
| class FakeModel: |
| pass |
| |
| data = { |
| 'name': 'test', |
| 'foo': 'bar', |
| 'number': 1 |
| } |
| |
| model = TOSCA_Parser.populate_model(FakeModel, data) |
| |
| self.assertEqual(model.name, 'test') |
| self.assertEqual(model.foo, 'bar') |
| self.assertEqual(model.number, 1) |
| |
| def test_translate_exception(self): |
| """ |
| [TOSCA_Parser] translate_exception: convert a TOSCA Parser exception in a user readable string |
| """ |
| e = TOSCA_Parser._translate_exception("Non tosca exception") |
| self.assertEqual(e, "Non tosca exception") |
| |
| e = TOSCA_Parser._translate_exception(""" |
| MissingRequiredFieldError: some message |
| followed by unreadable |
| and mystic |
| python error |
| starting at line |
| 38209834 of some file |
| """) |
| self.assertEqual(e, "MissingRequiredFieldError: some message") |
| |
| def test_save_recipe_to_tmp_file(self): |
| """ |
| [TOSCA_Parser] save_recipe_to_tmp_file: should save a TOSCA recipe to a tmp file |
| """ |
| parser = TOSCA_Parser('') |
| parser.recipe_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'test_tmp.yaml') |
| |
| parser.save_recipe_to_tmp_file('my tosca') |
| |
| self.assertTrue(os.path.exists(parser.recipe_file)) |
| |
| content = open(parser.recipe_file).read() |
| |
| self.assertEqual(content, 'my tosca') |
| |
| os.remove(parser.recipe_file) |