#!/bin/bash # Build the BNW system container, start it through the # run-guix-system-container daemon, wait for the web server, and run # the HTTP functional test suite plus the container tool check. # # Usage (from anywhere): # test/run_container_tests.sh [--keep] # # --keep leave the container running after the run (default: # killed on success, kept on failure for debugging) # # 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:8900) # BASE_URL BNW base url (default http://127.0.0.1:8881) set -u HERE="$(cd "$(dirname "$0")" && pwd)" # Layout note: the default base assumes the factory layout # (/code/). For a different layout (e.g. bare # ~/guix/) set the *_DIR variables directly: # GUIX_BIOINF, GN_MACHINES, GUIX_FORGE, RUN_GUIX_CONTAINER base="${base:-$(cd "$HERE/../../.." && pwd)}" DAEMON="${DAEMON:-127.0.0.1:8900}" export BASE_URL="${BASE_URL:-http://127.0.0.1:8881}" GUIX_BIOINF="${GUIX_BIOINF:-$base/code/guix-bioinformatics}" GN_MACHINES="${GN_MACHINES:-$base/code/gn-machines}" GUIX_FORGE="${GUIX_FORGE:-$base/code/contrib/guix-forge/guix}" CONTAINER_SCM="$GN_MACHINES/genenetwork/containers/bnw-container.scm" RUN_GUIX_CONTAINER="${RUN_GUIX_CONTAINER:-$base/code/run-guix-container/bin/run-guix-container.rb}" # The container's admin key lives in the invoking user's $HOME (see # the README; the keys are generated by hand and not in any repo). CLIENT_KEY="${CLIENT_KEY:-$HOME/hello-container-key}" SSH_PORT=2222 SSH_BIN="${SSH_BIN:-$(command -v ssh || echo /gnu/store/by018qdrlqk5akqkh7c9l6g1j2nijm2p-openssh-10.3p1/bin/ssh)}" KEEP=0 [ "${1:-}" = "--keep" ] && KEEP=1 die() { echo "ERROR: $*" >&2; exit 1; } client() { "$RUN_GUIX_CONTAINER" --daemon "$DAEMON" "$@"; } # --- 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 the container ------------------------------------------- echo "== building container ==" HASH=$(guix system container --network \ -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 # a container may still be running (stray); try once more 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" cleanup() { if [ "$KEEP" = 1 ] || [ "${FAILED:-0}" -gt 0 ]; then echo "container $PID left running (inspect via ssh -p $SSH_PORT -i $CLIENT_KEY root@127.0.0.1 or http://127.0.0.1:8881/)" else echo "== stopping container ==" client --kill "$PID" >/dev/null 2>&1 fi } trap cleanup EXIT # --- 4. wait for the web server --------------------------------------- echo "== waiting for the web server ==" UP=0 for i in $(seq 1 30); do code=$(curl -s -m 3 -o /dev/null -w '%{http_code}' "$BASE_URL/" 2>/dev/null) if [ "$code" = 200 ]; then UP=1; break; fi sleep 4 done [ "$UP" = 1 ] || die "web server did not come up on $BASE_URL" echo "web server is up" # --- 5. run the tests --------------------------------------------------- echo "== HTTP functional tests ==" python3 "$HERE/bnw_http_tests.py" TEST_RC=$? echo "== container tool check ==" "$SSH_BIN" -p "$SSH_PORT" -o StrictHostKeyChecking=no \ -o UserKnownHostsFile=/dev/null -i "$CLIENT_KEY" -o ConnectTimeout=10 \ root@127.0.0.1 'bash -s' < "$HERE/check_container_tools.sh" TOOLS_RC=$? echo if [ "$TEST_RC" = 0 ] && [ "$TOOLS_RC" = 0 ]; then echo "== ALL CONTAINER TESTS PASSED ==" FAILED=0 exit 0 else echo "== CONTAINER TESTS FAILED (tests=$TEST_RC tools=$TOOLS_RC) ==" FAILED=1 exit 1 fi