|
3 | 3 | import unittest |
4 | 4 |
|
5 | 5 | import mock |
| 6 | +import pytest |
6 | 7 | from requests import ConnectionError, HTTPError, Timeout |
7 | 8 | from yaml import load, SafeLoader |
8 | 9 |
|
@@ -32,7 +33,8 @@ def get(url, headers): |
32 | 33 |
|
33 | 34 | sys.modules['requests'].get = get |
34 | 35 |
|
35 | | - self.configuration = Configuration(load(open('config.yml', 'r'), SafeLoader), 0) |
| 36 | + self.configuration = Configuration( |
| 37 | + load(open(os.path.join(os.path.dirname(__file__), 'configs/config.yml'), 'rt'), SafeLoader), 0) |
36 | 38 | sys.modules['requests'].Timeout = Timeout |
37 | 39 | sys.modules['requests'].ConnectionError = ConnectionError |
38 | 40 | sys.modules['requests'].HTTPError = HTTPError |
@@ -174,3 +176,57 @@ def put(url, params=None, headers=None): |
174 | 176 | self.assertEqual(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_OPERATIONAL, |
175 | 177 | 'Incorrect component update parameters') |
176 | 178 | self.configuration.push_status() |
| 179 | + |
| 180 | + |
| 181 | +class ConfigurationMultipleUrlTest(unittest.TestCase): |
| 182 | + @mock.patch.dict(os.environ, {'CACHET_TOKEN': 'token2'}) |
| 183 | + def setUp(self): |
| 184 | + def getLogger(name): |
| 185 | + self.mock_logger = mock.Mock() |
| 186 | + return self.mock_logger |
| 187 | + |
| 188 | + sys.modules['logging'].getLogger = getLogger |
| 189 | + |
| 190 | + def get(url, headers): |
| 191 | + get_return = mock.Mock() |
| 192 | + get_return.ok = True |
| 193 | + get_return.json = mock.Mock() |
| 194 | + get_return.json.return_value = {'data': {'status': 1, 'default_value': 0.5}} |
| 195 | + return get_return |
| 196 | + |
| 197 | + sys.modules['requests'].get = get |
| 198 | + |
| 199 | + config_yaml = load(open(os.path.join(os.path.dirname(__file__), 'configs/config_multiple_urls.yml'), 'rt'), |
| 200 | + SafeLoader) |
| 201 | + self.configuration = [] |
| 202 | + |
| 203 | + for index in range(len(config_yaml['endpoints'])): |
| 204 | + self.configuration.append(Configuration(config_yaml, index)) |
| 205 | + |
| 206 | + sys.modules['requests'].Timeout = Timeout |
| 207 | + sys.modules['requests'].ConnectionError = ConnectionError |
| 208 | + sys.modules['requests'].HTTPError = HTTPError |
| 209 | + |
| 210 | + def test_init(self): |
| 211 | + expected_method = ['GET', 'POST'] |
| 212 | + expected_url = ['http://localhost:8080/swagger', 'http://localhost:8080/bar'] |
| 213 | + |
| 214 | + for index in range(len(self.configuration)): |
| 215 | + config = self.configuration[index] |
| 216 | + self.assertEqual(len(config.data), 2, 'Number of root elements in config.yml is incorrect') |
| 217 | + self.assertEqual(len(config.expectations), 1, 'Number of expectations read from file is incorrect') |
| 218 | + self.assertDictEqual(config.headers, {'X-Cachet-Token': 'token2'}, 'Header was not set correctly') |
| 219 | + self.assertEqual(config.api_url, 'https://demo.cachethq.io/api/v1', |
| 220 | + 'Cachet API URL was set incorrectly') |
| 221 | + |
| 222 | + self.assertEqual(expected_method[index], config.endpoint_method) |
| 223 | + self.assertEqual(expected_url[index], config.endpoint_url) |
| 224 | + |
| 225 | + |
| 226 | +class ConfigurationNegativeTest(unittest.TestCase): |
| 227 | + @mock.patch.dict(os.environ, {'CACHET_TOKEN': 'token2'}) |
| 228 | + def test_init(self): |
| 229 | + with pytest.raises(cachet_url_monitor.configuration.ConfigurationValidationError): |
| 230 | + self.configuration = Configuration( |
| 231 | + load(open(os.path.join(os.path.dirname(__file__), 'configs/config_invalid_type.yml'), 'rt'), |
| 232 | + SafeLoader), 0) |
0 commit comments