mirror of
https://github.com/mtan93/cachet-url-monitor.git
synced 2026-05-13 14:26:07 +01:00
* #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:
+46
-129
@@ -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']
|
||||
|
||||
Reference in New Issue
Block a user