refactor command to use subclass per command

This commit is contained in:
HgO 2022-07-16 23:08:12 +02:00
parent c4752d8192
commit 76b8ead9d7
4 changed files with 255 additions and 123 deletions

View file

@ -15,7 +15,7 @@ from nio import (
from matrix_alertbot.alertmanager import AlertmanagerClient
from matrix_alertbot.chat_functions import strip_fallback
from matrix_alertbot.command import Command
from matrix_alertbot.command import CommandFactory
from matrix_alertbot.config import Config
logger = logging.getLogger(__name__)
@ -80,22 +80,26 @@ class Callbacks:
return
source_content = event.source["content"]
try:
alert_event_id = source_content["m.relates_to"]["m.in_reply_to"]["event_id"]
except KeyError:
logger.debug("Unable to find the event ID of the alert")
return
alert_event_id = (
source_content.get("m.relates_to", {})
.get("m.in_reply_to", {})
.get("event_id")
)
if alert_event_id is None:
logger.warning("Unable to find the event ID of the alert")
# Remove the command prefix
msg = msg[len(self.command_prefix) :]
command = Command(
cmd = msg[len(self.command_prefix) :]
command = CommandFactory.create(
cmd,
self.client,
self.cache,
self.alertmanager,
self.config,
msg,
room,
event.sender,
event.event_id,
alert_event_id,
)
await command.process()
@ -145,7 +149,7 @@ class Callbacks:
await self.invite(room, event)
async def _reaction(
self, room: MatrixRoom, event: UnknownEvent, reacted_to_id: str
self, room: MatrixRoom, event: UnknownEvent, alert_event_id: str
) -> None:
"""A reaction was sent to one of our messages. Let's send a reply acknowledging it.
@ -173,9 +177,11 @@ class Callbacks:
duration = REACTION_DURATIONS[reaction]
# Get the original event that was reacted to
event_response = await self.client.room_get_event(room.room_id, reacted_to_id)
event_response = await self.client.room_get_event(room.room_id, alert_event_id)
if isinstance(event_response, RoomGetEventError):
logger.warning(f"Error getting event that was reacted to ({reacted_to_id})")
logger.warning(
f"Error getting event that was reacted to ({alert_event_id})"
)
return
reacted_to_event = event_response.event
@ -190,15 +196,16 @@ class Callbacks:
)
# Send a message acknowledging the reaction
command = Command(
command = CommandFactory.create(
f"ack {duration}",
self.client,
self.cache,
self.alertmanager,
self.config,
f"ack {duration}",
room,
event.sender,
reacted_to_id,
event.event_id,
alert_event_id,
)
await command.process()
@ -215,21 +222,18 @@ class Callbacks:
logger.warning(f"Error removing silence from reaction {event.redacts}")
return
reacted_to_id = self.cache[event.redacts]
reacted_to_event = await self.client.room_get_event(room.room_id, reacted_to_id)
if isinstance(reacted_to_event, RoomGetEventError):
logger.warning(f"Error getting event that was reacted to ({reacted_to_id})")
return
alert_event_id = self.cache[event.redacts]
command = Command(
command = CommandFactory.create(
"unack",
self.client,
self.cache,
self.alertmanager,
self.config,
"unack",
room,
event.sender,
reacted_to_id,
event.redacts,
alert_event_id,
)
await command.process()

View file

@ -1,5 +1,5 @@
import logging
from typing import List
from typing import List, Optional
import pytimeparse2
from diskcache import Cache
@ -18,55 +18,73 @@ from matrix_alertbot.matcher import AlertMatcher, AlertRegexMatcher
logger = logging.getLogger(__name__)
class Command:
class BaseCommand:
def __init__(
self,
client: AsyncClient,
cache: Cache,
alertmanager: AlertmanagerClient,
config: Config,
command: str,
cmd: str,
room: MatrixRoom,
sender: str,
event_id: str,
):
) -> None:
"""A command made by a user.
Args:
client: The client to communicate to matrix with.
client: The client to communicate with Matrix.
cache: Bot cache.
alertmanager: The client to communicate with Alertmanager.
config: Bot configuration parameters.
command: The command and arguments.
cmd: The command and arguments.
room: The room the command was sent in.
event: The event describing the command.
sender: The sender of the event
event_id: The ID of the event describing the command.
"""
self.client = client
self.cache = cache
self.alertmanager = alertmanager
self.config = config
self.command = command
self.cmd = cmd
self.args = cmd.split()[1:]
self.room = room
self.sender = sender
self.event_id = event_id
self.args = self.command.split()[1:]
async def process(self) -> None:
"""Process the command"""
if self.command.startswith("ack"):
await self._ack()
elif self.command.startswith("unack") or self.command.startswith("nack"):
await self._unack()
elif self.command.startswith("help"):
await self._show_help()
else:
await self._unknown_command()
raise NotImplementedError
async def _ack(self) -> None:
class BaseAlertCommand(BaseCommand):
def __init__(
self,
client: AsyncClient,
cache: Cache,
alertmanager: AlertmanagerClient,
config: Config,
cmd: str,
room: MatrixRoom,
sender: str,
event_id: str,
alert_event_id: str,
) -> None:
super().__init__(
client, cache, alertmanager, config, cmd, room, sender, event_id
)
self.alert_event_id = alert_event_id
class AckAlertCommand(BaseAlertCommand):
async def process(self) -> None:
"""Acknowledge an alert and silence it for a certain duration in Alertmanager"""
matchers: List[AlertMatcher] = []
durations = []
@ -101,36 +119,42 @@ class Command:
)
return
logger.debug(f"Read alert fingerprints for event {self.event_id} from cache")
logger.debug(
f"Read alert fingerprints for alert event {self.alert_event_id} from cache"
)
if self.event_id not in self.cache:
logger.error(f"Cannot find fingerprints for event {self.event_id} in cache")
if self.alert_event_id not in self.cache:
logger.error(
f"Cannot find fingerprints for alert event {self.alert_event_id} in cache"
)
return
alert_fingerprints = self.cache[self.event_id]
alert_fingerprints = self.cache[self.alert_event_id]
logger.debug(f"Found {len(alert_fingerprints)} in cache")
count_alert_not_found = 0
count_created_silences = 0
created_silences = []
for alert_fingerprint in alert_fingerprints:
logger.debug(
f"Create silence for alert with fingerprint {alert_fingerprint} for a duration of {duration}"
)
try:
await self.alertmanager.create_silence(
silence_id = await self.alertmanager.create_silence(
alert_fingerprint,
duration_seconds,
self.room.user_name(self.sender),
matchers,
)
count_created_silences += 1
created_silences.append(silence_id)
except AlertNotFoundError as e:
logger.warning(f"Unable to create silence: {e}")
count_alert_not_found += 1
except AlertmanagerError as e:
logger.exception(f"Unable to create silence: {e}", exc_info=e)
self.cache.set(self.event_id, tuple(created_silences), expire=duration_seconds)
if count_alert_not_found > 0:
await send_text_to_room(
self.client,
@ -138,14 +162,16 @@ class Command:
f"Sorry, I couldn't find {count_alert_not_found} alerts, therefore I couldn't create their silence.",
)
if count_created_silences > 0:
if len(created_silences) > 0:
await send_text_to_room(
self.client,
self.room.room_id,
f"Created {count_created_silences} silences with a duration of {duration}.",
f"Created {len(created_silences)} silences with a duration of {duration}.",
)
async def _unack(self) -> None:
class UnackAlertCommand(BaseAlertCommand):
async def process(self) -> None:
"""Delete an alert's acknowledgement of an alert and remove corresponding silence in Alertmanager"""
matchers: List[AlertMatcher] = []
for arg in self.args:
@ -159,13 +185,17 @@ class Command:
matchers.append(matcher)
logger.debug("Receiving a command to delete a silence")
logger.debug(f"Read alert fingerprints for event {self.event_id} from cache")
logger.debug(
f"Read alert fingerprints for alert event {self.alert_event_id} from cache"
)
if self.event_id not in self.cache:
logger.error(f"Cannot find fingerprints for event {self.event_id} in cache")
if self.alert_event_id not in self.cache:
logger.error(
f"Cannot find fingerprints for event {self.alert_event_id} in cache"
)
return
alert_fingerprints = self.cache[self.event_id]
alert_fingerprints = self.cache[self.alert_event_id]
logger.debug(f"Found {len(alert_fingerprints)} in cache")
count_alert_not_found = 0
@ -199,7 +229,9 @@ class Command:
f"Removed {count_removed_silences} silences.",
)
async def _show_help(self) -> None:
class HelpCommand(BaseCommand):
async def process(self) -> None:
"""Show the help text"""
logger.debug(f"Displaying help to room {self.room.display_name}")
if not self.args:
@ -219,12 +251,63 @@ class Command:
text = "Unknown help topic!"
await send_text_to_room(self.client, self.room.room_id, text)
async def _unknown_command(self) -> None:
class UnknownCommand(BaseCommand):
async def process(self) -> None:
logger.debug(
f"Sending unknown command response to room {self.room.display_name}"
)
await send_text_to_room(
self.client,
self.room.room_id,
f"Unknown command '{self.command}'. Try the 'help' command for more information.",
f"Unknown command '{self.cmd}'. Try the 'help' command for more information.",
)
class CommandFactory:
@staticmethod
def create(
cmd: str,
client: AsyncClient,
cache: Cache,
alertmanager: AlertmanagerClient,
config: Config,
room: MatrixRoom,
sender: str,
event_id: str,
reacted_to_event_id: Optional[str] = None,
) -> BaseCommand:
if cmd.startswith("ack"):
assert reacted_to_event_id is not None
return AckAlertCommand(
client,
cache,
alertmanager,
config,
cmd,
room,
sender,
event_id,
reacted_to_event_id,
)
elif cmd.startswith("unack") or cmd.startswith("nack"):
assert reacted_to_event_id is not None
return UnackAlertCommand(
client,
cache,
alertmanager,
config,
cmd,
room,
sender,
event_id,
reacted_to_event_id,
)
elif cmd.startswith("help"):
return HelpCommand(
client, cache, alertmanager, config, cmd, room, sender, event_id
)
else:
return UnknownCommand(
client, cache, alertmanager, config, cmd, room, sender, event_id
)

View file

@ -7,6 +7,7 @@ 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 BaseCommand
from tests.utils import make_awaitable
@ -49,8 +50,8 @@ class CallbacksTestCase(unittest.IsolatedAsyncioTestCase):
# Check that we attempted to join the room
self.fake_client.join.assert_called_once_with(self.fake_room.room_id)
@patch.object(matrix_alertbot.callback, "Command", autospec=True)
async def test_message_without_prefix(self, fake_command: Mock) -> None:
@patch.object(matrix_alertbot.callback.CommandFactory, "create", autospec=True)
async def test_message_without_prefix(self, fake_command_create: 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)
@ -61,14 +62,19 @@ class CallbacksTestCase(unittest.IsolatedAsyncioTestCase):
await self.callbacks.message(self.fake_room, fake_message_event)
# Check that the command was not executed
fake_command.assert_not_called()
fake_command_create.assert_not_called()
@patch.object(matrix_alertbot.callback, "Command", autospec=True)
async def test_message_not_in_reply_with_prefix(self, fake_command: Mock) -> None:
@patch.object(matrix_alertbot.callback.CommandFactory, "create", autospec=True)
async def test_message_not_in_reply_with_prefix(
self, fake_command_create: 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 = Mock(spec=BaseCommand)
fake_command_create.return_value = fake_command
fake_message_event = Mock(spec=nio.RoomMessageText)
fake_message_event.event_id = "some event id"
fake_message_event.sender = "@some_other_fake_user:example.com"
fake_message_event.body = "!alert help"
fake_message_event.source = {"content": {}}
@ -77,20 +83,35 @@ class CallbacksTestCase(unittest.IsolatedAsyncioTestCase):
await self.callbacks.message(self.fake_room, fake_message_event)
# Check that the command was not executed
fake_command.assert_not_called()
fake_command_create.assert_called_with(
"help",
self.fake_client,
self.fake_alertmanager,
self.fake_cache,
self.fake_config,
self.fake_room,
fake_message_event.sender,
fake_message_event.event_id,
None,
)
fake_command.process.assert_called_once()
@patch.object(matrix_alertbot.callback, "Command", autospec=True)
async def test_message_in_reply_with_prefix(self, fake_command: Mock) -> None:
@patch.object(matrix_alertbot.callback.CommandFactory, "create", autospec=True)
async def test_message_in_reply_with_prefix(
self, fake_command_create: 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 = Mock(spec=BaseCommand)
fake_command_create.return_value = fake_command
fake_message_event = Mock(spec=nio.RoomMessageText)
fake_message_event.event_id = "some event id"
fake_message_event.sender = "@some_other_fake_user:example.com"
fake_message_event.body = "!alert help"
fake_message_event.source = {
"content": {
"m.relates_to": {"m.in_reply_to": {"event_id": "some event id"}}
"m.relates_to": {"m.in_reply_to": {"event_id": "some alert event id"}}
}
}
@ -98,17 +119,18 @@ class CallbacksTestCase(unittest.IsolatedAsyncioTestCase):
await self.callbacks.message(self.fake_room, fake_message_event)
# Check that we attempted to execute the command
fake_command.assert_called_once_with(
fake_command_create.assert_called_once_with(
"help",
self.fake_client,
self.fake_alertmanager,
self.fake_cache,
self.fake_config,
"help",
self.fake_room,
fake_message_event.sender,
"some event id",
fake_message_event.event_id,
"some alert event id",
)
fake_command_instance.process.assert_called_once()
fake_command.process.assert_called_once()
if __name__ == "__main__":

View file

@ -6,8 +6,15 @@ import nio
from diskcache import Cache
import matrix_alertbot.callback
import matrix_alertbot.command
from matrix_alertbot.alertmanager import AlertmanagerClient
from matrix_alertbot.command import Command
from matrix_alertbot.command import (
AckAlertCommand,
CommandFactory,
HelpCommand,
UnackAlertCommand,
UnknownCommand,
)
from matrix_alertbot.errors import (
AlertmanagerError,
AlertNotFoundError,
@ -76,64 +83,67 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
self.fake_event_id = "some event id"
self.fake_sender = "@some_other_fake_user:example.com"
self.fake_alert_event_id = "some alert event id"
# 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
self.fake_config.command_prefix = "!alert "
@patch.object(matrix_alertbot.command.Command, "_ack")
@patch.object(matrix_alertbot.command.AckAlertCommand, "process")
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
command = Command(
command = CommandFactory.create(
"ack",
self.fake_client,
self.fake_cache,
self.fake_alertmanager,
self.fake_config,
"ack",
self.fake_room,
self.fake_sender,
self.fake_event_id,
self.fake_alert_event_id,
)
await command.process()
# Check that we attempted to process the command
fake_ack.assert_called_once()
@patch.object(matrix_alertbot.command.Command, "_unack")
@patch.object(matrix_alertbot.command.UnackAlertCommand, "process")
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
for command_word in ("unack", "nack"):
command = Command(
for unack_cmd in ("unack", "nack"):
command = CommandFactory.create(
unack_cmd,
self.fake_client,
self.fake_cache,
self.fake_alertmanager,
self.fake_config,
command_word,
self.fake_room,
self.fake_sender,
self.fake_event_id,
self.fake_alert_event_id,
)
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")
@patch.object(matrix_alertbot.command.HelpCommand, "process")
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
command = Command(
command = CommandFactory.create(
"help",
self.fake_client,
self.fake_cache,
self.fake_alertmanager,
self.fake_config,
"help",
self.fake_room,
self.fake_sender,
self.fake_event_id,
@ -143,17 +153,17 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
# Check that we attempted to process the command
fake_help.assert_called_once()
@patch.object(matrix_alertbot.command.Command, "_unknown_command")
@patch.object(matrix_alertbot.command.UnknownCommand, "process")
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
command = Command(
command = CommandFactory.create(
"",
self.fake_client,
self.fake_cache,
self.fake_alertmanager,
self.fake_config,
"",
self.fake_room,
self.fake_sender,
self.fake_event_id,
@ -170,7 +180,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
"""Tests the callback for InviteMemberEvents"""
# Tests that the bot attempts to join a room after being invited to it
command = Command(
command = AckAlertCommand(
self.fake_client,
self.fake_cache,
self.fake_alertmanager,
@ -179,8 +189,9 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
self.fake_room,
self.fake_sender,
self.fake_event_id,
self.fake_alert_event_id,
)
await command._ack()
await command.process()
# Check that we attempted to create silences
self.fake_alertmanager.create_silence.assert_has_calls(
@ -206,7 +217,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
AlertRegexMatcher(label="severity", regex="critical"),
]
command = Command(
command = AckAlertCommand(
self.fake_client,
self.fake_cache,
self.fake_alertmanager,
@ -215,8 +226,9 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
self.fake_room,
self.fake_sender,
self.fake_event_id,
self.fake_alert_event_id,
)
await command._ack()
await command.process()
# Check that we attempted to create silences
self.fake_alertmanager.create_silence.assert_has_calls(
@ -243,7 +255,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
"""Tests the callback for InviteMemberEvents"""
# Tests that the bot attempts to join a room after being invited to it
command = Command(
command = AckAlertCommand(
self.fake_client,
self.fake_cache,
self.fake_alertmanager,
@ -252,8 +264,9 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
self.fake_room,
self.fake_sender,
self.fake_event_id,
self.fake_alert_event_id,
)
await command._ack()
await command.process()
# Check that we attempted to create silences
self.fake_alertmanager.create_silence.assert_has_calls(
@ -279,7 +292,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
AlertMatcher(label="severity", value="critical"),
]
command = Command(
command = AckAlertCommand(
self.fake_client,
self.fake_cache,
self.fake_alertmanager,
@ -288,8 +301,9 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
self.fake_room,
self.fake_sender,
self.fake_event_id,
self.fake_alert_event_id,
)
await command._ack()
await command.process()
# Check that we attempted to create silences
self.fake_alertmanager.create_silence.assert_has_calls(
@ -316,7 +330,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
"""Tests the callback for InviteMemberEvents"""
# Tests that the bot attempts to join a room after being invited to it
command = Command(
command = AckAlertCommand(
self.fake_client,
self.fake_cache,
self.fake_alertmanager,
@ -325,12 +339,13 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
self.fake_room,
self.fake_sender,
self.fake_event_id,
self.fake_alert_event_id,
)
self.fake_alertmanager.create_silence.side_effect = (
create_silence_raise_alertmanager_error
)
await command._ack()
await command.process()
# Check that we attempted to create silences
self.fake_alertmanager.create_silence.assert_has_calls(
@ -352,7 +367,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
"""Tests the callback for InviteMemberEvents"""
# Tests that the bot attempts to join a room after being invited to it
command = Command(
command = AckAlertCommand(
self.fake_client,
self.fake_cache,
self.fake_alertmanager,
@ -361,12 +376,13 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
self.fake_room,
self.fake_sender,
self.fake_event_id,
self.fake_alert_event_id,
)
self.fake_alertmanager.create_silence.side_effect = (
create_silence_raise_alert_not_found_error
)
await command._ack()
await command.process()
# Check that we attempted to create silences
self.fake_alertmanager.create_silence.assert_has_calls(
@ -397,7 +413,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
"""Tests the callback for InviteMemberEvents"""
# Tests that the bot attempts to join a room after being invited to it
command = Command(
command = AckAlertCommand(
self.fake_client,
self.fake_cache,
self.fake_alertmanager,
@ -406,9 +422,10 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
self.fake_room,
self.fake_sender,
self.fake_event_id,
self.fake_alert_event_id,
)
await command._ack()
await command.process()
# Check that we attempted to create silences
self.fake_alertmanager.create_silence.assert_not_called()
@ -427,7 +444,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
self.fake_cache.__contains__.return_value = False
command = Command(
command = AckAlertCommand(
self.fake_client,
self.fake_cache,
self.fake_alertmanager,
@ -436,9 +453,10 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
self.fake_room,
self.fake_sender,
self.fake_event_id,
self.fake_alert_event_id,
)
await command._ack()
await command.process()
# Check that we attempted to create silences
self.fake_alertmanager.create_silence.assert_not_called()
@ -449,7 +467,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
"""Tests the callback for InviteMemberEvents"""
# Tests that the bot attempts to join a room after being invited to it
command = Command(
command = UnackAlertCommand(
self.fake_client,
self.fake_cache,
self.fake_alertmanager,
@ -458,8 +476,9 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
self.fake_room,
self.fake_sender,
self.fake_event_id,
self.fake_alert_event_id,
)
await command._unack()
await command.process()
# Check that we attempted to create silences
self.fake_alertmanager.delete_silences.assert_has_calls(
@ -479,7 +498,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
AlertRegexMatcher(label="severity", regex="critical"),
]
command = Command(
command = UnackAlertCommand(
self.fake_client,
self.fake_cache,
self.fake_alertmanager,
@ -488,8 +507,9 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
self.fake_room,
self.fake_sender,
self.fake_event_id,
self.fake_alert_event_id,
)
await command._unack()
await command.process()
# Check that we attempted to create silences
self.fake_alertmanager.delete_silences.assert_has_calls(
@ -506,7 +526,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
"""Tests the callback for InviteMemberEvents"""
# Tests that the bot attempts to join a room after being invited to it
command = Command(
command = UnackAlertCommand(
self.fake_client,
self.fake_cache,
self.fake_alertmanager,
@ -515,12 +535,13 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
self.fake_room,
self.fake_sender,
self.fake_event_id,
self.fake_alert_event_id,
)
self.fake_alertmanager.delete_silences.side_effect = (
delete_silence_raise_alertmanager_error
)
await command._unack()
await command.process()
# Check that we attempted to create silences
self.fake_alertmanager.delete_silences.assert_has_calls(
@ -537,7 +558,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
"""Tests the callback for InviteMemberEvents"""
# Tests that the bot attempts to join a room after being invited to it
command = Command(
command = UnackAlertCommand(
self.fake_client,
self.fake_cache,
self.fake_alertmanager,
@ -546,12 +567,13 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
self.fake_room,
self.fake_sender,
self.fake_event_id,
self.fake_alert_event_id,
)
self.fake_alertmanager.delete_silences.side_effect = (
delete_silence_raise_silence_not_found_error
)
await command._unack()
await command.process()
# Check that we attempted to create silences
self.fake_alertmanager.delete_silences.assert_has_calls(
@ -581,7 +603,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
self.fake_cache.__contains__.return_value = False
command = Command(
command = UnackAlertCommand(
self.fake_client,
self.fake_cache,
self.fake_alertmanager,
@ -590,9 +612,10 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
self.fake_room,
self.fake_sender,
self.fake_event_id,
self.fake_alert_event_id,
)
await command._unack()
await command.process()
# Check that we attempted to create silences
self.fake_alertmanager.create_silence.assert_not_called()
@ -603,7 +626,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
"""Tests the callback for InviteMemberEvents"""
# Tests that the bot attempts to join a room after being invited to it
command = Command(
command = HelpCommand(
self.fake_client,
self.fake_cache,
self.fake_alertmanager,
@ -614,7 +637,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
self.fake_event_id,
)
await command._show_help()
await command.process()
# Check that we attempted to create silences
fake_send_text_to_room.assert_called_once()
@ -626,7 +649,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
"""Tests the callback for InviteMemberEvents"""
# Tests that the bot attempts to join a room after being invited to it
command = Command(
command = HelpCommand(
self.fake_client,
self.fake_cache,
self.fake_alertmanager,
@ -637,7 +660,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
self.fake_event_id,
)
await command._show_help()
await command.process()
# Check that we attempted to create silences
fake_send_text_to_room.assert_called_once()
@ -649,7 +672,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
"""Tests the callback for InviteMemberEvents"""
# Tests that the bot attempts to join a room after being invited to it
command = Command(
command = HelpCommand(
self.fake_client,
self.fake_cache,
self.fake_alertmanager,
@ -660,7 +683,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
self.fake_event_id,
)
await command._show_help()
await command.process()
# Check that we attempted to create silences
fake_send_text_to_room.assert_called_once()
@ -672,7 +695,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
"""Tests the callback for InviteMemberEvents"""
# Tests that the bot attempts to join a room after being invited to it
command = Command(
command = HelpCommand(
self.fake_client,
self.fake_cache,
self.fake_alertmanager,
@ -683,7 +706,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
self.fake_event_id,
)
await command._show_help()
await command.process()
# Check that we attempted to create silences
fake_send_text_to_room.assert_called_once()
@ -695,7 +718,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
"""Tests the callback for InviteMemberEvents"""
# Tests that the bot attempts to join a room after being invited to it
command = Command(
command = UnknownCommand(
self.fake_client,
self.fake_cache,
self.fake_alertmanager,
@ -706,7 +729,7 @@ class CommandTestCase(unittest.IsolatedAsyncioTestCase):
self.fake_event_id,
)
await command._unknown_command()
await command.process()
# Check that we attempted to create silences
fake_send_text_to_room.assert_called_once_with(