|
| 1 | +import pytest |
| 2 | + |
| 3 | +from pyctuator.health.health_provider import HealthStatus, Status, HealthDetails, HealthProvider |
| 4 | +from pyctuator.impl.pyctuator_impl import PyctuatorImpl, AppInfo, AppDetails |
| 5 | +from pyctuator.pyctuator import default_logfile_format |
| 6 | + |
| 7 | + |
| 8 | +class MyHealthProvider(HealthProvider): |
| 9 | + def __init__(self, name: str = "kuki") -> None: |
| 10 | + self.name = name |
| 11 | + self.status = Status.UNKNOWN |
| 12 | + |
| 13 | + def down(self) -> None: |
| 14 | + self.status = Status.DOWN |
| 15 | + |
| 16 | + def up(self) -> None: |
| 17 | + self.status = Status.UP |
| 18 | + |
| 19 | + def is_supported(self) -> bool: |
| 20 | + return True |
| 21 | + |
| 22 | + def get_health(self) -> HealthStatus: |
| 23 | + return HealthStatus(self.status, HealthDetails()) |
| 24 | + |
| 25 | + def get_name(self) -> str: |
| 26 | + return self.name |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture |
| 30 | +def pyctuator_impl() -> PyctuatorImpl: |
| 31 | + return PyctuatorImpl( |
| 32 | + AppInfo(app=AppDetails(name="appy")), |
| 33 | + "http://appy/pyctuator", |
| 34 | + 10, |
| 35 | + default_logfile_format |
| 36 | + ) |
| 37 | + |
| 38 | + |
| 39 | +def test_health_status_single_provider(pyctuator_impl: PyctuatorImpl) -> None: |
| 40 | + health_provider = MyHealthProvider() |
| 41 | + pyctuator_impl.register_health_providers(health_provider) |
| 42 | + |
| 43 | + # Test's default status is UNKNOWN |
| 44 | + assert pyctuator_impl.get_health().status == Status.UNKNOWN |
| 45 | + |
| 46 | + health_provider.down() |
| 47 | + assert pyctuator_impl.get_health().status == Status.DOWN |
| 48 | + |
| 49 | + health_provider.up() |
| 50 | + assert pyctuator_impl.get_health().status == Status.UP |
| 51 | + |
| 52 | + |
| 53 | +def test_health_status_multiple_providers(pyctuator_impl: PyctuatorImpl) -> None: |
| 54 | + health_providers = [MyHealthProvider("kuki"), MyHealthProvider("puki"), MyHealthProvider("ruki")] |
| 55 | + for health_provider in health_providers: |
| 56 | + pyctuator_impl.register_health_providers(health_provider) |
| 57 | + |
| 58 | + # Test's default status is UNKNOWN - all 3 are UNKNOWN |
| 59 | + assert pyctuator_impl.get_health().status == Status.UNKNOWN |
| 60 | + |
| 61 | + health_providers[0].down() |
| 62 | + assert pyctuator_impl.get_health().status == Status.DOWN |
| 63 | + |
| 64 | + health_providers[0].up() |
| 65 | + assert pyctuator_impl.get_health().status == Status.UP |
| 66 | + |
| 67 | + # first provider is UP, but the second is DOWN |
| 68 | + health_providers[1].down() |
| 69 | + assert pyctuator_impl.get_health().status == Status.DOWN |
| 70 | + |
| 71 | + # first and second providers are UP, 3rd is UNKNOWN |
| 72 | + health_providers[1].up() |
| 73 | + assert pyctuator_impl.get_health().status == Status.UP |
0 commit comments