#!/bin/bash # Build the GeneCup system container, start it through the # run-guix-system-container daemon and check the Gemini API key at the # place the app expects it. # # The app (server.py) reads its key from # ~/.config/gemini/credentials of the genecup user and *exits* when the # mode is not 0400; when the file is missing it prints # Warning: ~/.config/gemini/credentials not found or empty. # Stress classification via Gemini will not work. # and keeps running. In the container the host's key is exposed # read-only (--expose=...) and copied into place by the genecup # activation, so this test checks the whole chain at boot: # # 1. the key file exists, is owned by genecup and is mode 0400 # 2. the one-shot 'gemini-check' service reports whether the API # accepts it ("Gemini classification available" / # "Gemini classification not available: ") # 3. the app itself reports GEMINI_API_KEY=set in its own log # # Usage (from anywhere): # test/run_genecup_container_tests.sh [--keep] [--expect-missing] # # --keep leave the container running after the run # --expect-missing invert the credential assertions: expect no key # exposed, i.e. "not available: ... not found" and # GEMINI_API_KEY=(not set) (the unkeyed container) # # Environment overrides: # base checkout root (default: derived from this script; set to # a directory holding the checkouts side by side) # DAEMON daemon address (default 127.0.0.1:8989) # DAEMON_FINGERPRINT daemon fingerprint (default: auto-detected) # GEMINI_KEY host key file to expose # (default: $HOME/.config/gemini/credentials) # KEY_IN_CONTAINER where the launcher exposes it # (default: /run/secrets/gemini-credentials, what # containers/genecup-container.scm expects) # SSH_PORT container ssh port (default 2223) # CLIENT_KEY ssh client key (default $HOME/hello-container-key) set -u HERE="$(cd "$(dirname "$0")" && pwd)" base="${base:-$(cd "$HERE/../../../../.." && pwd)}" DAEMON="${DAEMON:-127.0.0.1:8989}" die() { echo "ERROR: $*" >&2; exit 1; } # Checkout locations: the factory layout puts them under /code, # a flat host layout (e.g. balg01 ~/services/genecup-service) has them # side by side. Auto-detect, and let the *_DIR variables override. first_dir() { for d in "$@"; do [ -e "$d" ] && echo "$d" && return 0; done return 1 } GUIX_BIOINF="${GUIX_BIOINF:-$(first_dir "$base/code/guix-bioinformatics" "$base/guix-bioinformatics")}" GN_MACHINES="${GN_MACHINES:-$(first_dir "$base/code/gn-machines" "$base/gn-machines")}" # -L needs the directory that contains forge/ (the repo root's guix/ # sub-checkout on factory, the checkout itself on a flat layout). GUIX_FORGE="${GUIX_FORGE:-$(first_dir "$base/code/contrib/guix-forge/guix" \ "$base/contrib/guix-forge/guix" \ "$base/guix-forge/guix" \ "$base/guix-forge")}" [ -n "$GUIX_BIOINF" ] || die "cannot find guix-bioinformatics (set GUIX_BIOINF)" [ -n "$GN_MACHINES" ] || die "cannot find gn-machines (set GN_MACHINES)" [ -n "$GUIX_FORGE" ] || die "cannot find the guix-forge modules (set GUIX_FORGE to the directory holding forge/)" CONTAINER_SCM="$GN_MACHINES/genenetwork/containers/genecup-container.scm" RUN_GUIX_CONTAINER="${RUN_GUIX_CONTAINER:-}" [ -n "$RUN_GUIX_CONTAINER" ] || RUN_GUIX_CONTAINER=$(first_dir "$base/code/run-guix-container/bin/run-guix-container.rb" "$base/run-guix-container/bin/run-guix-container.rb") \ || die "cannot find run-guix-container.rb (set RUN_GUIX_CONTAINER)" CLIENT_KEY="${CLIENT_KEY:-$HOME/hello-container-key}" SSH_PORT="${SSH_PORT:-2223}" SSH_BIN="${SSH_BIN:-$(command -v ssh || echo /gnu/store/by018qdrlqk5akqkh7c9l6g1j2nijm2p-openssh-10.3p1/bin/ssh)}" GEMINI_KEY="${GEMINI_KEY:-$HOME/.config/gemini/credentials}" KEY_IN_CONTAINER="${KEY_IN_CONTAINER:-/run/secrets/gemini-credentials}" EXPECT_MISSING=0 KEEP=0 for arg in "$@"; do case "$arg" in --keep) KEEP=1;; --expect-missing) EXPECT_MISSING=1;; *) echo "unknown option: $arg" >&2; exit 2;; esac done client() { "$RUN_GUIX_CONTAINER" --daemon "$DAEMON" --fingerprint "$DAEMON_FINGERPRINT" "$@"; } # The client verifies the daemon's fingerprint before every command; the # daemon generates a random one unless --fingerprint was given, so ask. detect_fingerprint() { python3 - "$DAEMON" <<'PYEOF' import socket, sys host, port = sys.argv[1].rsplit(":", 1) s = socket.create_connection((host, int(port)), 5) s.sendall(b"FINGERPRINT\n") print(s.recv(4096).decode().strip().split()[1]) PYEOF } DAEMON_FINGERPRINT="${DAEMON_FINGERPRINT:-$(detect_fingerprint)}" [ -n "$DAEMON_FINGERPRINT" ] || die "could not detect daemon fingerprint" echo "== daemon fingerprint: $DAEMON_FINGERPRINT" # Container logs, over ssh, before it is killed. harvest_logs() { local DEST="$HERE/logs/$(date +%Y%m%d-%H%M%S)-genecup" mkdir -p "$DEST" echo "== harvesting container logs into $DEST ==" "$SSH_BIN" -p "$SSH_PORT" -o StrictHostKeyChecking=no \ -o UserKnownHostsFile=/dev/null -o ConnectTimeout=10 \ -i "$CLIENT_KEY" root@127.0.0.1 \ 'for f in /var/log/messages /var/log/genecup.log /var/log/gemini-check.log \ /var/log/gemini-check.json /var/log/anubis.log /var/log/nginx/error.log; \ do echo "===== $f"; cat "$f" 2>/dev/null; done; echo "===== credentials"; ls -l /var/lib/genecup/.config/gemini/ 2>&1; true' \ > "$DEST/varlog.txt" 2>/dev/null || echo "(log harvest failed; container unreachable?)" client --log "$HASH" > "$DEST/boot.log" 2>/dev/null || true echo "logs saved: $DEST" } # --- 1. stop any container the daemon is currently running ------------ echo "== stopping any running container ==" for pid in $(client --list 2>/dev/null | awk '$1 ~ /^[0-9]+$/ {print $1}'); do echo "killing $pid" client --kill "$pid" >/dev/null 2>&1 done sleep 2 # --- 2. build with the key exposed ------------------------------------ if [ "$EXPECT_MISSING" = 0 ] && [ ! -f "$GEMINI_KEY" ]; then # The key is mounted by the daemon on the *host*, so a sandboxed # runner may legitimately not see it; the container-side checks # below then report what actually happened. echo "WARNING: $GEMINI_KEY is not visible here; the container-side" >&2 echo " checks will say whether the daemon could expose it" >&2 fi echo "== building genecup container (exposing $GEMINI_KEY) ==" HASH=$(guix system container --network \ --expose="$GEMINI_KEY=$KEY_IN_CONTAINER" \ -L "$GUIX_BIOINF" -L "$GN_MACHINES" -L "$GUIX_FORGE" \ "$CONTAINER_SCM" 2>/dev/null | tail -1) [ -n "$HASH" ] || die "container build failed" echo "built: $HASH" # --- 3. start it via the daemon --------------------------------------- echo "== starting container ==" OUT=$(client "$HASH" 2>&1) || true PID=$(echo "$OUT" | awk '/^OK/ {print $2}') if [ -z "$PID" ]; then for pid in $(client --list 2>/dev/null | awk '$1 ~ /^[0-9]+$/ {print $1}'); do client --kill "$pid" >/dev/null 2>&1 done sleep 3 OUT=$(client "$HASH" 2>&1) || true PID=$(echo "$OUT" | awk '/^OK/ {print $2}') fi [ -n "$PID" ] || die "could not start container: $OUT" echo "running as PID $PID" FAILED=0 cleanup() { harvest_logs if [ "$KEEP" = 1 ] || [ "$FAILED" -gt 0 ]; then echo "container $PID left running (ssh -p $SSH_PORT -i $CLIENT_KEY root@127.0.0.1)" else echo "== stopping container ==" client --kill "$PID" >/dev/null 2>&1 fi } trap cleanup EXIT # --- 4. wait for the container's sshd --------------------------------- echo "== waiting for sshd on port $SSH_PORT ==" UP=0 for i in $(seq 1 30); do if "$SSH_BIN" -p "$SSH_PORT" -o StrictHostKeyChecking=no \ -o UserKnownHostsFile=/dev/null -o BatchMode=yes \ -o ConnectTimeout=5 -i "$CLIENT_KEY" root@127.0.0.1 true 2>/dev/null; then UP=1; break fi sleep 4 done [ "$UP" = 1 ] || die "container sshd never came up" # --- 5. the credential chain inside the container --------------------- echo "== checking the Gemini key inside the container ==" REPORT=$("$SSH_BIN" -p "$SSH_PORT" -o StrictHostKeyChecking=no \ -o UserKnownHostsFile=/dev/null -o BatchMode=yes -i "$CLIENT_KEY" \ root@127.0.0.1 'set -u echo "--- credential file:" ls -l /var/lib/genecup/.config/gemini/credentials 2>&1 echo "--- gemini-check log:" cat /var/log/gemini-check.log 2>&1 echo "--- gemini-check service:" /run/current-system/profile/bin/herd status gemini-check 2>&1 | head -3 echo "--- app log:" grep -m1 "GEMINI_API_KEY" /var/log/genecup.log 2>/dev/null || echo "(no GEMINI_API_KEY line in /var/log/genecup.log yet)" echo "--- genecup service:" /run/current-system/profile/bin/herd status genecup 2>&1 | head -3' 2>&1) echo "$REPORT" FAILED=0 check() { # name pattern if echo "$REPORT" | grep -q -- "$2"; then echo "PASS $1" else echo "FAIL $1 -- expected /$2/" FAILED=1 fi } check_not() { if echo "$REPORT" | grep -q -- "$2"; then echo "FAIL $1 -- found /$2/" FAILED=1 else echo "PASS $1" fi } if [ "$EXPECT_MISSING" = 1 ]; then check "no key file in the container" "credentials.*No such file" check "check service says not available" \ "Gemini classification not available: .*credentials not found" check "app did not load a key" "GEMINI_API_KEY=(not set)" else check "key file is genecup-owned and mode 0400" \ "^-r-------- [0-9][0-9]* genecup *genecup .*credentials$" check "check service says the API answers the model" \ "Gemini classification available: key in .* answered (HTTP 200)" check "app loaded the key" "GEMINI_API_KEY=set" # The app exits with SystemExit(1) on a non-0400 mode and the # service then fails to start: catch that too. check "genecup service is up" "It is running" fi # --- 6. sanity: the app is serving ------------------------------------ echo "== app sanity ==" CODE=000 for i in $(seq 1 20); do CODE=$(curl -s -m 10 -o /dev/null -w '%{http_code}' http://127.0.0.1:4202/ 2>/dev/null) [ "$CODE" = 200 ] && break sleep 2 done if [ "$CODE" = 200 ]; then echo "PASS Flask app answers on 4202 (HTTP 200)" else echo "FAIL Flask app on 4202 answered '$CODE'" FAILED=1 fi echo if [ "$FAILED" = 0 ]; then echo "== ALL GENECUP CONTAINER TESTS PASSED ==" exit 0 else echo "== GENECUP CONTAINER TESTS FAILED ==" exit 1 fi