| garciaale | 0a517b9 | 2021-01-12 15:44:44 -0300 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | # Copyright 2020 Whitestack, LLC |
| 4 | # ************************************************************* |
| 5 | # |
| 6 | # This file is part of OSM common repository. |
| 7 | # All Rights Reserved to Whitestack, LLC |
| 8 | # |
| 9 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 10 | # not use this file except in compliance with the License. You may obtain |
| 11 | # a copy of the License at |
| 12 | # |
| 13 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | # |
| 15 | # Unless required by applicable law or agreed to in writing, software |
| 16 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 18 | # License for the specific language governing permissions and limitations |
| 19 | # under the License. |
| 20 | # |
| 21 | # For those usages not covered by the Apache License, Version 2.0 please |
| 22 | # contact: agarcia@whitestack.com |
| 23 | ## |
| 24 | |
| 25 | from osm_im.im_translation import translate_im_vnfd_to_sol006, translate_im_nsd_to_sol006 |
| 26 | import unittest |
| 27 | import yaml |
| 28 | |
| 29 | TESTS_EXAMPLES_FOLDER = 'tests/examples/' |
| 30 | |
| 31 | IM_TO_SOL006_VNFD_FILES = { |
| 32 | 'cirros_vnfd_im.yaml': 'cirros_vnfd_sol006.yaml', |
| 33 | 'alternative_image_im.yaml': 'alternative_image_sol006.yaml', |
| 34 | 'epa_im.yaml': 'epa_sol006.yaml', |
| 35 | 'magma_knf_im.yaml': 'magma_knf_sol006.yaml', |
| 36 | 'vnfd_im.yaml': 'vnfd_sol006.yaml', |
| 37 | 'vepc_im.yaml': 'vepc_sol006.yaml', |
| 38 | 'hackfest_charmed_vnfd_im.yaml': 'hackfest_charmed_vnfd_sol006.yaml', |
| 39 | } |
| 40 | |
| 41 | IM_TO_SOL006_NSD_FILES = { |
| 42 | 'cirros_nsd_im.yaml': 'cirros_nsd_sol006.yaml', |
| 43 | 'vepc_nsd_im.yaml': 'vepc_nsd_sol006.yaml', |
| 44 | 'hackfest_charmed_nsd_im.yaml': 'hackfest_charmed_nsd_sol006.yaml', |
| 45 | } |
| 46 | |
| 47 | |
| 48 | class TranslationTest(unittest.TestCase): |
| 49 | def _sort_descriptor(self, descriptor): |
| 50 | if isinstance(descriptor, dict): |
| 51 | return sorted((k, self._sort_descriptor(v)) for k, v in descriptor.items()) |
| 52 | if isinstance(descriptor, list): |
| 53 | return sorted(self._sort_descriptor(x) for x in descriptor) |
| 54 | else: |
| 55 | return descriptor |
| 56 | |
| 57 | def _get_descriptor_file_data_as_dict(self, descriptor_file_path): |
| 58 | with open(descriptor_file_path, 'r') as descriptor_file: |
| 59 | descriptor_dict = yaml.safe_load(descriptor_file.read()) |
| 60 | return descriptor_dict |
| 61 | |
| 62 | def test_translate_im_vnfd_to_sol006(self): |
| 63 | for im_file in IM_TO_SOL006_VNFD_FILES: |
| 64 | im_file_path = TESTS_EXAMPLES_FOLDER + im_file |
| 65 | sol006_file_path = TESTS_EXAMPLES_FOLDER + IM_TO_SOL006_VNFD_FILES[im_file] |
| 66 | im_vnfd = self._get_descriptor_file_data_as_dict(im_file_path) |
| 67 | sol006_vnfd = self._get_descriptor_file_data_as_dict(sol006_file_path) |
| 68 | |
| 69 | translated_vnfd = translate_im_vnfd_to_sol006(im_vnfd) |
| 70 | self.assertEqual(self._sort_descriptor(sol006_vnfd), self._sort_descriptor(translated_vnfd)) |
| 71 | |
| 72 | def test_translate_im_nsd_to_sol006(self): |
| 73 | for im_file in IM_TO_SOL006_NSD_FILES: |
| 74 | im_file_path = TESTS_EXAMPLES_FOLDER + im_file |
| 75 | sol006_file_path = TESTS_EXAMPLES_FOLDER + IM_TO_SOL006_NSD_FILES[im_file] |
| 76 | im_nsd = self._get_descriptor_file_data_as_dict(im_file_path) |
| 77 | sol006_nsd = self._get_descriptor_file_data_as_dict(sol006_file_path) |
| 78 | |
| 79 | translated_nsd = translate_im_nsd_to_sol006(im_nsd) |
| 80 | self.assertEqual(self._sort_descriptor(sol006_nsd), self._sort_descriptor(translated_nsd)) |