blob: 1c89663ffed9775e0fd5e74c5d6b79ac87212dc3 [file] [log] [blame]
Timo Tietavainencede9a32019-12-07 20:09:31 +02001# 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
22import pytest
23from ricsdl.configuration import _Configuration
Timo Tietavainen598ca392020-01-08 16:49:11 +020024from ricsdl.configuration import DbBackendType
Timo Tietavainencede9a32019-12-07 20:09:31 +020025
26
27@pytest.fixture()
28def config_fixture(request, monkeypatch):
Timo Tietavainenc6c9af22021-05-11 14:43:09 +030029 monkeypatch.setenv('DBAAS_SERVICE_HOST', 'service-ricplt-dbaas-tcp-cluster-0.ricplt')
Timo Tietavainencede9a32019-12-07 20:09:31 +020030 monkeypatch.setenv('DBAAS_SERVICE_PORT', '10000')
31 monkeypatch.setenv('DBAAS_SERVICE_SENTINEL_PORT', '11000')
32 monkeypatch.setenv('DBAAS_MASTER_NAME', 'my-master')
Timo Tietavainenc6c9af22021-05-11 14:43:09 +030033 monkeypatch.setenv('DBAAS_CLUSTER_ADDR_LIST', 'service-ricplt-dbaas-tcp-cluster-0.ricplt,service-ricplt-dbaas-tcp-cluster-1.ricplt')
Timo Tietavainen598ca392020-01-08 16:49:11 +020034 request.cls.config = _Configuration(fake_db_backend=None)
Timo Tietavainencede9a32019-12-07 20:09:31 +020035
36
37@pytest.fixture
Timo Tietavainen598ca392020-01-08 16:49:11 +020038def fake_db_config_fixture(request, monkeypatch):
Timo Tietavainencede9a32019-12-07 20:09:31 +020039 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 Tietavainenc6c9af22021-05-11 14:43:09 +030043 monkeypatch.delenv('DBAAS_CLUSTER_ADDR_LIST', raising=False)
Timo Tietavainen598ca392020-01-08 16:49:11 +020044 request.cls.config = _Configuration(fake_db_backend='dict')
Timo Tietavainencede9a32019-12-07 20:09:31 +020045
46
47class TestConfiguration:
48 def test_get_params_function_returns_read_configuration(self, config_fixture):
Timo Tietavainenc6c9af22021-05-11 14:43:09 +030049 expected_config = _Configuration.Params(db_host='service-ricplt-dbaas-tcp-cluster-0.ricplt',
50 db_port='10000',
Timo Tietavainencede9a32019-12-07 20:09:31 +020051 db_sentinel_port='11000',
Timo Tietavainen598ca392020-01-08 16:49:11 +020052 db_sentinel_master_name='my-master',
Timo Tietavainenc6c9af22021-05-11 14:43:09 +030053 db_cluster_addr_list='service-ricplt-dbaas-tcp-cluster-0.ricplt,service-ricplt-dbaas-tcp-cluster-1.ricplt',
Timo Tietavainen598ca392020-01-08 16:49:11 +020054 db_type=DbBackendType.REDIS)
Timo Tietavainencede9a32019-12-07 20:09:31 +020055 assert expected_config == self.config.get_params()
56
Timo Tietavainen598ca392020-01-08 16:49:11 +020057 def test_get_params_function_can_return_fake_db_configuration(self, fake_db_config_fixture):
Timo Tietavainencede9a32019-12-07 20:09:31 +020058 expected_config = _Configuration.Params(db_host=None, db_port=None,
59 db_sentinel_port=None,
Timo Tietavainen598ca392020-01-08 16:49:11 +020060 db_sentinel_master_name=None,
Timo Tietavainenc6c9af22021-05-11 14:43:09 +030061 db_cluster_addr_list=None,
Timo Tietavainen598ca392020-01-08 16:49:11 +020062 db_type=DbBackendType.FAKE_DICT)
Timo Tietavainencede9a32019-12-07 20:09:31 +020063 assert expected_config == self.config.get_params()
64
Timo Tietavainendb775392021-06-09 05:56:54 +030065 def test_get_event_separator_function_return_expected_separator(self, config_fixture):
66 assert "___" == _Configuration.get_event_separator()
67
Timo Tietavainen598ca392020-01-08 16:49:11 +020068 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 Tietavainencede9a32019-12-07 20:09:31 +020073 def test_configuration_object_string_representation(self, config_fixture):
Timo Tietavainenc6c9af22021-05-11 14:43:09 +030074 expected_config_info = {'DB host': 'service-ricplt-dbaas-tcp-cluster-0.ricplt',
Timo Tietavainencede9a32019-12-07 20:09:31 +020075 'DB port': '10000',
76 'DB master sentinel': 'my-master',
Timo Tietavainen598ca392020-01-08 16:49:11 +020077 'DB sentinel port': '11000',
Timo Tietavainenc6c9af22021-05-11 14:43:09 +030078 'DB cluster address list': 'service-ricplt-dbaas-tcp-cluster-0.ricplt,service-ricplt-dbaas-tcp-cluster-1.ricplt',
Timo Tietavainen598ca392020-01-08 16:49:11 +020079 'DB type': 'REDIS'}
Timo Tietavainencede9a32019-12-07 20:09:31 +020080 assert str(self.config) == str(expected_config_info)
81
Timo Tietavainen598ca392020-01-08 16:49:11 +020082 def test_configuration_object_string_representation_if_fake_db(self, fake_db_config_fixture):
Timo Tietavainencede9a32019-12-07 20:09:31 +020083 expected_config_info = {'DB host': None,
84 'DB port': None,
85 'DB master sentinel': None,
Timo Tietavainen598ca392020-01-08 16:49:11 +020086 'DB sentinel port': None,
Timo Tietavainenc6c9af22021-05-11 14:43:09 +030087 'DB cluster address list': None,
Timo Tietavainen598ca392020-01-08 16:49:11 +020088 'DB type': 'FAKE_DICT'}
Timo Tietavainencede9a32019-12-07 20:09:31 +020089 assert str(self.config) == str(expected_config_info)