Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /microsoft_agent-governance-toolkit /agent-governance-python /agent-os /tests /test_health.py
| # Copyright (c) Microsoft Corporation. | |
| # Licensed under the MIT License. | |
| """Tests for HealthChecker. | |
| Run with: python -m pytest tests/test_health.py -v --tb=short | |
| """ | |
| import json | |
| import threading | |
| import time | |
| import pytest | |
| from agent_os.integrations.health import ( | |
| ComponentHealth, | |
| HealthChecker, | |
| HealthReport, | |
| HealthStatus, | |
| ) | |
| # ============================================================================= | |
| # Fixtures | |
| # ============================================================================= | |
| def checker(): | |
| # A blank-slate checker (no built-ins) for exercising registration and | |
| # aggregation logic in isolation. Default HealthChecker() auto-registers | |
| # built-ins; that path is covered by TestBuiltInAutoRegistration below. | |
| return HealthChecker(version="1.3.1", register_builtins=False) | |
| class _FakeAuditBackend: | |
| """Minimal audit backend exposing the write/flush interface for probes.""" | |
| def write(self, entry): | |
| pass | |
| def flush(self): | |
| pass | |
| class _FakeRuntime: | |
| def create_session(self): | |
| return object() | |
| def checker_with_checks(): | |
| checker = HealthChecker( | |
| version="1.3.1", | |
| register_builtins=False, | |
| audit_backend=_FakeAuditBackend(), | |
| ) | |
| checker._runtime = _FakeRuntime() | |
| checker.register_check("runtime", checker._check_runtime) | |
| checker.register_check("audit_backend", checker._check_audit_backend) | |
| return checker | |
| # ============================================================================= | |
| # HealthStatus enum | |
| # ============================================================================= | |
| class TestHealthStatus: | |
| def test_values(self): | |
| assert HealthStatus.HEALTHY.value == "healthy" | |
| assert HealthStatus.DEGRADED.value == "degraded" | |
| assert HealthStatus.UNHEALTHY.value == "unhealthy" | |
| # ============================================================================= | |
| # ComponentHealth | |
| # ============================================================================= | |
| class TestComponentHealth: | |
| def test_frozen(self): | |
| comp = ComponentHealth(name="db", status=HealthStatus.HEALTHY) | |
| with pytest.raises(AttributeError): | |
| comp.name = "other" | |
| def test_defaults(self): | |
| comp = ComponentHealth(name="db", status=HealthStatus.HEALTHY) | |
| assert comp.message == "" | |
| assert comp.latency_ms == 0.0 | |
| # ============================================================================= | |
| # HealthReport | |
| # ============================================================================= | |
| class TestHealthReport: | |
| def _make_report(self, status=HealthStatus.HEALTHY): | |
| return HealthReport( | |
| status=status, | |
| components={ | |
| "a": ComponentHealth( | |
| name="a", status=HealthStatus.HEALTHY, message="ok", latency_ms=1.5 | |
| ) | |
| }, | |
| timestamp="2025-01-01T00:00:00Z", | |
| version="1.0.0", | |
| uptime_seconds=42.0, | |
| ) | |
| def test_to_dict_is_json_serializable(self): | |
| report = self._make_report() | |
| d = report.to_dict() | |
| serialized = json.dumps(d) | |
| assert isinstance(serialized, str) | |
| def test_to_dict_fields(self): | |
| report = self._make_report() | |
| d = report.to_dict() | |
| assert d["status"] == "healthy" | |
| assert d["version"] == "1.0.0" | |
| assert d["uptime_seconds"] == 42.0 | |
| assert "a" in d["components"] | |
| assert d["components"]["a"]["latency_ms"] == 1.5 | |
| def test_is_healthy(self): | |
| assert self._make_report(HealthStatus.HEALTHY).is_healthy() is True | |
| assert self._make_report(HealthStatus.DEGRADED).is_healthy() is False | |
| assert self._make_report(HealthStatus.UNHEALTHY).is_healthy() is False | |
| def test_is_ready(self): | |
| assert self._make_report(HealthStatus.HEALTHY).is_ready() is True | |
| assert self._make_report(HealthStatus.DEGRADED).is_ready() is True | |
| assert self._make_report(HealthStatus.UNHEALTHY).is_ready() is False | |
| # ============================================================================= | |
| # HealthChecker — registration | |
| # ============================================================================= | |
| class TestRegisterCheck: | |
| def test_register_and_run(self, checker): | |
| checker.register_check( | |
| "dummy", | |
| lambda: ComponentHealth(name="dummy", status=HealthStatus.HEALTHY), | |
| ) | |
| report = checker.check_health() | |
| assert "dummy" in report.components | |
| assert report.is_healthy() | |
| def test_overwrite_check(self, checker): | |
| checker.register_check( | |
| "x", | |
| lambda: ComponentHealth(name="x", status=HealthStatus.HEALTHY), | |
| ) | |
| checker.register_check( | |
| "x", | |
| lambda: ComponentHealth(name="x", status=HealthStatus.DEGRADED), | |
| ) | |
| report = checker.check_health() | |
| assert report.components["x"].status == HealthStatus.DEGRADED | |
| # ============================================================================= | |
| # HealthChecker — probes | |
| # ============================================================================= | |
| class TestCheckHealth: | |
| def test_empty_report_is_unhealthy(self): | |
| # Fail closed: a checker that verifies nothing must not report HEALTHY. | |
| checker = HealthChecker(register_builtins=False) | |
| report = checker.check_health() | |
| assert report.components == {} | |
| assert report.status == HealthStatus.UNHEALTHY | |
| assert not report.is_healthy() | |
| assert not report.is_ready() | |
| def test_all_healthy(self, checker_with_checks): | |
| report = checker_with_checks.check_health() | |
| assert report.is_healthy() | |
| assert len(report.components) == 2 | |
| def test_degraded_aggregation(self, checker): | |
| checker.register_check( | |
| "ok", | |
| lambda: ComponentHealth(name="ok", status=HealthStatus.HEALTHY), | |
| ) | |
| checker.register_check( | |
| "slow", | |
| lambda: ComponentHealth( | |
| name="slow", status=HealthStatus.DEGRADED, message="high latency" | |
| ), | |
| ) | |
| report = checker.check_health() | |
| assert report.status == HealthStatus.DEGRADED | |
| assert report.is_ready() | |
| def test_unhealthy_aggregation(self, checker): | |
| checker.register_check( | |
| "ok", | |
| lambda: ComponentHealth(name="ok", status=HealthStatus.HEALTHY), | |
| ) | |
| checker.register_check( | |
| "broken", | |
| lambda: ComponentHealth( | |
| name="broken", status=HealthStatus.UNHEALTHY, message="down" | |
| ), | |
| ) | |
| report = checker.check_health() | |
| assert report.status == HealthStatus.UNHEALTHY | |
| assert not report.is_ready() | |
| def test_exception_becomes_unhealthy(self, checker): | |
| def boom(): | |
| raise RuntimeError("kaboom") | |
| checker.register_check("bad", boom) | |
| report = checker.check_health() | |
| assert report.status == HealthStatus.UNHEALTHY | |
| assert "kaboom" in report.components["bad"].message | |
| def test_report_contains_version(self, checker): | |
| report = checker.check_health() | |
| assert report.version == "1.3.1" | |
| def test_uptime_positive(self, checker): | |
| report = checker.check_health() | |
| assert report.uptime_seconds >= 0.0 | |
| def test_timestamp_format(self, checker): | |
| report = checker.check_health() | |
| assert report.timestamp.endswith("Z") | |
| class TestCheckReady: | |
| def test_ready_delegates_to_check_health(self, checker_with_checks): | |
| report = checker_with_checks.check_ready() | |
| assert report.is_ready() | |
| assert len(report.components) == 2 | |
| class TestCheckLive: | |
| def test_liveness_always_healthy(self, checker): | |
| report = checker.check_live() | |
| assert report.is_healthy() | |
| assert "process" in report.components | |
| # ============================================================================= | |
| # Built-in checks | |
| # ============================================================================= | |
| class TestBuiltInChecks: | |
| def test_runtime_check(self): | |
| checker = HealthChecker(register_builtins=False, runtime=_FakeRuntime()) | |
| result = checker._check_runtime() | |
| assert result.status == HealthStatus.HEALTHY | |
| assert result.name == "runtime" | |
| def test_audit_backend_check_degraded_without_backend(self, checker): | |
| # No backend configured -> DEGRADED, never a bare HEALTHY. | |
| result = checker._check_audit_backend() | |
| assert result.status == HealthStatus.DEGRADED | |
| def test_audit_backend_check_healthy_with_backend(self): | |
| checker = HealthChecker( | |
| register_builtins=False, audit_backend=_FakeAuditBackend() | |
| ) | |
| result = checker._check_audit_backend() | |
| assert result.status == HealthStatus.HEALTHY | |
| class TestBuiltInAutoRegistration: | |
| def test_fresh_checker_auto_registers_builtins(self): | |
| # Flips the defect: fresh checker verifies real components, not empty. | |
| report = HealthChecker().check_health() | |
| assert set(report.components) == {"runtime", "audit_backend"} | |
| # No runtime or audit backend injected -> DEGRADED, never HEALTHY. | |
| assert report.status == HealthStatus.DEGRADED | |
| assert not report.is_healthy() | |
| def test_fresh_checker_healthy_with_audit_backend(self): | |
| report = HealthChecker( | |
| audit_backend=_FakeAuditBackend(), | |
| runtime=_FakeRuntime(), | |
| ).check_health() | |
| assert report.is_healthy() | |
| assert len(report.components) == 2 | |
| # ============================================================================= | |
| # Thread safety | |
| # ============================================================================= | |
| class TestThreadSafety: | |
| def test_concurrent_registration_and_check(self, checker): | |
| errors = [] | |
| def register_many(start): | |
| try: | |
| for i in range(50): | |
| name = f"comp-{start}-{i}" | |
| checker.register_check( | |
| name, | |
| lambda n=name: ComponentHealth( | |
| name=n, status=HealthStatus.HEALTHY | |
| ), | |
| ) | |
| except Exception as exc: | |
| errors.append(exc) | |
| def check_many(): | |
| try: | |
| for _ in range(20): | |
| checker.check_health() | |
| except Exception as exc: | |
| errors.append(exc) | |
| threads = [ | |
| threading.Thread(target=register_many, args=(0,)), | |
| threading.Thread(target=register_many, args=(1,)), | |
| threading.Thread(target=check_many), | |
| ] | |
| for t in threads: | |
| t.start() | |
| for t in threads: | |
| t.join() | |
| assert errors == [] | |
| # ============================================================================= | |
| # Latency measurement | |
| # ============================================================================= | |
| class TestLatency: | |
| def test_latency_measured_for_check(self, checker): | |
| def slow_check(): | |
| time.sleep(0.05) | |
| return ComponentHealth(name="slow", status=HealthStatus.HEALTHY) | |
| checker.register_check("slow", slow_check) | |
| report = checker.check_health() | |
| assert report.components["slow"].latency_ms >= 40.0 | |
| def test_explicit_latency_preserved(self, checker): | |
| checker.register_check( | |
| "fast", | |
| lambda: ComponentHealth( | |
| name="fast", status=HealthStatus.HEALTHY, latency_ms=99.0 | |
| ), | |
| ) | |
| report = checker.check_health() | |
| assert report.components["fast"].latency_ms == 99.0 | |
Xet Storage Details
- Size:
- 11.9 kB
- Xet hash:
- 71092d294ff2825ada03454d82944295e5ffc4a7095635bf5807065ba99bdbc8
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.