46 lines
2.0 KiB
Python
46 lines
2.0 KiB
Python
import asyncio
|
|
from unittest.mock import Mock
|
|
|
|
from k1link.web import server_health as health
|
|
|
|
|
|
def test_probe_is_read_only_and_rejects_wrong_identity(monkeypatch):
|
|
request = Mock(return_value={"schema": "missioncore.vps-host/v1", "host_id": "other"})
|
|
monkeypatch.setattr(health, "request", request)
|
|
row = {"id": "ours"}
|
|
assert health.probe("arnavi", row, Mock())["state"] == "unavailable"
|
|
request.assert_called_once_with(row, "GET", "/v1/status")
|
|
request.return_value["host_id"] = "ours"
|
|
assert health.probe("arnavi", row, Mock())["state"] == "online"
|
|
request.side_effect = TimeoutError()
|
|
assert health.probe("arnavi", row, Mock())["state"] == "unavailable"
|
|
|
|
|
|
def test_wireguard_requires_existing_identity_validator(monkeypatch):
|
|
monkeypatch.setattr(health, "request", Mock(return_value={"schema": "wrong"}))
|
|
verify = Mock(side_effect=ValueError("mismatch"))
|
|
assert health.probe("wireguard", {}, verify)["state"] == "unavailable"
|
|
verify.assert_called_once()
|
|
|
|
|
|
def test_concurrent_checks_share_bounded_cache_and_binding_change_invalidates(monkeypatch):
|
|
probe = Mock(return_value={"state": "online", "checked_at": 100})
|
|
monkeypatch.setattr(health, "probe", probe)
|
|
|
|
async def scenario():
|
|
service = health.ServerHealth()
|
|
row = {"id": "one", "endpoint": "https://example:19443", "token": "old"}
|
|
replies = await asyncio.gather(*(service.observe("arnavi", row, Mock()) for _ in range(10)))
|
|
assert all(r["state"] == "online" for r in replies)
|
|
assert probe.call_count == 1
|
|
await service.observe("arnavi", row, Mock())
|
|
assert probe.call_count == 1
|
|
await service.observe("arnavi", {**row, "token": "changed"}, Mock())
|
|
assert probe.call_count == 2
|
|
service.cache = {k: (0, v) for k, (_, v) in service.cache.items()}
|
|
probe.return_value = {"state": "unavailable", "checked_at": 101}
|
|
assert (await service.observe("arnavi", row, Mock()))["state"] == "unavailable"
|
|
assert not service.pending
|
|
|
|
asyncio.run(scenario())
|