mirror of
https://github.com/mtan93/cachet-url-monitor.git
synced 2026-03-08 05:31:58 +00:00
Adding new expectation (Regex) and creating unit tests.
This commit is contained in:
11
tests/test_configuration.py
Normal file
11
tests/test_configuration.py
Normal file
@@ -0,0 +1,11 @@
|
||||
#!/usr/bin/env python
|
||||
import unittest
|
||||
from cachet_url_monitor.configuration import Configuration
|
||||
|
||||
|
||||
class ConfigurationTest(unittest.TestCase):
|
||||
def test_init(self):
|
||||
configuration = Configuration('config.yml')
|
||||
|
||||
assert len(configuration.data) == 3
|
||||
assert len(configuration.expectations) == 2
|
||||
99
tests/test_expectation.py
Normal file
99
tests/test_expectation.py
Normal file
@@ -0,0 +1,99 @@
|
||||
#!/usr/bin/env python
|
||||
import mock
|
||||
import re
|
||||
import unittest
|
||||
from cachet_url_monitor.configuration import Expectaction,Latency
|
||||
from cachet_url_monitor.configuration import HttpStatus,Regex
|
||||
|
||||
|
||||
class LatencyTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.expectation = Latency({'type': 'LATENCY', 'threshold': 1})
|
||||
|
||||
def test_init(self):
|
||||
assert self.expectation.threshold == 1
|
||||
|
||||
def test_get_status_healthy(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_status(request) == 1
|
||||
|
||||
def test_get_status_unhealthy(self):
|
||||
def total_seconds():
|
||||
return 2
|
||||
request = mock.Mock()
|
||||
elapsed = mock.Mock()
|
||||
request.elapsed = elapsed
|
||||
elapsed.total_seconds = total_seconds
|
||||
|
||||
assert self.expectation.get_status(request) == 2
|
||||
|
||||
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')
|
||||
|
||||
|
||||
class HttpStatusTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.expectation = HttpStatus({'type': 'HTTP_STATUS', 'status': 200})
|
||||
|
||||
def test_init(self):
|
||||
assert self.expectation.status == 200
|
||||
|
||||
def test_get_status_healthy(self):
|
||||
request = mock.Mock()
|
||||
request.status_code = 200
|
||||
|
||||
assert self.expectation.get_status(request) == 1
|
||||
|
||||
def test_get_status_unhealthy(self):
|
||||
request = mock.Mock()
|
||||
request.status_code = 400
|
||||
|
||||
assert self.expectation.get_status(request) == 3
|
||||
|
||||
def test_get_message(self):
|
||||
request = mock.Mock()
|
||||
request.status_code = 400
|
||||
|
||||
assert self.expectation.get_message(request) == ('Unexpected HTTP '
|
||||
'status (400)')
|
||||
|
||||
|
||||
class RegexTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.expectation = Regex({'type': 'REGEX', 'regex': '.*(found stuff).*'})
|
||||
|
||||
def test_init(self):
|
||||
assert self.expectation.regex == re.compile('.*(found stuff).*')
|
||||
|
||||
def test_get_status_healthy(self):
|
||||
request = mock.Mock()
|
||||
request.text = 'We cound found stuff in this body.'
|
||||
|
||||
assert self.expectation.get_status(request) == 1
|
||||
|
||||
def test_get_status_unhealthy(self):
|
||||
request = mock.Mock()
|
||||
request.text = 'We will not find here'
|
||||
|
||||
assert self.expectation.get_status(request) == 3
|
||||
|
||||
def test_get_message(self):
|
||||
request = mock.Mock()
|
||||
request.text = 'We will not find here'
|
||||
|
||||
assert self.expectation.get_message(request) == ('Regex did not match '
|
||||
'anything in the body')
|
||||
Reference in New Issue
Block a user