Timo Tietavainen | cede9a3 | 2019-12-07 20:09:31 +0200 | [diff] [blame] | 1 | # Copyright (c) 2019 AT&T Intellectual Property. |
| 2 | # Copyright (c) 2018-2019 Nokia. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | # |
| 17 | # This source code is part of the near-RT RIC (RAN Intelligent Controller) |
| 18 | # platform project (RICP). |
| 19 | # |
| 20 | |
| 21 | |
| 22 | import pytest |
| 23 | from ricsdl.configuration import _Configuration |
Timo Tietavainen | 598ca39 | 2020-01-08 16:49:11 +0200 | [diff] [blame] | 24 | from ricsdl.configuration import DbBackendType |
Timo Tietavainen | cede9a3 | 2019-12-07 20:09:31 +0200 | [diff] [blame] | 25 | |
| 26 | |
| 27 | @pytest.fixture() |
| 28 | def config_fixture(request, monkeypatch): |
Timo Tietavainen | c6c9af2 | 2021-05-11 14:43:09 +0300 | [diff] [blame] | 29 | monkeypatch.setenv('DBAAS_SERVICE_HOST', 'service-ricplt-dbaas-tcp-cluster-0.ricplt') |
Timo Tietavainen | cede9a3 | 2019-12-07 20:09:31 +0200 | [diff] [blame] | 30 | monkeypatch.setenv('DBAAS_SERVICE_PORT', '10000') |
| 31 | monkeypatch.setenv('DBAAS_SERVICE_SENTINEL_PORT', '11000') |
| 32 | monkeypatch.setenv('DBAAS_MASTER_NAME', 'my-master') |
Timo Tietavainen | c6c9af2 | 2021-05-11 14:43:09 +0300 | [diff] [blame] | 33 | monkeypatch.setenv('DBAAS_CLUSTER_ADDR_LIST', 'service-ricplt-dbaas-tcp-cluster-0.ricplt,service-ricplt-dbaas-tcp-cluster-1.ricplt') |
Timo Tietavainen | 598ca39 | 2020-01-08 16:49:11 +0200 | [diff] [blame] | 34 | request.cls.config = _Configuration(fake_db_backend=None) |
Timo Tietavainen | cede9a3 | 2019-12-07 20:09:31 +0200 | [diff] [blame] | 35 | |
| 36 | |
| 37 | @pytest.fixture |
Timo Tietavainen | 598ca39 | 2020-01-08 16:49:11 +0200 | [diff] [blame] | 38 | def fake_db_config_fixture(request, monkeypatch): |
Timo Tietavainen | cede9a3 | 2019-12-07 20:09:31 +0200 | [diff] [blame] | 39 | monkeypatch.delenv('DBAAS_SERVICE_HOST', raising=False) |
| 40 | monkeypatch.delenv('DBAAS_SERVICE_PORT', raising=False) |
| 41 | monkeypatch.delenv('DBAAS_SERVICE_SENTINEL_PORT', raising=False) |
| 42 | monkeypatch.delenv('DBAAS_MASTER_NAME', raising=False) |
Timo Tietavainen | c6c9af2 | 2021-05-11 14:43:09 +0300 | [diff] [blame] | 43 | monkeypatch.delenv('DBAAS_CLUSTER_ADDR_LIST', raising=False) |
Timo Tietavainen | 598ca39 | 2020-01-08 16:49:11 +0200 | [diff] [blame] | 44 | request.cls.config = _Configuration(fake_db_backend='dict') |
Timo Tietavainen | cede9a3 | 2019-12-07 20:09:31 +0200 | [diff] [blame] | 45 | |
| 46 | |
| 47 | class TestConfiguration: |
| 48 | def test_get_params_function_returns_read_configuration(self, config_fixture): |
Timo Tietavainen | c6c9af2 | 2021-05-11 14:43:09 +0300 | [diff] [blame] | 49 | expected_config = _Configuration.Params(db_host='service-ricplt-dbaas-tcp-cluster-0.ricplt', |
| 50 | db_port='10000', |
Timo Tietavainen | cede9a3 | 2019-12-07 20:09:31 +0200 | [diff] [blame] | 51 | db_sentinel_port='11000', |
Timo Tietavainen | 598ca39 | 2020-01-08 16:49:11 +0200 | [diff] [blame] | 52 | db_sentinel_master_name='my-master', |
Timo Tietavainen | c6c9af2 | 2021-05-11 14:43:09 +0300 | [diff] [blame] | 53 | db_cluster_addr_list='service-ricplt-dbaas-tcp-cluster-0.ricplt,service-ricplt-dbaas-tcp-cluster-1.ricplt', |
Timo Tietavainen | 598ca39 | 2020-01-08 16:49:11 +0200 | [diff] [blame] | 54 | db_type=DbBackendType.REDIS) |
Timo Tietavainen | cede9a3 | 2019-12-07 20:09:31 +0200 | [diff] [blame] | 55 | assert expected_config == self.config.get_params() |
| 56 | |
Timo Tietavainen | 598ca39 | 2020-01-08 16:49:11 +0200 | [diff] [blame] | 57 | def test_get_params_function_can_return_fake_db_configuration(self, fake_db_config_fixture): |
Timo Tietavainen | cede9a3 | 2019-12-07 20:09:31 +0200 | [diff] [blame] | 58 | expected_config = _Configuration.Params(db_host=None, db_port=None, |
| 59 | db_sentinel_port=None, |
Timo Tietavainen | 598ca39 | 2020-01-08 16:49:11 +0200 | [diff] [blame] | 60 | db_sentinel_master_name=None, |
Timo Tietavainen | c6c9af2 | 2021-05-11 14:43:09 +0300 | [diff] [blame] | 61 | db_cluster_addr_list=None, |
Timo Tietavainen | 598ca39 | 2020-01-08 16:49:11 +0200 | [diff] [blame] | 62 | db_type=DbBackendType.FAKE_DICT) |
Timo Tietavainen | cede9a3 | 2019-12-07 20:09:31 +0200 | [diff] [blame] | 63 | assert expected_config == self.config.get_params() |
| 64 | |
Timo Tietavainen | db77539 | 2021-06-09 05:56:54 +0300 | [diff] [blame] | 65 | def test_get_event_separator_function_return_expected_separator(self, config_fixture): |
| 66 | assert "___" == _Configuration.get_event_separator() |
| 67 | |
Timo Tietavainen | 598ca39 | 2020-01-08 16:49:11 +0200 | [diff] [blame] | 68 | def test_get_params_function_can_raise_exception_if_wrong_fake_db_type(self): |
| 69 | with pytest.raises(ValueError, match=r"Configuration error"): |
| 70 | _Configuration(fake_db_backend='bad value') |
| 71 | |
| 72 | |
Timo Tietavainen | cede9a3 | 2019-12-07 20:09:31 +0200 | [diff] [blame] | 73 | def test_configuration_object_string_representation(self, config_fixture): |
Timo Tietavainen | c6c9af2 | 2021-05-11 14:43:09 +0300 | [diff] [blame] | 74 | expected_config_info = {'DB host': 'service-ricplt-dbaas-tcp-cluster-0.ricplt', |
Timo Tietavainen | cede9a3 | 2019-12-07 20:09:31 +0200 | [diff] [blame] | 75 | 'DB port': '10000', |
| 76 | 'DB master sentinel': 'my-master', |
Timo Tietavainen | 598ca39 | 2020-01-08 16:49:11 +0200 | [diff] [blame] | 77 | 'DB sentinel port': '11000', |
Timo Tietavainen | c6c9af2 | 2021-05-11 14:43:09 +0300 | [diff] [blame] | 78 | 'DB cluster address list': 'service-ricplt-dbaas-tcp-cluster-0.ricplt,service-ricplt-dbaas-tcp-cluster-1.ricplt', |
Timo Tietavainen | 598ca39 | 2020-01-08 16:49:11 +0200 | [diff] [blame] | 79 | 'DB type': 'REDIS'} |
Timo Tietavainen | cede9a3 | 2019-12-07 20:09:31 +0200 | [diff] [blame] | 80 | assert str(self.config) == str(expected_config_info) |
| 81 | |
Timo Tietavainen | 598ca39 | 2020-01-08 16:49:11 +0200 | [diff] [blame] | 82 | def test_configuration_object_string_representation_if_fake_db(self, fake_db_config_fixture): |
Timo Tietavainen | cede9a3 | 2019-12-07 20:09:31 +0200 | [diff] [blame] | 83 | expected_config_info = {'DB host': None, |
| 84 | 'DB port': None, |
| 85 | 'DB master sentinel': None, |
Timo Tietavainen | 598ca39 | 2020-01-08 16:49:11 +0200 | [diff] [blame] | 86 | 'DB sentinel port': None, |
Timo Tietavainen | c6c9af2 | 2021-05-11 14:43:09 +0300 | [diff] [blame] | 87 | 'DB cluster address list': None, |
Timo Tietavainen | 598ca39 | 2020-01-08 16:49:11 +0200 | [diff] [blame] | 88 | 'DB type': 'FAKE_DICT'} |
Timo Tietavainen | cede9a3 | 2019-12-07 20:09:31 +0200 | [diff] [blame] | 89 | assert str(self.config) == str(expected_config_info) |