Files
DECNET/tests/api/fleet/test_lifecycle_endpoint.py
anti eacac9aa60 feat(api): GET /deckies/lifecycle + master startup sweep
GET /deckies/lifecycle?ids=<uuid>&ids=<uuid> returns the matching
DeckyLifecycle rows so the wizard can poll instead of holding an HTTP
request open across compose work. require_viewer gating -- read-only.

Startup sweep: on master boot, any pending/running row with
started_at older than 1h flips to failed with
error='master restarted during operation'. Pre-v1 substitute for a
durable task queue: if the master crashes mid-deploy, the wizard sees
FAILED on refresh and the operator retries. Idempotent + cheap; runs
unconditionally including in contract-test mode.
2026-05-22 16:44:17 -04:00

86 lines
3.0 KiB
Python

"""GET /deckies/lifecycle?ids=… — poll endpoint for the wizard."""
from __future__ import annotations
import httpx
import pytest
from decnet.web.dependencies import repo
@pytest.mark.anyio
async def test_get_lifecycle_unauthenticated_returns_401(client: httpx.AsyncClient):
resp = await client.get("/api/v1/deckies/lifecycle", params={"ids": "x"})
assert resp.status_code == 401
@pytest.mark.anyio
async def test_get_lifecycle_missing_ids_returns_validation_error(
client: httpx.AsyncClient, auth_token: str,
):
"""No ?ids= → validation rejection (Starlette short-circuits with 400
for the body-parse path; either 400 or 422 is acceptable contract)."""
resp = await client.get(
"/api/v1/deckies/lifecycle",
headers={"Authorization": f"Bearer {auth_token}"},
)
assert resp.status_code in (400, 422)
@pytest.mark.anyio
async def test_get_lifecycle_returns_matching_rows(
client: httpx.AsyncClient, auth_token: str,
):
a = await repo.create_lifecycle({"decky_name": "d1", "operation": "deploy"})
b = await repo.create_lifecycle({"decky_name": "d2", "operation": "mutate"})
await repo.update_lifecycle(a, {"status": "running"})
resp = await client.get(
"/api/v1/deckies/lifecycle",
params=[("ids", a), ("ids", b)],
headers={"Authorization": f"Bearer {auth_token}"},
)
assert resp.status_code == 200, resp.text
rows = resp.json()["rows"]
assert len(rows) == 2
by_id = {r["id"]: r for r in rows}
assert by_id[a]["status"] == "running"
assert by_id[a]["operation"] == "deploy"
assert by_id[b]["status"] == "pending"
assert by_id[b]["operation"] == "mutate"
@pytest.mark.anyio
async def test_get_lifecycle_unknown_id_silently_omitted(
client: httpx.AsyncClient, auth_token: str,
):
a = await repo.create_lifecycle({"decky_name": "d1", "operation": "deploy"})
resp = await client.get(
"/api/v1/deckies/lifecycle",
params=[("ids", a), ("ids", "no-such-id")],
headers={"Authorization": f"Bearer {auth_token}"},
)
assert resp.status_code == 200
rows = resp.json()["rows"]
assert len(rows) == 1
assert rows[0]["id"] == a
@pytest.mark.anyio
async def test_startup_sweep_marks_stale_rows_failed():
"""The sweep stamps reason='master restarted during operation' on
any non-terminal row older than the cutoff."""
from datetime import datetime, timedelta, timezone
lid = await repo.create_lifecycle({"decky_name": "stale", "operation": "deploy"})
# Backdate started_at into the past so the sweep picks it up.
await repo.update_lifecycle(
lid, {"started_at": datetime.now(timezone.utc) - timedelta(hours=2)},
)
swept = await repo.sweep_stale_lifecycle(
datetime.now(timezone.utc) - timedelta(hours=1),
reason="master restarted during operation",
)
assert swept >= 1
rows = await repo.get_lifecycle_by_ids([lid])
assert rows[0]["status"] == "failed"
assert rows[0]["error"] == "master restarted during operation"