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:
|
endpoint:
|
||||||
url: http://localhost:8080/swagger
|
url: http://localhost:8080/swagger
|
||||||
method: GET
|
method: GET
|
||||||
timeout: 0.1
|
timeout: 0.01
|
||||||
expectation:
|
expectation:
|
||||||
- type: HTTP_STATUS
|
- type: HTTP_STATUS
|
||||||
status: 200
|
status: 200
|
||||||
|
|||||||
@@ -1,20 +1,33 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
import sys
|
||||||
|
import unittest
|
||||||
|
|
||||||
import cachet_url_monitor.status
|
import cachet_url_monitor.status
|
||||||
import mock
|
import mock
|
||||||
import unittest
|
from requests import ConnectionError, HTTPError, Timeout
|
||||||
import sys
|
|
||||||
from requests import ConnectionError,HTTPError,Timeout
|
|
||||||
sys.modules['requests'] = mock.Mock()
|
sys.modules['requests'] = mock.Mock()
|
||||||
sys.modules['logging'] = 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):
|
||||||
def getLogger(name):
|
def getLogger(name):
|
||||||
self.mock_logger = mock.Mock()
|
self.mock_logger = mock.Mock()
|
||||||
return self.mock_logger
|
return self.mock_logger
|
||||||
|
|
||||||
sys.modules['logging'].getLogger = getLogger
|
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')
|
self.configuration = Configuration('config.yml')
|
||||||
sys.modules['requests'].Timeout = Timeout
|
sys.modules['requests'].Timeout = Timeout
|
||||||
sys.modules['requests'].ConnectionError = ConnectionError
|
sys.modules['requests'].ConnectionError = ConnectionError
|
||||||
@@ -27,6 +40,7 @@ class ConfigurationTest(unittest.TestCase):
|
|||||||
def test_evaluate(self):
|
def test_evaluate(self):
|
||||||
def total_seconds():
|
def total_seconds():
|
||||||
return 0.1
|
return 0.1
|
||||||
|
|
||||||
def request(method, url, timeout=None):
|
def request(method, url, timeout=None):
|
||||||
response = mock.Mock()
|
response = mock.Mock()
|
||||||
response.status_code = 200
|
response.status_code = 200
|
||||||
@@ -43,6 +57,7 @@ class ConfigurationTest(unittest.TestCase):
|
|||||||
def test_evaluate_with_failure(self):
|
def test_evaluate_with_failure(self):
|
||||||
def total_seconds():
|
def total_seconds():
|
||||||
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.
|
# 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
|
assert self.configuration.status == 3
|
||||||
self.mock_logger.warning.assert_called_with(('The URL is '
|
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 test_evaluate_with_http_error(self):
|
||||||
def request(method, url, timeout=None):
|
def request(method, url, timeout=None):
|
||||||
@@ -99,7 +114,7 @@ class ConfigurationTest(unittest.TestCase):
|
|||||||
|
|
||||||
assert self.configuration.status == 3
|
assert self.configuration.status == 3
|
||||||
self.mock_logger.exception.assert_called_with(('Unexpected HTTP '
|
self.mock_logger.exception.assert_called_with(('Unexpected HTTP '
|
||||||
'response'))
|
'response'))
|
||||||
|
|
||||||
def test_push_status(self):
|
def test_push_status(self):
|
||||||
def put(url, params=None, headers=None):
|
def put(url, params=None, headers=None):
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
import unittest
|
||||||
|
|
||||||
import mock
|
import mock
|
||||||
import re
|
import re
|
||||||
import unittest
|
from cachet_url_monitor.configuration import HttpStatus, Regex
|
||||||
from cachet_url_monitor.configuration import Expectaction,Latency
|
from cachet_url_monitor.configuration import Latency
|
||||||
from cachet_url_monitor.configuration import HttpStatus,Regex
|
|
||||||
|
|
||||||
|
|
||||||
class LatencyTest(unittest.TestCase):
|
class LatencyTest(unittest.TestCase):
|
||||||
@@ -16,6 +17,7 @@ class LatencyTest(unittest.TestCase):
|
|||||||
def test_get_status_healthy(self):
|
def test_get_status_healthy(self):
|
||||||
def total_seconds():
|
def total_seconds():
|
||||||
return 0.1
|
return 0.1
|
||||||
|
|
||||||
request = mock.Mock()
|
request = mock.Mock()
|
||||||
elapsed = mock.Mock()
|
elapsed = mock.Mock()
|
||||||
request.elapsed = elapsed
|
request.elapsed = elapsed
|
||||||
@@ -26,6 +28,7 @@ class LatencyTest(unittest.TestCase):
|
|||||||
def test_get_status_unhealthy(self):
|
def test_get_status_unhealthy(self):
|
||||||
def total_seconds():
|
def total_seconds():
|
||||||
return 2
|
return 2
|
||||||
|
|
||||||
request = mock.Mock()
|
request = mock.Mock()
|
||||||
elapsed = mock.Mock()
|
elapsed = mock.Mock()
|
||||||
request.elapsed = elapsed
|
request.elapsed = elapsed
|
||||||
@@ -36,13 +39,14 @@ class LatencyTest(unittest.TestCase):
|
|||||||
def test_get_message(self):
|
def test_get_message(self):
|
||||||
def total_seconds():
|
def total_seconds():
|
||||||
return 0.1
|
return 0.1
|
||||||
|
|
||||||
request = mock.Mock()
|
request = mock.Mock()
|
||||||
elapsed = mock.Mock()
|
elapsed = mock.Mock()
|
||||||
request.elapsed = elapsed
|
request.elapsed = elapsed
|
||||||
elapsed.total_seconds = total_seconds
|
elapsed.total_seconds = total_seconds
|
||||||
|
|
||||||
assert self.expectation.get_message(request) == ('Latency above '
|
assert self.expectation.get_message(request) == ('Latency above '
|
||||||
'threshold: 0.1000 seconds')
|
'threshold: 0.1000 seconds')
|
||||||
|
|
||||||
|
|
||||||
class HttpStatusTest(unittest.TestCase):
|
class HttpStatusTest(unittest.TestCase):
|
||||||
@@ -69,7 +73,7 @@ class HttpStatusTest(unittest.TestCase):
|
|||||||
request.status_code = 400
|
request.status_code = 400
|
||||||
|
|
||||||
assert self.expectation.get_message(request) == ('Unexpected HTTP '
|
assert self.expectation.get_message(request) == ('Unexpected HTTP '
|
||||||
'status (400)')
|
'status (400)')
|
||||||
|
|
||||||
|
|
||||||
class RegexTest(unittest.TestCase):
|
class RegexTest(unittest.TestCase):
|
||||||
@@ -96,4 +100,4 @@ class RegexTest(unittest.TestCase):
|
|||||||
request.text = 'We will not find it here'
|
request.text = 'We will not find it here'
|
||||||
|
|
||||||
assert self.expectation.get_message(request) == ('Regex did not match '
|
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
|
import mock
|
||||||
|
|
||||||
sys.modules['schedule'] = mock.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
|
from cachet_url_monitor.scheduler import Agent, Scheduler
|
||||||
|
|
||||||
|
|
||||||
@@ -36,9 +34,8 @@ class AgentTest(unittest.TestCase):
|
|||||||
|
|
||||||
|
|
||||||
class SchedulerTest(unittest.TestCase):
|
class SchedulerTest(unittest.TestCase):
|
||||||
@mock.patch('cachet_url_monitor.configuration.Configuration.__init__', mock.Mock(return_value=None))
|
@mock.patch('requests.get')
|
||||||
@mock.patch('cachet_url_monitor.configuration.Configuration.is_create_incident', mock.Mock(return_value=False))
|
def setUp(self, mock_requests):
|
||||||
def setUp(self):
|
|
||||||
def get(url, headers):
|
def get(url, headers):
|
||||||
get_return = mock.Mock()
|
get_return = mock.Mock()
|
||||||
get_return.ok = True
|
get_return.ok = True
|
||||||
@@ -46,18 +43,15 @@ class SchedulerTest(unittest.TestCase):
|
|||||||
get_return.json.return_value = {'data': {'status': 1}}
|
get_return.json.return_value = {'data': {'status': 1}}
|
||||||
return get_return
|
return get_return
|
||||||
|
|
||||||
sys.modules['requests'].get = get
|
mock_requests.get = get
|
||||||
|
|
||||||
self.scheduler = Scheduler('config.yml')
|
self.scheduler = Scheduler('config.yml')
|
||||||
|
|
||||||
def test_init(self):
|
def test_init(self):
|
||||||
assert self.scheduler.stop == False
|
assert self.scheduler.stop == False
|
||||||
|
|
||||||
@mock.patch('cachet_url_monitor.configuration.Configuration', create=True)
|
def test_start(self):
|
||||||
def test_start(self, mock_configuration):
|
|
||||||
# TODO(mtakaki|2016-05-01): We need a better way of testing this method.
|
# TODO(mtakaki|2016-05-01): We need a better way of testing this method.
|
||||||
# Leaving it as a placeholder.
|
# Leaving it as a placeholder.
|
||||||
mock_configuration.data = {'frequency': 30}
|
|
||||||
|
|
||||||
self.scheduler.stop = True
|
self.scheduler.stop = True
|
||||||
self.scheduler.start()
|
self.scheduler.start()
|
||||||
|
|||||||
Reference in New Issue
Block a user