7b17221e755ebf974ed003cf423f70d4267c497c
[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
25
26 @pytest.fixture()
27 def config_fixture(request, monkeypatch):
28     monkeypatch.setenv('DBAAS_SERVICE_HOST', '10.20.30.40')
29     monkeypatch.setenv('DBAAS_SERVICE_PORT', '10000')
30     monkeypatch.setenv('DBAAS_SERVICE_SENTINEL_PORT', '11000')
31     monkeypatch.setenv('DBAAS_MASTER_NAME', 'my-master')
32     request.cls.config = _Configuration()
33     request.cls.config._read_configuration()
34
35
36 @pytest.fixture
37 def config_missing_fixture(request, monkeypatch):
38     monkeypatch.delenv('DBAAS_SERVICE_HOST', raising=False)
39     monkeypatch.delenv('DBAAS_SERVICE_PORT', raising=False)
40     monkeypatch.delenv('DBAAS_SERVICE_SENTINEL_PORT', raising=False)
41     monkeypatch.delenv('DBAAS_MASTER_NAME', raising=False)
42     request.cls.config = _Configuration()
43     request.cls.config._read_configuration()
44
45
46 class TestConfiguration:
47     def test_get_params_function_returns_read_configuration(self, config_fixture):
48         expected_config = _Configuration.Params(db_host='10.20.30.40', db_port='10000',
49                                                 db_sentinel_port='11000',
50                                                 db_sentinel_master_name='my-master')
51         assert expected_config == self.config.get_params()
52
53     def test_get_params_function_can_return_empty_configuration(self, config_missing_fixture):
54         expected_config = _Configuration.Params(db_host=None, db_port=None,
55                                                 db_sentinel_port=None,
56                                                 db_sentinel_master_name=None)
57         assert expected_config == self.config.get_params()
58
59     def test_configuration_object_string_representation(self, config_fixture):
60         expected_config_info = {'DB host': '10.20.30.40',
61                                 'DB port': '10000',
62                                 'DB master sentinel': 'my-master',
63                                 'DB sentinel port': '11000'}
64         assert str(self.config) == str(expected_config_info)
65
66     def test_configuration_object_string_representation_if_no_config(self, config_missing_fixture):
67         expected_config_info = {'DB host': None,
68                                 'DB port': None,
69                                 'DB master sentinel': None,
70                                 'DB sentinel port': None}
71         assert str(self.config) == str(expected_config_info)