2022-07-09 09:56:28 +02:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-07-09 10:24:17 +02:00
|
|
|
import json
|
2022-07-09 09:56:28 +02:00
|
|
|
import unittest
|
2022-07-10 02:40:04 +02:00
|
|
|
from datetime import datetime
|
|
|
|
from typing import Any, List
|
|
|
|
from unittest.mock import MagicMock, Mock, patch
|
2022-07-09 10:24:17 +02:00
|
|
|
|
2022-07-09 09:56:28 +02:00
|
|
|
import aiohttp
|
|
|
|
import aiohttp.test_utils
|
2022-07-09 10:24:17 +02:00
|
|
|
import aiotools
|
|
|
|
from aiohttp import web, web_request
|
2022-07-09 09:56:28 +02:00
|
|
|
from diskcache import Cache
|
|
|
|
|
|
|
|
from matrix_alertbot.alertmanager import AlertmanagerClient
|
|
|
|
from matrix_alertbot.errors import (
|
2022-07-09 10:38:40 +02:00
|
|
|
AlertmanagerServerError,
|
2022-07-10 02:40:04 +02:00
|
|
|
AlertMismatchError,
|
2022-07-09 10:24:17 +02:00
|
|
|
AlertNotFoundError,
|
2022-07-09 09:56:28 +02:00
|
|
|
SilenceNotFoundError,
|
|
|
|
)
|
2022-07-10 12:51:49 +02:00
|
|
|
from matrix_alertbot.matcher import AlertMatcher, AlertRegexMatcher
|
2022-07-10 02:40:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
class FakeTimeDelta:
|
|
|
|
def __init__(self, seconds: int) -> None:
|
|
|
|
self.seconds = seconds
|
|
|
|
|
|
|
|
def __radd__(self, other: Any) -> datetime:
|
|
|
|
return datetime.utcfromtimestamp(self.seconds)
|
2022-07-09 09:56:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
)
|
2022-07-10 02:40:04 +02:00
|
|
|
async with aiotools.closing_async(alertmanager):
|
2022-07-09 09:56:28 +02:00
|
|
|
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
|
|
|
|
)
|
2022-07-10 02:40:04 +02:00
|
|
|
async with aiotools.closing_async(alertmanager):
|
2022-07-09 09:56:28 +02:00
|
|
|
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
|
|
|
|
)
|
2022-07-10 02:40:04 +02:00
|
|
|
async with aiotools.closing_async(alertmanager):
|
2022-07-09 10:38:40 +02:00
|
|
|
with self.assertRaises(AlertmanagerServerError):
|
2022-07-09 09:56:28 +02:00
|
|
|
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
|
|
|
|
)
|
2022-07-10 02:40:04 +02:00
|
|
|
async with aiotools.closing_async(alertmanager):
|
2022-07-09 09:56:28 +02:00
|
|
|
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
|
|
|
|
)
|
2022-07-10 02:40:04 +02:00
|
|
|
async with aiotools.closing_async(alertmanager):
|
2022-07-09 09:56:28 +02:00
|
|
|
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
|
|
|
|
)
|
2022-07-10 02:40:04 +02:00
|
|
|
async with aiotools.closing_async(alertmanager):
|
2022-07-09 10:38:40 +02:00
|
|
|
with self.assertRaises(AlertmanagerServerError):
|
2022-07-09 09:56:28 +02:00
|
|
|
await alertmanager.get_alert("fingerprint1")
|
|
|
|
|
2022-07-10 02:40:04 +02:00
|
|
|
@patch("matrix_alertbot.alertmanager.timedelta", side_effect=FakeTimeDelta)
|
|
|
|
async def test_create_silence_without_matchers(self, fake_timedelta: Mock) -> 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):
|
|
|
|
silence = await alertmanager.create_silence(
|
2022-07-12 00:27:17 +02:00
|
|
|
"fingerprint1", 86400, "user", []
|
2022-07-10 02:40:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual("silence1", silence)
|
|
|
|
fake_timedelta.assert_called_once_with(seconds=86400)
|
|
|
|
|
|
|
|
@patch("matrix_alertbot.alertmanager.timedelta", side_effect=FakeTimeDelta)
|
|
|
|
async def test_create_silence_with_matchers(self, fake_timedelta: Mock) -> None:
|
2022-07-10 12:51:49 +02:00
|
|
|
matchers = [AlertMatcher(label="alertname", value="alert1")]
|
2022-07-10 02:40:04 +02:00
|
|
|
|
|
|
|
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):
|
|
|
|
silence = await alertmanager.create_silence(
|
|
|
|
"fingerprint1",
|
2022-07-12 00:27:17 +02:00
|
|
|
86400,
|
2022-07-10 02:40:04 +02:00
|
|
|
"user",
|
|
|
|
matchers,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual("silence1", silence)
|
|
|
|
fake_timedelta.assert_called_once_with(seconds=86400)
|
|
|
|
|
|
|
|
@patch("matrix_alertbot.alertmanager.timedelta", side_effect=FakeTimeDelta)
|
|
|
|
async def test_create_silence_with_regex_matchers(
|
|
|
|
self, fake_timedelta: Mock
|
|
|
|
) -> None:
|
2022-07-10 12:51:49 +02:00
|
|
|
matchers: List[AlertMatcher] = [
|
2022-07-10 02:40:04 +02:00
|
|
|
AlertRegexMatcher(label="alertname", regex=r"alert\d+")
|
|
|
|
]
|
|
|
|
|
|
|
|
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):
|
|
|
|
silence = await alertmanager.create_silence(
|
|
|
|
"fingerprint1",
|
2022-07-12 00:27:17 +02:00
|
|
|
86400,
|
2022-07-10 02:40:04 +02:00
|
|
|
"user",
|
|
|
|
matchers,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual("silence1", silence)
|
|
|
|
fake_timedelta.assert_called_once_with(seconds=86400)
|
|
|
|
|
|
|
|
async def test_create_silence_raise_missing_label(self) -> None:
|
2022-07-10 12:51:49 +02:00
|
|
|
matchers = [
|
2022-07-10 02:40:04 +02:00
|
|
|
AlertMatcher(label="alertname", value="alert1"),
|
|
|
|
AlertMatcher(label="severity", value="critical"),
|
|
|
|
]
|
|
|
|
|
|
|
|
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):
|
|
|
|
with self.assertRaises(AlertMismatchError):
|
|
|
|
await alertmanager.create_silence(
|
|
|
|
"fingerprint1",
|
2022-07-12 00:27:17 +02:00
|
|
|
86400,
|
2022-07-10 02:40:04 +02:00
|
|
|
"user",
|
|
|
|
matchers,
|
|
|
|
)
|
|
|
|
|
|
|
|
async def test_create_silence_raise_mismatch_label(self) -> None:
|
2022-07-10 12:51:49 +02:00
|
|
|
matchers = [AlertMatcher(label="alertname", value="alert2")]
|
2022-07-10 02:40:04 +02:00
|
|
|
|
|
|
|
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):
|
|
|
|
with self.assertRaises(AlertMismatchError):
|
|
|
|
await alertmanager.create_silence(
|
|
|
|
"fingerprint1",
|
2022-07-12 00:27:17 +02:00
|
|
|
86400,
|
2022-07-10 02:40:04 +02:00
|
|
|
"user",
|
|
|
|
matchers,
|
|
|
|
)
|
|
|
|
|
|
|
|
async def test_create_silence_raise_mismatch_regex_label(self) -> None:
|
2022-07-10 12:51:49 +02:00
|
|
|
matchers: List[AlertMatcher] = [
|
2022-07-10 02:40:04 +02:00
|
|
|
AlertRegexMatcher(label="alertname", regex=r"alert[^\d]+")
|
|
|
|
]
|
|
|
|
|
|
|
|
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):
|
|
|
|
with self.assertRaises(AlertMismatchError):
|
|
|
|
await alertmanager.create_silence(
|
|
|
|
"fingerprint1",
|
2022-07-12 00:27:17 +02:00
|
|
|
86400,
|
2022-07-10 02:40:04 +02:00
|
|
|
"user",
|
|
|
|
matchers,
|
|
|
|
)
|
2022-07-09 09:56:28 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
)
|
2022-07-10 02:40:04 +02:00
|
|
|
async with aiotools.closing_async(alertmanager):
|
2022-07-09 09:56:28 +02:00
|
|
|
with self.assertRaises(AlertNotFoundError):
|
2022-07-12 00:27:17 +02:00
|
|
|
await alertmanager.create_silence("fingerprint1", 86400, "user", [])
|
2022-07-09 09:56:28 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
)
|
2022-07-10 02:40:04 +02:00
|
|
|
async with aiotools.closing_async(alertmanager):
|
2022-07-09 09:56:28 +02:00
|
|
|
await alertmanager.get_alert("fingerprint1")
|
|
|
|
|
2022-07-09 10:38:40 +02:00
|
|
|
with self.assertRaises(AlertmanagerServerError):
|
2022-07-12 00:27:17 +02:00
|
|
|
await alertmanager.create_silence("fingerprint1", 86400, "user", [])
|
2022-07-09 09:56:28 +02:00
|
|
|
|
2022-07-10 12:51:49 +02:00
|
|
|
async def test_delete_silences_without_matchers(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):
|
|
|
|
silences = await alertmanager.delete_silences("fingerprint2", [])
|
|
|
|
|
|
|
|
self.assertEqual(["silence1", "silence2"], silences)
|
|
|
|
|
|
|
|
async def test_delete_silences_with_matchers(self) -> None:
|
|
|
|
matchers = [AlertMatcher(label="alertname", value="alert2")]
|
|
|
|
|
|
|
|
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):
|
|
|
|
silences = await alertmanager.delete_silences("fingerprint2", matchers)
|
|
|
|
|
|
|
|
self.assertEqual(["silence1", "silence2"], silences)
|
|
|
|
|
|
|
|
async def test_delete_silences_with_regex_matchers(self) -> None:
|
|
|
|
matchers: List[AlertMatcher] = [
|
|
|
|
AlertRegexMatcher(label="alertname", regex=r"alert\d+")
|
|
|
|
]
|
|
|
|
|
|
|
|
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):
|
|
|
|
silences = await alertmanager.delete_silences("fingerprint2", matchers)
|
|
|
|
|
|
|
|
self.assertEqual(["silence1", "silence2"], silences)
|
|
|
|
|
|
|
|
async def test_delete_silences_raise_missing_label(self) -> None:
|
|
|
|
matchers = [
|
|
|
|
AlertMatcher(label="alertname", value="alert2"),
|
|
|
|
AlertMatcher(label="severity", value="critical"),
|
|
|
|
]
|
|
|
|
|
|
|
|
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):
|
|
|
|
with self.assertRaises(AlertMismatchError):
|
|
|
|
await alertmanager.delete_silences("fingerprint2", matchers)
|
|
|
|
|
|
|
|
async def test_delete_silences_raise_mismatch_label(self) -> None:
|
|
|
|
matchers = [
|
|
|
|
AlertMatcher(label="alertname", value="alert1"),
|
|
|
|
]
|
|
|
|
|
|
|
|
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):
|
|
|
|
with self.assertRaises(AlertMismatchError):
|
|
|
|
await alertmanager.delete_silences("fingerprint2", matchers)
|
|
|
|
|
|
|
|
async def test_delete_silences_raise_mismatch_regex_label(self) -> None:
|
|
|
|
matchers: List[AlertMatcher] = [
|
|
|
|
AlertRegexMatcher(label="alertname", regex=r"alert[^\d]+"),
|
|
|
|
]
|
|
|
|
|
2022-07-09 09:56:28 +02:00
|
|
|
async with FakeAlertmanagerServer() as fake_alertmanager_server:
|
|
|
|
port = fake_alertmanager_server.port
|
|
|
|
alertmanager = AlertmanagerClient(
|
|
|
|
f"http://localhost:{port}", self.fake_cache
|
|
|
|
)
|
2022-07-10 02:40:04 +02:00
|
|
|
async with aiotools.closing_async(alertmanager):
|
2022-07-10 12:51:49 +02:00
|
|
|
with self.assertRaises(AlertMismatchError):
|
|
|
|
await alertmanager.delete_silences("fingerprint2", matchers)
|
2022-07-09 09:56:28 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
)
|
2022-07-10 02:40:04 +02:00
|
|
|
async with aiotools.closing_async(alertmanager):
|
2022-07-09 09:56:28 +02:00
|
|
|
with self.assertRaises(SilenceNotFoundError):
|
2022-07-10 12:51:49 +02:00
|
|
|
await alertmanager.delete_silences("fingerprint1", [])
|
2022-07-09 09:56:28 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
)
|
2022-07-10 02:40:04 +02:00
|
|
|
async with aiotools.closing_async(alertmanager):
|
2022-07-09 09:56:28 +02:00
|
|
|
with self.assertRaises(AlertNotFoundError):
|
2022-07-10 12:51:49 +02:00
|
|
|
await alertmanager.delete_silences("fingerprint2", [])
|
2022-07-09 09:56:28 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
)
|
2022-07-10 02:40:04 +02:00
|
|
|
async with aiotools.closing_async(alertmanager):
|
2022-07-09 09:56:28 +02:00
|
|
|
await alertmanager.get_alert("fingerprint1")
|
|
|
|
|
2022-07-09 10:38:40 +02:00
|
|
|
with self.assertRaises(AlertmanagerServerError):
|
2022-07-10 12:51:49 +02:00
|
|
|
await alertmanager.delete_silences("fingerprint2", [])
|
2022-07-09 09:56:28 +02:00
|
|
|
|
|
|
|
async def test_find_alert_happy(self) -> None:
|
2022-07-09 15:25:16 +02:00
|
|
|
alertmanager = AlertmanagerClient("http://localhost", self.fake_cache)
|
2022-07-09 09:56:28 +02:00
|
|
|
alert = alertmanager._find_alert(
|
|
|
|
"fingerprint1", [{"fingerprint": "fingerprint1"}]
|
|
|
|
)
|
|
|
|
self.assertEqual({"fingerprint": "fingerprint1"}, alert)
|
|
|
|
|
|
|
|
async def test_find_alert_raise_alert_not_found(self) -> None:
|
2022-07-09 15:25:16 +02:00
|
|
|
alertmanager = AlertmanagerClient("http://localhost", self.fake_cache)
|
2022-07-09 09:56:28 +02:00
|
|
|
|
|
|
|
with self.assertRaises(AlertNotFoundError):
|
|
|
|
alertmanager._find_alert("fingerprint2", [{"fingerprint": "fingerprint1"}])
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|