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 |
| 23 | from unittest.mock import patch |
| 24 | |
| 25 | import requests |
| 26 | import responses |
| 27 | from tenacity import wait_none |
| 28 | |
| 29 | from pmsh_service.mod.config_handler import ConfigHandler |
| 30 | |
| 31 | |
| 32 | class ConfigHandlerTest(unittest.TestCase): |
| 33 | |
| 34 | def setUp(self): |
| 35 | self.env_vars = {'CONFIG_BINDING_SERVICE_SERVICE_HOST': 'cbs_hostname', |
| 36 | 'CONFIG_BINDING_SERVICE_SERVICE_PORT': '10000', |
| 37 | 'HOSTNAME': 'hostname'} |
| 38 | for key, value in self.env_vars.items(): |
| 39 | environ[key] = value |
| 40 | self.cbs_url = 'http://cbs_hostname:10000/service_component_all/hostname' |
| 41 | self.expected_config = self._get_expected_config() |
| 42 | |
| 43 | def test_missing_environment_variable(self): |
| 44 | for key, value in self.env_vars.items(): |
| 45 | with self.assertRaises(KeyError): |
| 46 | environ.pop(key) |
| 47 | test_value = globals()[value] |
| 48 | test_value() |
| 49 | environ[key] = value |
| 50 | |
| 51 | @responses.activate |
| 52 | def test_get_config_success(self): |
| 53 | responses.add(responses.GET, self.cbs_url, json=json.dumps(self.expected_config), |
| 54 | status=200) |
| 55 | |
| 56 | config_handler = ConfigHandler() |
| 57 | config_handler.get_config.retry.wait = wait_none() |
| 58 | |
| 59 | self.assertEqual(self.expected_config, config_handler.get_config()) |
| 60 | |
| 61 | def test_get_config_already_exists(self): |
| 62 | config_handler = ConfigHandler() |
| 63 | expected_config = self._get_expected_config() |
| 64 | config_handler._config = expected_config |
| 65 | |
| 66 | with patch.object(requests, 'get') as mock_get_request: |
| 67 | actual_config = config_handler.get_config() |
| 68 | |
| 69 | self.assertEqual(0, mock_get_request.call_count) |
| 70 | self.assertEqual(expected_config, actual_config) |
| 71 | |
| 72 | @responses.activate |
| 73 | def test_get_config_error(self): |
| 74 | responses.add(responses.GET, self.cbs_url, status=404) |
| 75 | config_handler = ConfigHandler() |
| 76 | config_handler.get_config.retry.wait = wait_none() |
| 77 | |
| 78 | with self.assertRaises(Exception): |
| 79 | config_handler.get_config() |
| 80 | |
| 81 | @responses.activate |
| 82 | def test_get_config_max_retries_error(self): |
| 83 | retry_limit = 5 |
| 84 | config_handler = ConfigHandler() |
| 85 | config_handler.get_config.retry.wait = wait_none() |
| 86 | |
| 87 | for __ in range(retry_limit): |
| 88 | responses.add(responses.GET, self.cbs_url, status=500) |
| 89 | |
| 90 | with self.assertRaises(Exception): |
| 91 | config_handler.get_config() |
| 92 | self.assertEqual(retry_limit, len(responses.calls)) |
| 93 | |
| 94 | @responses.activate |
| 95 | def test_get_config_less_than_5_retries_success(self): |
| 96 | retry_attempts = 4 |
| 97 | responses.add(responses.GET, self.cbs_url, status=500) |
| 98 | responses.add(responses.GET, self.cbs_url, status=400) |
| 99 | responses.add(responses.GET, self.cbs_url, status=300) |
| 100 | responses.add(responses.GET, self.cbs_url, json=json.dumps(self.expected_config), |
| 101 | status=200) |
| 102 | |
| 103 | config_handler = ConfigHandler() |
| 104 | config_handler.get_config.retry.wait = wait_none() |
| 105 | config_handler.get_config() |
| 106 | |
| 107 | self.assertEqual(retry_attempts, len(responses.calls)) |
| 108 | |
| 109 | @staticmethod |
| 110 | def _get_expected_config(): |
| 111 | with open(path.join(path.dirname(__file__), 'expected_config.json'))as json_file: |
| 112 | return json.load(json_file) |