add tests for alertmanager client
This commit is contained in:
parent
59f5284caf
commit
4873abca70
5 changed files with 813 additions and 106 deletions
|
@ -36,12 +36,10 @@ class AlertmanagerClient:
|
|||
alerts = await self.get_alerts()
|
||||
return self._find_alert(fingerprint, alerts)
|
||||
|
||||
async def get_alert_labels(self, fingerprint: str) -> Dict[str, str]:
|
||||
alert = await self.get_alert(fingerprint)
|
||||
return alert["labels"]
|
||||
|
||||
async def create_silence(self, fingerprint: str, duration: str, user: str) -> str:
|
||||
labels = await self.get_alert_labels(fingerprint)
|
||||
alert = await self.get_alert(fingerprint)
|
||||
labels = alert["labels"]
|
||||
|
||||
matchers = []
|
||||
for label_name, label_value in labels.items():
|
||||
matchers.append(
|
||||
|
@ -74,7 +72,7 @@ class AlertmanagerClient:
|
|||
|
||||
return data["silenceID"]
|
||||
|
||||
async def delete_silence(self, fingerprint: str) -> List[str]:
|
||||
async def delete_silences(self, fingerprint: str) -> List[str]:
|
||||
alert = await self.get_alert(fingerprint)
|
||||
|
||||
alert_state = alert["status"]["state"]
|
||||
|
@ -85,10 +83,10 @@ class AlertmanagerClient:
|
|||
|
||||
silences = alert["status"]["silencedBy"]
|
||||
for silence in silences:
|
||||
await self._delete_silence_by_id(silence)
|
||||
await self._delete_silence(silence)
|
||||
return silences
|
||||
|
||||
async def _delete_silence_by_id(self, silence: str) -> None:
|
||||
async def _delete_silence(self, silence: str) -> None:
|
||||
try:
|
||||
async with self.session.delete(
|
||||
f"{self.api_url}/silence/{silence}"
|
||||
|
|
|
@ -84,19 +84,17 @@ class Command:
|
|||
|
||||
count_created_silences = 0
|
||||
alert_fingerprints = self.cache[alert_event_id]
|
||||
print(self.cache[alert_event_id])
|
||||
for alert_fingerprint in alert_fingerprints:
|
||||
logger.debug(
|
||||
f"Create silence for alert with fingerprint {alert_fingerprint} for a duration of {duration}"
|
||||
)
|
||||
try:
|
||||
silence_id = await self.alertmanager.create_silence(
|
||||
await self.alertmanager.create_silence(
|
||||
alert_fingerprint, duration, self.room.user_name(self.event.sender)
|
||||
)
|
||||
count_created_silences += 1
|
||||
except (AlertNotFoundError, AlertmanagerError) as e:
|
||||
logger.exception(f"Unable to create silence: {e}", exc_info=e)
|
||||
continue
|
||||
count_created_silences += 1
|
||||
|
||||
await send_text_to_room(
|
||||
self.client,
|
||||
|
@ -126,13 +124,12 @@ class Command:
|
|||
f"Delete silence for alert with fingerprint {alert_fingerprint}"
|
||||
)
|
||||
try:
|
||||
removed_silences = await self.alertmanager.delete_silence(
|
||||
removed_silences = await self.alertmanager.delete_silences(
|
||||
alert_fingerprint
|
||||
)
|
||||
count_removed_silences += len(removed_silences)
|
||||
except (AlertNotFoundError, SilenceNotFoundError, AlertmanagerError) as e:
|
||||
logger.exception(f"Unable to delete silence: {e}", exc_info=e)
|
||||
continue
|
||||
count_removed_silences += len(removed_silences)
|
||||
|
||||
await send_text_to_room(
|
||||
self.client,
|
||||
|
|
552
tests/test_alertmanager.py
Normal file
552
tests/test_alertmanager.py
Normal file
|
@ -0,0 +1,552 @@
|
|||
from __future__ import annotations
|
||||
import json
|
||||
from sys import implementation
|
||||
|
||||
import unittest
|
||||
from typing import Any
|
||||
from unittest.mock import MagicMock, Mock, patch
|
||||
import aiotools
|
||||
import aiohttp
|
||||
from aiohttp import web, web_request
|
||||
import aiohttp.test_utils
|
||||
import nio
|
||||
from diskcache import Cache
|
||||
|
||||
from matrix_alertbot.alertmanager import AlertmanagerClient
|
||||
from matrix_alertbot.errors import (
|
||||
AlertNotFoundError,
|
||||
AlertmanagerError,
|
||||
SilenceNotFoundError,
|
||||
)
|
||||
|
||||
|
||||
class AbstractFakeAlertmanagerServer:
|
||||
def __init__(self) -> None:
|
||||
self.app = web.Application()
|
||||
self.app.router.add_routes(
|
||||
[
|
||||
web.get("/api/v2/alerts", self.get_alerts),
|
||||
web.post("/api/v2/silences", self.create_silence),
|
||||
web.delete("/api/v2/silence/{silence}", self.delete_silence),
|
||||
]
|
||||
)
|
||||
|
||||
self.runner = web.AppRunner(self.app)
|
||||
|
||||
async def __aenter__(self) -> AbstractFakeAlertmanagerServer:
|
||||
await self.start()
|
||||
return self
|
||||
|
||||
async def __aexit__(self, *args: Any) -> None:
|
||||
await self.stop()
|
||||
|
||||
async def start(self) -> None:
|
||||
self.port = aiohttp.test_utils.unused_port()
|
||||
|
||||
await self.runner.setup()
|
||||
|
||||
site = web.TCPSite(self.runner, "localhost", self.port)
|
||||
await site.start()
|
||||
|
||||
async def stop(self) -> None:
|
||||
await self.runner.cleanup()
|
||||
|
||||
async def get_alerts(self, request: web_request.Request) -> web.Response:
|
||||
raise NotImplementedError
|
||||
|
||||
async def create_silence(self, request: web_request.Request) -> web.Response:
|
||||
raise NotImplementedError
|
||||
|
||||
async def delete_silence(self, request: web_request.Request) -> web.Response:
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
class FakeAlertmanagerServer(AbstractFakeAlertmanagerServer):
|
||||
async def get_alerts(self, request: web_request.Request) -> web.Response:
|
||||
return web.Response(
|
||||
body=json.dumps(
|
||||
[
|
||||
{
|
||||
"fingerprint": "fingerprint1",
|
||||
"labels": {"alertname": "alert1"},
|
||||
"status": {"state": "active"},
|
||||
},
|
||||
{
|
||||
"fingerprint": "fingerprint2",
|
||||
"labels": {"alertname": "alert2"},
|
||||
"status": {
|
||||
"state": "suppressed",
|
||||
"silencedBy": ["silence1", "silence2"],
|
||||
},
|
||||
},
|
||||
]
|
||||
),
|
||||
content_type="application/json",
|
||||
)
|
||||
|
||||
async def create_silence(self, request: web_request.Request) -> web.Response:
|
||||
return web.Response(
|
||||
body=json.dumps({"silenceID": "silence1"}), content_type="application/json"
|
||||
)
|
||||
|
||||
async def delete_silence(self, request: web_request.Request) -> web.Response:
|
||||
return web.Response(status=200, content_type="application/json")
|
||||
|
||||
|
||||
class FakeAlertmanagerServerWithoutAlert(AbstractFakeAlertmanagerServer):
|
||||
async def get_alerts(self, request: web_request.Request) -> web.Response:
|
||||
return web.Response(body=json.dumps([]), content_type="application/json")
|
||||
|
||||
|
||||
class FakeAlertmanagerServerWithErrorAlerts(AbstractFakeAlertmanagerServer):
|
||||
async def get_alerts(self, request: web_request.Request) -> web.Response:
|
||||
return web.Response(status=500)
|
||||
|
||||
|
||||
class FakeAlertmanagerServerWithErrorCreateSilence(FakeAlertmanagerServer):
|
||||
async def create_silence(self, request: web_request.Request) -> web.Response:
|
||||
return web.Response(status=500)
|
||||
|
||||
|
||||
class FakeAlertmanagerServerWithErrorDeleteSilence(FakeAlertmanagerServer):
|
||||
async def delete_silence(self, request: web_request.Request) -> web.Response:
|
||||
return web.Response(status=500)
|
||||
|
||||
|
||||
class AlertmanagerClientTestCase(unittest.IsolatedAsyncioTestCase):
|
||||
async def asyncSetUp(self) -> None:
|
||||
|
||||
self.fake_fingerprints = Mock(return_value=["fingerprint1", "fingerprint2"])
|
||||
self.fake_cache = MagicMock(spec=Cache)
|
||||
self.fake_cache.__getitem__ = self.fake_fingerprints
|
||||
|
||||
async def test_get_alerts_happy(self) -> None:
|
||||
async with FakeAlertmanagerServer() as fake_alertmanager_server:
|
||||
port = fake_alertmanager_server.port
|
||||
alertmanager = AlertmanagerClient(
|
||||
f"http://localhost:{port}", self.fake_cache
|
||||
)
|
||||
async with aiotools.closing_async(alertmanager) as alertmanager:
|
||||
alerts = await alertmanager.get_alerts()
|
||||
self.assertEqual(
|
||||
[
|
||||
{
|
||||
"fingerprint": "fingerprint1",
|
||||
"labels": {"alertname": "alert1"},
|
||||
"status": {"state": "active"},
|
||||
},
|
||||
{
|
||||
"fingerprint": "fingerprint2",
|
||||
"labels": {"alertname": "alert2"},
|
||||
"status": {
|
||||
"state": "suppressed",
|
||||
"silencedBy": ["silence1", "silence2"],
|
||||
},
|
||||
},
|
||||
],
|
||||
alerts,
|
||||
)
|
||||
|
||||
async def test_get_alerts_empty(self) -> None:
|
||||
async with FakeAlertmanagerServerWithoutAlert() as fake_alertmanager_server:
|
||||
port = fake_alertmanager_server.port
|
||||
alertmanager = AlertmanagerClient(
|
||||
f"http://localhost:{port}", self.fake_cache
|
||||
)
|
||||
async with aiotools.closing_async(alertmanager) as alertmanager:
|
||||
alerts = await alertmanager.get_alerts()
|
||||
self.assertEqual([], alerts)
|
||||
|
||||
async def test_get_alerts_raise_alertmanager_error(self) -> None:
|
||||
async with FakeAlertmanagerServerWithErrorAlerts() as fake_alertmanager_server:
|
||||
port = fake_alertmanager_server.port
|
||||
alertmanager = AlertmanagerClient(
|
||||
f"http://localhost:{port}", self.fake_cache
|
||||
)
|
||||
async with aiotools.closing_async(alertmanager) as alertmanager:
|
||||
with self.assertRaises(AlertmanagerError):
|
||||
await alertmanager.get_alerts()
|
||||
|
||||
async def test_get_alert_happy(self) -> None:
|
||||
async with FakeAlertmanagerServer() as fake_alertmanager_server:
|
||||
port = fake_alertmanager_server.port
|
||||
alertmanager = AlertmanagerClient(
|
||||
f"http://localhost:{port}", self.fake_cache
|
||||
)
|
||||
async with aiotools.closing_async(alertmanager) as alertmanager:
|
||||
alert = await alertmanager.get_alert("fingerprint1")
|
||||
self.assertEqual(
|
||||
{
|
||||
"fingerprint": "fingerprint1",
|
||||
"labels": {"alertname": "alert1"},
|
||||
"status": {"state": "active"},
|
||||
},
|
||||
alert,
|
||||
)
|
||||
|
||||
async def test_get_alert_raise_alert_not_found(self) -> None:
|
||||
async with FakeAlertmanagerServerWithoutAlert() as fake_alertmanager_server:
|
||||
port = fake_alertmanager_server.port
|
||||
alertmanager = AlertmanagerClient(
|
||||
f"http://localhost:{port}", self.fake_cache
|
||||
)
|
||||
async with aiotools.closing_async(alertmanager) as alertmanager:
|
||||
with self.assertRaises(AlertNotFoundError):
|
||||
await alertmanager.get_alert("fingerprint1")
|
||||
|
||||
async def test_get_alert_raise_alertmanager_error(self) -> None:
|
||||
async with FakeAlertmanagerServerWithErrorAlerts() as fake_alertmanager_server:
|
||||
port = fake_alertmanager_server.port
|
||||
alertmanager = AlertmanagerClient(
|
||||
f"http://localhost:{port}", self.fake_cache
|
||||
)
|
||||
async with aiotools.closing_async(alertmanager) as alertmanager:
|
||||
with self.assertRaises(AlertmanagerError):
|
||||
await alertmanager.get_alert("fingerprint1")
|
||||
|
||||
async def test_create_silence_happy(self) -> None:
|
||||
async with FakeAlertmanagerServer() as fake_alertmanager_server:
|
||||
port = fake_alertmanager_server.port
|
||||
alertmanager = AlertmanagerClient(
|
||||
f"http://localhost:{port}", self.fake_cache
|
||||
)
|
||||
async with aiotools.closing_async(alertmanager) as alertmanager:
|
||||
silence = await alertmanager.create_silence(
|
||||
"fingerprint1", "1d", "user"
|
||||
)
|
||||
self.assertEqual("silence1", silence)
|
||||
|
||||
async def test_create_silence_raise_alert_not_found(self) -> None:
|
||||
async with FakeAlertmanagerServerWithoutAlert() as fake_alertmanager_server:
|
||||
port = fake_alertmanager_server.port
|
||||
alertmanager = AlertmanagerClient(
|
||||
f"http://localhost:{port}", self.fake_cache
|
||||
)
|
||||
async with aiotools.closing_async(alertmanager) as alertmanager:
|
||||
with self.assertRaises(AlertNotFoundError):
|
||||
await alertmanager.create_silence("fingerprint1", "1d", "user")
|
||||
|
||||
async def test_create_silence_raise_alertmanager_error(self) -> None:
|
||||
async with FakeAlertmanagerServerWithErrorCreateSilence() as fake_alertmanager_server:
|
||||
port = fake_alertmanager_server.port
|
||||
alertmanager = AlertmanagerClient(
|
||||
f"http://localhost:{port}", self.fake_cache
|
||||
)
|
||||
async with aiotools.closing_async(alertmanager) as alertmanager:
|
||||
await alertmanager.get_alert("fingerprint1")
|
||||
|
||||
with self.assertRaises(AlertmanagerError):
|
||||
await alertmanager.create_silence("fingerprint1", "1d", "user")
|
||||
|
||||
async def test_delete_silences_happy(self) -> None:
|
||||
async with FakeAlertmanagerServer() as fake_alertmanager_server:
|
||||
port = fake_alertmanager_server.port
|
||||
alertmanager = AlertmanagerClient(
|
||||
f"http://localhost:{port}", self.fake_cache
|
||||
)
|
||||
async with aiotools.closing_async(alertmanager) as alertmanager:
|
||||
silences = await alertmanager.delete_silences("fingerprint2")
|
||||
self.assertEqual(["silence1", "silence2"], silences)
|
||||
|
||||
async def test_delete_silences_raise_silence_not_found(self) -> None:
|
||||
async with FakeAlertmanagerServer() as fake_alertmanager_server:
|
||||
port = fake_alertmanager_server.port
|
||||
alertmanager = AlertmanagerClient(
|
||||
f"http://localhost:{port}", self.fake_cache
|
||||
)
|
||||
async with aiotools.closing_async(alertmanager) as alertmanager:
|
||||
with self.assertRaises(SilenceNotFoundError):
|
||||
await alertmanager.delete_silences("fingerprint1")
|
||||
|
||||
async def test_delete_silences_raise_alert_not_found(self) -> None:
|
||||
async with FakeAlertmanagerServerWithoutAlert() as fake_alertmanager_server:
|
||||
port = fake_alertmanager_server.port
|
||||
alertmanager = AlertmanagerClient(
|
||||
f"http://localhost:{port}", self.fake_cache
|
||||
)
|
||||
async with aiotools.closing_async(alertmanager) as alertmanager:
|
||||
with self.assertRaises(AlertNotFoundError):
|
||||
await alertmanager.delete_silences("fingerprint2")
|
||||
|
||||
async def test_delete_silences_raise_alertmanager_error(self) -> None:
|
||||
async with FakeAlertmanagerServerWithErrorDeleteSilence() as fake_alertmanager_server:
|
||||
port = fake_alertmanager_server.port
|
||||
alertmanager = AlertmanagerClient(
|
||||
f"http://localhost:{port}", self.fake_cache
|
||||
)
|
||||
async with aiotools.closing_async(alertmanager) as alertmanager:
|
||||
await alertmanager.get_alert("fingerprint1")
|
||||
|
||||
with self.assertRaises(AlertmanagerError):
|
||||
await alertmanager.delete_silences("fingerprint2")
|
||||
|
||||
async def test_find_alert_happy(self) -> None:
|
||||
alertmanager = AlertmanagerClient(f"http://localhost", self.fake_cache)
|
||||
alert = alertmanager._find_alert(
|
||||
"fingerprint1", [{"fingerprint": "fingerprint1"}]
|
||||
)
|
||||
self.assertEqual({"fingerprint": "fingerprint1"}, alert)
|
||||
|
||||
async def test_find_alert_raise_alert_not_found(self) -> None:
|
||||
alertmanager = AlertmanagerClient(f"http://localhost", self.fake_cache)
|
||||
|
||||
with self.assertRaises(AlertNotFoundError):
|
||||
alertmanager._find_alert("fingerprint2", [{"fingerprint": "fingerprint1"}])
|
||||
|
||||
# fake_session_get.assert_called_once_with("http://localhost:9093/api/v2/alerts")
|
||||
|
||||
# async def test_get_alerts_not_empty(self) -> None:
|
||||
# alerts = await self.alertmanager.get_alerts()
|
||||
|
||||
# self.assertEqual(["alert1", "alert2"], alerts)
|
||||
# # fake_session_get.assert_called_once_with("http://localhost:9093/api/v2/alerts")
|
||||
|
||||
# async def test_get_alerts_raise_alertmanager_error(self) -> None:
|
||||
# with self.assertRaises(AlertmanagerError):
|
||||
# await self.alertmanager.get_alerts()
|
||||
# # fake_session_get.assert_called_once_with("http://localhost:9093/api/v2/alerts")
|
||||
|
||||
# @patch.object(matrix_alertbot.command.Command, "_ack")
|
||||
# async def test_process_ack_command(self, fake_ack: Mock) -> None:
|
||||
# """Tests the callback for InviteMemberEvents"""
|
||||
# # Tests that the bot attempts to join a room after being invited to it
|
||||
|
||||
# fake_message_event = Mock(spec=nio.RoomMessageText)
|
||||
|
||||
# command = Command(
|
||||
# self.fake_client,
|
||||
# self.fake_cache,
|
||||
# self.fake_alertmanager,
|
||||
# self.fake_config,
|
||||
# "ack",
|
||||
# self.fake_room,
|
||||
# fake_message_event,
|
||||
# )
|
||||
# await command.process()
|
||||
|
||||
# @patch.object(matrix_alertbot.command.Command, "_unack")
|
||||
# async def test_process_unack_command(self, fake_unack: Mock) -> None:
|
||||
# """Tests the callback for InviteMemberEvents"""
|
||||
# # Tests that the bot attempts to join a room after being invited to it
|
||||
|
||||
# fake_message_event = Mock(spec=nio.RoomMessageText)
|
||||
|
||||
# for command_word in ("unack", "nack"):
|
||||
# command = Command(
|
||||
# self.fake_client,
|
||||
# self.fake_cache,
|
||||
# self.fake_alertmanager,
|
||||
# self.fake_config,
|
||||
# command_word,
|
||||
# self.fake_room,
|
||||
# fake_message_event,
|
||||
# )
|
||||
# await command.process()
|
||||
|
||||
# # Check that we attempted to process the command
|
||||
# fake_unack.assert_has_calls([call(), call()])
|
||||
|
||||
# @patch.object(matrix_alertbot.command.Command, "_show_help")
|
||||
# async def test_process_help_command(self, fake_help: Mock) -> None:
|
||||
# """Tests the callback for InviteMemberEvents"""
|
||||
# # Tests that the bot attempts to join a room after being invited to it
|
||||
|
||||
# fake_message_event = Mock(spec=nio.RoomMessageText)
|
||||
|
||||
# command = Command(
|
||||
# self.fake_client,
|
||||
# self.fake_cache,
|
||||
# self.fake_alertmanager,
|
||||
# self.fake_config,
|
||||
# "help",
|
||||
# self.fake_room,
|
||||
# fake_message_event,
|
||||
# )
|
||||
# await command.process()
|
||||
|
||||
# # Check that we attempted to process the command
|
||||
# fake_help.assert_called_once()
|
||||
|
||||
# @patch.object(matrix_alertbot.command.Command, "_unknown_command")
|
||||
# async def test_process_unknown_command(self, fake_unknown: Mock) -> None:
|
||||
# """Tests the callback for InviteMemberEvents"""
|
||||
# # Tests that the bot attempts to join a room after being invited to it
|
||||
|
||||
# fake_message_event = Mock(spec=nio.RoomMessageText)
|
||||
|
||||
# command = Command(
|
||||
# self.fake_client,
|
||||
# self.fake_cache,
|
||||
# self.fake_alertmanager,
|
||||
# self.fake_config,
|
||||
# "",
|
||||
# self.fake_room,
|
||||
# fake_message_event,
|
||||
# )
|
||||
# await command.process()
|
||||
|
||||
# # Check that we attempted to process the command
|
||||
# fake_unknown.assert_called_once()
|
||||
|
||||
# async def test_ack_not_in_reply_without_duration(self) -> None:
|
||||
# """Tests the callback for InviteMemberEvents"""
|
||||
# # Tests that the bot attempts to join a room after being invited to it
|
||||
|
||||
# fake_message_event = Mock(spec=nio.RoomMessageText)
|
||||
# fake_message_event.sender = "@some_other_fake_user:example.com"
|
||||
# fake_message_event.body = ""
|
||||
# fake_message_event.source = self.fake_source_not_in_reply
|
||||
|
||||
# command = Command(
|
||||
# self.fake_client,
|
||||
# self.fake_cache,
|
||||
# self.fake_alertmanager,
|
||||
# self.fake_config,
|
||||
# "ack",
|
||||
# self.fake_room,
|
||||
# fake_message_event,
|
||||
# )
|
||||
# await command._ack()
|
||||
|
||||
# # Check that we didn't attempt to create silences
|
||||
# self.fake_alertmanager.create_silence.assert_not_called()
|
||||
# self.fake_client.room_send.assert_not_called()
|
||||
|
||||
# async def test_ack_not_in_reply_with_duration(self) -> None:
|
||||
# """Tests the callback for InviteMemberEvents"""
|
||||
# # Tests that the bot attempts to join a room after being invited to it
|
||||
|
||||
# fake_message_event = Mock(spec=nio.RoomMessageText)
|
||||
# fake_message_event.sender = "@some_other_fake_user:example.com"
|
||||
# fake_message_event.body = ""
|
||||
# fake_message_event.source = self.fake_source_not_in_reply
|
||||
|
||||
# command = Command(
|
||||
# self.fake_client,
|
||||
# self.fake_cache,
|
||||
# self.fake_alertmanager,
|
||||
# self.fake_config,
|
||||
# "ack 2d",
|
||||
# self.fake_room,
|
||||
# fake_message_event,
|
||||
# )
|
||||
# await command._ack()
|
||||
|
||||
# # Check that we didn't attempt to create silences
|
||||
# self.fake_alertmanager.create_silence.assert_not_called()
|
||||
# self.fake_client.room_send.assert_not_called()
|
||||
|
||||
# @patch.object(matrix_alertbot.command, "send_text_to_room")
|
||||
# async def test_ack_in_reply_without_duration(
|
||||
# self, fake_send_text_to_room: Mock
|
||||
# ) -> None:
|
||||
# """Tests the callback for InviteMemberEvents"""
|
||||
# # Tests that the bot attempts to join a room after being invited to it
|
||||
|
||||
# fake_message_event = Mock(spec=nio.RoomMessageText)
|
||||
# fake_message_event.sender = "@some_other_fake_user:example.com"
|
||||
# fake_message_event.body = ""
|
||||
# fake_message_event.source = self.fake_source_in_reply
|
||||
|
||||
# command = Command(
|
||||
# self.fake_client,
|
||||
# self.fake_cache,
|
||||
# self.fake_alertmanager,
|
||||
# self.fake_config,
|
||||
# "ack",
|
||||
# self.fake_room,
|
||||
# fake_message_event,
|
||||
# )
|
||||
# await command._ack()
|
||||
|
||||
# # Check that we attempted to create silences
|
||||
# self.fake_alertmanager.create_silence.assert_has_calls(
|
||||
# list(
|
||||
# call(
|
||||
# fingerprint,
|
||||
# "1d",
|
||||
# fake_message_event.sender,
|
||||
# )
|
||||
# for fingerprint in self.fake_fingerprints.return_value
|
||||
# )
|
||||
# )
|
||||
# fake_send_text_to_room.assert_called_once_with(
|
||||
# self.fake_client,
|
||||
# self.fake_room.room_id,
|
||||
# "Created 2 silences with a duration of 1d.",
|
||||
# )
|
||||
|
||||
# @patch.object(matrix_alertbot.command, "send_text_to_room")
|
||||
# async def test_ack_in_reply_with_duration(
|
||||
# self, fake_send_text_to_room: Mock
|
||||
# ) -> None:
|
||||
# """Tests the callback for InviteMemberEvents"""
|
||||
# # Tests that the bot attempts to join a room after being invited to it
|
||||
|
||||
# fake_message_event = Mock(spec=nio.RoomMessageText)
|
||||
# fake_message_event.sender = "@some_other_fake_user:example.com"
|
||||
# fake_message_event.body = ""
|
||||
# fake_message_event.source = self.fake_source_in_reply
|
||||
|
||||
# command = Command(
|
||||
# self.fake_client,
|
||||
# self.fake_cache,
|
||||
# self.fake_alertmanager,
|
||||
# self.fake_config,
|
||||
# "ack 2d",
|
||||
# self.fake_room,
|
||||
# fake_message_event,
|
||||
# )
|
||||
# await command._ack()
|
||||
|
||||
# # Check that we attempted to create silences
|
||||
# self.fake_alertmanager.create_silence.assert_has_calls(
|
||||
# list(
|
||||
# call(
|
||||
# fingerprint,
|
||||
# "2d",
|
||||
# fake_message_event.sender,
|
||||
# )
|
||||
# for fingerprint in self.fake_fingerprints.return_value
|
||||
# )
|
||||
# )
|
||||
# fake_send_text_to_room.assert_called_once_with(
|
||||
# self.fake_client,
|
||||
# self.fake_room.room_id,
|
||||
# "Created 2 silences with a duration of 2d.",
|
||||
# )
|
||||
|
||||
# @patch.object(matrix_alertbot.command, "send_text_to_room")
|
||||
# async def test_unack_in_reply(self, fake_send_text_to_room: Mock) -> None:
|
||||
# """Tests the callback for InviteMemberEvents"""
|
||||
# # Tests that the bot attempts to join a room after being invited to it
|
||||
|
||||
# fake_message_event = Mock(spec=nio.RoomMessageText)
|
||||
# fake_message_event.sender = "@some_other_fake_user:example.com"
|
||||
# fake_message_event.body = ""
|
||||
# fake_message_event.source = self.fake_source_in_reply
|
||||
|
||||
# command = Command(
|
||||
# self.fake_client,
|
||||
# self.fake_cache,
|
||||
# self.fake_alertmanager,
|
||||
# self.fake_config,
|
||||
# "unack",
|
||||
# self.fake_room,
|
||||
# fake_message_event,
|
||||
# )
|
||||
# await command._unack()
|
||||
|
||||
# # Check that we attempted to create silences
|
||||
# self.fake_alertmanager.delete_silence.assert_has_calls(
|
||||
# list(
|
||||
# call(fingerprint) for fingerprint in self.fake_fingerprints.return_value
|
||||
# )
|
||||
# )
|
||||
# fake_send_text_to_room.assert_called_with(
|
||||
# self.fake_client, self.fake_room.room_id, "Removed 2 silences."
|
||||
# )
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
|
@ -68,7 +68,6 @@ class CallbacksTestCase(unittest.IsolatedAsyncioTestCase):
|
|||
"""Tests the callback for RoomMessageText with the command prefix"""
|
||||
# Tests that the bot process messages in the room that contain a command
|
||||
fake_command_instance = fake_command.return_value
|
||||
fake_command_instance.process.side_effect = lambda: print("hello")
|
||||
|
||||
fake_message_event = Mock(spec=nio.RoomMessageText)
|
||||
fake_message_event.sender = "@some_other_fake_user:example.com"
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
from typing import Dict
|
||||
import unittest
|
||||
from typing import Dict, List
|
||||
from unittest.mock import MagicMock, Mock, call, patch
|
||||
|
||||
import nio
|
||||
|
@ -7,12 +7,50 @@ from diskcache import Cache
|
|||
|
||||
import matrix_alertbot.callback
|
||||
from matrix_alertbot.alertmanager import AlertmanagerClient
|
||||
from matrix_alertbot.callback import Callbacks
|
||||
from matrix_alertbot.command import Command
|
||||
from matrix_alertbot.errors import (
|
||||
AlertmanagerError,
|
||||
AlertNotFoundError,
|
||||
SilenceNotFoundError,
|
||||
)
|
||||
|
||||
from tests.utils import make_awaitable
|
||||
|
||||
|
||||
async def create_silence_raise_alert_not_found(
|
||||
fingerprint: str, duration: str, user: str
|
||||
) -> str:
|
||||
if fingerprint == "fingerprint1":
|
||||
raise AlertNotFoundError
|
||||
return "silence1"
|
||||
|
||||
|
||||
async def create_silence_raise_alertmanager_error(
|
||||
fingerprint: str, duration: str, user: str
|
||||
) -> str:
|
||||
if fingerprint == "fingerprint1":
|
||||
raise AlertmanagerError
|
||||
return "silence2"
|
||||
|
||||
|
||||
async def delete_silence_raise_silence_not_found(fingerprint: str) -> List[str]:
|
||||
if fingerprint == "fingerprint1":
|
||||
raise SilenceNotFoundError
|
||||
return ["silence1"]
|
||||
|
||||
|
||||
async def delete_silence_raise_alert_not_found(fingerprint: str) -> List[str]:
|
||||
if fingerprint == "fingerprint1":
|
||||
raise AlertNotFoundError
|
||||
return ["silence1", "silence2"]
|
||||
|
||||
|
||||
async def delete_silence_raise_alertmanager_error(fingerprint: str) -> List[str]:
|
||||
if fingerprint == "fingerprint1":
|
||||
raise AlertmanagerError
|
||||
return ["silence1", "silence2", "silence3"]
|
||||
|
||||
|
||||
class CommandTestCase(unittest.IsolatedAsyncioTestCase):
|
||||
def setUp(self) -> None:
|
||||
# Create a Command object and give it some Mock'd objects to use
|
||||
|
@ -21,11 +59,14 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
|
|||
# Pretend that attempting to send a message is always successful
|
||||
self.fake_client.room_send.return_value = make_awaitable(None)
|
||||
|
||||
self.fake_fingerprints = Mock(return_value=["fingerprint1", "fingerprint2"])
|
||||
self.fake_fingerprints = ["fingerprint1", "fingerprint2"]
|
||||
self.fake_silences = ["silence1", "silence2"]
|
||||
|
||||
self.fake_cache = MagicMock(spec=Cache)
|
||||
self.fake_cache.__getitem__ = self.fake_fingerprints
|
||||
self.fake_cache.__getitem__ = Mock(return_value=self.fake_fingerprints)
|
||||
|
||||
self.fake_alertmanager = Mock(spec=AlertmanagerClient)
|
||||
self.fake_alertmanager.delete_silences.return_value = self.fake_silences
|
||||
|
||||
# Create a fake room to play with
|
||||
self.fake_room = Mock(spec=nio.MatrixRoom)
|
||||
|
@ -33,13 +74,17 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
|
|||
self.fake_room.display_name = "Fake Room"
|
||||
self.fake_room.user_name.side_effect = lambda x: x
|
||||
|
||||
self.fake_source_not_in_reply = {"content": {}}
|
||||
self.fake_source_in_reply = {
|
||||
self.fake_source_not_in_reply: Dict = {"content": {}}
|
||||
self.fake_source_in_reply: Dict = {
|
||||
"content": {
|
||||
"m.relates_to": {"m.in_reply_to": {"event_id": "some event id"}}
|
||||
}
|
||||
}
|
||||
|
||||
self.fake_message_event = Mock(spec=nio.RoomMessageText)
|
||||
self.fake_message_event.sender = "@some_other_fake_user:example.com"
|
||||
self.fake_message_event.body = ""
|
||||
|
||||
# We don't spec config, as it doesn't currently have well defined attributes
|
||||
self.fake_config = Mock()
|
||||
self.fake_config.room_id = self.fake_room.room_id
|
||||
|
@ -50,8 +95,6 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
|
|||
"""Tests the callback for InviteMemberEvents"""
|
||||
# Tests that the bot attempts to join a room after being invited to it
|
||||
|
||||
fake_message_event = Mock(spec=nio.RoomMessageText)
|
||||
|
||||
command = Command(
|
||||
self.fake_client,
|
||||
self.fake_cache,
|
||||
|
@ -59,17 +102,18 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
|
|||
self.fake_config,
|
||||
"ack",
|
||||
self.fake_room,
|
||||
fake_message_event,
|
||||
self.fake_message_event,
|
||||
)
|
||||
await command.process()
|
||||
|
||||
# Check that we attempted to process the command
|
||||
fake_ack.assert_called_once()
|
||||
|
||||
@patch.object(matrix_alertbot.command.Command, "_unack")
|
||||
async def test_process_unack_command(self, fake_unack: Mock) -> None:
|
||||
"""Tests the callback for InviteMemberEvents"""
|
||||
# Tests that the bot attempts to join a room after being invited to it
|
||||
|
||||
fake_message_event = Mock(spec=nio.RoomMessageText)
|
||||
|
||||
for command_word in ("unack", "nack"):
|
||||
command = Command(
|
||||
self.fake_client,
|
||||
|
@ -78,7 +122,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
|
|||
self.fake_config,
|
||||
command_word,
|
||||
self.fake_room,
|
||||
fake_message_event,
|
||||
self.fake_message_event,
|
||||
)
|
||||
await command.process()
|
||||
|
||||
|
@ -90,8 +134,6 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
|
|||
"""Tests the callback for InviteMemberEvents"""
|
||||
# Tests that the bot attempts to join a room after being invited to it
|
||||
|
||||
fake_message_event = Mock(spec=nio.RoomMessageText)
|
||||
|
||||
command = Command(
|
||||
self.fake_client,
|
||||
self.fake_cache,
|
||||
|
@ -99,7 +141,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
|
|||
self.fake_config,
|
||||
"help",
|
||||
self.fake_room,
|
||||
fake_message_event,
|
||||
self.fake_message_event,
|
||||
)
|
||||
await command.process()
|
||||
|
||||
|
@ -111,8 +153,6 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
|
|||
"""Tests the callback for InviteMemberEvents"""
|
||||
# Tests that the bot attempts to join a room after being invited to it
|
||||
|
||||
fake_message_event = Mock(spec=nio.RoomMessageText)
|
||||
|
||||
command = Command(
|
||||
self.fake_client,
|
||||
self.fake_cache,
|
||||
|
@ -120,7 +160,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
|
|||
self.fake_config,
|
||||
"",
|
||||
self.fake_room,
|
||||
fake_message_event,
|
||||
self.fake_message_event,
|
||||
)
|
||||
await command.process()
|
||||
|
||||
|
@ -131,10 +171,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
|
|||
"""Tests the callback for InviteMemberEvents"""
|
||||
# Tests that the bot attempts to join a room after being invited to it
|
||||
|
||||
fake_message_event = Mock(spec=nio.RoomMessageText)
|
||||
fake_message_event.sender = "@some_other_fake_user:example.com"
|
||||
fake_message_event.body = ""
|
||||
fake_message_event.source = self.fake_source_not_in_reply
|
||||
self.fake_message_event.source = self.fake_source_not_in_reply
|
||||
|
||||
command = Command(
|
||||
self.fake_client,
|
||||
|
@ -143,7 +180,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
|
|||
self.fake_config,
|
||||
"ack",
|
||||
self.fake_room,
|
||||
fake_message_event,
|
||||
self.fake_message_event,
|
||||
)
|
||||
await command._ack()
|
||||
|
||||
|
@ -155,10 +192,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
|
|||
"""Tests the callback for InviteMemberEvents"""
|
||||
# Tests that the bot attempts to join a room after being invited to it
|
||||
|
||||
fake_message_event = Mock(spec=nio.RoomMessageText)
|
||||
fake_message_event.sender = "@some_other_fake_user:example.com"
|
||||
fake_message_event.body = ""
|
||||
fake_message_event.source = self.fake_source_not_in_reply
|
||||
self.fake_message_event.source = self.fake_source_not_in_reply
|
||||
|
||||
command = Command(
|
||||
self.fake_client,
|
||||
|
@ -167,7 +201,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
|
|||
self.fake_config,
|
||||
"ack 2d",
|
||||
self.fake_room,
|
||||
fake_message_event,
|
||||
self.fake_message_event,
|
||||
)
|
||||
await command._ack()
|
||||
|
||||
|
@ -182,10 +216,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
|
|||
"""Tests the callback for InviteMemberEvents"""
|
||||
# Tests that the bot attempts to join a room after being invited to it
|
||||
|
||||
fake_message_event = Mock(spec=nio.RoomMessageText)
|
||||
fake_message_event.sender = "@some_other_fake_user:example.com"
|
||||
fake_message_event.body = ""
|
||||
fake_message_event.source = self.fake_source_in_reply
|
||||
self.fake_message_event.source = self.fake_source_in_reply
|
||||
|
||||
command = Command(
|
||||
self.fake_client,
|
||||
|
@ -194,20 +225,20 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
|
|||
self.fake_config,
|
||||
"ack",
|
||||
self.fake_room,
|
||||
fake_message_event,
|
||||
self.fake_message_event,
|
||||
)
|
||||
await command._ack()
|
||||
|
||||
# Check that we attempted to create silences
|
||||
self.fake_alertmanager.create_silence.assert_has_calls(
|
||||
list(
|
||||
[
|
||||
call(
|
||||
fingerprint,
|
||||
"1d",
|
||||
fake_message_event.sender,
|
||||
)
|
||||
for fingerprint in self.fake_fingerprints.return_value
|
||||
self.fake_message_event.sender,
|
||||
)
|
||||
for fingerprint in self.fake_fingerprints
|
||||
]
|
||||
)
|
||||
fake_send_text_to_room.assert_called_once_with(
|
||||
self.fake_client,
|
||||
|
@ -222,10 +253,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
|
|||
"""Tests the callback for InviteMemberEvents"""
|
||||
# Tests that the bot attempts to join a room after being invited to it
|
||||
|
||||
fake_message_event = Mock(spec=nio.RoomMessageText)
|
||||
fake_message_event.sender = "@some_other_fake_user:example.com"
|
||||
fake_message_event.body = ""
|
||||
fake_message_event.source = self.fake_source_in_reply
|
||||
self.fake_message_event.source = self.fake_source_in_reply
|
||||
|
||||
command = Command(
|
||||
self.fake_client,
|
||||
|
@ -234,20 +262,20 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
|
|||
self.fake_config,
|
||||
"ack 2d",
|
||||
self.fake_room,
|
||||
fake_message_event,
|
||||
self.fake_message_event,
|
||||
)
|
||||
await command._ack()
|
||||
|
||||
# Check that we attempted to create silences
|
||||
self.fake_alertmanager.create_silence.assert_has_calls(
|
||||
list(
|
||||
[
|
||||
call(
|
||||
fingerprint,
|
||||
"2d",
|
||||
fake_message_event.sender,
|
||||
)
|
||||
for fingerprint in self.fake_fingerprints.return_value
|
||||
self.fake_message_event.sender,
|
||||
)
|
||||
for fingerprint in self.fake_fingerprints
|
||||
]
|
||||
)
|
||||
fake_send_text_to_room.assert_called_once_with(
|
||||
self.fake_client,
|
||||
|
@ -255,15 +283,94 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
|
|||
"Created 2 silences with a duration of 2d.",
|
||||
)
|
||||
|
||||
@patch.object(matrix_alertbot.command, "send_text_to_room")
|
||||
async def test_ack_raise_alert_not_found(
|
||||
self, fake_send_text_to_room: Mock
|
||||
) -> None:
|
||||
"""Tests the callback for InviteMemberEvents"""
|
||||
# Tests that the bot attempts to join a room after being invited to it
|
||||
|
||||
self.fake_message_event.source = self.fake_source_in_reply
|
||||
|
||||
command = Command(
|
||||
self.fake_client,
|
||||
self.fake_cache,
|
||||
self.fake_alertmanager,
|
||||
self.fake_config,
|
||||
"ack",
|
||||
self.fake_room,
|
||||
self.fake_message_event,
|
||||
)
|
||||
|
||||
self.fake_alertmanager.create_silence.side_effect = (
|
||||
create_silence_raise_alert_not_found
|
||||
)
|
||||
await command._ack()
|
||||
|
||||
# Check that we attempted to create silences
|
||||
self.fake_alertmanager.create_silence.assert_has_calls(
|
||||
[
|
||||
call(
|
||||
fingerprint,
|
||||
"1d",
|
||||
self.fake_message_event.sender,
|
||||
)
|
||||
for fingerprint in self.fake_fingerprints
|
||||
]
|
||||
)
|
||||
fake_send_text_to_room.assert_called_once_with(
|
||||
self.fake_client,
|
||||
self.fake_room.room_id,
|
||||
"Created 1 silences with a duration of 1d.",
|
||||
)
|
||||
|
||||
@patch.object(matrix_alertbot.command, "send_text_to_room")
|
||||
async def test_ack_raise_alertmanager_error(
|
||||
self, fake_send_text_to_room: Mock
|
||||
) -> None:
|
||||
"""Tests the callback for InviteMemberEvents"""
|
||||
# Tests that the bot attempts to join a room after being invited to it
|
||||
|
||||
self.fake_message_event.source = self.fake_source_in_reply
|
||||
|
||||
command = Command(
|
||||
self.fake_client,
|
||||
self.fake_cache,
|
||||
self.fake_alertmanager,
|
||||
self.fake_config,
|
||||
"ack",
|
||||
self.fake_room,
|
||||
self.fake_message_event,
|
||||
)
|
||||
|
||||
self.fake_alertmanager.create_silence.side_effect = (
|
||||
create_silence_raise_alertmanager_error
|
||||
)
|
||||
await command._ack()
|
||||
|
||||
# Check that we attempted to create silences
|
||||
self.fake_alertmanager.create_silence.assert_has_calls(
|
||||
[
|
||||
call(
|
||||
fingerprint,
|
||||
"1d",
|
||||
self.fake_message_event.sender,
|
||||
)
|
||||
for fingerprint in self.fake_fingerprints
|
||||
]
|
||||
)
|
||||
fake_send_text_to_room.assert_called_once_with(
|
||||
self.fake_client,
|
||||
self.fake_room.room_id,
|
||||
"Created 1 silences with a duration of 1d.",
|
||||
)
|
||||
|
||||
@patch.object(matrix_alertbot.command, "send_text_to_room")
|
||||
async def test_unack_in_reply(self, fake_send_text_to_room: Mock) -> None:
|
||||
"""Tests the callback for InviteMemberEvents"""
|
||||
# Tests that the bot attempts to join a room after being invited to it
|
||||
|
||||
fake_message_event = Mock(spec=nio.RoomMessageText)
|
||||
fake_message_event.sender = "@some_other_fake_user:example.com"
|
||||
fake_message_event.body = ""
|
||||
fake_message_event.source = self.fake_source_in_reply
|
||||
self.fake_message_event.source = self.fake_source_in_reply
|
||||
|
||||
command = Command(
|
||||
self.fake_client,
|
||||
|
@ -272,59 +379,113 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
|
|||
self.fake_config,
|
||||
"unack",
|
||||
self.fake_room,
|
||||
fake_message_event,
|
||||
self.fake_message_event,
|
||||
)
|
||||
await command._unack()
|
||||
|
||||
# Check that we attempted to create silences
|
||||
self.fake_alertmanager.delete_silence.assert_has_calls(
|
||||
list(
|
||||
call(fingerprint) for fingerprint in self.fake_fingerprints.return_value
|
||||
self.fake_alertmanager.delete_silences.assert_has_calls(
|
||||
[call(fingerprint) for fingerprint in self.fake_fingerprints]
|
||||
)
|
||||
fake_send_text_to_room.assert_called_with(
|
||||
self.fake_client, self.fake_room.room_id, "Removed 4 silences."
|
||||
)
|
||||
|
||||
@patch.object(matrix_alertbot.command, "send_text_to_room")
|
||||
async def test_unack_raise_silence_not_found(
|
||||
self, fake_send_text_to_room: Mock
|
||||
) -> None:
|
||||
"""Tests the callback for InviteMemberEvents"""
|
||||
# Tests that the bot attempts to join a room after being invited to it
|
||||
|
||||
self.fake_message_event.source = self.fake_source_in_reply
|
||||
|
||||
command = Command(
|
||||
self.fake_client,
|
||||
self.fake_cache,
|
||||
self.fake_alertmanager,
|
||||
self.fake_config,
|
||||
"unack",
|
||||
self.fake_room,
|
||||
self.fake_message_event,
|
||||
)
|
||||
|
||||
self.fake_alertmanager.delete_silences.side_effect = (
|
||||
delete_silence_raise_silence_not_found
|
||||
)
|
||||
await command._unack()
|
||||
|
||||
# Check that we attempted to create silences
|
||||
self.fake_alertmanager.delete_silences.assert_has_calls(
|
||||
[call(fingerprint) for fingerprint in self.fake_fingerprints]
|
||||
)
|
||||
fake_send_text_to_room.assert_called_with(
|
||||
self.fake_client, self.fake_room.room_id, "Removed 1 silences."
|
||||
)
|
||||
|
||||
@patch.object(matrix_alertbot.command, "send_text_to_room")
|
||||
async def test_unack_raise_alert_not_found(
|
||||
self, fake_send_text_to_room: Mock
|
||||
) -> None:
|
||||
"""Tests the callback for InviteMemberEvents"""
|
||||
# Tests that the bot attempts to join a room after being invited to it
|
||||
|
||||
self.fake_message_event.source = self.fake_source_in_reply
|
||||
|
||||
command = Command(
|
||||
self.fake_client,
|
||||
self.fake_cache,
|
||||
self.fake_alertmanager,
|
||||
self.fake_config,
|
||||
"unack",
|
||||
self.fake_room,
|
||||
self.fake_message_event,
|
||||
)
|
||||
|
||||
self.fake_alertmanager.delete_silences.side_effect = (
|
||||
delete_silence_raise_alert_not_found
|
||||
)
|
||||
await command._unack()
|
||||
|
||||
# Check that we attempted to create silences
|
||||
self.fake_alertmanager.delete_silences.assert_has_calls(
|
||||
[call(fingerprint) for fingerprint in self.fake_fingerprints]
|
||||
)
|
||||
fake_send_text_to_room.assert_called_with(
|
||||
self.fake_client, self.fake_room.room_id, "Removed 2 silences."
|
||||
)
|
||||
|
||||
# @patch.object(matrix_alertbot.callback, "Command", autospec=True)
|
||||
# async def test_message_without_prefix(self, fake_command: Mock) -> None:
|
||||
# """Tests the callback for RoomMessageText without any command prefix"""
|
||||
# # Tests that the bot process messages in the room
|
||||
# fake_message_event = Mock(spec=nio.RoomMessageText)
|
||||
# fake_message_event.sender = "@some_other_fake_user:example.com"
|
||||
# fake_message_event.body = "Hello world!"
|
||||
@patch.object(matrix_alertbot.command, "send_text_to_room")
|
||||
async def test_unack_silence_raise_alertmanager_error(
|
||||
self, fake_send_text_to_room: Mock
|
||||
) -> None:
|
||||
"""Tests the callback for InviteMemberEvents"""
|
||||
# Tests that the bot attempts to join a room after being invited to it
|
||||
|
||||
# # Pretend that we received a text message event
|
||||
# await self.callbacks.message(self.fake_room, fake_message_event)
|
||||
self.fake_message_event.source = self.fake_source_in_reply
|
||||
|
||||
# # Check that the command was not executed
|
||||
# fake_command.assert_not_called()
|
||||
command = Command(
|
||||
self.fake_client,
|
||||
self.fake_cache,
|
||||
self.fake_alertmanager,
|
||||
self.fake_config,
|
||||
"unack",
|
||||
self.fake_room,
|
||||
self.fake_message_event,
|
||||
)
|
||||
|
||||
# @patch.object(matrix_alertbot.callback, "Command", autospec=True)
|
||||
# async def test_message_with_prefix(self, fake_command: Mock) -> None:
|
||||
# """Tests the callback for RoomMessageText with the command prefix"""
|
||||
# # Tests that the bot process messages in the room that contain a command
|
||||
# fake_command_instance = fake_command.return_value
|
||||
# fake_command_instance.process.side_effect = lambda: print("hello")
|
||||
self.fake_alertmanager.delete_silences.side_effect = (
|
||||
delete_silence_raise_alertmanager_error
|
||||
)
|
||||
await command._unack()
|
||||
|
||||
# fake_message_event = Mock(spec=nio.RoomMessageText)
|
||||
# fake_message_event.sender = "@some_other_fake_user:example.com"
|
||||
# fake_message_event.body = "!alert help"
|
||||
|
||||
# # Pretend that we received a text message event
|
||||
# await self.callbacks.message(self.fake_room, fake_message_event)
|
||||
|
||||
# # Check that we attempted to execute the command
|
||||
# fake_command.assert_called_once_with(
|
||||
# self.fake_client,
|
||||
# self.fake_alertmanager,
|
||||
# self.fake_cache,
|
||||
# self.fake_config,
|
||||
# "help",
|
||||
# self.fake_room,
|
||||
# fake_message_event,
|
||||
# )
|
||||
# fake_command_instance.process.assert_called_once()
|
||||
# Check that we attempted to create silences
|
||||
self.fake_alertmanager.delete_silences.assert_has_calls(
|
||||
[call(fingerprint) for fingerprint in self.fake_fingerprints]
|
||||
)
|
||||
fake_send_text_to_room.assert_called_with(
|
||||
self.fake_client, self.fake_room.room_id, "Removed 3 silences."
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
Loading…
Reference in a new issue