|
23 | 23 | from lite_bootstrap import FastAPIBootstrapper, FastAPIConfig, import_checker |
24 | 24 | from lite_bootstrap.bootstrappers import fastapi_bootstrapper |
25 | 25 | from lite_bootstrap.exceptions import ConfigurationError, InstrumentDependencyMissingWarning |
| 26 | +from lite_bootstrap.instruments.healthchecks_instrument import HealthProbe |
26 | 27 | from lite_bootstrap.types import UNSET |
27 | | -from tests.conftest import CustomInstrumentor, SentryTestTransport, emulate_package_missing, warning_source_files |
| 28 | +from tests.conftest import ( |
| 29 | + HEALTH_PROBE_CASES, |
| 30 | + CustomInstrumentor, |
| 31 | + SentryTestTransport, |
| 32 | + capture_probe_warnings, |
| 33 | + emulate_package_missing, |
| 34 | + failed_probe_names, |
| 35 | + warning_source_files, |
| 36 | +) |
28 | 37 |
|
29 | 38 |
|
30 | 39 | logger = structlog.getLogger(__name__) |
@@ -501,3 +510,26 @@ def test_fastapi_otel_honors_excluded_urls_from_environment( |
501 | 510 | span_names = _server_span_names_for(fastapi_config, ["/from-env", "/custom-healthy"]) |
502 | 511 |
|
503 | 512 | assert span_names == ["GET /custom-healthy"] |
| 513 | + |
| 514 | + |
| 515 | +@pytest.mark.parametrize(("probes", "expected_status", "expected_health_status", "expected_failed"), HEALTH_PROBE_CASES) |
| 516 | +def test_fastapi_health_check_runs_probes( |
| 517 | + fastapi_config: FastAPIConfig, |
| 518 | + probes: dict[str, HealthProbe], |
| 519 | + expected_status: int, |
| 520 | + expected_health_status: bool, |
| 521 | + expected_failed: list[str], |
| 522 | +) -> None: |
| 523 | + config = dataclasses.replace(fastapi_config, health_checks_probes=probes) |
| 524 | + bootstrapper = FastAPIBootstrapper(bootstrap_config=config) |
| 525 | + application = bootstrapper.bootstrap() |
| 526 | + with capture_probe_warnings() as probe_warnings, TestClient(application) as test_client: |
| 527 | + response = test_client.get(config.health_checks_path) |
| 528 | + |
| 529 | + assert response.status_code == expected_status |
| 530 | + assert response.json() == { |
| 531 | + "health_status": expected_health_status, |
| 532 | + "service_name": "microservice", |
| 533 | + "service_version": "2.0.0", |
| 534 | + } |
| 535 | + assert failed_probe_names(probe_warnings) == expected_failed |
0 commit comments