#10 - Creating auxiliary client class to generate configuration class (#78)

* #10 - Creating auxiliary client class to generate configuration class based on cachet's component list.

* Updating the python version in codacy to reduce false positives

* Moving some of the cachet operations to the client class to clean up the configuration class and making better constants

* Refactoring status to have proper classes and adding more tests. Refactoring the requests tests to use requests-mock.

* Removing unused imports from test_scheduler

* Adding more tests and the ability to run the client from command line

* Updating README and client arg parsing

* Fixing broken unit tests
This commit is contained in:
mtakaki
2020-01-18 13:55:07 -08:00
committed by GitHub
parent bcafbd64f7
commit 5be0217c00
13 changed files with 530 additions and 269 deletions

147
tests/test_client.py Normal file
View File

@@ -0,0 +1,147 @@
#!/usr/bin/env python
import unittest
from typing import Dict, List
import requests_mock
from cachet_url_monitor.client import CachetClient
from cachet_url_monitor.exceptions import MetricNonexistentError
from cachet_url_monitor.status import ComponentStatus
TOKEN: str = 'token_123'
CACHET_URL: str = 'http://foo.localhost'
JSON: Dict[str, List[Dict[str, int]]] = {'data': [{'id': 1}]}
class ClientTest(unittest.TestCase):
def setUp(self):
self.client = CachetClient('foo.localhost', TOKEN)
def test_init(self):
self.assertEqual(self.client.headers, {'X-Cachet-Token': TOKEN}, 'Header was not set correctly')
self.assertEqual(self.client.url, CACHET_URL, 'Cachet API URL was set incorrectly')
@requests_mock.mock()
def test_get_components(self, m):
m.get(f'{CACHET_URL}/components', json=JSON, headers={'X-Cachet-Token': TOKEN})
components = self.client.get_components()
self.assertEqual(components, [{'id': 1}],
'Getting components list is incorrect.')
@requests_mock.mock()
def test_get_metrics(self, m):
m.get(f'{CACHET_URL}/metrics', json=JSON)
metrics = self.client.get_metrics()
self.assertEqual(metrics, [{'id': 1}],
'Getting metrics list is incorrect.')
@requests_mock.mock()
def test_generate_config(self, m):
def components():
return {
'data': [
{
'id': '1',
'name': 'apache',
'link': 'http://abc.def',
'enabled': True
},
{
'id': '2',
'name': 'haproxy',
'link': 'http://ghi.jkl',
'enabled': False
},
{
'id': '3',
'name': 'nginx',
'link': 'http://mno.pqr',
'enabled': True
}
]
}
m.get(f'{CACHET_URL}/components', json=components(), headers={'X-Cachet-Token': TOKEN})
config = self.client.generate_config()
self.assertEqual(config, {
'cachet': {
'api_url': CACHET_URL,
'token': TOKEN
},
'endpoints': [
{
'name': 'apache',
'url': 'http://abc.def',
'method': 'GET',
'timeout': 1,
'expectation': [
{
'type': 'HTTP_STATUS',
'status_range': '200-300',
'incident': 'MAJOR'
}
],
'allowed_fails': 0,
'frequency': 30,
'component_id': '1',
'action': [
'CREATE_INCIDENT',
'UPDATE_STATUS',
],
'public_incidents': True,
},
{
'name': 'nginx',
'url': 'http://mno.pqr',
'method': 'GET',
'timeout': 1,
'expectation': [
{
'type': 'HTTP_STATUS',
'status_range': '200-300',
'incident': 'MAJOR'
}
],
'allowed_fails': 0,
'frequency': 30,
'component_id': '3',
'action': [
'CREATE_INCIDENT',
'UPDATE_STATUS',
],
'public_incidents': True,
}
]
}, 'Generated config is incorrect.')
@requests_mock.mock()
def test_get_default_metric_value(self, m):
m.get(f'{CACHET_URL}/metrics/123', json={'data': {'default_value': 0.456}}, headers={'X-Cachet-Token': TOKEN})
default_metric_value = self.client.get_default_metric_value(123)
self.assertEqual(default_metric_value, 0.456,
'Getting default metric value is incorrect.')
@requests_mock.mock()
def test_get_default_metric_value_invalid_id(self, m):
m.get(f'{CACHET_URL}/metrics/123', headers={'X-Cachet-Token': TOKEN}, status_code=400)
with self.assertRaises(MetricNonexistentError):
self.client.get_default_metric_value(123)
@requests_mock.mock()
def test_get_component_status(self, m):
def json():
return {
'data': {
'status': ComponentStatus.OPERATIONAL.value
}
}
m.get(f'{CACHET_URL}/components/123', json=json(), headers={'X-Cachet-Token': TOKEN})
status = self.client.get_component_status(123)
self.assertEqual(status, ComponentStatus.OPERATIONAL,
'Getting component status value is incorrect.')

View File

@@ -4,12 +4,12 @@ import unittest
import mock
import pytest
from requests import ConnectionError, HTTPError, Timeout
import requests
import requests_mock
from yaml import load, SafeLoader
import cachet_url_monitor.status
sys.modules['requests'] = mock.Mock()
sys.modules['logging'] = mock.Mock()
from cachet_url_monitor.configuration import Configuration
import os
@@ -24,20 +24,20 @@ class ConfigurationTest(unittest.TestCase):
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
# 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
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
# sys.modules['requests'].Timeout = Timeout
# sys.modules['requests'].ConnectionError = ConnectionError
# sys.modules['requests'].HTTPError = HTTPError
def test_init(self):
self.assertEqual(len(self.configuration.data), 2, 'Number of root elements in config.yml is incorrect')
@@ -47,133 +47,69 @@ class ConfigurationTest(unittest.TestCase):
'Cachet API URL was set incorrectly')
self.assertDictEqual(self.configuration.endpoint_header, {'SOME-HEADER': 'SOME-VALUE'}, 'Header is incorrect')
def test_evaluate(self):
def total_seconds():
return 0.1
def request(method, url, headers, timeout=None):
response = mock.Mock()
response.status_code = 200
response.elapsed = mock.Mock()
response.elapsed.total_seconds = total_seconds
response.text = '<body>'
return response
sys.modules['requests'].request = request
@requests_mock.mock()
def test_evaluate(self, m):
m.get('http://localhost:8080/swagger', text='<body>')
self.configuration.evaluate()
self.assertEqual(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_OPERATIONAL,
self.assertEqual(self.configuration.status, cachet_url_monitor.status.ComponentStatus.OPERATIONAL,
'Component status set incorrectly')
def test_evaluate_without_header(self):
def total_seconds():
return 0.1
def request(method, url, headers=None, timeout=None):
response = mock.Mock()
response.status_code = 200
response.elapsed = mock.Mock()
response.elapsed.total_seconds = total_seconds
response.text = '<body>'
return response
sys.modules['requests'].request = request
@requests_mock.mock()
def test_evaluate_without_header(self, m):
m.get('http://localhost:8080/swagger', text='<body>')
self.configuration.evaluate()
self.assertEqual(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_OPERATIONAL,
self.assertEqual(self.configuration.status, cachet_url_monitor.status.ComponentStatus.OPERATIONAL,
'Component status set incorrectly')
def test_evaluate_with_failure(self):
def total_seconds():
return 0.1
def request(method, url, headers, timeout=None):
response = mock.Mock()
# We are expecting a 200 response, so this will fail the expectation.
response.status_code = 400
response.elapsed = mock.Mock()
response.elapsed.total_seconds = total_seconds
response.text = '<body>'
return response
sys.modules['requests'].request = request
@requests_mock.mock()
def test_evaluate_with_failure(self, m):
m.get('http://localhost:8080/swagger', text='<body>', status_code=400)
self.configuration.evaluate()
self.assertEqual(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_MAJOR_OUTAGE,
self.assertEqual(self.configuration.status, cachet_url_monitor.status.ComponentStatus.MAJOR_OUTAGE,
'Component status set incorrectly or custom incident status is incorrectly parsed')
def test_evaluate_with_timeout(self):
def request(method, url, headers, timeout=None):
self.assertEqual(method, 'GET', 'Incorrect HTTP method')
self.assertEqual(url, 'http://localhost:8080/swagger', 'Monitored URL is incorrect')
self.assertEqual(timeout, 0.010)
raise Timeout()
sys.modules['requests'].request = request
@requests_mock.mock()
def test_evaluate_with_timeout(self, m):
m.get('http://localhost:8080/swagger', exc=requests.Timeout)
self.configuration.evaluate()
self.assertEqual(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_PERFORMANCE_ISSUES,
self.assertEqual(self.configuration.status, cachet_url_monitor.status.ComponentStatus.PERFORMANCE_ISSUES,
'Component status set incorrectly')
self.mock_logger.warning.assert_called_with('Request timed out')
def test_evaluate_with_connection_error(self):
def request(method, url, headers, timeout=None):
self.assertEqual(method, 'GET', 'Incorrect HTTP method')
self.assertEqual(url, 'http://localhost:8080/swagger', 'Monitored URL is incorrect')
self.assertEqual(timeout, 0.010)
raise ConnectionError()
sys.modules['requests'].request = request
@requests_mock.mock()
def test_evaluate_with_connection_error(self, m):
m.get('http://localhost:8080/swagger', exc=requests.ConnectionError)
self.configuration.evaluate()
self.assertEqual(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_PARTIAL_OUTAGE,
self.assertEqual(self.configuration.status, cachet_url_monitor.status.ComponentStatus.PARTIAL_OUTAGE,
'Component status set incorrectly')
self.mock_logger.warning.assert_called_with('The URL is unreachable: GET http://localhost:8080/swagger')
def test_evaluate_with_http_error(self):
def request(method, url, headers, timeout=None):
self.assertEqual(method, 'GET', 'Incorrect HTTP method')
self.assertEqual(url, 'http://localhost:8080/swagger', 'Monitored URL is incorrect')
self.assertEqual(timeout, 0.010)
raise HTTPError()
sys.modules['requests'].request = request
@requests_mock.mock()
def test_evaluate_with_http_error(self, m):
m.get('http://localhost:8080/swagger', exc=requests.HTTPError)
self.configuration.evaluate()
self.assertEqual(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_PARTIAL_OUTAGE,
self.assertEqual(self.configuration.status, cachet_url_monitor.status.ComponentStatus.PARTIAL_OUTAGE,
'Component status set incorrectly')
self.mock_logger.exception.assert_called_with('Unexpected HTTP response')
def test_push_status(self):
def put(url, params=None, headers=None):
self.assertEqual(url, 'https://demo.cachethq.io/api/v1/components/1', 'Incorrect cachet API URL')
self.assertDictEqual(params, {'id': 1, 'status': 1}, 'Incorrect component update parameters')
self.assertDictEqual(headers, {'X-Cachet-Token': 'token2'}, 'Incorrect component update parameters')
response = mock.Mock()
response.status_code = 200
return response
sys.modules['requests'].put = put
self.assertEqual(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_OPERATIONAL,
@requests_mock.mock()
def test_push_status(self, m):
m.put('https://demo.cachethq.io/api/v1/components/1?id=1&status=1', headers={'X-Cachet-Token': 'token2'})
self.assertEqual(self.configuration.status, cachet_url_monitor.status.ComponentStatus.OPERATIONAL,
'Incorrect component update parameters')
self.configuration.push_status()
def test_push_status_with_failure(self):
def put(url, params=None, headers=None):
self.assertEqual(url, 'https://demo.cachethq.io/api/v1/components/1', 'Incorrect cachet API URL')
self.assertDictEqual(params, {'id': 1, 'status': 1}, 'Incorrect component update parameters')
self.assertDictEqual(headers, {'X-Cachet-Token': 'token2'}, 'Incorrect component update parameters')
response = mock.Mock()
response.status_code = 400
return response
sys.modules['requests'].put = put
self.assertEqual(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_OPERATIONAL,
@requests_mock.mock()
def test_push_status_with_failure(self, m):
m.put('https://demo.cachethq.io/api/v1/components/1?id=1&status=1', headers={'X-Cachet-Token': 'token2'},
status_code=400)
self.assertEqual(self.configuration.status, cachet_url_monitor.status.ComponentStatus.OPERATIONAL,
'Incorrect component update parameters')
self.configuration.push_status()
@@ -181,21 +117,6 @@ class ConfigurationTest(unittest.TestCase):
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 = []
@@ -203,10 +124,6 @@ class ConfigurationMultipleUrlTest(unittest.TestCase):
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']

View File

@@ -7,6 +7,7 @@ import pytest
from cachet_url_monitor.configuration import HttpStatus, Regex
from cachet_url_monitor.configuration import Latency
from cachet_url_monitor.status import ComponentStatus
class LatencyTest(unittest.TestCase):
@@ -25,7 +26,7 @@ class LatencyTest(unittest.TestCase):
request.elapsed = elapsed
elapsed.total_seconds = total_seconds
assert self.expectation.get_status(request) == 1
assert self.expectation.get_status(request) == ComponentStatus.OPERATIONAL
def test_get_status_unhealthy(self):
def total_seconds():
@@ -36,7 +37,7 @@ class LatencyTest(unittest.TestCase):
request.elapsed = elapsed
elapsed.total_seconds = total_seconds
assert self.expectation.get_status(request) == 2
assert self.expectation.get_status(request) == ComponentStatus.PERFORMANCE_ISSUES
def test_get_message(self):
def total_seconds():
@@ -73,13 +74,13 @@ class HttpStatusTest(unittest.TestCase):
request = mock.Mock()
request.status_code = 200
assert self.expectation.get_status(request) == 1
assert self.expectation.get_status(request) == ComponentStatus.OPERATIONAL
def test_get_status_unhealthy(self):
request = mock.Mock()
request.status_code = 400
assert self.expectation.get_status(request) == 3
assert self.expectation.get_status(request) == ComponentStatus.PARTIAL_OUTAGE
def test_get_message(self):
request = mock.Mock()
@@ -100,13 +101,13 @@ class RegexTest(unittest.TestCase):
request = mock.Mock()
request.text = 'We could find stuff\n in this body.'
assert self.expectation.get_status(request) == 1
assert self.expectation.get_status(request) == ComponentStatus.OPERATIONAL
def test_get_status_unhealthy(self):
request = mock.Mock()
request.text = 'We will not find it here'
assert self.expectation.get_status(request) == 3
assert self.expectation.get_status(request) == ComponentStatus.PARTIAL_OUTAGE
def test_get_message(self):
request = mock.Mock()

View File

@@ -3,7 +3,6 @@ import sys
import unittest
import mock
from yaml import load, SafeLoader
sys.modules['schedule'] = mock.Mock()
from cachet_url_monitor.scheduler import Agent, Scheduler
@@ -46,13 +45,43 @@ class SchedulerTest(unittest.TestCase):
mock_requests.get = get
self.scheduler = Scheduler(load(open('config.yml', 'r'), SafeLoader), 0)
self.agent = mock.MagicMock()
self.scheduler = Scheduler(
{
'endpoints': [
{
'name': 'foo',
'url': 'http://localhost:8080/swagger',
'method': 'GET',
'expectation': [
{
'type': 'HTTP_STATUS',
'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'
}
}, self.agent)
def test_init(self):
assert self.scheduler.stop == False
self.assertFalse(self.scheduler.stop)
def test_start(self):
# TODO(mtakaki|2016-05-01): We need a better way of testing this method.
# Leaving it as a placeholder.
self.scheduler.stop = True
self.scheduler.start()
self.agent.start.assert_called()