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 import any_pb2 |
| 10 | from google.protobuf.json_format import MessageToJson |
| 11 | from configuration import mconfig_managers |
| 12 | from configuration.exceptions import LoadConfigError |
| 13 | from orc8r.protos.mconfig import mconfigs_pb2 |
| 14 | |
| 15 | |
| 16 | class MconfigManagerImplTest(unittest.TestCase): |
| 17 | @mock.patch('magma.configuration.service_configs.load_service_config') |
| 18 | def test_load_mconfig(self, get_service_config_value_mock): |
| 19 | # Fixture mconfig has 1 unrecognized service, 1 unregistered type |
| 20 | magmad_fixture = mconfigs_pb2.MagmaD( |
| 21 | checkin_interval=10, |
| 22 | checkin_timeout=5, |
| 23 | autoupgrade_enabled=True, |
| 24 | autoupgrade_poll_interval=300, |
| 25 | package_version='1.0.0-0', |
| 26 | images=[], |
| 27 | tier_id='default', |
| 28 | feature_flags={'flag1': False}, |
| 29 | ) |
| 30 | magmad_fixture_any = any_pb2.Any() |
| 31 | magmad_fixture_any.Pack(magmad_fixture) |
| 32 | magmad_fixture_serialized = MessageToJson(magmad_fixture_any) |
| 33 | fixture = ''' |
| 34 | { |
| 35 | "configs_by_key": { |
| 36 | "magmad": %s, |
| 37 | "foo": { |
| 38 | "@type": "type.googleapis.com/magma.mconfig.NotAType", |
| 39 | "value": "test1" |
| 40 | }, |
| 41 | "not_a_service": { |
| 42 | "@type": "type.googleapis.com/magma.mconfig.MagmaD", |
| 43 | "value": "test2" |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | ''' % magmad_fixture_serialized |
| 48 | get_service_config_value_mock.return_value = { |
| 49 | 'magma_services': ['foo'], |
| 50 | } |
| 51 | |
| 52 | with mock.patch('builtins.open', mock.mock_open(read_data=fixture)): |
| 53 | manager = mconfig_managers.MconfigManagerImpl() |
| 54 | with self.assertRaises(LoadConfigError): |
| 55 | manager.load_mconfig() |