Merge feat/mcp-server (9708154) into feat/mcp-auth
The base branch moved twelve commits while this work was in flight --
including the per-session live stream and its own contract bump -- so this
integrates rather than leaving a PR nobody can merge. Six files
conflicted; the interesting resolutions are the two that were not
textual.
**The contract is mcp-v3.1, not v2.1.** This branch bumped v2 -> v2.1 for
an additive change; `feat/mcp-server` bumped v2 -> v3 for the live stream,
also additive. Both point releases, both correct, and they cannot both be
the same document. So v3 stands as written and the HTTP transport is the
point release on top of it, by the same rule each of us applied
independently: v2 is still the only major bump, because it is still the
only one that removed anything.
**The counts are no longer literals.** This branch asserted `21 tools` and
`5 resources` in `checks/vm-mcp-http.nix` and `5` in `test_mcp_http.py`;
the stream added two tools and one fixed resource, so the surface is 23
and 6. Rather than re-hardcode numbers that will go stale again, the
check now counts against `--list-tools` and `--list-resources` -- the
server's own table -- and the unit test against `resources.FIXED`. That
is the lesson 9708154 ("count the unnarrowed resources against the
listing, not a literal") had already learned on the other side of this
merge, arrived at twice. A stale number here would have failed in a way
indistinguishable from the HTTP transport dropping tools, which is the
one thing those subtests exist to catch.
The rest were additive on both sides and both halves are kept: the TODO
table, the checks wiring, the README check rows, the architecture diagram
(their 23/10 counts with this branch's two-transport annotation).
`nix/module.nix` needed reconstructing -- git interleaved the bodies of
`hhstream-live` and `hhstream-mcp` because both units land between the
same surrounding lines -- so the live unit is taken verbatim and the MCP
unit placed after it, with this branch's options, tmpfiles entry,
firewall rule, two assertions and two warnings re-applied on top.
One thing deliberately NOT touched: the v3 change table cites `§6` for the
idle-clock exception, but the rule it means is `§8`. That is the other
branch's prose and its author's call, not something to quietly rewrite in
a merge.
Verified after resolution: 394 unit tests pass, and the module evaluates
with both units, the right ExecStart, one firewall port and both
warnings.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KchcvnbfhhZhUHy83YL3Ya
This commit is contained in:
commit
f83717ba71
37 files changed
+4241
-101
No files matched your search
@@ -2,6 +2,7 @@
|
||||
#
|
||||
# unit -- pytest over tests/, against the nix/package.nix build
|
||||
# vm-mcp -- the MCP server over stdio, against a live broker
|
||||
# vm-stream -- the per-session live stream, with a real HTTP viewer
|
||||
# vm-mcp-http -- the MCP server's HTTP transport, its token, and its unit
|
||||
# vm-basic -- nixosTest with a fake capture backend: module + wire contract
|
||||
# vm-hyprland -- nixosTest with the real compositor and grim
|
||||
@@ -82,6 +83,10 @@ in
|
||||
# the others have no reason to carry.
|
||||
vm-mcp = pkgs.callPackage ./vm-mcp.nix (sessionArgs // { inherit hhstream-mcp; });
|
||||
|
||||
# The live stream: `stream enable`, a real HTTP viewer inside the guest, and
|
||||
# the one thing no in-process test can say -- that a connected viewer moves
|
||||
# the idle clock. Spends real time on purpose; see the file's header.
|
||||
vm-stream = pkgs.callPackage ./vm-stream.nix sessionArgs;
|
||||
# The same machine again with `mcp.http.enable`. A separate check and not a
|
||||
# subtest of `vm-mcp`, because it proves a different thing: that one is about
|
||||
# the surface over a pipe, this one is about a unit systemd supervises and a
|
||||
|
||||
Executable
+148
@@ -0,0 +1,148 @@
|
||||
#!/usr/bin/env python3
|
||||
"""``hhs-mjpeg-read`` -- read a real MJPEG stream, and optionally keep watching.
|
||||
|
||||
What `checks/vm-stream.nix` uses as the *person with a phone*. Two jobs, and the
|
||||
second is the one that matters:
|
||||
|
||||
1. Read N frames off ``multipart/x-mixed-replace`` and report their sizes, so
|
||||
"the stream works" is a statement about bytes a client actually received
|
||||
rather than about a 200.
|
||||
2. **Hold the connection open** for a while afterwards. The broker counts
|
||||
connected viewers and treats one as activity, so proving that needs a viewer
|
||||
that is still there a minute later -- a client that read three frames and
|
||||
exited would make the sweep correct and untested.
|
||||
|
||||
Standard library only, and a hand-rolled multipart reader rather than anything
|
||||
that understands HTTP for us: the framing is this repository's own
|
||||
(`hhstream_live.server`), and a parser that shared code with it could not tell
|
||||
you the boundary was wrong.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import socket
|
||||
import sys
|
||||
import time
|
||||
import urllib.parse
|
||||
|
||||
SOI = b"\xff\xd8"
|
||||
EOI = b"\xff\xd9"
|
||||
|
||||
|
||||
def connect(url: str, timeout: float) -> socket.socket:
|
||||
parsed = urllib.parse.urlsplit(url)
|
||||
host = parsed.hostname or "127.0.0.1"
|
||||
port = parsed.port or 80
|
||||
path = parsed.path or "/"
|
||||
sock = socket.create_connection((host, port), timeout=timeout)
|
||||
request = (
|
||||
f"GET {path} HTTP/1.1\r\n"
|
||||
f"Host: {host}:{port}\r\n"
|
||||
"Accept: */*\r\n"
|
||||
"Connection: close\r\n\r\n"
|
||||
)
|
||||
sock.sendall(request.encode("ascii"))
|
||||
return sock
|
||||
|
||||
|
||||
def read_headers(sock: socket.socket) -> tuple[int, dict[str, str], bytes]:
|
||||
buffer = b""
|
||||
while b"\r\n\r\n" not in buffer:
|
||||
chunk = sock.recv(4096)
|
||||
if not chunk:
|
||||
raise SystemExit("the server closed the connection before any headers")
|
||||
buffer += chunk
|
||||
head, rest = buffer.split(b"\r\n\r\n", 1)
|
||||
lines = head.decode("latin-1").split("\r\n")
|
||||
status = int(lines[0].split()[1])
|
||||
headers = {}
|
||||
for line in lines[1:]:
|
||||
if ":" in line:
|
||||
name, _, value = line.partition(":")
|
||||
headers[name.strip().lower()] = value.strip()
|
||||
return status, headers, rest
|
||||
|
||||
|
||||
def frames(sock: socket.socket, rest: bytes, wanted: int, deadline: float) -> list[int]:
|
||||
"""Sizes of the JPEGs read off the multipart body, by SOI/EOI.
|
||||
|
||||
Framed on the markers rather than on ``Content-Length``: a stream that sent
|
||||
the right lengths and the wrong bytes would pass a length-based reader, and
|
||||
what the check is about is whether a browser would paint this.
|
||||
"""
|
||||
|
||||
sizes: list[int] = []
|
||||
buffer = rest
|
||||
while len(sizes) < wanted:
|
||||
if time.monotonic() > deadline:
|
||||
raise SystemExit(
|
||||
f"only {len(sizes)} of {wanted} frames arrived before the deadline"
|
||||
)
|
||||
start = buffer.find(SOI)
|
||||
end = buffer.find(EOI, start + 2) if start != -1 else -1
|
||||
if start != -1 and end != -1:
|
||||
sizes.append(end + 2 - start)
|
||||
buffer = buffer[end + 2 :]
|
||||
continue
|
||||
try:
|
||||
chunk = sock.recv(65536)
|
||||
except TimeoutError:
|
||||
raise SystemExit("the stream went quiet") from None
|
||||
if not chunk:
|
||||
raise SystemExit("the stream ended before enough frames arrived")
|
||||
buffer += chunk
|
||||
return sizes
|
||||
|
||||
|
||||
def main(argv: list[str] | None = None) -> int:
|
||||
parser = argparse.ArgumentParser(prog="hhs-mjpeg-read", description=__doc__)
|
||||
parser.add_argument("url", help="the stream.mjpg URL")
|
||||
parser.add_argument("--frames", type=int, default=2, help="how many to read")
|
||||
parser.add_argument(
|
||||
"--hold",
|
||||
type=float,
|
||||
default=0.0,
|
||||
metavar="SECONDS",
|
||||
help="keep the connection open this long after reading, so the broker "
|
||||
"counts a viewer for that whole time",
|
||||
)
|
||||
parser.add_argument("--timeout", type=float, default=30.0)
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
sock = connect(args.url, args.timeout)
|
||||
sock.settimeout(args.timeout)
|
||||
try:
|
||||
status, headers, rest = read_headers(sock)
|
||||
content_type = headers.get("content-type", "")
|
||||
if status != 200:
|
||||
print(json.dumps({"status": status}), flush=True)
|
||||
raise SystemExit(f"the stream answered {status}, not 200")
|
||||
if "multipart/x-mixed-replace" not in content_type:
|
||||
raise SystemExit(f"content-type is {content_type!r}, not multipart")
|
||||
sizes = frames(sock, rest, args.frames, time.monotonic() + args.timeout)
|
||||
print(
|
||||
json.dumps(
|
||||
{"status": status, "content_type": content_type, "frames": sizes}
|
||||
),
|
||||
flush=True,
|
||||
)
|
||||
if args.hold > 0:
|
||||
# Still connected, and still reading: a viewer that stopped reading
|
||||
# would have its socket buffer fill and the server would see it go.
|
||||
deadline = time.monotonic() + args.hold
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
if not sock.recv(65536):
|
||||
break
|
||||
except TimeoutError:
|
||||
continue
|
||||
print(json.dumps({"held_for_s": args.hold}), flush=True)
|
||||
finally:
|
||||
sock.close()
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
+22
-3
@@ -259,10 +259,29 @@ testers.runNixOSTest (
|
||||
assert result["protocolVersion"], result
|
||||
|
||||
with subtest("the whole surface is offered to an unscoped token"):
|
||||
# Against the server's **own** table rather than a literal. A
|
||||
# number here would have to be edited every time the surface grows
|
||||
# -- and the failure that produces is indistinguishable from the
|
||||
# HTTP transport dropping tools, which is what this subtest is for.
|
||||
# `--list-tools` is the same table `tools/list` serves, printed by
|
||||
# the binary without opening a transport.
|
||||
expected_tools = len(machine.succeed(
|
||||
as_user(f"{MCP} --list-tools"), timeout=120
|
||||
).strip().splitlines())
|
||||
listed = http("--list", token=token)["tools"]
|
||||
assert len(listed) == 21, f"{len(listed)} tools, expected 21"
|
||||
assert len(listed) == expected_tools, (
|
||||
f"{len(listed)} tools over http, {expected_tools} in the table"
|
||||
)
|
||||
# The fixed resources only: `--list-resources` prints the templates
|
||||
# beside them, and `resources/list` is the fixed ones by protocol.
|
||||
printed = machine.succeed(
|
||||
as_user(f"{MCP} --list-resources"), timeout=120
|
||||
).strip().splitlines()
|
||||
expected_fixed = len([line for line in printed if "{" not in line])
|
||||
fixed = http("--resources", token=token)["resources"]
|
||||
assert len(fixed) == 5, f"{len(fixed)} resources, expected 5"
|
||||
assert len(fixed) == expected_fixed, (
|
||||
f"{len(fixed)} resources over http, {expected_fixed} fixed in the table"
|
||||
)
|
||||
|
||||
with subtest("a resource read reaches the broker"):
|
||||
contents = http("--read hhstream://sessions", token=token)["contents"]
|
||||
@@ -289,7 +308,7 @@ testers.runNixOSTest (
|
||||
# The two tokens are served by ONE process, which is the whole
|
||||
# argument for putting toolsets on the token rather than only on
|
||||
# the command line: a launch-time flag cannot do this.
|
||||
assert len(http("--list", token=token)["tools"]) == 21
|
||||
assert len(http("--list", token=token)["tools"]) == expected_tools
|
||||
|
||||
with subtest("token list shows both and neither secret"):
|
||||
out = machine.succeed(as_user(f"{MCP} token list"), timeout=120)
|
||||
|
||||
+69
-4
@@ -11,7 +11,7 @@
|
||||
# WHAT THIS PROVES, AND WHAT IT DOES NOT.
|
||||
#
|
||||
# It proves the *surface*, both halves of it: the handshake completes over a
|
||||
# real pipe, all twenty-one tools and all eight resources are listed, a tool
|
||||
# real pipe, all twenty-three tools and all ten resources are listed, a tool
|
||||
# call reaches the broker and comes back with the broker's own protocol object,
|
||||
# a resource read reaches it the same way, an image arrives as a blob with the
|
||||
# geometry the module configured, `--toolsets` actually narrows what is
|
||||
@@ -119,6 +119,17 @@ testers.runNixOSTest (
|
||||
package = hhstream-mcp;
|
||||
};
|
||||
|
||||
# Enabled because two tools and two resources here are about streams,
|
||||
# and with no daemon they could only be shown to *refuse*. What a stream
|
||||
# actually serves is `vm-stream`'s subject -- it reads JPEGs off the
|
||||
# port; what this check owns is that the URL (with its phrase) comes back
|
||||
# through the MCP layer at all.
|
||||
services.hyprland-headless-stream.sessions.stream = {
|
||||
enable = true;
|
||||
bind = "127.0.0.1";
|
||||
port = 9988;
|
||||
};
|
||||
|
||||
environment.systemPackages = [ client ];
|
||||
};
|
||||
|
||||
@@ -227,10 +238,11 @@ testers.runNixOSTest (
|
||||
listed = mcp("--list")["tools"]
|
||||
names = sorted(tool["name"] for tool in listed)
|
||||
print(f"{len(names)} tools: {', '.join(names)}")
|
||||
assert len(names) == 21, names
|
||||
assert len(names) == 23, names
|
||||
for expected in (
|
||||
"shot", "record", "run",
|
||||
"session_start", "session_touch", "session_kill",
|
||||
"session_stream_enable", "session_stream_disable",
|
||||
"pointer_acquire", "pointer_click", "keyboard_type",
|
||||
):
|
||||
assert expected in names, f"{expected} is missing from the listing"
|
||||
@@ -257,10 +269,12 @@ testers.runNixOSTest (
|
||||
"hhstream://screen",
|
||||
"hhstream://sessions",
|
||||
"hhstream://status",
|
||||
"hhstream://streams",
|
||||
], uris
|
||||
assert templates == [
|
||||
"hhstream://outputs/{name}",
|
||||
"hhstream://sessions/{id}/logs{?lines}",
|
||||
"hhstream://sessions/{id}/stream",
|
||||
"hhstream://sessions/{id}{?events}",
|
||||
], templates
|
||||
by_uri = {r["uri"]: r for r in both["resources"]}
|
||||
@@ -505,8 +519,12 @@ testers.runNixOSTest (
|
||||
timeout=180,
|
||||
)
|
||||
)
|
||||
assert len(still_there["resources"]) == 5, still_there
|
||||
assert len(still_there["resourceTemplates"]) == 3, still_there
|
||||
# Every one of them, unnarrowed: `--toolsets` is a tool budget and
|
||||
# must not take the host's context away with it. Counted against
|
||||
# the full listing above rather than against a literal, so the next
|
||||
# resource added cannot make this pass by being forgotten.
|
||||
assert len(still_there["resources"]) == len(uris), still_there
|
||||
assert len(still_there["resourceTemplates"]) == len(templates), still_there
|
||||
|
||||
# And a tool outside the selection must be unreachable, not merely
|
||||
# unlisted, or the flag is cosmetic.
|
||||
@@ -532,6 +550,53 @@ testers.runNixOSTest (
|
||||
except Exception:
|
||||
dump("session_kill over mcp", session_id)
|
||||
raise
|
||||
|
||||
with subtest("a stream asked for at start comes back as a url, twice"):
|
||||
# The two places an agent can learn the phrase, and they have to
|
||||
# agree: the `session_start` reply, and the resource for that
|
||||
# session's stream. A URL is the daemon's to name and is never
|
||||
# stored, so a reply built from the broker's record alone would
|
||||
# carry a phrase and no URL -- which is how this went wrong once
|
||||
# (see `SessionManager.start`).
|
||||
streamed_id = None
|
||||
try:
|
||||
streamed = succeeded(
|
||||
"session_start", {"app": "foot", "stream": True}, timeout=300
|
||||
)
|
||||
streamed_id = streamed["structuredContent"]["id"]
|
||||
stream = streamed["structuredContent"]["stream"]
|
||||
print(f"{streamed_id} -> {stream['url']}")
|
||||
assert stream["url"].startswith("http://127.0.0.1:9988/"), stream
|
||||
# The phrase is IN the URL and is the whole of its protection,
|
||||
# so the text a model reads has to carry it too.
|
||||
assert stream["url"] in text_of(streamed)
|
||||
assert stream["phrase"] in stream["url"]
|
||||
assert streamed["structuredContent"]["stream_error"] is None
|
||||
|
||||
body = json.loads(
|
||||
read(f"hhstream://sessions/{streamed_id}/stream")[0]["text"]
|
||||
)
|
||||
assert body["stream"]["url"] == stream["url"], body
|
||||
assert body["stream"]["attached"] is True, body
|
||||
|
||||
# And the listing resource names it, with the daemon reachable.
|
||||
listed = json.loads(read("hhstream://streams")[0]["text"])
|
||||
assert listed["available"] is True, listed
|
||||
assert [row["id"] for row in listed["streams"]] == [streamed_id], listed
|
||||
except Exception:
|
||||
dump("session_start --stream over mcp", streamed_id)
|
||||
raise
|
||||
|
||||
with subtest("disabling over mcp takes the stream off both resources"):
|
||||
try:
|
||||
succeeded("session_stream_disable", {"id": streamed_id}, timeout=180)
|
||||
body = json.loads(
|
||||
read(f"hhstream://sessions/{streamed_id}/stream")[0]["text"]
|
||||
)
|
||||
assert body["stream"] is None, body
|
||||
assert json.loads(read("hhstream://streams")[0]["text"])["streams"] == []
|
||||
finally:
|
||||
succeeded("session_kill", {"id": streamed_id}, timeout=300)
|
||||
'';
|
||||
}
|
||||
)
|
||||
@@ -0,0 +1,372 @@
|
||||
# checks.<system>.vm-stream -- the per-session live stream, end to end, with a
|
||||
# real HTTP viewer inside the guest.
|
||||
#
|
||||
# The session machine plus `sessions.stream.enable = true`, which runs
|
||||
# `hhstream-live` and hands the broker `/run/hhstream/live.sock`. Then:
|
||||
# `hhstream session stream enable` on a live session, and a client that reads
|
||||
# actual JPEGs off `/<phrase>/stream.mjpg`.
|
||||
#
|
||||
# WHAT THIS PROVES THAT THE UNIT TESTS CANNOT.
|
||||
#
|
||||
# `tests/live/test_live_control.py` and `tests/sessions/unit/test_stream_verbs.py`
|
||||
# already run both ends of `live.sock` against each other with a fake capture.
|
||||
# What they cannot say is that `grim` captures a real compositor from inside a
|
||||
# *unit* -- which is the one failure this feature has already been bitten by
|
||||
# once elsewhere in this tree (`docs/TODO/capture-binary-and-the-unit-path.md`:
|
||||
# a unit's PATH has no `grim`, and the symptom is an empty frame area with no
|
||||
# error). So the capture command is passed explicitly by the module and this
|
||||
# check reads the bytes that come out of it.
|
||||
#
|
||||
# It also proves the one thing no in-process test can: that a *connected
|
||||
# viewer* moves the idle clock. That needs the broker's real sweep, on a real
|
||||
# timer, with a client that is genuinely still there a minute later -- which is
|
||||
# why `hhs-mjpeg-read --hold` exists and why this check spends real time.
|
||||
#
|
||||
# THE TWO REAL-TIME WAITS ARE THE POINT OF THEIR SUBTESTS. The sweep runs once
|
||||
# a minute (`hhstreamd.manager.STREAM_SWEEP_S`), and both the positive case and
|
||||
# its negative control have to outlast one pass. No clock is injected anywhere:
|
||||
# an injected one would prove the arithmetic, which the unit tests already do,
|
||||
# and not the daemon.
|
||||
#
|
||||
# Habits copied from the other session checks: every wait is bounded, each
|
||||
# subtest asserts once, and a failure dumps the compositor's diagnostics and
|
||||
# the broker's journal before it propagates.
|
||||
{
|
||||
testers,
|
||||
hhstream,
|
||||
hhsModule,
|
||||
testTools,
|
||||
hyprland,
|
||||
foot,
|
||||
jq,
|
||||
grim,
|
||||
procps,
|
||||
coreutils,
|
||||
dejavu_fonts,
|
||||
width ? 1280,
|
||||
height ? 720,
|
||||
sessionUser ? "hermes",
|
||||
port ? 9988,
|
||||
}:
|
||||
testers.runNixOSTest (
|
||||
{ pkgs, ... }:
|
||||
let
|
||||
tools = pkgs.callPackage ./lib/session-tools.nix { inherit hhstream; };
|
||||
guest = import ./lib/session-machine.nix {
|
||||
inherit
|
||||
hhsModule
|
||||
hhstream
|
||||
hyprland
|
||||
testTools
|
||||
foot
|
||||
jq
|
||||
procps
|
||||
grim
|
||||
coreutils
|
||||
dejavu_fonts
|
||||
;
|
||||
sessionTools = tools;
|
||||
};
|
||||
prelude = import ./lib/session-prelude.nix {
|
||||
inherit hhstream coreutils sessionUser;
|
||||
};
|
||||
|
||||
# The viewer. Plain python3 and a hand-rolled multipart reader, so the
|
||||
# framing this repository serves is read by something that does not share
|
||||
# code with it -- the same choice `tests/assert_stream.py` makes about the
|
||||
# frame socket, for the same reason.
|
||||
viewer = pkgs.runCommandLocal "hhs-mjpeg-read" { } ''
|
||||
install -Dm755 ${./lib/mjpeg_read.py} $out/bin/hhs-mjpeg-read
|
||||
sed -i '1s|.*|#!${pkgs.python3}/bin/python3|' $out/bin/hhs-mjpeg-read
|
||||
$out/bin/hhs-mjpeg-read --help > /dev/null
|
||||
'';
|
||||
in
|
||||
{
|
||||
name = "hhstream-vm-stream";
|
||||
|
||||
# Boot, plus two sweep windows of real waiting (~70 s each) and a session
|
||||
# launch. Capped so a hang is a failure rather than a wedged builder.
|
||||
globalTimeout = 2400;
|
||||
|
||||
nodes.machine = {
|
||||
imports = [
|
||||
(guest {
|
||||
inherit width height sessionUser;
|
||||
apps.foot = {
|
||||
exec = "${foot}/bin/foot";
|
||||
argsAllow = [ ];
|
||||
};
|
||||
})
|
||||
];
|
||||
|
||||
services.hyprland-headless-stream.sessions.stream = {
|
||||
enable = true;
|
||||
# Loopback: the viewer runs inside the guest. Reaching it from outside
|
||||
# is `vm-stream-firewall`'s subject, and opening a port here would make
|
||||
# two checks assert the same thing badly.
|
||||
bind = "127.0.0.1";
|
||||
inherit port;
|
||||
};
|
||||
|
||||
environment.systemPackages = [ viewer ];
|
||||
};
|
||||
|
||||
testScript =
|
||||
prelude
|
||||
+ ''
|
||||
# `machine.sleep` wants a `timedelta`: a bare number is deprecated as
|
||||
# `secs=` and refused outright by the driver's type check as
|
||||
# `duration=`. MEASURED, as `nix build` failing before the VM even
|
||||
# booted: "Expected `timedelta | None`, found `Literal[75]`".
|
||||
import datetime as dt
|
||||
|
||||
READ = "${viewer}/bin/hhs-mjpeg-read"
|
||||
PORT = ${toString port}
|
||||
LIVE_SOCKET = "/run/hhstream/live.sock"
|
||||
#: `hhstreamd.manager.STREAM_SWEEP_S`, plus room for one round trip.
|
||||
SWEEP_WINDOW_S = 75
|
||||
|
||||
|
||||
def expires_in(session_id):
|
||||
"""The session's own idle deadline, as the broker reports it."""
|
||||
|
||||
return cli_json(f"status {session_id} --json")["session"]["expires_in_s"]
|
||||
|
||||
|
||||
def stream_json(args=""):
|
||||
return cli_json(f"stream {args} --json".strip())
|
||||
|
||||
|
||||
start_all()
|
||||
bring_up("the compositor and broker")
|
||||
|
||||
with subtest("the as-user wrapper propagates exit status"):
|
||||
machine.succeed(as_user(TRUE), timeout=120)
|
||||
machine.fail(as_user(FALSE), timeout=120)
|
||||
|
||||
with subtest("the stream daemon came up and owns its socket, not the broker's"):
|
||||
try:
|
||||
machine.wait_for_unit("hhstream-live.service", timeout=180)
|
||||
machine.wait_for_file(LIVE_SOCKET, timeout=120)
|
||||
restarts = machine.succeed(
|
||||
"systemctl show -p NRestarts --value hhstream-live.service"
|
||||
).strip()
|
||||
assert restarts == "0", (
|
||||
f"hhstream-live has restarted {restarts} time(s): it is "
|
||||
"crash-looping, not running"
|
||||
)
|
||||
# The risk the plan names by name: a second RuntimeDirectory on
|
||||
# /run/hhstream would have deleted the broker's socket.
|
||||
machine.succeed(f"test -S {CONTROL}")
|
||||
machine.succeed(f"test -S {LIVE_SOCKET}")
|
||||
# And it is listening on the port it was given, with no stream
|
||||
# on it: running the daemon turns nothing on.
|
||||
machine.wait_until_succeeds(
|
||||
f"ss -ltn | grep -q 127.0.0.1:{PORT}", timeout=60
|
||||
)
|
||||
assert stream_json("list")["streams"] == []
|
||||
except Exception:
|
||||
dump("the stream daemon did not come up")
|
||||
raise
|
||||
|
||||
with subtest("a session with no stream reports none"):
|
||||
session_id = machine.succeed(cli("start foot"), timeout=300).strip()
|
||||
print(f"session {session_id}")
|
||||
assert stream_json(f"status {session_id}")["stream"] is None
|
||||
|
||||
with subtest("enabling mints a phrase and the daemon publishes the output"):
|
||||
try:
|
||||
url = machine.succeed(
|
||||
cli(f"stream enable {session_id}"), timeout=180
|
||||
).strip()
|
||||
print(f"stream url: {url}")
|
||||
assert url.startswith(f"http://127.0.0.1:{PORT}/"), url
|
||||
|
||||
status = stream_json(f"status {session_id}")["stream"]
|
||||
assert status["attached"] is True, status
|
||||
assert status["output"] == f"hh-{session_id}", status
|
||||
assert status["url"] == url, status
|
||||
# The phrase is in the URL and in the record, and nowhere else.
|
||||
assert status["phrase"] in url
|
||||
except Exception:
|
||||
dump("enabling the stream", session_id)
|
||||
raise
|
||||
|
||||
with subtest("the phrase is not in the audit log"):
|
||||
# It is the stream's only credential, and the log is read by
|
||||
# whoever can read logs (CONTRACT-sessions.md §3.5).
|
||||
phrase = stream_json(f"status {session_id}")["stream"]["phrase"]
|
||||
events = machine.succeed(
|
||||
"cat /var/lib/hhstream/events.jsonl || true", timeout=60
|
||||
)
|
||||
assert "stream_enable" in events, "the enable was not audited at all"
|
||||
assert phrase not in events, "the phrase reached the audit log"
|
||||
|
||||
with subtest("a real client reads JPEGs off the stream"):
|
||||
# The subtest the unit tests cannot write: `grim` capturing a real
|
||||
# compositor, from inside a unit, through the module's own capture
|
||||
# command.
|
||||
try:
|
||||
out = machine.succeed(
|
||||
as_user(f"{READ} {url}stream.mjpg --frames 3"), timeout=180
|
||||
)
|
||||
first = json.loads(out.strip().splitlines()[0])
|
||||
print(f"viewer read: {first}")
|
||||
assert first["status"] == 200, first
|
||||
assert "multipart/x-mixed-replace" in first["content_type"], first
|
||||
assert len(first["frames"]) == 3, first
|
||||
# Real frames of a real desktop, not a 1x1 placeholder.
|
||||
assert all(size > 1024 for size in first["frames"]), first["frames"]
|
||||
except Exception:
|
||||
dump("reading the stream", session_id)
|
||||
raise
|
||||
|
||||
with subtest("a wrong phrase is a 404 and is never echoed back"):
|
||||
wrong = "wrong-words-entirely-here-00000"
|
||||
status, body = machine.execute(
|
||||
as_user(f"{READ} http://127.0.0.1:{PORT}/{wrong}/stream.mjpg --frames 1"),
|
||||
timeout=120,
|
||||
)
|
||||
assert status != 0, f"a wrong phrase was served: {body}"
|
||||
assert wrong not in body, "the wrong phrase was echoed back"
|
||||
|
||||
with subtest("an enabled stream nobody watches keeps nothing alive"):
|
||||
# The negative control, and it has to come first: if the sweep
|
||||
# touched an unwatched stream, the positive case below would pass
|
||||
# for the wrong reason.
|
||||
try:
|
||||
assert stream_json("list")["streams"][0]["viewers"] == 0
|
||||
before = expires_in(session_id)
|
||||
machine.sleep(duration=dt.timedelta(seconds=SWEEP_WINDOW_S))
|
||||
after = expires_in(session_id)
|
||||
print(f"with no viewer: {before:.0f}s -> {after:.0f}s remaining")
|
||||
assert after < before, (
|
||||
"the idle deadline did not move closer over a whole sweep "
|
||||
"window with nobody watching, so something is touching this "
|
||||
"session"
|
||||
)
|
||||
except Exception:
|
||||
dump("the unwatched stream", session_id)
|
||||
raise
|
||||
|
||||
with subtest("a connected viewer is activity and moves the idle deadline"):
|
||||
# The one exception to "touch is the only mark of activity"
|
||||
# (CONTRACT-sessions.md §4). The viewer holds its connection open
|
||||
# across a sweep, because a client that read and left would make
|
||||
# the sweep correct and untested.
|
||||
try:
|
||||
before = expires_in(session_id)
|
||||
machine.succeed(
|
||||
background(
|
||||
"hhs-viewer",
|
||||
f"{READ} {url}stream.mjpg --frames 2 "
|
||||
f"--hold {SWEEP_WINDOW_S} --timeout {SWEEP_WINDOW_S + 60}",
|
||||
user=USER,
|
||||
),
|
||||
timeout=120,
|
||||
)
|
||||
watching = wait_for(
|
||||
lambda: stream_json("list")["streams"][0]["viewers"] > 0,
|
||||
timeout=60,
|
||||
what="the broker to see a connected viewer",
|
||||
)
|
||||
assert watching
|
||||
after = wait_for(
|
||||
lambda: expires_in(session_id) > before,
|
||||
timeout=SWEEP_WINDOW_S + 60,
|
||||
what="the viewer sweep to reset the idle clock",
|
||||
)
|
||||
print(f"with a viewer: {before:.0f}s -> {expires_in(session_id):.0f}s")
|
||||
|
||||
events = machine.succeed("cat /var/lib/hhstream/events.jsonl")
|
||||
assert "stream_viewer_activity" in events
|
||||
assert stream_json(f"status {session_id}")["stream"]["phrase"] not in events
|
||||
except Exception:
|
||||
dump("the watched stream", session_id)
|
||||
raise
|
||||
|
||||
with subtest("re-enabling returns the same url and starts no second capture"):
|
||||
again = machine.succeed(cli(f"stream enable {session_id}"), timeout=180).strip()
|
||||
assert again == url, f"the phrase rotated: {again} != {url}"
|
||||
assert len(stream_json("list")["streams"]) == 1
|
||||
|
||||
with subtest("a phrase that is not this session's is refused"):
|
||||
# Of the generated SHAPE and not this session's phrase -- the two
|
||||
# refusals are different exits, and a fixture that is merely
|
||||
# malformed would take the shape path (exit 2) and never reach the
|
||||
# broker. MEASURED: the first fixture here was
|
||||
# `other-words-go-here-99999`, which fails on `go` being two
|
||||
# letters -- the CLI refused it on shape and this subtest read
|
||||
# exit 2 where it wanted 8.
|
||||
status, out = machine.execute(
|
||||
cli(f"stream enable {session_id} --phrase other-words-going-here-99999"),
|
||||
timeout=120,
|
||||
)
|
||||
# Exit 8 (BUSY): rotating under somebody holding the old URL is the
|
||||
# one outcome neither answer can be mistaken for.
|
||||
assert status == 8, f"expected exit 8, got {status}: {out}"
|
||||
assert stream_json(f"status {session_id}")["stream"]["url"] == url
|
||||
|
||||
with subtest("a malformed phrase is refused without echoing it"):
|
||||
status, out = machine.execute(
|
||||
cli(f"stream enable {session_id} --phrase hunter2"), timeout=120
|
||||
)
|
||||
assert status == 2, f"expected exit 2, got {status}: {out}"
|
||||
assert "hunter2" not in out, "the refusal echoed the phrase back"
|
||||
|
||||
with subtest("disabling stops the capture and forgets the phrase"):
|
||||
machine.succeed(cli(f"stream disable {session_id}"), timeout=180)
|
||||
assert stream_json(f"status {session_id}")["stream"] is None
|
||||
assert stream_json("list")["streams"] == []
|
||||
status, _ = machine.execute(
|
||||
as_user(f"{READ} {url}stream.mjpg --frames 1"), timeout=120
|
||||
)
|
||||
assert status != 0, "the old URL still serves after disable"
|
||||
|
||||
with subtest("disable then enable rotates the phrase"):
|
||||
rotated = machine.succeed(cli(f"stream enable {session_id}"), timeout=180).strip()
|
||||
assert rotated != url, "the phrase did not rotate"
|
||||
url = rotated
|
||||
|
||||
with subtest("kill takes the stream down with the session"):
|
||||
# The teardown *order* is unit-tested (a capture aimed at a removed
|
||||
# output fails every time); what is asserted here is the outcome
|
||||
# both halves have to agree on.
|
||||
try:
|
||||
machine.succeed(cli(f"kill {session_id}"), timeout=300)
|
||||
assert stream_json("list")["streams"] == []
|
||||
status, _ = machine.execute(
|
||||
as_user(f"{READ} {url}stream.mjpg --frames 1"), timeout=120
|
||||
)
|
||||
assert status != 0, "the stream outlived its session"
|
||||
# And the daemon is still up: a session ending is not its
|
||||
# problem.
|
||||
machine.succeed("systemctl is-active hhstream-live.service")
|
||||
except Exception:
|
||||
dump("killing a streaming session", session_id)
|
||||
raise
|
||||
|
||||
with subtest("start --stream does it in one call"):
|
||||
# The usual way an agent sets one up, and the reason `start` takes
|
||||
# the flag at all: a stream needs the container output that
|
||||
# starting creates.
|
||||
try:
|
||||
out = machine.succeed(cli("start foot --stream"), timeout=300)
|
||||
lines = [line for line in out.strip().splitlines() if line]
|
||||
streamed_id, streamed_url = lines[0], lines[1]
|
||||
print(f"{streamed_id} -> {streamed_url}")
|
||||
assert streamed_url.startswith(f"http://127.0.0.1:{PORT}/")
|
||||
read = json.loads(
|
||||
machine.succeed(
|
||||
as_user(f"{READ} {streamed_url}stream.mjpg --frames 2"),
|
||||
timeout=180,
|
||||
).strip().splitlines()[0]
|
||||
)
|
||||
assert len(read["frames"]) == 2, read
|
||||
machine.succeed(cli(f"kill {streamed_id}"), timeout=300)
|
||||
except Exception:
|
||||
dump("start --stream")
|
||||
raise
|
||||
'';
|
||||
}
|
||||
)
|
||||
Reference in new issue
Block a user