mirror of
https://github.com/mtan93/cachet-url-monitor.git
synced 2026-03-08 05:31:58 +00:00
83 (#84)
* #83 - Fixing the bug that was preventing the status update * #83 - Refactoring unit tests for configuration to ensure we catch more cases
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
import sys
|
||||
import unittest
|
||||
|
||||
import mock
|
||||
import pytest
|
||||
@@ -8,151 +7,178 @@ import requests
|
||||
import requests_mock
|
||||
from yaml import load, SafeLoader
|
||||
|
||||
import cachet_url_monitor.status
|
||||
from cachet_url_monitor.client import CachetClient
|
||||
import cachet_url_monitor.exceptions
|
||||
import cachet_url_monitor.status
|
||||
|
||||
sys.modules['logging'] = mock.Mock()
|
||||
from cachet_url_monitor.configuration import Configuration
|
||||
import os
|
||||
|
||||
|
||||
class ConfigurationTest(unittest.TestCase):
|
||||
client: CachetClient
|
||||
configuration: Configuration
|
||||
@pytest.fixture()
|
||||
def mock_client():
|
||||
client = mock.Mock()
|
||||
client.get_component_status.return_value = cachet_url_monitor.status.ComponentStatus.OPERATIONAL
|
||||
yield client
|
||||
|
||||
def setUp(self):
|
||||
def getLogger(name):
|
||||
self.mock_logger = mock.Mock()
|
||||
return self.mock_logger
|
||||
|
||||
sys.modules['logging'].getLogger = getLogger
|
||||
self.client = mock.Mock()
|
||||
# We set the initial status to OPERATIONAL.
|
||||
self.client.get_component_status.return_value = cachet_url_monitor.status.ComponentStatus.OPERATIONAL
|
||||
self.configuration = Configuration(
|
||||
load(open(os.path.join(os.path.dirname(__file__), 'configs/config.yml'), 'rt'), SafeLoader), 0, self.client,
|
||||
'token2')
|
||||
@pytest.fixture()
|
||||
def config_file():
|
||||
with open(os.path.join(os.path.dirname(__file__), 'configs/config.yml'), 'rt') as yaml_file:
|
||||
config_file_data = load(yaml_file, SafeLoader)
|
||||
yield config_file_data
|
||||
|
||||
def test_init(self):
|
||||
self.assertEqual(len(self.configuration.data), 2, 'Number of root elements in config.yml is incorrect')
|
||||
self.assertEqual(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.assertDictEqual(self.configuration.endpoint_header, {'SOME-HEADER': 'SOME-VALUE'}, 'Header is incorrect')
|
||||
|
||||
@requests_mock.mock()
|
||||
def test_evaluate(self, m):
|
||||
@pytest.fixture()
|
||||
def multiple_urls_config_file():
|
||||
with open(os.path.join(os.path.dirname(__file__), 'configs/config_multiple_urls.yml'), 'rt') as yaml_file:
|
||||
config_file_data = load(yaml_file, SafeLoader)
|
||||
yield config_file_data
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def invalid_config_file():
|
||||
with open(os.path.join(os.path.dirname(__file__), 'configs/config_invalid_type.yml'), 'rt') as yaml_file:
|
||||
config_file_data = load(yaml_file, SafeLoader)
|
||||
yield config_file_data
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def mock_logger():
|
||||
mock_logger = mock.Mock()
|
||||
|
||||
def getLogger(name):
|
||||
return mock_logger
|
||||
|
||||
sys.modules['logging'].getLogger = getLogger
|
||||
yield mock_logger
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def configuration(config_file, mock_client, mock_logger):
|
||||
yield Configuration(config_file, 0, mock_client, 'token2')
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def multiple_urls_configuration(multiple_urls_config_file, mock_client, mock_logger):
|
||||
yield [Configuration(multiple_urls_config_file, index, mock_client, 'token2') for index in
|
||||
range(len(multiple_urls_config_file['endpoints']))]
|
||||
|
||||
|
||||
def test_init(configuration):
|
||||
assert len(configuration.data) == 2, 'Number of root elements in config.yml is incorrect'
|
||||
assert len(configuration.expectations) == 3, 'Number of expectations read from file is incorrect'
|
||||
assert configuration.headers == {'X-Cachet-Token': 'token2'}, 'Header was not set correctly'
|
||||
assert configuration.endpoint_header == {'SOME-HEADER': 'SOME-VALUE'}, 'Header is incorrect'
|
||||
|
||||
|
||||
def test_init_unknown_status(config_file, mock_client):
|
||||
mock_client.get_component_status.return_value = cachet_url_monitor.status.ComponentStatus.UNKNOWN
|
||||
configuration = Configuration(config_file, 0, mock_client, 'token2')
|
||||
|
||||
assert configuration.previous_status == cachet_url_monitor.status.ComponentStatus.UNKNOWN
|
||||
|
||||
|
||||
def test_evaluate(configuration):
|
||||
with requests_mock.mock() as m:
|
||||
m.get('http://localhost:8080/swagger', text='<body>')
|
||||
self.configuration.evaluate()
|
||||
configuration.evaluate()
|
||||
|
||||
self.assertEqual(self.configuration.status, cachet_url_monitor.status.ComponentStatus.OPERATIONAL,
|
||||
'Component status set incorrectly')
|
||||
assert configuration.status == cachet_url_monitor.status.ComponentStatus.OPERATIONAL, 'Component status set incorrectly'
|
||||
|
||||
@requests_mock.mock()
|
||||
def test_evaluate_without_header(self, m):
|
||||
|
||||
def test_evaluate_without_header(configuration):
|
||||
with requests_mock.mock() as m:
|
||||
m.get('http://localhost:8080/swagger', text='<body>')
|
||||
self.configuration.evaluate()
|
||||
configuration.evaluate()
|
||||
|
||||
self.assertEqual(self.configuration.status, cachet_url_monitor.status.ComponentStatus.OPERATIONAL,
|
||||
'Component status set incorrectly')
|
||||
assert configuration.status == cachet_url_monitor.status.ComponentStatus.OPERATIONAL, 'Component status set incorrectly'
|
||||
|
||||
@requests_mock.mock()
|
||||
def test_evaluate_with_failure(self, m):
|
||||
|
||||
def test_evaluate_with_failure(configuration):
|
||||
with requests_mock.mock() as m:
|
||||
m.get('http://localhost:8080/swagger', text='<body>', status_code=400)
|
||||
self.configuration.evaluate()
|
||||
configuration.evaluate()
|
||||
|
||||
self.assertEqual(self.configuration.status, cachet_url_monitor.status.ComponentStatus.MAJOR_OUTAGE,
|
||||
'Component status set incorrectly or custom incident status is incorrectly parsed')
|
||||
assert configuration.status == cachet_url_monitor.status.ComponentStatus.MAJOR_OUTAGE, 'Component status set incorrectly or custom incident status is incorrectly parsed'
|
||||
|
||||
@requests_mock.mock()
|
||||
def test_evaluate_with_timeout(self, m):
|
||||
|
||||
def test_evaluate_with_timeout(configuration, mock_logger):
|
||||
with requests_mock.mock() as m:
|
||||
m.get('http://localhost:8080/swagger', exc=requests.Timeout)
|
||||
self.configuration.evaluate()
|
||||
configuration.evaluate()
|
||||
|
||||
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')
|
||||
assert configuration.status == cachet_url_monitor.status.ComponentStatus.PERFORMANCE_ISSUES, 'Component status set incorrectly'
|
||||
mock_logger.warning.assert_called_with('Request timed out')
|
||||
|
||||
@requests_mock.mock()
|
||||
def test_evaluate_with_connection_error(self, m):
|
||||
|
||||
def test_evaluate_with_connection_error(configuration, mock_logger):
|
||||
with requests_mock.mock() as m:
|
||||
m.get('http://localhost:8080/swagger', exc=requests.ConnectionError)
|
||||
self.configuration.evaluate()
|
||||
configuration.evaluate()
|
||||
|
||||
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')
|
||||
assert configuration.status == cachet_url_monitor.status.ComponentStatus.PARTIAL_OUTAGE, 'Component status set incorrectly'
|
||||
mock_logger.warning.assert_called_with('The URL is unreachable: GET http://localhost:8080/swagger')
|
||||
|
||||
@requests_mock.mock()
|
||||
def test_evaluate_with_http_error(self, m):
|
||||
|
||||
def test_evaluate_with_http_error(configuration, mock_logger):
|
||||
with requests_mock.mock() as m:
|
||||
m.get('http://localhost:8080/swagger', exc=requests.HTTPError)
|
||||
self.configuration.evaluate()
|
||||
configuration.evaluate()
|
||||
|
||||
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):
|
||||
self.client.get_component_status.return_value = cachet_url_monitor.status.ComponentStatus.OPERATIONAL
|
||||
push_status_response = mock.Mock()
|
||||
self.client.push_status.return_value = push_status_response
|
||||
push_status_response.ok = True
|
||||
self.configuration.status = cachet_url_monitor.status.ComponentStatus.PARTIAL_OUTAGE
|
||||
|
||||
self.configuration.push_status()
|
||||
|
||||
self.client.push_status.assert_called_once_with(1, cachet_url_monitor.status.ComponentStatus.OPERATIONAL)
|
||||
|
||||
def test_push_status_with_failure(self):
|
||||
self.client.get_component_status.return_value = cachet_url_monitor.status.ComponentStatus.OPERATIONAL
|
||||
push_status_response = mock.Mock()
|
||||
self.client.push_status.return_value = push_status_response
|
||||
push_status_response.ok = False
|
||||
self.configuration.status = cachet_url_monitor.status.ComponentStatus.PARTIAL_OUTAGE
|
||||
|
||||
self.configuration.push_status()
|
||||
|
||||
self.client.push_status.assert_called_once_with(1, cachet_url_monitor.status.ComponentStatus.OPERATIONAL)
|
||||
|
||||
def test_push_status_same_status(self):
|
||||
self.client.get_component_status.return_value = cachet_url_monitor.status.ComponentStatus.OPERATIONAL
|
||||
self.configuration.status = cachet_url_monitor.status.ComponentStatus.OPERATIONAL
|
||||
|
||||
self.configuration.push_status()
|
||||
|
||||
self.client.push_status.assert_not_called()
|
||||
assert configuration.status == cachet_url_monitor.status.ComponentStatus.PARTIAL_OUTAGE, 'Component status set incorrectly'
|
||||
mock_logger.exception.assert_called_with('Unexpected HTTP response')
|
||||
|
||||
|
||||
class ConfigurationMultipleUrlTest(unittest.TestCase):
|
||||
@mock.patch.dict(os.environ, {'CACHET_TOKEN': 'token2'})
|
||||
def setUp(self):
|
||||
config_yaml = load(open(os.path.join(os.path.dirname(__file__), 'configs/config_multiple_urls.yml'), 'rt'),
|
||||
SafeLoader)
|
||||
self.client = []
|
||||
self.configuration = []
|
||||
def test_push_status(configuration, mock_client):
|
||||
mock_client.get_component_status.return_value = cachet_url_monitor.status.ComponentStatus.PARTIAL_OUTAGE
|
||||
push_status_response = mock.Mock()
|
||||
mock_client.push_status.return_value = push_status_response
|
||||
push_status_response.ok = True
|
||||
configuration.previous_status = cachet_url_monitor.status.ComponentStatus.PARTIAL_OUTAGE
|
||||
configuration.status = cachet_url_monitor.status.ComponentStatus.OPERATIONAL
|
||||
|
||||
for index in range(len(config_yaml['endpoints'])):
|
||||
client = mock.Mock()
|
||||
self.client.append(client)
|
||||
self.configuration.append(Configuration(config_yaml, index, client, 'token2'))
|
||||
configuration.push_status()
|
||||
|
||||
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(expected_method[index], config.endpoint_method)
|
||||
self.assertEqual(expected_url[index], config.endpoint_url)
|
||||
mock_client.push_status.assert_called_once_with(1, cachet_url_monitor.status.ComponentStatus.OPERATIONAL)
|
||||
|
||||
|
||||
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, mock.Mock(), 'token2')
|
||||
def test_push_status_with_new_failure(configuration, mock_client):
|
||||
mock_client.get_component_status.return_value = cachet_url_monitor.status.ComponentStatus.OPERATIONAL
|
||||
push_status_response = mock.Mock()
|
||||
mock_client.push_status.return_value = push_status_response
|
||||
push_status_response.ok = False
|
||||
configuration.status = cachet_url_monitor.status.ComponentStatus.PARTIAL_OUTAGE
|
||||
|
||||
configuration.push_status()
|
||||
|
||||
mock_client.push_status.assert_called_once_with(1, cachet_url_monitor.status.ComponentStatus.PARTIAL_OUTAGE)
|
||||
|
||||
|
||||
def test_push_status_same_status(configuration, mock_client):
|
||||
mock_client.get_component_status.return_value = cachet_url_monitor.status.ComponentStatus.OPERATIONAL
|
||||
configuration.status = cachet_url_monitor.status.ComponentStatus.OPERATIONAL
|
||||
|
||||
configuration.push_status()
|
||||
|
||||
mock_client.push_status.assert_not_called()
|
||||
|
||||
|
||||
def test_init_multiple_urls(multiple_urls_configuration):
|
||||
expected_method = ['GET', 'POST']
|
||||
expected_url = ['http://localhost:8080/swagger', 'http://localhost:8080/bar']
|
||||
|
||||
assert len(multiple_urls_configuration) == 2
|
||||
for index in range(len(multiple_urls_configuration)):
|
||||
config = multiple_urls_configuration[index]
|
||||
assert len(config.data) == 2, 'Number of root elements in config.yml is incorrect'
|
||||
assert len(config.expectations) == 1, 'Number of expectations read from file is incorrect'
|
||||
assert config.headers == {'X-Cachet-Token': 'token2'}, 'Header was not set correctly'
|
||||
|
||||
assert expected_method[index] == config.endpoint_method
|
||||
assert expected_url[index] == config.endpoint_url
|
||||
|
||||
|
||||
def test_init_invalid_configuration(invalid_config_file, mock_client):
|
||||
with pytest.raises(cachet_url_monitor.configuration.ConfigurationValidationError):
|
||||
Configuration(invalid_config_file, 0, mock_client, 'token2')
|
||||
|
||||
@@ -5,8 +5,7 @@ import unittest
|
||||
import mock
|
||||
import pytest
|
||||
|
||||
from cachet_url_monitor.configuration import HttpStatus, Regex
|
||||
from cachet_url_monitor.configuration import Latency
|
||||
from cachet_url_monitor.expectation import HttpStatus, Regex, Latency
|
||||
from cachet_url_monitor.status import ComponentStatus
|
||||
|
||||
|
||||
@@ -76,12 +75,24 @@ class HttpStatusTest(unittest.TestCase):
|
||||
|
||||
assert self.expectation.get_status(request) == ComponentStatus.OPERATIONAL
|
||||
|
||||
def test_get_status_healthy_boundary(self):
|
||||
request = mock.Mock()
|
||||
request.status_code = 299
|
||||
|
||||
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) == ComponentStatus.PARTIAL_OUTAGE
|
||||
|
||||
def test_get_status_unhealthy_boundary(self):
|
||||
request = mock.Mock()
|
||||
request.status_code = 300
|
||||
|
||||
assert self.expectation.get_status(request) == ComponentStatus.PARTIAL_OUTAGE
|
||||
|
||||
def test_get_message(self):
|
||||
request = mock.Mock()
|
||||
request.status_code = 400
|
||||
|
||||
Reference in New Issue
Block a user