1c89663ffed9775e0fd5e74c5d6b79ac87212dc3
[ric-plt/sdlpy.git] / ricsdl-package / tests / test_configuration.py
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
24 from ricsdl.configuration import DbBackendType
25
26
27 @pytest.fixture()
28 def config_fixture(request, monkeypatch):
29     monkeypatch.setenv('DBAAS_SERVICE_HOST', 'service-ricplt-dbaas-tcp-cluster-0.ricplt')
30     monkeypatch.setenv('DBAAS_SERVICE_PORT', '10000')
31     monkeypatch.setenv('DBAAS_SERVICE_SENTINEL_PORT', '11000')
32     monkeypatch.setenv('DBAAS_MASTER_NAME', 'my-master')
33     monkeypatch.setenv('DBAAS_CLUSTER_ADDR_LIST', 'service-ricplt-dbaas-tcp-cluster-0.ricplt,service-ricplt-dbaas-tcp-cluster-1.ricplt')
34     request.cls.config = _Configuration(fake_db_backend=None)
35
36
37 @pytest.fixture
38 def fake_db_config_fixture(request, monkeypatch):
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)
43     monkeypatch.delenv('DBAAS_CLUSTER_ADDR_LIST', raising=False)
44     request.cls.config = _Configuration(fake_db_backend='dict')
45
46
47 class TestConfiguration:
48     def test_get_params_function_returns_read_configuration(self, config_fixture):
49         expected_config = _Configuration.Params(db_host='service-ricplt-dbaas-tcp-cluster-0.ricplt',
50                                                 db_port='10000',
51                                                 db_sentinel_port='11000',
52                                                 db_sentinel_master_name='my-master',
53                                                 db_cluster_addr_list='service-ricplt-dbaas-tcp-cluster-0.ricplt,service-ricplt-dbaas-tcp-cluster-1.ricplt',
54                                                 db_type=DbBackendType.REDIS)
55         assert expected_config == self.config.get_params()
56
57     def test_get_params_function_can_return_fake_db_configuration(self, fake_db_config_fixture):
58         expected_config = _Configuration.Params(db_host=None, db_port=None,
59                                                 db_sentinel_port=None,
60                                                 db_sentinel_master_name=None,
61                                                 db_cluster_addr_list=None,
62                                                 db_type=DbBackendType.FAKE_DICT)
63         assert expected_config == self.config.get_params()
64
65     def test_get_event_separator_function_return_expected_separator(self, config_fixture):
66         assert "___" == _Configuration.get_event_separator()
67
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
73     def test_configuration_object_string_representation(self, config_fixture):
74         expected_config_info = {'DB host': 'service-ricplt-dbaas-tcp-cluster-0.ricplt',
75                                 'DB port': '10000',
76                                 'DB master sentinel': 'my-master',
77                                 'DB sentinel port': '11000',
78                                 'DB cluster address list': 'service-ricplt-dbaas-tcp-cluster-0.ricplt,service-ricplt-dbaas-tcp-cluster-1.ricplt',
79                                 'DB type': 'REDIS'}
80         assert str(self.config) == str(expected_config_info)
81
82     def test_configuration_object_string_representation_if_fake_db(self, fake_db_config_fixture):
83         expected_config_info = {'DB host': None,
84                                 'DB port': None,
85                                 'DB master sentinel': None,
86                                 'DB sentinel port': None,
87                                 'DB cluster address list': None,
88                                 'DB type': 'FAKE_DICT'}
89         assert str(self.config) == str(expected_config_info)