#!/usr/bin/env bash # A throwaway midas workspace on this machine, and bot mode driven through it end to # end. See README.md beside this file. # # tests/e2e/run.sh [MIDAS_BIN] # # A hub runs unprivileged on a loopback port with a temporary state directory; # one node is a container built from node/Containerfile by rootless podman, its # sshd published on another loopback port. Nothing of the real workspace is read or # written: the CLI gets its own HOME, hub address and key, and ssh its own # config (-F, through a wrapper first on PATH). Everything it creates is named # midas-e2e-* and removed on exit — except the image, kept so the next run # builds in seconds (E2E_REMOVE_IMAGE=1 removes it too). set -euo pipefail here=$(cd "$(dirname "$0")" && pwd) repo=$(cd "$here/../.." && pwd) midas=${1:-${MIDAS_BIN:-$repo/target/x86_64-unknown-linux-musl/release/midas}} [ -x "$midas" ] || { echo "e2e: no midas at $midas — build it: cargo build --release --target x86_64-unknown-linux-musl" >&2; exit 2; } midas=$(cd "$(dirname "$midas")" && pwd)/$(basename "$midas") for tool in podman python3 ssh ssh-keygen git curl; do command -v "$tool" >/dev/null || { echo "e2e: needs $tool" >&2; exit 2; } done name=midas-e2e-node image=localhost/midas-e2e-node:latest volume=midas-e2e-work node=e2e-1 e2e=$(mktemp -d /tmp/midas-e2e.XXXXXX) hub_pid="" # podman keeps the real HOME: the agent's side below gets a HOME of its own, # and podman under it would use a second, empty image store. real_home=$HOME podman() { HOME=$real_home command podman "$@"; } # On a machine whose rootless podman also runs a distrobox (`arch` on the # Steam Deck), podman build, rm and rmi unmount that box's root filesystem; # mount it back after each, or the box's next command finds an empty root. remount_distrobox() { podman container exists "${E2E_DISTROBOX:-arch}" 2>/dev/null \ && podman unshare podman mount "${E2E_DISTROBOX:-arch}" >/dev/null 2>&1 return 0 } cleanup() { status=$? set +e if [ "$status" -ne 0 ]; then echo "e2e: FAILED — the node's log, the hub's events:" >&2 podman logs --tail 30 "$name" 2>&1 | sed 's/^/ node | /' >&2 cat "$e2e"/state/log/*.jsonl 2>/dev/null | tail -30 | sed 's/^/ hub | /' >&2 fi [ -n "$hub_pid" ] && kill "$hub_pid" 2>/dev/null && wait "$hub_pid" 2>/dev/null # ssh's shared connections, if any are still up. for s in "$e2e"/cm-*; do [ -S "$s" ] && ssh -F "$e2e/ssh_config" -o ControlPath="$s" -O exit x 2>/dev/null; done podman rm -f -t 2 "$name" >/dev/null 2>&1 podman volume rm -f "$volume" >/dev/null 2>&1 [ "${E2E_REMOVE_IMAGE:-0}" = 1 ] && podman rmi -f "$image" >/dev/null 2>&1 remount_distrobox # Files the container's build user wrote belong to a subordinate id. podman unshare rm -rf "$e2e" 2>/dev/null || rm -rf "$e2e" exit "$status" } trap cleanup EXIT INT TERM step() { printf '\n== %s\n' "$*"; } fail() { echo "e2e: $*" >&2; exit 1; } free_port() { python3 -c 'import socket; s=socket.socket(); s.bind(("127.0.0.1",0)); print(s.getsockname()[1])'; } hub_port=$(free_port) ssh_port=$(free_port) hub=http://127.0.0.1:$hub_port key=$(head -c 30 /dev/urandom | od -An -tx1 | tr -d ' \n') # ─── the hub ────────────────────────────────────────────────────────────────── step "hub on 127.0.0.1:$hub_port, state in $e2e/state" mkdir -p "$e2e/state" printf '{"e2e": {"key": "%s"}}\n' "$key" > "$e2e/callers.json" # The node is defined before the hub starts: `nodes add` would also rewrite the # workspace's own ssh files (/srv/midas/.ssh), which an unprivileged hub cannot. printf '[{"id": "%s", "machine": "e2e", "address": "127.0.0.1", "ssh_port": %s, "gpu": false, "renderer": "e2e"}]\n' \ "$node" "$ssh_port" > "$e2e/state/nodes.json" MIDAS_STATE=$e2e/state MIDAS_CALLERS=$e2e/callers.json "$midas" hub serve --listen "127.0.0.1:$hub_port" \ > "$e2e/hub.log" 2>&1 & hub_pid=$! # Agents' side: its own HOME (no ~/.config/midas/hub.env), hub, key and ssh. export HOME=$e2e/home MIDAS_HUB=$hub MIDAS_HUB_KEY=$key unset MIDAS_STATE MIDAS_OWNER MIDAS_PURPOSE MIDAS_SSH_ANYWAY MIDAS_FLUSHING mkdir -p "$HOME" "$e2e/bin" for _ in $(seq 50); do curl -fs "$hub/v1/health" >/dev/null 2>&1 && break; sleep 0.2; done curl -fs "$hub/v1/health" >/dev/null || fail "the hub did not come up: $(cat "$e2e/hub.log")" # ─── the node ───────────────────────────────────────────────────────────────── step "node $node: image from node/Containerfile, sshd on 127.0.0.1:$ssh_port" ssh-keygen -q -t ed25519 -N '' -C midas-e2e-agent -f "$e2e/agent_key" cfg=$e2e/config mkdir -p "$cfg" cat > "$cfg/node.env" < "$cfg/operator_keys" chmod 755 "$cfg" chmod 644 "$cfg"/* # The bots' secret, as `midas bot setup` leaves it: 0600, the container's build # user's (through the subordinate ids, which is what `podman unshare` maps). token=e2e-$(head -c 12 /dev/urandom | od -An -tx1 | tr -d ' \n') token_sha=$(printf '%s' "$token" | sha256sum | cut -c1-16) printf '# test\nE2E_TOKEN=%s\n' "$token" > "$cfg/bot.env" chmod 600 "$cfg/bot.env" podman unshare chown 1000:1000 "$cfg/bot.env" ctx=$e2e/image mkdir -p "$ctx" cp "$repo/node/Containerfile" "$repo/node/sshd_config" "$repo/node/entrypoint.sh" "$ctx/" cp "$midas" "$ctx/midas" podman build -q -t "$image" "$ctx" >/dev/null remount_distrobox # As install.sh runs a node, but on loopback: pasta forwards the hub's port from # the container's loopback to this machine's (-T), so the node reaches the hub # at 127.0.0.1 and the hub sees it come from 127.0.0.1, the address it has. podman run -d --name "$name" --hostname "$node" \ --network "pasta:-T,$hub_port" -p "127.0.0.1:$ssh_port:2222" \ --cap-drop all --cap-add NET_ADMIN,SETUID,SETGID,CHOWN,DAC_OVERRIDE,FOWNER,SYS_CHROOT,KILL,AUDIT_WRITE \ --security-opt no-new-privileges \ -v "$volume:/work" -v "$cfg:/config:ro" "$image" >/dev/null state() { curl -fs "$hub/v1/state"; } node_field() { state | python3 -c "import json,sys; n=[n for n in json.load(sys.stdin)['nodes'] if n['id']=='$node'][0]; print(eval(sys.argv[1]))" "$1"; } echo "waiting for it to register and send a heartbeat (the hub checks every 30 s)" for _ in $(seq 60); do [ "$(node_field 'bool(n.get("host_key")) and n.get("enabled") is True' 2>/dev/null)" = True ] && break sleep 2 done [ "$(node_field 'bool(n.get("host_key")) and n.get("enabled") is True')" = True ] || fail "$node did not register or report" # Its checkout: the real one is cloned from the hub's git mirror, which this # workspace does not have. An empty repository is enough — the flush fills it. podman exec -u build "$name" git init -q /work/checkout # ─── the agent's worktree and ssh ───────────────────────────────────────────── wt=$e2e/wt-e2e mkdir -p "$wt" cp "$here/fakebot.py" "$here/relay.py" "$wt/" git -C "$wt" init -q git -C "$wt" -c user.name=e2e -c user.email=e2e@example.invalid add . git -C "$wt" -c user.name=e2e -c user.email=e2e@example.invalid commit -qm "a toy bot" "$midas" ssh-config --flush --identity "$e2e/agent_key" --known-hosts "$e2e/known_hosts" --write "$e2e/ssh_config" 2>/dev/null # Its shared-connection sockets here, not in the real ~/.ssh. sed -i "s|ControlPath ~/.ssh/cm-%C|ControlPath $e2e/cm-%C|" "$e2e/ssh_config" printf '#!/bin/sh\nexec %s -F %s "$@"\n' "$(command -v ssh)" "$e2e/ssh_config" > "$e2e/bin/ssh" chmod +x "$e2e/bin/ssh" export PATH=$e2e/bin:$PATH cd "$wt" relay='python3 relay.py /tmp/bot/mcp.sock' # ─── a node taken for building refuses a bot ────────────────────────────────── step "a build lease refuses a bot" "$midas" take --node "$node" --wait 0 --purpose e2e-build > "$e2e/out" 2>"$e2e/err" || fail "take: $(cat "$e2e/err")" [ "$(cat "$e2e/out")" = "$node" ] || fail "take printed $(cat "$e2e/out")" if ssh "$node" "midas node bot start --mcp 'true' -- sleep 60" 2>"$e2e/err"; then fail "a bot started under a build lease"; fi grep -q "for building, not for a bot" "$e2e/err" || fail "refused for the wrong reason: $(cat "$e2e/err")" echo "refused: $(grep 'for building' "$e2e/err")" "$midas" release >/dev/null # ─── bot start → mcp → stop ─────────────────────────────────────────────────── step "midas bot start" "$midas" bot start --node "$node" --purpose "e2e bot" --timeout 120 --mcp "$relay" -- \ python3 fakebot.py /tmp/bot/mcp.sock 6 > "$e2e/out" 2> >(tee "$e2e/err" >&2) [ "$(cat "$e2e/out")" = "$node" ] || fail "bot start printed '$(cat "$e2e/out")' on stdout, not the node's name" grep -q "the bot answers MCP" "$e2e/err" || fail "no readiness reported" step "the workspace shows a bot lease" "$midas" status --recent 0 | tee "$e2e/status" grep -q "$node (e2e, e2e): bot, held by wt-e2e — e2e bot" "$e2e/status" || fail "status does not show the bot lease" [ "$("$midas" mine | python3 -c 'import json,sys; print(json.load(sys.stdin)["mode"])')" = bot ] || fail "mine: not bot mode" [ "$(state | python3 -c "import json,sys; print(json.load(sys.stdin)['leases']['$node']['mode'])")" = bot ] || fail "status --json: not bot mode" step "midas bot mcp: an MCP session through ssh" python3 "$here/client.py" "$token_sha" -- "$midas" bot mcp step "midas bot status" "$midas" bot status | tee "$e2e/bstatus" grep -q "bot running since" "$e2e/bstatus" || fail "bot status: not running" step "midas bot stop" "$midas" bot stop | tee "$e2e/out" grep -qx "released $node" "$e2e/out" || fail "bot stop did not release" [ "$(state | python3 -c "import json,sys; print('$node' in json.load(sys.stdin)['leases'])")" = False ] || fail "still leased" if podman exec "$name" pgrep -f fakebot.py >/dev/null; then fail "the bot still runs after bot stop"; fi if podman exec "$name" test -e /work/bot/state.json; then fail "the bot's record outlived it"; fi echo "lease released, bot process gone, record removed" # ─── the heartbeat stops a bot whose lease went away ───────────────────────── step "the heartbeat reports a bot, and stops one whose lease is gone" "$midas" bot start --node "$node" --purpose "e2e bot 2" --timeout 120 --mcp "$relay" -- \ python3 fakebot.py /tmp/bot/mcp.sock 1 > /dev/null 2>"$e2e/err" || fail "second bot start: $(cat "$e2e/err")" echo "waiting for a heartbeat that reports it (one a minute)" for _ in $(seq 40); do [ "$(node_field '(n.get("reading") or {}).get("bot") and n["reading"]["bot"]["running"]')" = True ] && break sleep 3 done [ "$(node_field '(n.get("reading") or {}).get("bot") and n["reading"]["bot"]["running"]')" = True ] || fail "no heartbeat reported the bot" "$midas" status --recent 0 | grep "bot up" # Released behind the bot's back: straight to the hub, no ssh to the node. curl -fs -X POST -H "Authorization: Bearer $key" -d '{"owner": "wt-e2e"}' "$hub/v1/release" >/dev/null echo "released at the hub; waiting for the node's heartbeat to stop the bot" for _ in $(seq 40); do podman exec "$name" pgrep -f fakebot.py >/dev/null || break sleep 3 done if podman exec "$name" pgrep -f fakebot.py >/dev/null; then fail "the heartbeat left the bot running"; fi "$midas" log --since 10m --kind bot_started,bot_ready,bot_stopped | tee "$e2e/events" grep -q "bot_stopped.*why=the node is no longer leased" "$e2e/events" || fail "no bot_stopped event for the lapsed lease" grep -q "E2E_TOKEN" "$e2e/events" && ! grep -q "$token" "$e2e/events" || fail "the events name the secret, or carry its value" grep -rq "$token" "$e2e/state" && fail "the secret reached the hub's state or log" # ─── the next holder's handover clears a bot left behind ───────────────────── step "a new holder's first session clears the previous holder's bot" "$midas" bot start --node "$node" --purpose "e2e bot 3" --timeout 120 --mcp "$relay" -- \ python3 fakebot.py /tmp/bot/mcp.sock 1 > /dev/null 2>"$e2e/err" || fail "third bot start: $(cat "$e2e/err")" # Released and taken by somebody else at once, before a heartbeat can notice. curl -fs -X POST -H "Authorization: Bearer $key" -d '{"owner": "wt-e2e"}' "$hub/v1/release" >/dev/null curl -fs -X POST -H "Authorization: Bearer $key" -d "{\"owner\": \"someone-else\", \"node\": \"$node\"}" "$hub/v1/claim" >/dev/null MIDAS_SSH_ANYWAY=1 ssh "$node" true 2>&1 | grep -i "changed hands" || true if podman exec "$name" pgrep -f fakebot.py >/dev/null; then fail "the handover left the bot running"; fi if podman exec "$name" test -e /work/bot; then fail "the handover left /work/bot"; fi "$midas" log --since 2m --kind handover,bot_stopped | tail -2 curl -fs -X POST -H "Authorization: Bearer $key" -d '{"owner": "someone-else"}' "$hub/v1/release" >/dev/null step "all passed" echo "the machine firewall (sudo midas setup) and midas bot setup (root) are not exercised here"