mirror of
https://github.com/mtan93/cachet-url-monitor.git
synced 2026-03-08 05:31:58 +00:00
Adding Agent unit test and asserting calls to logging.
This commit is contained in:
@@ -4,9 +4,9 @@ import unittest
|
|||||||
import sys
|
import sys
|
||||||
from requests import ConnectionError,HTTPError,Timeout
|
from requests import ConnectionError,HTTPError,Timeout
|
||||||
sys.modules['requests'] = mock.Mock()
|
sys.modules['requests'] = mock.Mock()
|
||||||
|
sys.modules['logging'] = mock.Mock()
|
||||||
from cachet_url_monitor.configuration import Configuration
|
from cachet_url_monitor.configuration import Configuration
|
||||||
|
|
||||||
|
|
||||||
class ConfigurationTest(unittest.TestCase):
|
class ConfigurationTest(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.configuration = Configuration('config.yml')
|
self.configuration = Configuration('config.yml')
|
||||||
@@ -38,6 +38,7 @@ class ConfigurationTest(unittest.TestCase):
|
|||||||
return 0.1
|
return 0.1
|
||||||
def request(method, url, timeout=None):
|
def request(method, url, timeout=None):
|
||||||
response = mock.Mock()
|
response = mock.Mock()
|
||||||
|
# We are expecting a 200 response, so this will fail the expectation.
|
||||||
response.status_code = 400
|
response.status_code = 400
|
||||||
response.elapsed = mock.Mock()
|
response.elapsed = mock.Mock()
|
||||||
response.elapsed.total_seconds = total_seconds
|
response.elapsed.total_seconds = total_seconds
|
||||||
@@ -60,6 +61,7 @@ class ConfigurationTest(unittest.TestCase):
|
|||||||
self.configuration.evaluate()
|
self.configuration.evaluate()
|
||||||
|
|
||||||
assert self.configuration.status == 3
|
assert self.configuration.status == 3
|
||||||
|
sys.modules['logging'].warning.assert_called_with('Request timed out')
|
||||||
|
|
||||||
def test_evaluate_with_connection_error(self):
|
def test_evaluate_with_connection_error(self):
|
||||||
def request(method, url, timeout=None):
|
def request(method, url, timeout=None):
|
||||||
@@ -73,6 +75,8 @@ class ConfigurationTest(unittest.TestCase):
|
|||||||
self.configuration.evaluate()
|
self.configuration.evaluate()
|
||||||
|
|
||||||
assert self.configuration.status == 3
|
assert self.configuration.status == 3
|
||||||
|
sys.modules['logging'].warning.assert_called_with(('The URL is '
|
||||||
|
'unreachable: GET http://localhost:8080/swagger'))
|
||||||
|
|
||||||
def test_evaluate_with_http_error(self):
|
def test_evaluate_with_http_error(self):
|
||||||
def request(method, url, timeout=None):
|
def request(method, url, timeout=None):
|
||||||
@@ -86,6 +90,8 @@ class ConfigurationTest(unittest.TestCase):
|
|||||||
self.configuration.evaluate()
|
self.configuration.evaluate()
|
||||||
|
|
||||||
assert self.configuration.status == 3
|
assert self.configuration.status == 3
|
||||||
|
sys.modules['logging'].exception.assert_called_with(('Unexpected HTTP '
|
||||||
|
'response'))
|
||||||
|
|
||||||
def test_push_status_and_metrics(self):
|
def test_push_status_and_metrics(self):
|
||||||
def put(url, params=None, headers=None):
|
def put(url, params=None, headers=None):
|
||||||
|
|||||||
31
tests/test_scheduler.py
Normal file
31
tests/test_scheduler.py
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
import mock
|
||||||
|
import unittest
|
||||||
|
import sys
|
||||||
|
sys.modules['schedule'] = mock.Mock()
|
||||||
|
from cachet_url_monitor.scheduler import Agent
|
||||||
|
|
||||||
|
|
||||||
|
class AgentTest(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.configuration = mock.Mock()
|
||||||
|
self.agent = Agent(self.configuration)
|
||||||
|
|
||||||
|
def test_init(self):
|
||||||
|
assert self.agent.configuration == self.configuration
|
||||||
|
|
||||||
|
def test_execute(self):
|
||||||
|
evaluate = self.configuration.evaluate
|
||||||
|
push_status_and_metrics = self.configuration.push_status_and_metrics
|
||||||
|
self.agent.execute()
|
||||||
|
|
||||||
|
evaluate.assert_called_once()
|
||||||
|
push_status_and_metrics.assert_called_once()
|
||||||
|
|
||||||
|
def test_start(self):
|
||||||
|
every = sys.modules['schedule'].every
|
||||||
|
self.configuration.data = {'frequency': 5}
|
||||||
|
|
||||||
|
self.agent.start()
|
||||||
|
|
||||||
|
every.assert_called_with(5)
|
||||||
Reference in New Issue
Block a user