|
| 1 | +# |
| 2 | +# Copyright 2025 ABSA Group Limited |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | + |
| 17 | +import json |
| 18 | +from unittest.mock import MagicMock, patch |
| 19 | +import logging |
| 20 | + |
| 21 | +from src.handlers.handler_health import HandlerHealth |
| 22 | + |
| 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) |
| 30 | + |
| 31 | + # Mock all writers as healthy |
| 32 | + 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), |
| 43 | + ): |
| 44 | + response = handler.get_health() |
| 45 | + |
| 46 | + assert response["statusCode"] == 200 |
| 47 | + body = json.loads(response["body"]) |
| 48 | + assert body["status"] == "ok" |
| 49 | + assert "uptime_seconds" in body |
| 50 | + assert isinstance(body["uptime_seconds"], int) |
| 51 | + assert body["uptime_seconds"] >= 0 |
| 52 | + |
| 53 | + |
| 54 | +## get_health() - degraded state - kafka |
| 55 | +def test_get_health_kafka_not_initialized(): |
| 56 | + """Health check returns 503 when Kafka writer is not initialized.""" |
| 57 | + logger = logging.getLogger("test") |
| 58 | + config = {} |
| 59 | + handler = HandlerHealth(logger, config) |
| 60 | + |
| 61 | + # Mock Kafka as not initialized (missing producer key) |
| 62 | + with ( |
| 63 | + patch("src.handlers.handler_health.writer_kafka.STATE", {"logger": logger}), |
| 64 | + patch( |
| 65 | + "src.handlers.handler_health.writer_eventbridge.STATE", |
| 66 | + {"logger": logger, "client": MagicMock(), "event_bus_arn": "arn"}, |
| 67 | + ), |
| 68 | + patch("src.handlers.handler_health.writer_postgres.logger", logger), |
| 69 | + ): |
| 70 | + response = handler.get_health() |
| 71 | + |
| 72 | + assert response["statusCode"] == 503 |
| 73 | + body = json.loads(response["body"]) |
| 74 | + assert body["status"] == "degraded" |
| 75 | + assert "details" in body |
| 76 | + assert "kafka" in body["details"] |
| 77 | + assert body["details"]["kafka"] == "not_initialized" |
| 78 | + |
| 79 | + |
| 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) |
| 86 | + |
| 87 | + # Mock EventBridge as not initialized (missing client key) |
| 88 | + 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 | + ): |
| 93 | + response = handler.get_health() |
| 94 | + |
| 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" |
| 100 | + |
| 101 | + |
| 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) |
| 108 | + |
| 109 | + # Mock multiple writers as not initialized |
| 110 | + 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 |
| 114 | + ): |
| 115 | + response = handler.get_health() |
| 116 | + |
| 117 | + assert response["statusCode"] == 503 |
| 118 | + 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"] |
| 123 | + |
| 124 | + |
| 125 | +## get_health() - uptime calculation |
| 126 | +def test_get_health_uptime_is_positive(): |
| 127 | + """Verify uptime_seconds is calculated and is a positive integer.""" |
| 128 | + logger = logging.getLogger("test") |
| 129 | + config = {} |
| 130 | + handler = HandlerHealth(logger, config) |
| 131 | + |
| 132 | + 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), |
| 139 | + ): |
| 140 | + response = handler.get_health() |
| 141 | + |
| 142 | + body = json.loads(response["body"]) |
| 143 | + assert "uptime_seconds" in body |
| 144 | + assert isinstance(body["uptime_seconds"], int) |
| 145 | + assert body["uptime_seconds"] >= 0 |
| 146 | + |
| 147 | + |
| 148 | +## Integration test with event_gate_module |
| 149 | +def test_health_endpoint_integration(event_gate_module, make_event): |
| 150 | + """Test /health endpoint through lambda_handler.""" |
| 151 | + event = make_event("/health") |
| 152 | + resp = event_gate_module.lambda_handler(event) |
| 153 | + |
| 154 | + # Should return 200 since writers are mocked as initialized in conftest |
| 155 | + assert resp["statusCode"] == 200 |
| 156 | + body = json.loads(resp["body"]) |
| 157 | + assert body["status"] == "ok" |
| 158 | + assert "uptime_seconds" in body |
0 commit comments