emartin | 331a93a | 2019-12-11 11:48:53 +0000 | [diff] [blame] | 1 | # ============LICENSE_START=================================================== |
| 2 | # Copyright (C) 2019-2020 Nordix Foundation. |
| 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 | # SPDX-License-Identifier: Apache-2.0 |
| 17 | # ============LICENSE_END===================================================== |
| 18 | |
| 19 | import json |
| 20 | import unittest |
| 21 | from os import environ |
| 22 | from os import path |
emartin | 331a93a | 2019-12-11 11:48:53 +0000 | [diff] [blame] | 23 | |
emartin | 331a93a | 2019-12-11 11:48:53 +0000 | [diff] [blame] | 24 | import responses |
| 25 | from tenacity import wait_none |
| 26 | |
ERIMROB | 26b76c0 | 2020-02-12 11:35:20 +0000 | [diff] [blame^] | 27 | from mod.config_handler import ConfigHandler |
emartin | 331a93a | 2019-12-11 11:48:53 +0000 | [diff] [blame] | 28 | |
| 29 | |
efiacor | 8b3fc62 | 2020-01-24 13:19:01 +0000 | [diff] [blame] | 30 | class ConfigHandlerTestCase(unittest.TestCase): |
emartin | 331a93a | 2019-12-11 11:48:53 +0000 | [diff] [blame] | 31 | |
| 32 | def setUp(self): |
| 33 | self.env_vars = {'CONFIG_BINDING_SERVICE_SERVICE_HOST': 'cbs_hostname', |
| 34 | 'CONFIG_BINDING_SERVICE_SERVICE_PORT': '10000', |
| 35 | 'HOSTNAME': 'hostname'} |
| 36 | for key, value in self.env_vars.items(): |
| 37 | environ[key] = value |
| 38 | self.cbs_url = 'http://cbs_hostname:10000/service_component_all/hostname' |
efiacor | 8b3fc62 | 2020-01-24 13:19:01 +0000 | [diff] [blame] | 39 | with open(path.join(path.dirname(__file__), 'data/cbs_data_2.json'))as json_file: |
| 40 | self.expected_config = json.load(json_file) |
emartin | 331a93a | 2019-12-11 11:48:53 +0000 | [diff] [blame] | 41 | |
| 42 | def test_missing_environment_variable(self): |
| 43 | for key, value in self.env_vars.items(): |
| 44 | with self.assertRaises(KeyError): |
| 45 | environ.pop(key) |
| 46 | test_value = globals()[value] |
| 47 | test_value() |
| 48 | environ[key] = value |
| 49 | |
| 50 | @responses.activate |
| 51 | def test_get_config_success(self): |
ERIMROB | 26b76c0 | 2020-02-12 11:35:20 +0000 | [diff] [blame^] | 52 | responses.add(responses.GET, self.cbs_url, json=self.expected_config, |
emartin | 331a93a | 2019-12-11 11:48:53 +0000 | [diff] [blame] | 53 | status=200) |
| 54 | |
| 55 | config_handler = ConfigHandler() |
| 56 | config_handler.get_config.retry.wait = wait_none() |
| 57 | |
| 58 | self.assertEqual(self.expected_config, config_handler.get_config()) |
| 59 | |
emartin | 331a93a | 2019-12-11 11:48:53 +0000 | [diff] [blame] | 60 | @responses.activate |
| 61 | def test_get_config_error(self): |
| 62 | responses.add(responses.GET, self.cbs_url, status=404) |
| 63 | config_handler = ConfigHandler() |
| 64 | config_handler.get_config.retry.wait = wait_none() |
| 65 | |
| 66 | with self.assertRaises(Exception): |
| 67 | config_handler.get_config() |
| 68 | |
| 69 | @responses.activate |
| 70 | def test_get_config_max_retries_error(self): |
| 71 | retry_limit = 5 |
| 72 | config_handler = ConfigHandler() |
| 73 | config_handler.get_config.retry.wait = wait_none() |
| 74 | |
| 75 | for __ in range(retry_limit): |
| 76 | responses.add(responses.GET, self.cbs_url, status=500) |
| 77 | |
| 78 | with self.assertRaises(Exception): |
| 79 | config_handler.get_config() |
| 80 | self.assertEqual(retry_limit, len(responses.calls)) |
| 81 | |
| 82 | @responses.activate |
| 83 | def test_get_config_less_than_5_retries_success(self): |
| 84 | retry_attempts = 4 |
| 85 | responses.add(responses.GET, self.cbs_url, status=500) |
| 86 | responses.add(responses.GET, self.cbs_url, status=400) |
| 87 | responses.add(responses.GET, self.cbs_url, status=300) |
| 88 | responses.add(responses.GET, self.cbs_url, json=json.dumps(self.expected_config), |
| 89 | status=200) |
| 90 | |
| 91 | config_handler = ConfigHandler() |
| 92 | config_handler.get_config.retry.wait = wait_none() |
| 93 | config_handler.get_config() |
| 94 | |
| 95 | self.assertEqual(retry_attempts, len(responses.calls)) |