|
| 1 | +"""Tests for slack_alert ECS start-impaired alerts — run: python3 -m unittest discover -s terraform/lambda-src/tests""" |
| 2 | + |
| 3 | +import importlib.util |
| 4 | +import os |
| 5 | +import sys |
| 6 | +import types |
| 7 | +import unittest |
| 8 | + |
| 9 | +LAMBDA_SRC = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..") |
| 10 | + |
| 11 | + |
| 12 | +class FakeTable: |
| 13 | + def __init__(self): |
| 14 | + self.items = {} |
| 15 | + |
| 16 | + def get_item(self, Key): |
| 17 | + item = self.items.get(Key["finding_key"]) |
| 18 | + return {"Item": item} if item else {} |
| 19 | + |
| 20 | + def put_item(self, Item): |
| 21 | + self.items[Item["finding_key"]] = Item |
| 22 | + |
| 23 | + |
| 24 | +def load(table): |
| 25 | + boto3 = types.ModuleType("boto3") |
| 26 | + boto3.client = lambda *a, **kw: object() |
| 27 | + boto3.resource = lambda *a, **kw: types.SimpleNamespace(Table=lambda name: table) |
| 28 | + botocore = types.ModuleType("botocore") |
| 29 | + botocore_config = types.ModuleType("botocore.config") |
| 30 | + botocore_config.Config = lambda **kw: None |
| 31 | + sys.modules.update({"boto3": boto3, "botocore": botocore, "botocore.config": botocore_config}) |
| 32 | + os.environ.update({ |
| 33 | + "INFRA_WEBHOOK_PARAM": "/x/infra", |
| 34 | + "COST_WEBHOOK_PARAM": "/x/cost", |
| 35 | + "SECURITY_TOPIC_ARN": "arn:aws:sns:eu-central-1:111111111111:javabin-security", |
| 36 | + }) |
| 37 | + if LAMBDA_SRC not in sys.path: |
| 38 | + sys.path.insert(0, LAMBDA_SRC) |
| 39 | + for name in [m for m in sys.modules if m == "shared" or m.startswith("shared.")]: |
| 40 | + del sys.modules[name] |
| 41 | + spec = importlib.util.spec_from_file_location( |
| 42 | + "slack_alert_handler", os.path.join(LAMBDA_SRC, "slack_alert", "handler.py")) |
| 43 | + mod = importlib.util.module_from_spec(spec) |
| 44 | + spec.loader.exec_module(mod) |
| 45 | + return mod |
| 46 | + |
| 47 | + |
| 48 | +def event(service="testteam-platform-test-app", name="SERVICE_TASK_START_IMPAIRED", |
| 49 | + time="2026-10-24T10:00:00Z"): |
| 50 | + return { |
| 51 | + "version": "0", |
| 52 | + "detail-type": "ECS Service Action", |
| 53 | + "source": "aws.ecs", |
| 54 | + "account": "111111111111", |
| 55 | + "time": time, |
| 56 | + "region": "eu-central-1", |
| 57 | + "resources": [f"arn:aws:ecs:eu-central-1:111111111111:service/javabin-platform/{service}"], |
| 58 | + "detail": { |
| 59 | + "eventType": "WARN", |
| 60 | + "eventName": name, |
| 61 | + "clusterArn": "arn:aws:ecs:eu-central-1:111111111111:cluster/javabin-platform", |
| 62 | + }, |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | +class EcsStartImpairedTests(unittest.TestCase): |
| 67 | + def setUp(self): |
| 68 | + self.table = FakeTable() |
| 69 | + self.mod = load(self.table) |
| 70 | + |
| 71 | + def test_formats_service_and_console_link(self): |
| 72 | + msg = self.mod.format_structured_alert("subject", event()) |
| 73 | + self.assertEqual(msg["text"], "ECS service testteam-platform-test-app cannot start tasks") |
| 74 | + rendered = str(msg["blocks"]) |
| 75 | + self.assertIn("`javabin-platform`", rendered) |
| 76 | + self.assertIn( |
| 77 | + "https://eu-central-1.console.aws.amazon.com/ecs/v2/clusters/javabin-platform" |
| 78 | + "/services/testteam-platform-test-app/events?region=eu-central-1", |
| 79 | + rendered, |
| 80 | + ) |
| 81 | + |
| 82 | + def test_alerts_once_per_service_per_day(self): |
| 83 | + self.assertIsNotNone(self.mod.format_structured_alert("s", event())) |
| 84 | + self.assertIsNone(self.mod.format_structured_alert("s", event(time="2026-10-24T18:00:00Z"))) |
| 85 | + self.assertIsNotNone(self.mod.format_structured_alert("s", event(service="other-app"))) |
| 86 | + self.assertIsNotNone(self.mod.format_structured_alert("s", event(time="2026-10-25T08:00:00Z"))) |
| 87 | + |
| 88 | + def test_ignores_other_ecs_service_actions(self): |
| 89 | + self.assertIsNone(self.mod.format_structured_alert("s", event(name="SERVICE_STEADY_STATE"))) |
| 90 | + self.assertEqual(self.table.items, {}) |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == "__main__": |
| 94 | + unittest.main() |
0 commit comments