Skip to content

Commit

Permalink
21 2 (#77)
Browse files Browse the repository at this point in the history
* 21 - Improving exception when an invalid type is used in the config

* Bumping the version
  • Loading branch information
mtakaki committed Jan 6, 2020
1 parent a13a42d commit bcafbd6
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 2 deletions.
3 changes: 3 additions & 0 deletions cachet_url_monitor/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,9 @@ def create(configuration):
'LATENCY': Latency,
'REGEX': Regex
}
if configuration['type'] not in expectations:
raise ConfigurationValidationError(f"Invalid type: {configuration['type']}")

return expectations.get(configuration['type'])(configuration)

def __init__(self, configuration):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from setuptools import setup

setup(name='cachet-url-monitor',
version='0.6.0',
version='0.6.1',
description='Cachet URL monitor plugin',
author='Mitsuo Takaki',
author_email='mitsuotakaki@gmail.com',
Expand Down
26 changes: 26 additions & 0 deletions tests/configs/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
endpoints:
- name: foo
url: http://localhost:8080/swagger
method: GET
header:
SOME-HEADER: SOME-VALUE
timeout: 0.01
expectation:
- type: HTTP_STATUS
status_range: 200-300
incident: MAJOR
- type: LATENCY
threshold: 1
- type: REGEX
regex: '.*(<body).*'
allowed_fails: 0
component_id: 1
action:
- CREATE_INCIDENT
- UPDATE_STATUS
public_incidents: true
latency_unit: ms
frequency: 30
cachet:
api_url: https://demo.cachethq.io/api/v1
token: my_token
22 changes: 22 additions & 0 deletions tests/configs/config_invalid_type.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
endpoints:
- name: foo
url: http://localhost:8080/swagger
method: GET
header:
SOME-HEADER: SOME-VALUE
timeout: 0.01
expectation:
- type: HTTP
status_range: 200-300
incident: MAJOR
allowed_fails: 0
component_id: 1
action:
- CREATE_INCIDENT
- UPDATE_STATUS
public_incidents: true
latency_unit: ms
frequency: 30
cachet:
api_url: https://demo.cachethq.io/api/v1
token: my_token
28 changes: 28 additions & 0 deletions tests/configs/config_multiple_urls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
endpoints:
- name: foo
url: http://localhost:8080/swagger
method: GET
expectation:
- type: HTTP_STATUS
status_range: 200-300
allowed_fails: 0
component_id: 1
latency_unit: ms
frequency: 30
timeout: 1
public_incidents: true
- name: bar
url: http://localhost:8080/bar
method: POST
expectation:
- type: HTTP_STATUS
status_range: 500
allowed_fails: 0
component_id: 2
latency_unit: ms
frequency: 30
timeout: 1
public_incidents: true
cachet:
api_url: https://demo.cachethq.io/api/v1
token: my_token
58 changes: 57 additions & 1 deletion tests/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import unittest

import mock
import pytest
from requests import ConnectionError, HTTPError, Timeout
from yaml import load, SafeLoader

Expand Down Expand Up @@ -32,7 +33,8 @@ def get(url, headers):

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

self.configuration = Configuration(load(open('config.yml', 'r'), SafeLoader), 0)
self.configuration = Configuration(
load(open(os.path.join(os.path.dirname(__file__), 'configs/config.yml'), 'rt'), SafeLoader), 0)
sys.modules['requests'].Timeout = Timeout
sys.modules['requests'].ConnectionError = ConnectionError
sys.modules['requests'].HTTPError = HTTPError
Expand Down Expand Up @@ -174,3 +176,57 @@ def put(url, params=None, headers=None):
self.assertEqual(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_OPERATIONAL,
'Incorrect component update parameters')
self.configuration.push_status()


class ConfigurationMultipleUrlTest(unittest.TestCase):
@mock.patch.dict(os.environ, {'CACHET_TOKEN': 'token2'})
def setUp(self):
def getLogger(name):
self.mock_logger = mock.Mock()
return self.mock_logger

sys.modules['logging'].getLogger = getLogger

def get(url, headers):
get_return = mock.Mock()
get_return.ok = True
get_return.json = mock.Mock()
get_return.json.return_value = {'data': {'status': 1, 'default_value': 0.5}}
return get_return

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

config_yaml = load(open(os.path.join(os.path.dirname(__file__), 'configs/config_multiple_urls.yml'), 'rt'),
SafeLoader)
self.configuration = []

for index in range(len(config_yaml['endpoints'])):
self.configuration.append(Configuration(config_yaml, index))

sys.modules['requests'].Timeout = Timeout
sys.modules['requests'].ConnectionError = ConnectionError
sys.modules['requests'].HTTPError = HTTPError

def test_init(self):
expected_method = ['GET', 'POST']
expected_url = ['http://localhost:8080/swagger', 'http://localhost:8080/bar']

for index in range(len(self.configuration)):
config = self.configuration[index]
self.assertEqual(len(config.data), 2, 'Number of root elements in config.yml is incorrect')
self.assertEqual(len(config.expectations), 1, 'Number of expectations read from file is incorrect')
self.assertDictEqual(config.headers, {'X-Cachet-Token': 'token2'}, 'Header was not set correctly')
self.assertEqual(config.api_url, 'https://demo.cachethq.io/api/v1',
'Cachet API URL was set incorrectly')

self.assertEqual(expected_method[index], config.endpoint_method)
self.assertEqual(expected_url[index], config.endpoint_url)


class ConfigurationNegativeTest(unittest.TestCase):
@mock.patch.dict(os.environ, {'CACHET_TOKEN': 'token2'})
def test_init(self):
with pytest.raises(cachet_url_monitor.configuration.ConfigurationValidationError):
self.configuration = Configuration(
load(open(os.path.join(os.path.dirname(__file__), 'configs/config_invalid_type.yml'), 'rt'),
SafeLoader), 0)

0 comments on commit bcafbd6

Please sign in to comment.