Skip to content

armgit / ODS

Public Imported from GitHub
0

ODS V3 Pre-Release: Public testing and refinement ahead of the official V3 launch. Turn your PC, Mac, or Linux box into a private AI server.

3.5 KB
Raw Blame
import asyncio

import pytest

from models import ServiceStatus
from routers import extensions


@pytest.mark.parametrize("container,expected", [
    ({"state": "running", "health": "healthy"}, "healthy"),
    ({"state": "running", "health": "unhealthy"}, "unhealthy"),
    ({"state": "running", "health": "none"}, "degraded"),
    ({"state": "running", "health": "starting"}, "degraded"),
    ({"state": "exited", "health": "healthy"}, "down"),
    (None, "unknown"),
])
def test_non_http_readiness_requires_running_container_and_healthcheck(monkeypatch, container, expected):
    rows = [{"service_id": "broker", **container}] if container else []
    monkeypatch.setattr(extensions, "request_agent_json", lambda *a, **k: {
        "schema_version": "ods.host-service-health.v1", "containers": rows,
    })
    statuses = {"broker": ServiceStatus(id="broker", name="Broker", port=1883, external_port=1883, status="healthy")}
    asyncio.run(extensions._inspect_non_http_user_services({"broker": {"port": 1883, "health": ""}}, statuses))
    assert statuses["broker"].status == expected
    assert statuses["broker"].startup_pending is (container == {"state": "running", "health": "starting"})


@pytest.mark.parametrize("container", [
    {"state": "restarting", "health": "starting", "restart_count": 5},
    {"state": "running", "health": "starting", "restart_count": 3},
])
def test_non_http_crash_loop_is_unhealthy_not_starting(monkeypatch, container):
    monkeypatch.setattr(extensions, "request_agent_json", lambda *a, **k: {
        "schema_version": "ods.host-service-health.v1", "containers": [{"service_id": "broker", **container}],
    })
    statuses: dict[str, ServiceStatus] = {}
    asyncio.run(extensions._inspect_non_http_user_services({"broker": {"port": 1883, "health": ""}}, statuses))
    assert (statuses["broker"].status, statuses["broker"].startup_pending,
            statuses["broker"].crash_looping) == ("unhealthy", False, True)


@pytest.mark.parametrize("snapshot", [
    {"schema_version": "wrong", "containers": []},
    {"schema_version": "ods.host-service-health.v1", "containers": "bad"},
    {"schema_version": "ods.host-service-health.v1", "containers": [
        {"service_id": "broker", "state": "running", "health": "healthy"},
        {"service_id": "broker", "state": "exited", "health": "none"},
    ]},
])
def test_bad_or_ambiguous_snapshot_cannot_confirm_readiness(monkeypatch, snapshot):
    monkeypatch.setattr(extensions, "request_agent_json", lambda *a, **k: snapshot)
    statuses = {}
    asyncio.run(extensions._inspect_non_http_user_services({"broker": {"port": 1883}}, statuses))
    assert statuses["broker"].status == "unknown"
    assert statuses["broker"].startup_pending is False


def test_host_unavailable_does_not_fabricate_healthy(monkeypatch):
    def unavailable(*args, **kwargs):
        raise extensions.AgentUnavailable("offline")
    monkeypatch.setattr(extensions, "request_agent_json", unavailable)
    statuses = {}
    asyncio.run(extensions._inspect_non_http_user_services({"broker": {"port": 1883}}, statuses))
    assert statuses["broker"].status == "unknown"


def test_http_and_oneshot_extensions_do_not_trigger_host_probe(monkeypatch):
    def unexpected(*args, **kwargs):
        pytest.fail("unnecessary host probe")
    monkeypatch.setattr(extensions, "request_agent_json", unexpected)
    statuses = {}
    asyncio.run(extensions._inspect_non_http_user_services({
        "http": {"port": 8080, "health": "/health"},
        "cli": {"port": 0, "health": ""},
    }, statuses))
    assert statuses == {}