blob: 5e80db5d8d199bd82fcbdd580e3a1201ae18c2fd [file] [log] [blame]
emartin331a93a2019-12-11 11:48:53 +00001# ============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
19import json
20import unittest
21from os import environ
22from os import path
emartin331a93a2019-12-11 11:48:53 +000023
emartin331a93a2019-12-11 11:48:53 +000024import responses
25from tenacity import wait_none
26
27from pmsh_service.mod.config_handler import ConfigHandler
28
29
efiacor8b3fc622020-01-24 13:19:01 +000030class ConfigHandlerTestCase(unittest.TestCase):
emartin331a93a2019-12-11 11:48:53 +000031
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'
efiacor8b3fc622020-01-24 13:19:01 +000039 with open(path.join(path.dirname(__file__), 'data/cbs_data_2.json'))as json_file:
40 self.expected_config = json.load(json_file)
emartin331a93a2019-12-11 11:48:53 +000041
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):
52 responses.add(responses.GET, self.cbs_url, json=json.dumps(self.expected_config),
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
emartin331a93a2019-12-11 11:48:53 +000060 @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))