Wei-Yu Chen | 49950b9 | 2021-11-08 19:19:18 +0800 | [diff] [blame^] | 1 | """ |
| 2 | Copyright 2020 The Magma Authors. |
| 3 | |
| 4 | This source code is licensed under the BSD-style license found in the |
| 5 | LICENSE file in the root directory of this source tree. |
| 6 | |
| 7 | Unless required by applicable law or agreed to in writing, software |
| 8 | distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | See the License for the specific language governing permissions and |
| 11 | limitations under the License. |
| 12 | """ |
| 13 | |
| 14 | import unittest |
| 15 | from unittest import mock |
| 16 | |
| 17 | from google.protobuf.any_pb2 import Any |
| 18 | from configuration import mconfigs |
| 19 | from orc8r.protos.mconfig import mconfigs_pb2 |
| 20 | |
| 21 | |
| 22 | class MconfigsTest(unittest.TestCase): |
| 23 | |
| 24 | @mock.patch('magma.configuration.service_configs.load_service_config') |
| 25 | def test_filter_configs_by_key(self, load_service_config_mock): |
| 26 | # All services present, but 1 type not |
| 27 | configs_by_key = { |
| 28 | 'magmad': { |
| 29 | '@type': 'type.googleapis.com/magma.mconfig.MagmaD', |
| 30 | 'value': 'world'.encode(), |
| 31 | }, |
| 32 | 'directoryd': { |
| 33 | '@type': 'type.googleapis.com/magma.mconfig.DirectoryD', |
| 34 | 'value': 'hello'.encode(), |
| 35 | }, |
| 36 | 'foo': { |
| 37 | '@type': 'type.googleapis.com/magma.mconfig.Foo', |
| 38 | 'value': 'test'.encode(), |
| 39 | }, |
| 40 | } |
| 41 | |
| 42 | # Directoryd not present |
| 43 | load_service_config_mock.return_value = { |
| 44 | 'magma_services': ['mme', 'foo'], |
| 45 | } |
| 46 | actual = mconfigs.filter_configs_by_key(configs_by_key) |
| 47 | expected = { |
| 48 | 'magmad': configs_by_key['magmad'], |
| 49 | 'foo': configs_by_key['foo'], |
| 50 | } |
| 51 | self.assertEqual(expected, actual) |
| 52 | |
| 53 | # No services present |
| 54 | load_service_config_mock.return_value = { |
| 55 | 'magma_services': [], |
| 56 | } |
| 57 | actual = mconfigs.filter_configs_by_key(configs_by_key) |
| 58 | expected = {'magmad': configs_by_key['magmad']} |
| 59 | self.assertEqual(expected, actual) |
| 60 | |
| 61 | # Directoryd service present as a dynamic service |
| 62 | load_service_config_mock.return_value = { |
| 63 | 'magma_services': [], |
| 64 | 'registered_dynamic_services': ['directoryd'], |
| 65 | } |
| 66 | actual = mconfigs.filter_configs_by_key(configs_by_key) |
| 67 | expected = { |
| 68 | 'magmad': configs_by_key['magmad'], |
| 69 | 'directoryd': configs_by_key['directoryd'], |
| 70 | } |
| 71 | self.assertEqual(expected, actual) |
| 72 | |
| 73 | def test_unpack_mconfig_any(self): |
| 74 | magmad_mconfig = mconfigs_pb2.MagmaD( |
| 75 | checkin_interval=10, |
| 76 | checkin_timeout=5, |
| 77 | autoupgrade_enabled=True, |
| 78 | autoupgrade_poll_interval=300, |
| 79 | package_version='1.0.0-0', |
| 80 | images=[], |
| 81 | tier_id='default', |
| 82 | feature_flags={'flag1': False}, |
| 83 | ) |
| 84 | magmad_any = Any( |
| 85 | type_url='type.googleapis.com/magma.mconfig.MagmaD', |
| 86 | value=magmad_mconfig.SerializeToString(), |
| 87 | ) |
| 88 | actual = mconfigs.unpack_mconfig_any(magmad_any, mconfigs_pb2.MagmaD()) |
| 89 | self.assertEqual(magmad_mconfig, actual) |
| 90 | |
| 91 | def test_unpack_mconfig_directoryd(self): |
| 92 | directoryd_mconfig = mconfigs_pb2.DirectoryD( |
| 93 | log_level=5, |
| 94 | ) |
| 95 | magmad_any = Any( |
| 96 | type_url='type.googleapis.com/magma.mconfig.DirectoryD', |
| 97 | value=directoryd_mconfig.SerializeToString(), |
| 98 | ) |
| 99 | |
| 100 | actual = mconfigs.unpack_mconfig_any( |
| 101 | magmad_any, mconfigs_pb2.DirectoryD(), |
| 102 | ) |
| 103 | self.assertEqual(directoryd_mconfig, actual) |