Skip to content

Commit bcafbd6

Browse files
authored
21 2 (#77)
* 21 - Improving exception when an invalid type is used in the config * Bumping the version
1 parent a13a42d commit bcafbd6

File tree

6 files changed

+137
-2
lines changed

6 files changed

+137
-2
lines changed

cachet_url_monitor/configuration.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,9 @@ def create(configuration):
329329
'LATENCY': Latency,
330330
'REGEX': Regex
331331
}
332+
if configuration['type'] not in expectations:
333+
raise ConfigurationValidationError(f"Invalid type: {configuration['type']}")
334+
332335
return expectations.get(configuration['type'])(configuration)
333336

334337
def __init__(self, configuration):

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from setuptools import setup
44

55
setup(name='cachet-url-monitor',
6-
version='0.6.0',
6+
version='0.6.1',
77
description='Cachet URL monitor plugin',
88
author='Mitsuo Takaki',
99
author_email='[email protected]',

tests/configs/config.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
endpoints:
2+
- name: foo
3+
url: http://localhost:8080/swagger
4+
method: GET
5+
header:
6+
SOME-HEADER: SOME-VALUE
7+
timeout: 0.01
8+
expectation:
9+
- type: HTTP_STATUS
10+
status_range: 200-300
11+
incident: MAJOR
12+
- type: LATENCY
13+
threshold: 1
14+
- type: REGEX
15+
regex: '.*(<body).*'
16+
allowed_fails: 0
17+
component_id: 1
18+
action:
19+
- CREATE_INCIDENT
20+
- UPDATE_STATUS
21+
public_incidents: true
22+
latency_unit: ms
23+
frequency: 30
24+
cachet:
25+
api_url: https://demo.cachethq.io/api/v1
26+
token: my_token
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
endpoints:
2+
- name: foo
3+
url: http://localhost:8080/swagger
4+
method: GET
5+
header:
6+
SOME-HEADER: SOME-VALUE
7+
timeout: 0.01
8+
expectation:
9+
- type: HTTP
10+
status_range: 200-300
11+
incident: MAJOR
12+
allowed_fails: 0
13+
component_id: 1
14+
action:
15+
- CREATE_INCIDENT
16+
- UPDATE_STATUS
17+
public_incidents: true
18+
latency_unit: ms
19+
frequency: 30
20+
cachet:
21+
api_url: https://demo.cachethq.io/api/v1
22+
token: my_token
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
endpoints:
2+
- name: foo
3+
url: http://localhost:8080/swagger
4+
method: GET
5+
expectation:
6+
- type: HTTP_STATUS
7+
status_range: 200-300
8+
allowed_fails: 0
9+
component_id: 1
10+
latency_unit: ms
11+
frequency: 30
12+
timeout: 1
13+
public_incidents: true
14+
- name: bar
15+
url: http://localhost:8080/bar
16+
method: POST
17+
expectation:
18+
- type: HTTP_STATUS
19+
status_range: 500
20+
allowed_fails: 0
21+
component_id: 2
22+
latency_unit: ms
23+
frequency: 30
24+
timeout: 1
25+
public_incidents: true
26+
cachet:
27+
api_url: https://demo.cachethq.io/api/v1
28+
token: my_token

tests/test_configuration.py

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import unittest
44

55
import mock
6+
import pytest
67
from requests import ConnectionError, HTTPError, Timeout
78
from yaml import load, SafeLoader
89

@@ -32,7 +33,8 @@ def get(url, headers):
3233

3334
sys.modules['requests'].get = get
3435

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)
3638
sys.modules['requests'].Timeout = Timeout
3739
sys.modules['requests'].ConnectionError = ConnectionError
3840
sys.modules['requests'].HTTPError = HTTPError
@@ -174,3 +176,57 @@ def put(url, params=None, headers=None):
174176
self.assertEqual(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_OPERATIONAL,
175177
'Incorrect component update parameters')
176178
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

Comments
 (0)