|
| 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 | + |
| 20 | +from src.handlers.handler_health import HandlerHealth |
| 21 | + |
| 22 | +### get_health() |
| 23 | + |
| 24 | + |
| 25 | +## Minimal healthy state (just kafka) |
| 26 | +def test_get_health_minimal_kafka_healthy(): |
| 27 | + """Health check returns 200 when Kafka is initialized and optional writers are disabled.""" |
| 28 | + handler = HandlerHealth() |
| 29 | + |
| 30 | + with ( |
| 31 | + patch("src.handlers.handler_health.writer_kafka.STATE", {"producer": MagicMock()}), |
| 32 | + patch("src.handlers.handler_health.writer_eventbridge.STATE", {"client": None, "event_bus_arn": ""}), |
| 33 | + patch("src.handlers.handler_health.writer_postgres.POSTGRES", {"database": ""}), |
| 34 | + ): |
| 35 | + response = handler.get_health() |
| 36 | + |
| 37 | + assert response["statusCode"] == 200 |
| 38 | + body = json.loads(response["body"]) |
| 39 | + assert body["status"] == "ok" |
| 40 | + assert "uptime_seconds" in body |
| 41 | + |
| 42 | + |
| 43 | +## Healthy state with all writers enabled |
| 44 | +def test_get_health_all_writers_enabled_and_healthy(): |
| 45 | + """Health check returns 200 when all writers are enabled and properly configured.""" |
| 46 | + handler = HandlerHealth() |
| 47 | + postgres_config = {"database": "db", "host": "localhost", "user": "user", "password": "pass", "port": "5432"} |
| 48 | + |
| 49 | + with ( |
| 50 | + patch("src.handlers.handler_health.writer_kafka.STATE", {"producer": MagicMock()}), |
| 51 | + patch("src.handlers.handler_health.writer_eventbridge.STATE", {"client": MagicMock(), "event_bus_arn": "arn"}), |
| 52 | + patch("src.handlers.handler_health.writer_postgres.POSTGRES", postgres_config), |
| 53 | + ): |
| 54 | + response = handler.get_health() |
| 55 | + |
| 56 | + assert response["statusCode"] == 200 |
| 57 | + body = json.loads(response["body"]) |
| 58 | + assert body["status"] == "ok" |
| 59 | + assert "uptime_seconds" in body |
| 60 | + |
| 61 | + |
| 62 | +## Degraded state with all writers enabled |
| 63 | +def test_get_health_kafka_not_initialized(): |
| 64 | + """Health check returns 503 when Kafka writer is not initialized.""" |
| 65 | + handler = HandlerHealth() |
| 66 | + postgres_config = {"database": "db", "host": "", "user": "", "password": "", "port": ""} |
| 67 | + |
| 68 | + with ( |
| 69 | + patch("src.handlers.handler_health.writer_kafka.STATE", {"producer": None}), |
| 70 | + patch( |
| 71 | + "src.handlers.handler_health.writer_eventbridge.STATE", |
| 72 | + {"client": None, "event_bus_arn": "arn:aws:events:us-east-1:123:event-bus/bus"}, |
| 73 | + ), |
| 74 | + patch("src.handlers.handler_health.writer_postgres.POSTGRES", postgres_config), |
| 75 | + ): |
| 76 | + response = handler.get_health() |
| 77 | + |
| 78 | + assert response["statusCode"] == 503 |
| 79 | + body = json.loads(response["body"]) |
| 80 | + assert body["status"] == "degraded" |
| 81 | + assert "kafka" in body["failures"] |
| 82 | + assert "eventbridge" in body["failures"] |
| 83 | + assert "postgres" in body["failures"] |
| 84 | + |
| 85 | + |
| 86 | +## Healthy when eventbridge is disabled |
| 87 | +def test_get_health_eventbridge_disabled(): |
| 88 | + """Health check returns 200 when EventBridge is disabled (empty event_bus_arn).""" |
| 89 | + handler = HandlerHealth() |
| 90 | + postgres_config = {"database": "db", "host": "localhost", "user": "user", "password": "pass", "port": "5432"} |
| 91 | + |
| 92 | + with ( |
| 93 | + patch("src.handlers.handler_health.writer_kafka.STATE", {"producer": MagicMock()}), |
| 94 | + patch("src.handlers.handler_health.writer_eventbridge.STATE", {"client": None, "event_bus_arn": ""}), |
| 95 | + patch("src.handlers.handler_health.writer_postgres.POSTGRES", postgres_config), |
| 96 | + ): |
| 97 | + response = handler.get_health() |
| 98 | + |
| 99 | + assert response["statusCode"] == 200 |
| 100 | + |
| 101 | + |
| 102 | +## Healthy when postgres is disabled |
| 103 | +def test_get_health_postgres_disabled(): |
| 104 | + """Health check returns 200 when PostgreSQL is disabled (empty database).""" |
| 105 | + handler = HandlerHealth() |
| 106 | + |
| 107 | + with ( |
| 108 | + patch("src.handlers.handler_health.writer_kafka.STATE", {"producer": MagicMock()}), |
| 109 | + patch("src.handlers.handler_health.writer_eventbridge.STATE", {"client": MagicMock(), "event_bus_arn": "arn"}), |
| 110 | + patch("src.handlers.handler_health.writer_postgres.POSTGRES", {"database": ""}), |
| 111 | + ): |
| 112 | + response = handler.get_health() |
| 113 | + |
| 114 | + assert response["statusCode"] == 200 |
| 115 | + |
| 116 | + |
| 117 | +## Degraded state - postgres host not configured |
| 118 | +def test_get_health_postgres_host_not_configured(): |
| 119 | + """Health check returns 503 when PostgreSQL host is not configured.""" |
| 120 | + handler = HandlerHealth() |
| 121 | + postgres_config = {"database": "db", "host": "", "user": "user", "password": "pass", "port": "5432"} |
| 122 | + |
| 123 | + with ( |
| 124 | + patch("src.handlers.handler_health.writer_kafka.STATE", {"producer": MagicMock()}), |
| 125 | + patch("src.handlers.handler_health.writer_eventbridge.STATE", {"client": MagicMock(), "event_bus_arn": "arn"}), |
| 126 | + patch("src.handlers.handler_health.writer_postgres.POSTGRES", postgres_config), |
| 127 | + ): |
| 128 | + response = handler.get_health() |
| 129 | + |
| 130 | + assert response["statusCode"] == 503 |
| 131 | + body = json.loads(response["body"]) |
| 132 | + assert body["failures"]["postgres"] == "host not configured" |
| 133 | + |
| 134 | + |
| 135 | +## Uptime calculation |
| 136 | +def test_get_health_uptime_is_positive(): |
| 137 | + """Verify uptime_seconds is calculated and is a positive integer.""" |
| 138 | + handler = HandlerHealth() |
| 139 | + postgres_config = {"database": "db", "host": "localhost", "user": "user", "password": "pass", "port": "5432"} |
| 140 | + |
| 141 | + with ( |
| 142 | + patch("src.handlers.handler_health.writer_kafka.STATE", {"producer": MagicMock()}), |
| 143 | + patch("src.handlers.handler_health.writer_eventbridge.STATE", {"client": MagicMock(), "event_bus_arn": "arn"}), |
| 144 | + patch("src.handlers.handler_health.writer_postgres.POSTGRES", postgres_config), |
| 145 | + ): |
| 146 | + response = handler.get_health() |
| 147 | + |
| 148 | + body = json.loads(response["body"]) |
| 149 | + assert "uptime_seconds" in body |
| 150 | + assert isinstance(body["uptime_seconds"], int) |
| 151 | + assert body["uptime_seconds"] >= 0 |
| 152 | + |
| 153 | + |
| 154 | +## Integration test with event_gate_module |
| 155 | +def test_health_endpoint_integration(event_gate_module, make_event): |
| 156 | + """Test /health endpoint through lambda_handler.""" |
| 157 | + event = make_event("/health") |
| 158 | + resp = event_gate_module.lambda_handler(event) |
| 159 | + |
| 160 | + # Should return 200 since writers are mocked as initialized in conftest |
| 161 | + assert resp["statusCode"] == 200 |
| 162 | + body = json.loads(resp["body"]) |
| 163 | + assert body["status"] == "ok" |
| 164 | + assert "uptime_seconds" in body |
0 commit comments