|
| 1 | +from dataclasses import dataclass |
| 2 | + |
| 3 | +from pyctuator.health.composite_health_provider import CompositeHealthProvider, CompositeHealthStatus |
| 4 | +from pyctuator.health.health_provider import HealthProvider, HealthStatus, Status, HealthDetails |
| 5 | + |
| 6 | + |
| 7 | +@dataclass |
| 8 | +class CustomHealthDetails(HealthDetails): |
| 9 | + details: str |
| 10 | + |
| 11 | + |
| 12 | +class CustomHealthProvider(HealthProvider): |
| 13 | + |
| 14 | + def __init__(self, name: str, status: HealthStatus) -> None: |
| 15 | + super().__init__() |
| 16 | + self.name = name |
| 17 | + self.status = status |
| 18 | + |
| 19 | + def is_supported(self) -> bool: |
| 20 | + return True |
| 21 | + |
| 22 | + def get_name(self) -> str: |
| 23 | + return self.name |
| 24 | + |
| 25 | + def get_health(self) -> HealthStatus: |
| 26 | + return self.status |
| 27 | + |
| 28 | + |
| 29 | +def test_composite_health_provider_no_providers() -> None: |
| 30 | + health_provider = CompositeHealthProvider( |
| 31 | + "comp1", |
| 32 | + ) |
| 33 | + |
| 34 | + assert health_provider.get_name() == "comp1" |
| 35 | + |
| 36 | + assert health_provider.get_health() == CompositeHealthStatus( |
| 37 | + status=Status.UP, |
| 38 | + details={} |
| 39 | + ) |
| 40 | + |
| 41 | + |
| 42 | +def test_composite_health_provider_all_up() -> None: |
| 43 | + health_provider = CompositeHealthProvider( |
| 44 | + "comp2", |
| 45 | + CustomHealthProvider("hp1", HealthStatus(Status.UP, CustomHealthDetails("d1"))), |
| 46 | + CustomHealthProvider("hp2", HealthStatus(Status.UP, CustomHealthDetails("d2"))), |
| 47 | + ) |
| 48 | + |
| 49 | + assert health_provider.get_name() == "comp2" |
| 50 | + |
| 51 | + assert health_provider.get_health() == CompositeHealthStatus( |
| 52 | + status=Status.UP, |
| 53 | + details={ |
| 54 | + "hp1": HealthStatus(Status.UP, CustomHealthDetails("d1")), |
| 55 | + "hp2": HealthStatus(Status.UP, CustomHealthDetails("d2")), |
| 56 | + } |
| 57 | + ) |
| 58 | + |
| 59 | + |
| 60 | +def test_composite_health_provider_one_down() -> None: |
| 61 | + health_provider = CompositeHealthProvider( |
| 62 | + "comp3", |
| 63 | + CustomHealthProvider("hp1", HealthStatus(Status.UP, CustomHealthDetails("d1"))), |
| 64 | + CustomHealthProvider("hp2", HealthStatus(Status.DOWN, CustomHealthDetails("d2"))), |
| 65 | + ) |
| 66 | + |
| 67 | + assert health_provider.get_name() == "comp3" |
| 68 | + |
| 69 | + assert health_provider.get_health() == CompositeHealthStatus( |
| 70 | + status=Status.DOWN, |
| 71 | + details={ |
| 72 | + "hp1": HealthStatus(Status.UP, CustomHealthDetails("d1")), |
| 73 | + "hp2": HealthStatus(Status.DOWN, CustomHealthDetails("d2")), |
| 74 | + } |
| 75 | + ) |
0 commit comments