From a83abfd1d38a4444908a046d5851ba8fd362a4d5 Mon Sep 17 00:00:00 2001 From: Mitsuo Takaki Date: Thu, 19 May 2016 08:26:18 -0700 Subject: [PATCH] Fixing failing test. Using @mock.path decorator, instead of overriding the sys.modules. --- config.yml | 2 +- tests/test_configuration.py | 25 ++++++++++++++++++++----- tests/test_expectation.py | 16 ++++++++++------ tests/test_scheduler.py | 14 ++++---------- 4 files changed, 35 insertions(+), 22 deletions(-) diff --git a/config.yml b/config.yml index 07d6f73..5ba9817 100644 --- a/config.yml +++ b/config.yml @@ -1,7 +1,7 @@ endpoint: url: http://localhost:8080/swagger method: GET - timeout: 0.1 + timeout: 0.01 expectation: - type: HTTP_STATUS status: 200 diff --git a/tests/test_configuration.py b/tests/test_configuration.py index 4adc0db..eb8308c 100644 --- a/tests/test_configuration.py +++ b/tests/test_configuration.py @@ -1,20 +1,33 @@ #!/usr/bin/env python +import sys +import unittest + import cachet_url_monitor.status import mock -import unittest -import sys -from requests import ConnectionError,HTTPError,Timeout +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): def getLogger(name): self.mock_logger = mock.Mock() return self.mock_logger + sys.modules['logging'].getLogger = getLogger + def get(url, headers): + get_return = mock.Mock() + get_return.ok = True + get_return.json = mock.Mock() + get_return.json.return_value = {'data': {'status': 1}} + return get_return + + sys.modules['requests'].get = get + self.configuration = Configuration('config.yml') sys.modules['requests'].Timeout = Timeout sys.modules['requests'].ConnectionError = ConnectionError @@ -27,6 +40,7 @@ def test_init(self): def test_evaluate(self): def total_seconds(): return 0.1 + def request(method, url, timeout=None): response = mock.Mock() response.status_code = 200 @@ -43,6 +57,7 @@ def request(method, url, timeout=None): def test_evaluate_with_failure(self): 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. @@ -84,7 +99,7 @@ def request(method, url, timeout=None): assert self.configuration.status == 3 self.mock_logger.warning.assert_called_with(('The URL is ' - 'unreachable: GET http://localhost:8080/swagger')) + 'unreachable: GET http://localhost:8080/swagger')) def test_evaluate_with_http_error(self): def request(method, url, timeout=None): @@ -99,7 +114,7 @@ def request(method, url, timeout=None): assert self.configuration.status == 3 self.mock_logger.exception.assert_called_with(('Unexpected HTTP ' - 'response')) + 'response')) def test_push_status(self): def put(url, params=None, headers=None): diff --git a/tests/test_expectation.py b/tests/test_expectation.py index 6469448..071b75f 100644 --- a/tests/test_expectation.py +++ b/tests/test_expectation.py @@ -1,9 +1,10 @@ #!/usr/bin/env python +import unittest + import mock import re -import unittest -from cachet_url_monitor.configuration import Expectaction,Latency -from cachet_url_monitor.configuration import HttpStatus,Regex +from cachet_url_monitor.configuration import HttpStatus, Regex +from cachet_url_monitor.configuration import Latency class LatencyTest(unittest.TestCase): @@ -16,6 +17,7 @@ def test_init(self): def test_get_status_healthy(self): def total_seconds(): return 0.1 + request = mock.Mock() elapsed = mock.Mock() request.elapsed = elapsed @@ -26,6 +28,7 @@ def total_seconds(): def test_get_status_unhealthy(self): def total_seconds(): return 2 + request = mock.Mock() elapsed = mock.Mock() request.elapsed = elapsed @@ -36,13 +39,14 @@ def total_seconds(): def test_get_message(self): def total_seconds(): return 0.1 + request = mock.Mock() elapsed = mock.Mock() request.elapsed = elapsed elapsed.total_seconds = total_seconds assert self.expectation.get_message(request) == ('Latency above ' - 'threshold: 0.1000 seconds') + 'threshold: 0.1000 seconds') class HttpStatusTest(unittest.TestCase): @@ -69,7 +73,7 @@ def test_get_message(self): request.status_code = 400 assert self.expectation.get_message(request) == ('Unexpected HTTP ' - 'status (400)') + 'status (400)') class RegexTest(unittest.TestCase): @@ -96,4 +100,4 @@ def test_get_message(self): request.text = 'We will not find it here' assert self.expectation.get_message(request) == ('Regex did not match ' - 'anything in the body') + 'anything in the body') diff --git a/tests/test_scheduler.py b/tests/test_scheduler.py index ec3945b..30de4ce 100644 --- a/tests/test_scheduler.py +++ b/tests/test_scheduler.py @@ -5,8 +5,6 @@ import mock sys.modules['schedule'] = mock.Mock() -# sys.modules['cachet_url_monitor.configuration.Configuration'] = mock.Mock() -sys.modules['requests'] = mock.Mock() from cachet_url_monitor.scheduler import Agent, Scheduler @@ -36,9 +34,8 @@ def test_start(self): class SchedulerTest(unittest.TestCase): - @mock.patch('cachet_url_monitor.configuration.Configuration.__init__', mock.Mock(return_value=None)) - @mock.patch('cachet_url_monitor.configuration.Configuration.is_create_incident', mock.Mock(return_value=False)) - def setUp(self): + @mock.patch('requests.get') + def setUp(self, mock_requests): def get(url, headers): get_return = mock.Mock() get_return.ok = True @@ -46,18 +43,15 @@ def get(url, headers): get_return.json.return_value = {'data': {'status': 1}} return get_return - sys.modules['requests'].get = get + mock_requests.get = get self.scheduler = Scheduler('config.yml') def test_init(self): assert self.scheduler.stop == False - @mock.patch('cachet_url_monitor.configuration.Configuration', create=True) - def test_start(self, mock_configuration): + def test_start(self): # TODO(mtakaki|2016-05-01): We need a better way of testing this method. # Leaving it as a placeholder. - mock_configuration.data = {'frequency': 30} - self.scheduler.stop = True self.scheduler.start()