mirror of
https://github.com/mtan93/cachet-url-monitor.git
synced 2026-03-08 05:31:58 +00:00
* feat(multihreading): each url has it's own thread * Fixing broken unit tests * Improving readability when there are multiple URLs registerd and creating new action to upload metrics * Improving error message when there's no file found * Bumping the version Co-authored-by: Alex Berenshtein <aberenshtein@yotpo.com>
This commit is contained in:
@@ -4,6 +4,7 @@ import unittest
|
||||
|
||||
import mock
|
||||
from requests import ConnectionError, HTTPError, Timeout
|
||||
from yaml import load, SafeLoader
|
||||
|
||||
import cachet_url_monitor.status
|
||||
|
||||
@@ -31,17 +32,17 @@ class ConfigurationTest(unittest.TestCase):
|
||||
|
||||
sys.modules['requests'].get = get
|
||||
|
||||
self.configuration = Configuration('config.yml')
|
||||
self.configuration = Configuration(load(open('config.yml', 'r'), SafeLoader), 0)
|
||||
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), 3, 'Number of root elements in config.yml is incorrect')
|
||||
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.assertEqual(self.configuration.api_url, 'https://demo.cachethq.io/api/v1',
|
||||
'Cachet API URL was set incorrectly')
|
||||
'Cachet API URL was set incorrectly')
|
||||
self.assertDictEqual(self.configuration.endpoint_header, {'SOME-HEADER': 'SOME-VALUE'}, 'Header is incorrect')
|
||||
|
||||
def test_evaluate(self):
|
||||
@@ -60,7 +61,7 @@ class ConfigurationTest(unittest.TestCase):
|
||||
self.configuration.evaluate()
|
||||
|
||||
self.assertEqual(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_OPERATIONAL,
|
||||
'Component status set incorrectly')
|
||||
'Component status set incorrectly')
|
||||
|
||||
def test_evaluate_without_header(self):
|
||||
def total_seconds():
|
||||
@@ -78,7 +79,7 @@ class ConfigurationTest(unittest.TestCase):
|
||||
self.configuration.evaluate()
|
||||
|
||||
self.assertEqual(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_OPERATIONAL,
|
||||
'Component status set incorrectly')
|
||||
'Component status set incorrectly')
|
||||
|
||||
def test_evaluate_with_failure(self):
|
||||
def total_seconds():
|
||||
@@ -97,7 +98,7 @@ class ConfigurationTest(unittest.TestCase):
|
||||
self.configuration.evaluate()
|
||||
|
||||
self.assertEqual(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_MAJOR_OUTAGE,
|
||||
'Component status set incorrectly or custom incident status is incorrectly parsed')
|
||||
'Component status set incorrectly or custom incident status is incorrectly parsed')
|
||||
|
||||
def test_evaluate_with_timeout(self):
|
||||
def request(method, url, headers, timeout=None):
|
||||
@@ -111,7 +112,7 @@ class ConfigurationTest(unittest.TestCase):
|
||||
self.configuration.evaluate()
|
||||
|
||||
self.assertEqual(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_PERFORMANCE_ISSUES,
|
||||
'Component status set incorrectly')
|
||||
'Component status set incorrectly')
|
||||
self.mock_logger.warning.assert_called_with('Request timed out')
|
||||
|
||||
def test_evaluate_with_connection_error(self):
|
||||
@@ -126,7 +127,7 @@ class ConfigurationTest(unittest.TestCase):
|
||||
self.configuration.evaluate()
|
||||
|
||||
self.assertEqual(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_PARTIAL_OUTAGE,
|
||||
'Component status set incorrectly')
|
||||
'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):
|
||||
@@ -141,7 +142,7 @@ class ConfigurationTest(unittest.TestCase):
|
||||
self.configuration.evaluate()
|
||||
|
||||
self.assertEqual(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_PARTIAL_OUTAGE,
|
||||
'Component status set incorrectly')
|
||||
'Component status set incorrectly')
|
||||
self.mock_logger.exception.assert_called_with('Unexpected HTTP response')
|
||||
|
||||
def test_push_status(self):
|
||||
@@ -156,7 +157,7 @@ class ConfigurationTest(unittest.TestCase):
|
||||
|
||||
sys.modules['requests'].put = put
|
||||
self.assertEqual(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_OPERATIONAL,
|
||||
'Incorrect component update parameters')
|
||||
'Incorrect component update parameters')
|
||||
self.configuration.push_status()
|
||||
|
||||
def test_push_status_with_failure(self):
|
||||
@@ -171,5 +172,5 @@ class ConfigurationTest(unittest.TestCase):
|
||||
|
||||
sys.modules['requests'].put = put
|
||||
self.assertEqual(self.configuration.status, cachet_url_monitor.status.COMPONENT_STATUS_OPERATIONAL,
|
||||
'Incorrect component update parameters')
|
||||
'Incorrect component update parameters')
|
||||
self.configuration.push_status()
|
||||
|
||||
@@ -3,6 +3,7 @@ import sys
|
||||
import unittest
|
||||
|
||||
import mock
|
||||
from yaml import load, SafeLoader
|
||||
|
||||
sys.modules['schedule'] = mock.Mock()
|
||||
from cachet_url_monitor.scheduler import Agent, Scheduler
|
||||
@@ -26,7 +27,7 @@ class AgentTest(unittest.TestCase):
|
||||
|
||||
def test_start(self):
|
||||
every = sys.modules['schedule'].every
|
||||
self.configuration.data = {'frequency': 5}
|
||||
self.configuration.endpoint = {'frequency': 5}
|
||||
|
||||
self.agent.start()
|
||||
|
||||
@@ -45,7 +46,7 @@ class SchedulerTest(unittest.TestCase):
|
||||
|
||||
mock_requests.get = get
|
||||
|
||||
self.scheduler = Scheduler('config.yml')
|
||||
self.scheduler = Scheduler(load(open('config.yml', 'r'), SafeLoader), 0)
|
||||
|
||||
def test_init(self):
|
||||
assert self.scheduler.stop == False
|
||||
|
||||
Reference in New Issue
Block a user