Skip to content

Commit

Permalink
Adding Agent unit test and asserting calls to logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
mtakaki committed Apr 30, 2016
1 parent a6cf64e commit c7a28ed
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
8 changes: 7 additions & 1 deletion tests/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import sys
from requests import ConnectionError,HTTPError,Timeout
sys.modules['requests'] = mock.Mock()
sys.modules['logging'] = mock.Mock()
from cachet_url_monitor.configuration import Configuration


class ConfigurationTest(unittest.TestCase):
def setUp(self):
self.configuration = Configuration('config.yml')
Expand Down Expand Up @@ -38,6 +38,7 @@ def total_seconds():
return 0.1
def request(method, url, 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
Expand All @@ -60,6 +61,7 @@ def request(method, url, timeout=None):
self.configuration.evaluate()

assert self.configuration.status == 3
sys.modules['logging'].warning.assert_called_with('Request timed out')

def test_evaluate_with_connection_error(self):
def request(method, url, timeout=None):
Expand All @@ -73,6 +75,8 @@ def request(method, url, timeout=None):
self.configuration.evaluate()

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 request(method, url, timeout=None):
Expand All @@ -86,6 +90,8 @@ def request(method, url, timeout=None):
self.configuration.evaluate()

assert self.configuration.status == 3
sys.modules['logging'].exception.assert_called_with(('Unexpected HTTP '
'response'))

def test_push_status_and_metrics(self):
def put(url, params=None, headers=None):
Expand Down
31 changes: 31 additions & 0 deletions tests/test_scheduler.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit c7a28ed

Please sign in to comment.