SPB Git

Repository Public

local-stack.sh 4.6 KB 65 lines shellscript

1#!/bin/bash2# End-to-end smoke test with the REAL binaries on this machine (development only):3#   real agent → control-api relay (signed requests) → telemetry-ingest → PostgreSQL → read API,4#   then a signed action: API → orchestrator → ingest → agent → result, and an idempotent replay.5# Uses a throwaway database and ports 8770-8775; everything is stopped and dropped at the end.6set -euo pipefail7ROOT="$(cd "$(dirname "$0")/../.." && pwd)"; W="$(mktemp -d /tmp/mcc-e2e.XXXXXX)"; DB="mcc_e2e_$RANDOM"8cleanup() { for f in "$W"/*.pid; do [ -f "$f" ] && kill "$(cat "$f")" 2>/dev/null || true; done; sleep 19  psql -q -d postgres -c "drop database if exists $DB with (force)" >/dev/null 2>&1 || true; [ "${KEEP:-0}" = 1 ] || rm -rf "$W"; }10trap cleanup EXIT11( cd "$ROOT/services" && for b in control-api telemetry-ingest action-orchestrator mcc-agent mcc-devlogin; do go build -o "$W/$b" ./cmd/$b; done )12psql -q -d postgres -c "create database $DB"13export MCC_ENV=dev MCC_DATABASE_URL="postgres://127.0.0.1/$DB?sslmode=disable" MCC_SECRETS_DIR="$W/secrets" \14  MCC_API_LISTEN=127.0.0.1:8770 MCC_PUBLIC_ORIGIN=http://localhost:8770 MCC_RP_ID=localhost MCC_WEB_UPSTREAM= \15  MCC_INGEST_LISTEN=127.0.0.1:8771 MCC_INGEST_SIGNED_LISTEN=127.0.0.1:8774 MCC_INGEST_HEALTH_LISTEN=127.0.0.1:8775 \16  MCC_INGEST_ADVERTISE=http://localhost:8770 MCC_ORCHESTRATOR_LISTEN=127.0.0.1:877217cd "$W"18./control-api > api.log 2>&1 & echo $! > api.pid; sleep 219./telemetry-ingest serve > ingest.log 2>&1 & echo $! > ingest.pid20./action-orchestrator > orch.log 2>&1 & echo $! > orch.pid; sleep 321TOKEN=$(./telemetry-ingest dev-token --alias e2e-node | tail -1)22FP=$(curl -fsS localhost:8775/healthz | python3 -c "import sys,json;print(json.load(sys.stdin)['ca_fingerprint'])")23./mcc-agent enroll --endpoints http://localhost:8770 --token "$TOKEN" --ca-fingerprint "$FP" --state-dir "$W/agent" >/dev/null24./mcc-agent run --state-dir "$W/agent" > agent.log 2>&1 & echo $! > agent.pid25sleep 2526AT=$(./mcc-devlogin); H="Authorization: Bearer $AT"; B=http://localhost:8770/api/v127python3 - "$B" "$AT" <<'PY'28import json, sys, time, urllib.request29base, tok = sys.argv[1], sys.argv[2]30def call(method, path, body=None, headers=None):31    req = urllib.request.Request(base + path, method=method, data=json.dumps(body).encode() if body is not None else None,32                                 headers={"Authorization": "Bearer " + tok, "Content-Type": "application/json", **(headers or {})})33    with urllib.request.urlopen(req, timeout=20) as r:34        return r.status, json.loads(r.read() or b"{}")35def check(cond, msg):36    print(("ok   " if cond else "FAIL ") + msg)37    if not cond: sys.exit(1)38_, nodes = call("GET", "/nodes")39n = nodes["items"][0]40check(n["freshness"] == "fresh" and n["observed_at"].endswith("Z") and n.get("chip"), f"node {n['alias']} is fresh with real hardware ({n.get('chip')})")41_, d = call("GET", f"/nodes/{n['id']}")42latest = d["latest"]43check(latest["cpu.usage"]["availability"] == "available" and 0 <= latest["cpu.usage"]["value"] <= 100, "cpu.usage measured")44check(latest["thermal.temp"]["availability"] in ("unsupported", "permission_denied") and "value" not in latest["thermal.temp"], "thermal.temp declared, not invented")45_, m = call("GET", f"/nodes/{n['id']}/metrics?metrics=cpu.usage,net.rx")46check(any(s["metric"] == "net.rx" and s["label"] and s["points"] for s in m["series"]), "labeled time series returned")47_, ov = call("GET", "/cluster/overview")48check(ov["capacity"]["cpu_cores"]["coverage"] == 1, "overview coverage counts the reporting node")49body = {"type": "diagnostic.run", "target": {"kind": "node", "id": n["id"]}, "params": {"profile": "basic"}}50st, a = call("POST", "/actions", body, {"Idempotency-Key": "e2e-smoke-0000000001"})51check(st == 201 and a["state"] in ("queued", "dispatched", "running", "succeeded"), "action accepted")52for _ in range(20):53    _, det = call("GET", f"/actions/{a['id']}")54    if det["action"]["state"] in ("succeeded", "failed", "timed_out"): break55    time.sleep(1)56check(det["action"]["state"] == "succeeded" and det["action"]["result"]["outcome_unknown"] is False, "action executed by the real agent")57_, out = call("GET", f"/actions/{a['id']}/output")58check(len(out["items"]) > 5, f"output streamed back ({len(out['items'])} lines)")59st2, a2 = call("POST", "/actions", body, {"Idempotency-Key": "e2e-smoke-0000000001"})60check(st2 == 200 and a2["id"] == a["id"], "idempotent replay returns the same action")61PY62N=$(psql -At -d "$DB" -c "select count(*) from agent_commands")63[ "$N" = 1 ] && echo "ok   exactly one command issued" || { echo "FAIL $N commands"; exit 1; }64echo "E2E OK"65
Files main
Menu

Theme