Resolve #8: Add beginnings of a test framework
This commit is contained in:
parent
6644a0b20e
commit
1672331700
|
@ -59,14 +59,14 @@ if __name__ == '__main__':
|
||||||
# forwarder = alertify.Alertify(args.config)
|
# forwarder = alertify.Alertify(args.config)
|
||||||
forwarder = alertify.Alertify()
|
forwarder = alertify.Alertify()
|
||||||
|
|
||||||
#-----------------------------
|
# -----------------------------
|
||||||
# Calculate logging level
|
# Calculate logging level
|
||||||
#-----------------------------
|
# -----------------------------
|
||||||
# Config :: Verbose: 0 = WARNING, 1 = INFO, 2 = DEBUG
|
# Config :: Verbose: 0 = WARNING, 1 = INFO, 2 = DEBUG
|
||||||
# Logging :: Loglevel: 30 = WARNING, 20 = INFO, 10 = DEBUG
|
# Logging :: Loglevel: 30 = WARNING, 20 = INFO, 10 = DEBUG
|
||||||
logger = logging.getLogger()
|
logger = logging.getLogger()
|
||||||
logger.setLevel(30 - (forwarder.config.verbose * 10))
|
logger.setLevel(30 - (forwarder.config.verbose * 10))
|
||||||
#-----------------------------
|
# -----------------------------
|
||||||
|
|
||||||
if args.healthcheck:
|
if args.healthcheck:
|
||||||
# Invert the sense of 'healthy' for Unix CLI usage
|
# Invert the sense of 'healthy' for Unix CLI usage
|
||||||
|
|
|
@ -59,7 +59,7 @@ class Config:
|
||||||
@classmethod
|
@classmethod
|
||||||
def keys(cls):
|
def keys(cls):
|
||||||
"""
|
"""
|
||||||
Method to return the defaults as a list of keys
|
Method to return the defaults as a list of dict_keys
|
||||||
"""
|
"""
|
||||||
return [
|
return [
|
||||||
attr[0]
|
attr[0]
|
||||||
|
|
|
@ -64,7 +64,7 @@ class Gotify:
|
||||||
"""
|
"""
|
||||||
Method to delete a message from the Gotify server
|
Method to delete a message from the Gotify server
|
||||||
"""
|
"""
|
||||||
logging.debug('Deleting message ID: %s', msg_id)
|
logging.info('Deleting message ID: %s', msg_id)
|
||||||
return self._call('DELETE', f'/message/{msg_id}')
|
return self._call('DELETE', f'/message/{msg_id}')
|
||||||
|
|
||||||
def find_byfingerprint(self, message):
|
def find_byfingerprint(self, message):
|
||||||
|
|
|
@ -15,10 +15,9 @@ class Healthcheck:
|
||||||
"""
|
"""
|
||||||
Simple method to return a boolean state of the general health
|
Simple method to return a boolean state of the general health
|
||||||
"""
|
"""
|
||||||
|
|
||||||
return all(
|
return all(
|
||||||
[
|
[
|
||||||
self.gotify.healthcheck(),
|
self.gotify_alive(),
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -31,6 +31,7 @@ class Server:
|
||||||
try:
|
try:
|
||||||
with HTTPServer(('', self.port), http_handler) as webserver:
|
with HTTPServer(('', self.port), http_handler) as webserver:
|
||||||
webserver.serve_forever()
|
webserver.serve_forever()
|
||||||
|
return True
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
logging.info('Exiting')
|
logging.info('Exiting')
|
||||||
|
|
||||||
|
|
0
src/conftest.py
Normal file
0
src/conftest.py
Normal file
22
src/tests/alertify/test___init__.py
Normal file
22
src/tests/alertify/test___init__.py
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
"""Test"""
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
|
||||||
|
class AlertifyTest(unittest.TestCase):
|
||||||
|
"""
|
||||||
|
Tests for methods in the Alertify class.
|
||||||
|
"""
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def setUpClass(cls):
|
||||||
|
pass
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def tearDownClass(cls):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
pass
|
54
src/tests/alertify/test_config.py
Normal file
54
src/tests/alertify/test_config.py
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
"""Test"""
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from alertify import config # pylint: disable=import-error
|
||||||
|
|
||||||
|
|
||||||
|
class ConfigTest(unittest.TestCase):
|
||||||
|
"""
|
||||||
|
Tests for methods in the Config class.
|
||||||
|
"""
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def setUpClass(cls):
|
||||||
|
cls.defaults = {
|
||||||
|
'delete_onresolve': bool(False),
|
||||||
|
'disable_resolved': bool(False),
|
||||||
|
'gotify_key_app': str(),
|
||||||
|
'gotify_key_client': str(),
|
||||||
|
'gotify_port': int(80),
|
||||||
|
'gotify_server': str('localhost'),
|
||||||
|
'listen_port': int(8080),
|
||||||
|
'verbose': int(0),
|
||||||
|
}
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def tearDownClass(cls):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def test_items(self):
|
||||||
|
"""Test"""
|
||||||
|
self.assertEqual(
|
||||||
|
config.Config().items(),
|
||||||
|
self.defaults.items(),
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_keys(self):
|
||||||
|
"""Test"""
|
||||||
|
self.assertListEqual(
|
||||||
|
config.Config.keys(),
|
||||||
|
list(self.defaults.keys()),
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_defaults(self):
|
||||||
|
"""Test"""
|
||||||
|
self.assertDictEqual(
|
||||||
|
config.Config.defaults(),
|
||||||
|
self.defaults,
|
||||||
|
)
|
106
src/tests/alertify/test_gotify.py
Normal file
106
src/tests/alertify/test_gotify.py
Normal file
|
@ -0,0 +1,106 @@
|
||||||
|
"""
|
||||||
|
Module to handle unit tests for the alertify.gotify module
|
||||||
|
"""
|
||||||
|
import unittest
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
from alertify import gotify # pylint: disable=import-error
|
||||||
|
|
||||||
|
|
||||||
|
class GotifyTest(unittest.TestCase):
|
||||||
|
"""
|
||||||
|
Tests for methods in the Gotify class.
|
||||||
|
"""
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def setUpClass(cls):
|
||||||
|
cls.gotify_client = gotify.Gotify('', 0, '', '')
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def tearDownClass(cls):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
@patch('http.client.HTTPConnection.request')
|
||||||
|
@patch('http.client.HTTPConnection.getresponse')
|
||||||
|
@patch('http.client.HTTPResponse.read')
|
||||||
|
def test_delete(self, mock_request, mock_getresponse, mock_read):
|
||||||
|
"""Test"""
|
||||||
|
mock_request.return_value.status = {}
|
||||||
|
mock_getresponse.return_value.status = 200
|
||||||
|
mock_getresponse.return_value.reason = 'OK'
|
||||||
|
mock_read.return_value = {}
|
||||||
|
|
||||||
|
self.assertDictEqual(
|
||||||
|
self.gotify_client.delete('123'),
|
||||||
|
{
|
||||||
|
'status': 200,
|
||||||
|
'reason': 'OK',
|
||||||
|
'json': None,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
@patch('alertify.gotify.Gotify.messages')
|
||||||
|
def test_find_byfingerprint(self, mock_messages):
|
||||||
|
"""Test"""
|
||||||
|
mock_messages.return_value = [
|
||||||
|
{
|
||||||
|
'id': 42,
|
||||||
|
'extras': {'alertify': {'fingerprint': 'deadbeefcafebabe'}},
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
self.gotify_client.find_byfingerprint({'fingerprint': 'deadbeefcafebabe'}),
|
||||||
|
42,
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_messages(self):
|
||||||
|
"""Test"""
|
||||||
|
self.assertListEqual(
|
||||||
|
self.gotify_client.messages(),
|
||||||
|
[],
|
||||||
|
)
|
||||||
|
|
||||||
|
@patch('http.client.HTTPConnection.request')
|
||||||
|
@patch('http.client.HTTPConnection.getresponse')
|
||||||
|
@patch('http.client.HTTPResponse.read')
|
||||||
|
def test_send_alert(self, mock_request, mock_getresponse, mock_read):
|
||||||
|
"""Test"""
|
||||||
|
mock_request.return_value.status = {}
|
||||||
|
mock_getresponse.return_value.status = 200
|
||||||
|
mock_getresponse.return_value.reason = 'OK'
|
||||||
|
mock_read.return_value = {}
|
||||||
|
|
||||||
|
self.assertDictEqual(
|
||||||
|
self.gotify_client.send_alert({}),
|
||||||
|
{
|
||||||
|
'status': 200,
|
||||||
|
'reason': 'OK',
|
||||||
|
'json': None,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
@patch('http.client.HTTPConnection.request')
|
||||||
|
@patch('http.client.HTTPConnection.getresponse')
|
||||||
|
@patch('http.client.HTTPResponse.read')
|
||||||
|
def test_healthcheck(self, mock_request, mock_getresponse, mock_read):
|
||||||
|
"""Test"""
|
||||||
|
mock_request.return_value.status = {}
|
||||||
|
mock_getresponse.return_value.status = 200
|
||||||
|
mock_getresponse.return_value.reason = 'OK'
|
||||||
|
mock_read.return_value = {}
|
||||||
|
|
||||||
|
self.assertDictEqual(
|
||||||
|
self.gotify_client.healthcheck(),
|
||||||
|
{
|
||||||
|
'status': 200,
|
||||||
|
'reason': 'OK',
|
||||||
|
'json': None,
|
||||||
|
},
|
||||||
|
)
|
54
src/tests/alertify/test_healthcheck.py
Normal file
54
src/tests/alertify/test_healthcheck.py
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
"""Test"""
|
||||||
|
import unittest
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
from alertify import healthcheck, gotify # pylint: disable=import-error
|
||||||
|
|
||||||
|
|
||||||
|
class HealthcheckTest(unittest.TestCase):
|
||||||
|
"""
|
||||||
|
Tests for methods in the Healthcheck class.
|
||||||
|
"""
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def setUpClass(cls):
|
||||||
|
cls.healthcheck = healthcheck.Healthcheck(gotify.Gotify('', 0, '', ''))
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def tearDownClass(cls):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
@patch('alertify.healthcheck.Healthcheck.gotify_alive')
|
||||||
|
def test_report(self, mock_healthcheck):
|
||||||
|
"""Test"""
|
||||||
|
mock_healthcheck.return_value = {
|
||||||
|
'status': 200,
|
||||||
|
'reason': 'OK',
|
||||||
|
'json': None,
|
||||||
|
}
|
||||||
|
|
||||||
|
self.assertTrue(self.healthcheck.report())
|
||||||
|
|
||||||
|
@patch('alertify.healthcheck.Healthcheck.gotify_alive')
|
||||||
|
def test_gotify_alive(self, mock_healthcheck):
|
||||||
|
"""Test"""
|
||||||
|
mock_healthcheck.return_value = {
|
||||||
|
'status': 200,
|
||||||
|
'reason': 'OK',
|
||||||
|
'json': None,
|
||||||
|
}
|
||||||
|
|
||||||
|
self.assertDictEqual(
|
||||||
|
self.healthcheck.gotify_alive(),
|
||||||
|
{
|
||||||
|
'status': 200,
|
||||||
|
'reason': 'OK',
|
||||||
|
'json': None,
|
||||||
|
},
|
||||||
|
)
|
49
src/tests/alertify/test_messaging.py
Normal file
49
src/tests/alertify/test_messaging.py
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
"""Test"""
|
||||||
|
import unittest
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
from alertify import messaging, gotify # pylint: disable=import-error
|
||||||
|
|
||||||
|
|
||||||
|
class MessageHandlerTest(unittest.TestCase):
|
||||||
|
"""
|
||||||
|
Tests for methods in the MessageHandler class.
|
||||||
|
"""
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def setUpClass(cls):
|
||||||
|
cls.messaging = messaging.MessageHandler(gotify.Gotify('', 0, '', ''))
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def tearDownClass(cls):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
@patch('alertify.gotify.Gotify.send_alert')
|
||||||
|
def test_process(self, mock_send_alert):
|
||||||
|
"""Test"""
|
||||||
|
mock_send_alert.return_value = {
|
||||||
|
'status': 200,
|
||||||
|
'reason': 'OK',
|
||||||
|
'json': None,
|
||||||
|
}
|
||||||
|
|
||||||
|
self.assertDictEqual(
|
||||||
|
self.messaging.process(
|
||||||
|
{
|
||||||
|
'status': 'firing',
|
||||||
|
'labels': {},
|
||||||
|
'annotations': {},
|
||||||
|
}
|
||||||
|
),
|
||||||
|
{
|
||||||
|
'status': 200,
|
||||||
|
'reason': 'OK',
|
||||||
|
'json': None,
|
||||||
|
},
|
||||||
|
)
|
32
src/tests/alertify/test_server.py
Normal file
32
src/tests/alertify/test_server.py
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
"""Test"""
|
||||||
|
import unittest
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
from alertify import server # pylint: disable=import-error
|
||||||
|
|
||||||
|
|
||||||
|
class ServerTest(unittest.TestCase):
|
||||||
|
"""
|
||||||
|
Tests for methods in the Server class.
|
||||||
|
"""
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def setUpClass(cls):
|
||||||
|
cls.server = server.Server(0, None, None)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def tearDownClass(cls):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
@patch('http.server.HTTPServer.serve_forever')
|
||||||
|
def test_listen_and_run(self, mock_serve_forever):
|
||||||
|
"""Test"""
|
||||||
|
mock_serve_forever.return_value = True
|
||||||
|
|
||||||
|
self.assertTrue(self.server.listen_and_run())
|
Loading…
Reference in a new issue