Wei-Yu Chen | ad55cb8 | 2022-02-15 20:07:01 +0800 | [diff] [blame] | 1 | # SPDX-FileCopyrightText: 2020 The Magma Authors. |
| 2 | # SPDX-FileCopyrightText: 2022 Open Networking Foundation <support@opennetworking.org> |
| 3 | # |
| 4 | # SPDX-License-Identifier: BSD-3-Clause |
Wei-Yu Chen | 49950b9 | 2021-11-08 19:19:18 +0800 | [diff] [blame] | 5 | |
| 6 | import unittest |
| 7 | from unittest import mock |
| 8 | |
| 9 | from google.protobuf.any_pb2 import Any |
| 10 | from configuration import mconfigs |
| 11 | from orc8r.protos.mconfig import mconfigs_pb2 |
| 12 | |
| 13 | |
| 14 | class MconfigsTest(unittest.TestCase): |
| 15 | |
| 16 | @mock.patch('magma.configuration.service_configs.load_service_config') |
| 17 | def test_filter_configs_by_key(self, load_service_config_mock): |
| 18 | # All services present, but 1 type not |
| 19 | configs_by_key = { |
| 20 | 'magmad': { |
| 21 | '@type': 'type.googleapis.com/magma.mconfig.MagmaD', |
| 22 | 'value': 'world'.encode(), |
| 23 | }, |
| 24 | 'directoryd': { |
| 25 | '@type': 'type.googleapis.com/magma.mconfig.DirectoryD', |
| 26 | 'value': 'hello'.encode(), |
| 27 | }, |
| 28 | 'foo': { |
| 29 | '@type': 'type.googleapis.com/magma.mconfig.Foo', |
| 30 | 'value': 'test'.encode(), |
| 31 | }, |
| 32 | } |
| 33 | |
| 34 | # Directoryd not present |
| 35 | load_service_config_mock.return_value = { |
| 36 | 'magma_services': ['mme', 'foo'], |
| 37 | } |
| 38 | actual = mconfigs.filter_configs_by_key(configs_by_key) |
| 39 | expected = { |
| 40 | 'magmad': configs_by_key['magmad'], |
| 41 | 'foo': configs_by_key['foo'], |
| 42 | } |
| 43 | self.assertEqual(expected, actual) |
| 44 | |
| 45 | # No services present |
| 46 | load_service_config_mock.return_value = { |
| 47 | 'magma_services': [], |
| 48 | } |
| 49 | actual = mconfigs.filter_configs_by_key(configs_by_key) |
| 50 | expected = {'magmad': configs_by_key['magmad']} |
| 51 | self.assertEqual(expected, actual) |
| 52 | |
| 53 | # Directoryd service present as a dynamic service |
| 54 | load_service_config_mock.return_value = { |
| 55 | 'magma_services': [], |
| 56 | 'registered_dynamic_services': ['directoryd'], |
| 57 | } |
| 58 | actual = mconfigs.filter_configs_by_key(configs_by_key) |
| 59 | expected = { |
| 60 | 'magmad': configs_by_key['magmad'], |
| 61 | 'directoryd': configs_by_key['directoryd'], |
| 62 | } |
| 63 | self.assertEqual(expected, actual) |
| 64 | |
| 65 | def test_unpack_mconfig_any(self): |
| 66 | magmad_mconfig = mconfigs_pb2.MagmaD( |
| 67 | checkin_interval=10, |
| 68 | checkin_timeout=5, |
| 69 | autoupgrade_enabled=True, |
| 70 | autoupgrade_poll_interval=300, |
| 71 | package_version='1.0.0-0', |
| 72 | images=[], |
| 73 | tier_id='default', |
| 74 | feature_flags={'flag1': False}, |
| 75 | ) |
| 76 | magmad_any = Any( |
| 77 | type_url='type.googleapis.com/magma.mconfig.MagmaD', |
| 78 | value=magmad_mconfig.SerializeToString(), |
| 79 | ) |
| 80 | actual = mconfigs.unpack_mconfig_any(magmad_any, mconfigs_pb2.MagmaD()) |
| 81 | self.assertEqual(magmad_mconfig, actual) |
| 82 | |
| 83 | def test_unpack_mconfig_directoryd(self): |
| 84 | directoryd_mconfig = mconfigs_pb2.DirectoryD( |
| 85 | log_level=5, |
| 86 | ) |
| 87 | magmad_any = Any( |
| 88 | type_url='type.googleapis.com/magma.mconfig.DirectoryD', |
| 89 | value=directoryd_mconfig.SerializeToString(), |
| 90 | ) |
| 91 | |
| 92 | actual = mconfigs.unpack_mconfig_any( |
| 93 | magmad_any, mconfigs_pb2.DirectoryD(), |
| 94 | ) |
| 95 | self.assertEqual(directoryd_mconfig, actual) |