* 21 - Improving exception when an invalid type is used in the config

* Bumping the version
This commit is contained in:
mtakaki
2020-01-06 07:40:11 -08:00
committed by GitHub
parent a13a42d51c
commit bcafbd64f7
6 changed files with 137 additions and 2 deletions

View File

@@ -329,6 +329,9 @@ class Expectation(object):
'LATENCY': Latency, 'LATENCY': Latency,
'REGEX': Regex 'REGEX': Regex
} }
if configuration['type'] not in expectations:
raise ConfigurationValidationError(f"Invalid type: {configuration['type']}")
return expectations.get(configuration['type'])(configuration) return expectations.get(configuration['type'])(configuration)
def __init__(self, configuration): def __init__(self, configuration):

View File

@@ -3,7 +3,7 @@
from setuptools import setup from setuptools import setup
setup(name='cachet-url-monitor', setup(name='cachet-url-monitor',
version='0.6.0', version='0.6.1',
description='Cachet URL monitor plugin', description='Cachet URL monitor plugin',
author='Mitsuo Takaki', author='Mitsuo Takaki',
author_email='mitsuotakaki@gmail.com', author_email='mitsuotakaki@gmail.com',

26
tests/configs/config.yml Normal file
View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -3,6 +3,7 @@ import sys
import unittest import unittest
import mock import mock
import pytest
from requests import ConnectionError, HTTPError, Timeout from requests import ConnectionError, HTTPError, Timeout
from yaml import load, SafeLoader from yaml import load, SafeLoader
@@ -32,7 +33,8 @@ class ConfigurationTest(unittest.TestCase):
sys.modules['requests'].get = get 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'].Timeout = Timeout
sys.modules['requests'].ConnectionError = ConnectionError sys.modules['requests'].ConnectionError = ConnectionError
sys.modules['requests'].HTTPError = HTTPError sys.modules['requests'].HTTPError = HTTPError
@@ -174,3 +176,57 @@ class ConfigurationTest(unittest.TestCase):
self.assertEqual(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_OPERATIONAL, self.assertEqual(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_OPERATIONAL,
'Incorrect component update parameters') 'Incorrect component update parameters')
self.configuration.push_status() 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)