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