mirror of
https://github.com/mtan93/cachet-url-monitor.git
synced 2026-03-08 05:31:58 +00:00
Documenting better the code and small tweaks to the unit tests.
This commit is contained in:
@@ -38,10 +38,11 @@ class ConfigurationTest(unittest.TestCase):
|
||||
sys.modules['requests'].HTTPError = HTTPError
|
||||
|
||||
def test_init(self):
|
||||
assert len(self.configuration.data) == 3
|
||||
assert len(self.configuration.expectations) == 3
|
||||
assert self.configuration.headers == {'X-Cachet-Token': 'token2'}
|
||||
assert self.configuration.api_url == 'https://demo.cachethq.io/api/v1'
|
||||
self.assertEqual(len(self.configuration.data), 3, 'Configuration data size is incorrect')
|
||||
self.assertEquals(len(self.configuration.expectations), 3, 'Number of expectations read from file is incorrect')
|
||||
self.assertDictEqual(self.configuration.headers, {'X-Cachet-Token': 'token2'}, 'Header was not set correctly')
|
||||
self.assertEquals(self.configuration.api_url, 'https://demo.cachethq.io/api/v1',
|
||||
'Cachet API URL was set incorrectly')
|
||||
|
||||
def test_evaluate(self):
|
||||
def total_seconds():
|
||||
@@ -58,7 +59,8 @@ class ConfigurationTest(unittest.TestCase):
|
||||
sys.modules['requests'].request = request
|
||||
self.configuration.evaluate()
|
||||
|
||||
assert self.configuration.status == cachet_url_monitor.status.COMPONENT_STATUS_OPERATIONAL
|
||||
self.assertEquals(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_OPERATIONAL,
|
||||
'Component status set incorrectly')
|
||||
|
||||
def test_evaluate_with_failure(self):
|
||||
def total_seconds():
|
||||
@@ -76,76 +78,80 @@ class ConfigurationTest(unittest.TestCase):
|
||||
sys.modules['requests'].request = request
|
||||
self.configuration.evaluate()
|
||||
|
||||
assert self.configuration.status == cachet_url_monitor.status.COMPONENT_STATUS_PARTIAL_OUTAGE
|
||||
self.assertEquals(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_PARTIAL_OUTAGE,
|
||||
'Component status set incorrectly')
|
||||
|
||||
def test_evaluate_with_timeout(self):
|
||||
def request(method, url, timeout=None):
|
||||
assert method == 'GET'
|
||||
assert url == 'http://localhost:8080/swagger'
|
||||
assert timeout == 0.010
|
||||
self.assertEquals(method, 'GET', 'Incorrect HTTP method')
|
||||
self.assertEquals(url, 'http://localhost:8080/swagger', 'Monitored URL is incorrect')
|
||||
self.assertEquals(timeout, 0.010)
|
||||
|
||||
raise Timeout()
|
||||
|
||||
sys.modules['requests'].request = request
|
||||
self.configuration.evaluate()
|
||||
|
||||
assert self.configuration.status == cachet_url_monitor.status.COMPONENT_STATUS_PERFORMANCE_ISSUES
|
||||
self.assertEquals(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_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, timeout=None):
|
||||
assert method == 'GET'
|
||||
assert url == 'http://localhost:8080/swagger'
|
||||
assert timeout == 0.010
|
||||
self.assertEquals(method, 'GET', 'Incorrect HTTP method')
|
||||
self.assertEquals(url, 'http://localhost:8080/swagger', 'Monitored URL is incorrect')
|
||||
self.assertEquals(timeout, 0.010)
|
||||
|
||||
raise ConnectionError()
|
||||
|
||||
sys.modules['requests'].request = request
|
||||
self.configuration.evaluate()
|
||||
|
||||
assert self.configuration.status == 3
|
||||
self.mock_logger.warning.assert_called_with(('The URL is '
|
||||
'unreachable: GET http://localhost:8080/swagger'))
|
||||
self.assertEquals(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_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, timeout=None):
|
||||
assert method == 'GET'
|
||||
assert url == 'http://localhost:8080/swagger'
|
||||
assert timeout == 0.010
|
||||
self.assertEquals(method, 'GET', 'Incorrect HTTP method')
|
||||
self.assertEquals(url, 'http://localhost:8080/swagger', 'Monitored URL is incorrect')
|
||||
self.assertEquals(timeout, 0.010)
|
||||
|
||||
raise HTTPError()
|
||||
|
||||
sys.modules['requests'].request = request
|
||||
self.configuration.evaluate()
|
||||
|
||||
assert self.configuration.status == 3
|
||||
self.mock_logger.exception.assert_called_with(('Unexpected HTTP '
|
||||
'response'))
|
||||
self.assertEquals(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_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):
|
||||
assert url == 'https://demo.cachethq.io/api/v1/components/1'
|
||||
assert params == {'id': 1, 'status': 1}
|
||||
assert headers == {'X-Cachet-Token': 'token2'}
|
||||
self.assertEquals(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.configuration.status = 1
|
||||
self.assertEquals(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_OPERATIONAL,
|
||||
'Incorrect component update parameters')
|
||||
self.configuration.push_status()
|
||||
|
||||
def test_push_status_with_failure(self):
|
||||
def put(url, params=None, headers=None):
|
||||
assert url == 'https://demo.cachethq.io/api/v1/components/1'
|
||||
assert params == {'id': 1, 'status': 1}
|
||||
assert headers == {'X-Cachet-Token': 'token2'}
|
||||
self.assertEquals(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 = 300
|
||||
response.status_code = 400
|
||||
return response
|
||||
|
||||
sys.modules['requests'].put = put
|
||||
self.configuration.status = 1
|
||||
self.assertEquals(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_OPERATIONAL,
|
||||
'Incorrect component update parameters')
|
||||
self.configuration.push_status()
|
||||
|
||||
Reference in New Issue
Block a user