Resolve #8: Add beginnings of a test framework

This commit is contained in:
Scott Wallace 2020-10-28 17:31:29 +00:00
parent 6644a0b20e
commit 1672331700
12 changed files with 324 additions and 7 deletions

View file

@ -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]

View file

@ -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):

View file

@ -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(),
] ]
) )

View file

@ -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
View file

View 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

View 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,
)

View 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,
},
)

View 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,
},
)

View 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,
},
)

View 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())