Files
gabrielvidalandClaude Opus 5.5 8c7264f6e2 feat(wol): Wake-on-LAN core support for the wake-on-lan plugin
- sidecar: POST /wol (routes_wol.py, host only) — strict MAC / IPv4 /
  port checks, a UDP magic packet with SO_BROADCAST.
- manifest: the machines.wake capability (operator-approved) and
  provides: machine.wake.
- plugins/hubwake.py: the machines.wake bridge for the plugin's own
  handlers — POST …/hub/wol, POST …/hub/workers/{wid}/probe,
  GET …/hub/wake/workers (each also under …/bridge/…).
- plugins/machine_wake.py: list_wakeable (30 s cache, dropped on the
  provider's actions / install changes, SSE machine-wake), wake_machine,
  maybe_wake_and_wait, probe_in_background.
- runs/service.py: a run pinned to an offline worker, and a resume of a
  session on one, wait for an auto-wake before the 409 worker_offline;
  an unpinned run still falls back to the next runner up, no wake.
- GET /api/machines/wakeable (lab.read), POST /api/machines/{id}/wake
  (workers.manage), schemas + regenerated openapi / orval types.
- Settings → Machines: ⚡ Wake on an offline worker the plugin has
  configured (Waking… until the workers SSE flips it online).
- tests: sidecar/test_wol.py, backend/test_machine_wake.py,
  WakeButton.test.tsx; docs/plugins.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 22:23:28 +01:00

114 lines
4.3 KiB
Python

"""``POST /wol`` (routes_wol.py): the magic packet's bytes, the strict MAC /
IPv4 / port checks, the bearer, and the datagram actually sent (a loopback
UDP socket stands in for the LAN)."""
import os
import socket
os.environ.setdefault("SIDECAR_TOKEN", "test-token")
import pytest # noqa: E402
from fastapi import FastAPI, HTTPException # noqa: E402
from fastapi.testclient import TestClient # noqa: E402
import routes_wol # noqa: E402
import validate # noqa: E402
H = {"Authorization": "Bearer test-token"}
@pytest.fixture
def client(monkeypatch):
monkeypatch.setattr(validate, "TOKEN", "test-token")
app = FastAPI()
app.include_router(routes_wol.router)
return TestClient(app)
def test_magic_packet_format():
pkt = routes_wol.magic_packet("AA-BB-CC-DD-EE-FF")
assert len(pkt) == 102
assert pkt[:6] == b"\xff" * 6
assert pkt[6:] == bytes.fromhex("aabbccddeeff") * 16
@pytest.mark.parametrize("mac", ["aa:bb:cc:dd:ee:ff", "AA:BB:CC:DD:EE:FF", "aa-bb-cc-dd-ee-ff"])
def test_mac_normalized(mac):
assert routes_wol.normalize_mac(mac) == "aa:bb:cc:dd:ee:ff"
@pytest.mark.parametrize("mac", ["", "aa:bb:cc:dd:ee", "aa:bb:cc:dd:ee:ff:00", "gg:bb:cc:dd:ee:ff",
"aabbccddeeff", "aa:bb:cc:dd:ee:f", " aa:bb:cc:dd:ee:ff",
"aa:bb:cc:dd:ee:ff\n", "aa.bb.cc.dd.ee.ff", None, 12])
def test_bad_macs(mac):
with pytest.raises(HTTPException) as e:
routes_wol.normalize_mac(mac)
assert e.value.status_code == 400
@pytest.mark.parametrize("addr", ["255.255.255.255", "192.168.1.255", "10.0.0.255"])
def test_good_broadcasts(addr):
assert routes_wol.check_broadcast(addr) == addr
@pytest.mark.parametrize("addr", ["", "lan.local", "192.168.1", "192.168.1.256", "::1", "fe80::1",
"192.168.1.255:9", None])
def test_bad_broadcasts(addr):
with pytest.raises(HTTPException):
routes_wol.check_broadcast(addr)
@pytest.mark.parametrize("port", [0, -1, 65536, True])
def test_bad_ports(port):
with pytest.raises(HTTPException):
routes_wol.check_port(port)
def test_send_reaches_a_udp_listener():
rx = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
rx.bind(("127.0.0.1", 0))
rx.settimeout(3)
try:
routes_wol.send("aa:bb:cc:dd:ee:ff", "127.0.0.1", rx.getsockname()[1])
data, _ = rx.recvfrom(1024)
finally:
rx.close()
assert data == b"\xff" * 6 + bytes.fromhex("aabbccddeeff") * 16
def test_route_sends_and_answers(client, monkeypatch):
sent = []
monkeypatch.setattr(routes_wol, "send", lambda *a: sent.append(a))
r = client.post("/wol", headers=H, json={"mac": "AA-BB-CC-DD-EE-FF"})
assert r.status_code == 200, r.text
assert r.json() == {"ok": True, "mac": "aa:bb:cc:dd:ee:ff", "broadcast": "192.168.1.255", "port": 9}
assert sent == [("aa:bb:cc:dd:ee:ff", "192.168.1.255", 9)]
r = client.post("/wol", headers=H, json={"mac": "aa:bb:cc:dd:ee:ff", "broadcast": "10.0.0.255", "port": 7})
assert r.json()["broadcast"] == "10.0.0.255" and r.json()["port"] == 7
@pytest.mark.parametrize("body", [{}, {"mac": "nope"}, {"mac": "aa:bb:cc:dd:ee:ff", "broadcast": "x"},
{"mac": "aa:bb:cc:dd:ee:ff", "port": 0},
{"mac": "aa:bb:cc:dd:ee:ff", "port": 99999},
{"mac": "aa:bb:cc:dd:ee:ff", "port": "nine"}])
def test_route_refuses_bad_bodies(client, monkeypatch, body):
monkeypatch.setattr(routes_wol, "send", lambda *a: pytest.fail("sent"))
r = client.post("/wol", headers=H, json=body)
assert r.status_code in (400, 422), r.text
def test_route_needs_the_bearer(client, monkeypatch):
monkeypatch.setattr(routes_wol, "send", lambda *a: pytest.fail("sent"))
assert client.post("/wol", json={"mac": "aa:bb:cc:dd:ee:ff"}).status_code == 401
assert client.post("/wol", headers={"Authorization": "Bearer wrong"},
json={"mac": "aa:bb:cc:dd:ee:ff"}).status_code == 401
def test_a_socket_error_is_a_502(client, monkeypatch):
def boom(*a):
raise OSError("Network is unreachable")
monkeypatch.setattr(routes_wol, "send", boom)
r = client.post("/wol", headers=H, json={"mac": "aa:bb:cc:dd:ee:ff"})
assert r.status_code == 502
assert r.json()["detail"]["kind"] == "wol"