|
16 | 16 |
|
17 | 17 | import json |
18 | 18 | from unittest.mock import MagicMock, patch |
19 | | -import logging |
20 | 19 |
|
21 | 20 | from src.handlers.handler_health import HandlerHealth |
22 | 21 |
|
| 22 | +### get_health() |
23 | 23 |
|
24 | | -## get_health() - healthy state |
25 | | -def test_get_health_all_dependencies_healthy(): |
26 | | - """Health check returns 200 when all writer STATEs are properly initialized.""" |
27 | | - logger = logging.getLogger("test") |
28 | | - config = {} |
29 | | - handler = HandlerHealth(logger, config) |
| 24 | +## Minimal healthy state (just kafka) |
| 25 | +def test_get_health_minimal_kafka_healthy(): |
| 26 | + """Health check returns 200 when Kafka is initialized and optional writers are disabled.""" |
| 27 | + handler = HandlerHealth() |
30 | 28 |
|
31 | | - # Mock all writers as healthy |
32 | 29 | with ( |
33 | | - patch("src.handlers.handler_health.writer_kafka.STATE", {"logger": logger, "producer": MagicMock()}), |
34 | | - patch( |
35 | | - "src.handlers.handler_health.writer_eventbridge.STATE", |
36 | | - { |
37 | | - "logger": logger, |
38 | | - "client": MagicMock(), |
39 | | - "event_bus_arn": "arn:aws:events:us-east-1:123456789012:event-bus/my-bus", |
40 | | - }, |
41 | | - ), |
42 | | - patch("src.handlers.handler_health.writer_postgres.logger", logger), |
| 30 | + patch("src.handlers.handler_health.writer_kafka.STATE", {"producer": MagicMock()}), |
| 31 | + patch("src.handlers.handler_health.writer_eventbridge.STATE", {"client": None, "event_bus_arn": ""}), |
| 32 | + patch("src.handlers.handler_health.writer_postgres.POSTGRES", {"database": ""}), |
| 33 | + ): |
| 34 | + response = handler.get_health() |
| 35 | + |
| 36 | + assert response["statusCode"] == 200 |
| 37 | + body = json.loads(response["body"]) |
| 38 | + assert body["status"] == "ok" |
| 39 | + assert "uptime_seconds" in body |
| 40 | + |
| 41 | + |
| 42 | +## Healthy state with all writers enabled |
| 43 | +def test_get_health_all_writers_enabled_and_healthy(): |
| 44 | + """Health check returns 200 when all writers are enabled and properly configured.""" |
| 45 | + handler = HandlerHealth() |
| 46 | + postgres_config = {"database": "db", "host": "localhost", "user": "user", "password": "pass", "port": "5432"} |
| 47 | + |
| 48 | + with ( |
| 49 | + patch("src.handlers.handler_health.writer_kafka.STATE", {"producer": MagicMock()}), |
| 50 | + patch("src.handlers.handler_health.writer_eventbridge.STATE", {"client": MagicMock(), "event_bus_arn": "arn"}), |
| 51 | + patch("src.handlers.handler_health.writer_postgres.POSTGRES", postgres_config), |
43 | 52 | ): |
44 | 53 | response = handler.get_health() |
45 | 54 |
|
46 | 55 | assert response["statusCode"] == 200 |
47 | 56 | body = json.loads(response["body"]) |
48 | 57 | assert body["status"] == "ok" |
49 | 58 | assert "uptime_seconds" in body |
50 | | - assert isinstance(body["uptime_seconds"], int) |
51 | | - assert body["uptime_seconds"] >= 0 |
52 | 59 |
|
53 | 60 |
|
54 | | -## get_health() - degraded state - kafka |
| 61 | +## Degraded state with all writers enabled |
55 | 62 | def test_get_health_kafka_not_initialized(): |
56 | 63 | """Health check returns 503 when Kafka writer is not initialized.""" |
57 | | - logger = logging.getLogger("test") |
58 | | - config = {} |
59 | | - handler = HandlerHealth(logger, config) |
| 64 | + handler = HandlerHealth() |
| 65 | + postgres_config = {"database": "db", "host": "", "user": "", "password": "", "port": ""} |
60 | 66 |
|
61 | | - # Mock Kafka as not initialized (missing producer key) |
62 | 67 | with ( |
63 | | - patch("src.handlers.handler_health.writer_kafka.STATE", {"logger": logger}), |
| 68 | + patch("src.handlers.handler_health.writer_kafka.STATE", {"producer": None}), |
64 | 69 | patch( |
65 | 70 | "src.handlers.handler_health.writer_eventbridge.STATE", |
66 | | - {"logger": logger, "client": MagicMock(), "event_bus_arn": "arn"}, |
| 71 | + {"client": None, "event_bus_arn": "arn:aws:events:us-east-1:123:event-bus/bus"} |
67 | 72 | ), |
68 | | - patch("src.handlers.handler_health.writer_postgres.logger", logger), |
| 73 | + patch("src.handlers.handler_health.writer_postgres.POSTGRES", postgres_config), |
69 | 74 | ): |
70 | 75 | response = handler.get_health() |
71 | 76 |
|
72 | 77 | assert response["statusCode"] == 503 |
73 | 78 | body = json.loads(response["body"]) |
74 | 79 | assert body["status"] == "degraded" |
75 | | - assert "details" in body |
76 | | - assert "kafka" in body["details"] |
77 | | - assert body["details"]["kafka"] == "not_initialized" |
| 80 | + assert "kafka" in body["failures"] |
| 81 | + assert "eventbridge" in body["failures"] |
| 82 | + assert "postgres" in body["failures"] |
78 | 83 |
|
79 | 84 |
|
80 | | -## get_health() - degraded state - eventbridge |
81 | | -def test_get_health_eventbridge_not_initialized(): |
82 | | - """Health check returns 503 when EventBridge writer is not initialized.""" |
83 | | - logger = logging.getLogger("test") |
84 | | - config = {} |
85 | | - handler = HandlerHealth(logger, config) |
| 85 | +## Healthy when eventbridge is disabled |
| 86 | +def test_get_health_eventbridge_disabled(): |
| 87 | + """Health check returns 200 when EventBridge is disabled (empty event_bus_arn).""" |
| 88 | + handler = HandlerHealth() |
| 89 | + postgres_config = {"database": "db", "host": "localhost", "user": "user", "password": "pass", "port": "5432"} |
86 | 90 |
|
87 | | - # Mock EventBridge as not initialized (missing client key) |
88 | 91 | with ( |
89 | | - patch("src.handlers.handler_health.writer_kafka.STATE", {"logger": logger, "producer": MagicMock()}), |
90 | | - patch("src.handlers.handler_health.writer_eventbridge.STATE", {"logger": logger}), |
91 | | - patch("src.handlers.handler_health.writer_postgres.logger", logger), |
| 92 | + patch("src.handlers.handler_health.writer_kafka.STATE", {"producer": MagicMock()}), |
| 93 | + patch("src.handlers.handler_health.writer_eventbridge.STATE", {"client": None, "event_bus_arn": ""}), |
| 94 | + patch("src.handlers.handler_health.writer_postgres.POSTGRES", postgres_config), |
92 | 95 | ): |
93 | 96 | response = handler.get_health() |
94 | 97 |
|
95 | | - assert response["statusCode"] == 503 |
96 | | - body = json.loads(response["body"]) |
97 | | - assert body["status"] == "degraded" |
98 | | - assert "eventbridge" in body["details"] |
99 | | - assert body["details"]["eventbridge"] == "not_initialized" |
| 98 | + assert response["statusCode"] == 200 |
| 99 | + |
| 100 | + |
| 101 | +## Healthy when postgres is disabled |
| 102 | +def test_get_health_postgres_disabled(): |
| 103 | + """Health check returns 200 when PostgreSQL is disabled (empty database).""" |
| 104 | + handler = HandlerHealth() |
| 105 | + |
| 106 | + with ( |
| 107 | + patch("src.handlers.handler_health.writer_kafka.STATE", {"producer": MagicMock()}), |
| 108 | + patch("src.handlers.handler_health.writer_eventbridge.STATE", {"client": MagicMock(), "event_bus_arn": "arn"}), |
| 109 | + patch("src.handlers.handler_health.writer_postgres.POSTGRES", {"database": ""}), |
| 110 | + ): |
| 111 | + response = handler.get_health() |
| 112 | + |
| 113 | + assert response["statusCode"] == 200 |
100 | 114 |
|
101 | 115 |
|
102 | | -## get_health() - degraded state - multiple failures |
103 | | -def test_get_health_multiple_dependencies_not_initialized(): |
104 | | - """Health check returns 503 when multiple writers are not initialized.""" |
105 | | - logger = logging.getLogger("test") |
106 | | - config = {} |
107 | | - handler = HandlerHealth(logger, config) |
| 116 | +## Degraded state - postgres host not configured |
| 117 | +def test_get_health_postgres_host_not_configured(): |
| 118 | + """Health check returns 503 when PostgreSQL host is not configured.""" |
| 119 | + handler = HandlerHealth() |
| 120 | + postgres_config = {"database": "db", "host": "", "user": "user", "password": "pass", "port": "5432"} |
108 | 121 |
|
109 | | - # Mock multiple writers as not initialized |
110 | 122 | with ( |
111 | | - patch("src.handlers.handler_health.writer_kafka.STATE", {}), |
112 | | - patch("src.handlers.handler_health.writer_eventbridge.STATE", {}), |
113 | | - patch("src.handlers.handler_health.writer_postgres", spec=[]), # spec=[] makes logger not exist |
| 123 | + patch("src.handlers.handler_health.writer_kafka.STATE", {"producer": MagicMock()}), |
| 124 | + patch("src.handlers.handler_health.writer_eventbridge.STATE", {"client": MagicMock(), "event_bus_arn": "arn"}), |
| 125 | + patch("src.handlers.handler_health.writer_postgres.POSTGRES", postgres_config), |
114 | 126 | ): |
115 | 127 | response = handler.get_health() |
116 | 128 |
|
117 | 129 | assert response["statusCode"] == 503 |
118 | 130 | body = json.loads(response["body"]) |
119 | | - assert body["status"] == "degraded" |
120 | | - assert len(body["details"]) >= 2 # At least kafka and eventbridge |
121 | | - assert "kafka" in body["details"] |
122 | | - assert "eventbridge" in body["details"] |
| 131 | + assert body["failures"]["postgres"] == "host not configured" |
123 | 132 |
|
124 | 133 |
|
125 | | -## get_health() - uptime calculation |
| 134 | +## Uptime calculation |
126 | 135 | def test_get_health_uptime_is_positive(): |
127 | 136 | """Verify uptime_seconds is calculated and is a positive integer.""" |
128 | | - logger = logging.getLogger("test") |
129 | | - config = {} |
130 | | - handler = HandlerHealth(logger, config) |
| 137 | + handler = HandlerHealth() |
| 138 | + postgres_config = {"database": "db", "host": "localhost", "user": "user", "password": "pass", "port": "5432"} |
131 | 139 |
|
132 | 140 | with ( |
133 | | - patch("src.handlers.handler_health.writer_kafka.STATE", {"logger": logger, "producer": MagicMock()}), |
134 | | - patch( |
135 | | - "src.handlers.handler_health.writer_eventbridge.STATE", |
136 | | - {"logger": logger, "client": MagicMock(), "event_bus_arn": "arn"}, |
137 | | - ), |
138 | | - patch("src.handlers.handler_health.writer_postgres.logger", logger), |
| 141 | + patch("src.handlers.handler_health.writer_kafka.STATE", {"producer": MagicMock()}), |
| 142 | + patch("src.handlers.handler_health.writer_eventbridge.STATE", {"client": MagicMock(), "event_bus_arn": "arn"}), |
| 143 | + patch("src.handlers.handler_health.writer_postgres.POSTGRES", postgres_config), |
139 | 144 | ): |
140 | 145 | response = handler.get_health() |
141 | 146 |
|
|
0 commit comments