mirror of
https://github.com/mtan93/cachet-url-monitor.git
synced 2026-03-08 05:31:58 +00:00
Fixing failing test. Using @mock.path decorator, instead of overriding the sys.modules.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
endpoint:
|
||||
url: http://localhost:8080/swagger
|
||||
method: GET
|
||||
timeout: 0.1
|
||||
timeout: 0.01
|
||||
expectation:
|
||||
- type: HTTP_STATUS
|
||||
status: 200
|
||||
|
||||
@@ -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 @@ class ConfigurationTest(unittest.TestCase):
|
||||
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 @@ class ConfigurationTest(unittest.TestCase):
|
||||
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 @@ class ConfigurationTest(unittest.TestCase):
|
||||
|
||||
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 @@ class ConfigurationTest(unittest.TestCase):
|
||||
|
||||
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):
|
||||
|
||||
@@ -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 @@ class LatencyTest(unittest.TestCase):
|
||||
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 @@ class LatencyTest(unittest.TestCase):
|
||||
def test_get_status_unhealthy(self):
|
||||
def total_seconds():
|
||||
return 2
|
||||
|
||||
request = mock.Mock()
|
||||
elapsed = mock.Mock()
|
||||
request.elapsed = elapsed
|
||||
@@ -36,13 +39,14 @@ class LatencyTest(unittest.TestCase):
|
||||
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 @@ class HttpStatusTest(unittest.TestCase):
|
||||
request.status_code = 400
|
||||
|
||||
assert self.expectation.get_message(request) == ('Unexpected HTTP '
|
||||
'status (400)')
|
||||
'status (400)')
|
||||
|
||||
|
||||
class RegexTest(unittest.TestCase):
|
||||
@@ -96,4 +100,4 @@ class RegexTest(unittest.TestCase):
|
||||
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')
|
||||
|
||||
@@ -5,8 +5,6 @@ import unittest
|
||||
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 @@ class AgentTest(unittest.TestCase):
|
||||
|
||||
|
||||
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 @@ class SchedulerTest(unittest.TestCase):
|
||||
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()
|
||||
|
||||
Reference in New Issue
Block a user