about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPjotr <wrk@bosa.lan>2026-10-14 07:17:02 +0000
committerPjotr <wrk@bosa.lan>2026-10-14 07:17:02 +0000
commitb653bd0bcc73ea1ec6cce0adf2ad78f86618ecb1 (patch)
treeca7a9f7f47e9767a1791cd4e12b2f93671e7091d
parenta4a75e2b78882de0cae397960f2383306dd6c3c7 (diff)
downloadgn-machines-b653bd0bcc73ea1ec6cce0adf2ad78f86618ecb1.tar.gz
julia-notebook: run Pkg offline; add the container tests
Set JULIA_PKG_OFFLINE=true for the notebook server: the packaged
dependencies are already on the read-only JULIA_LOAD_PATH, and letting
Pkg hit the registry crashed the server mid-run (a libcurl
"curlmultiassign" error).  Notebooks whose packages are not packaged now
fail cleanly instead of taking the server down.

Add julia_notebook_http_tests.py -- the index is served, the notebooks
are listed, and the power notebook runs and is served as power.html --
and run_julia_notebook_container_tests.sh to drive the
build/start/test/log-harvest cycle.
-rw-r--r--genenetwork/services/julia-notebook.scm7
-rw-r--r--genenetwork/services/test/julia_notebook_http_tests.py105
-rwxr-xr-xgenenetwork/services/test/run_julia_notebook_container_tests.sh129
3 files changed, 241 insertions, 0 deletions
diff --git a/genenetwork/services/julia-notebook.scm b/genenetwork/services/julia-notebook.scm
index 224cd59..59bdba5 100644
--- a/genenetwork/services/julia-notebook.scm
+++ b/genenetwork/services/julia-notebook.scm
@@ -125,6 +125,13 @@
                                (string-append %julia-notebook-datadir
                                               "/.julia")))
                        (string-append "PLUTO_PORT=" #$port)
+                       ;; The notebook dependencies that are packaged live
+                       ;; on the (read-only) JULIA_LOAD_PATH; don't let Pkg
+                       ;; hit the registry for them.  This also avoids a
+                       ;; libcurl crash (curlmultiassign) that killed the
+                       ;; server mid-run.  Notebooks needing unpackaged
+                       ;; packages then fail rather than download.
+                       "JULIA_PKG_OFFLINE=true"
                        ;; Julia's Downloads uses libcurl, which reads
                        ;; CURL_CA_BUNDLE/SSL_CERT_FILE; point all of them
                        ;; at the concatenated nss-certs bundle.
diff --git a/genenetwork/services/test/julia_notebook_http_tests.py b/genenetwork/services/test/julia_notebook_http_tests.py
new file mode 100644
index 0000000..b3f4d6f
--- /dev/null
+++ b/genenetwork/services/test/julia_notebook_http_tests.py
@@ -0,0 +1,105 @@
+#!/usr/bin/env python3
+"""HTTP tests for the julia-notebook (PlutoSliderServer) container.
+
+Checks that the server is up, lists the bundled notebooks, and actually
+serves the 'power' notebook -- the one that uses the packaged (Plots,
+Distributions, ...) Julia environment, so it runs offline from the shared
+JULIA_LOAD_PATH.
+
+Usage:
+    BASE_URL=http://127.0.0.1:8890 python3 julia_notebook_http_tests.py
+"""
+
+import json
+import os
+import time
+import urllib.error
+import urllib.request
+
+BASE_URL = os.environ.get("BASE_URL", "http://127.0.0.1:8890")
+# How long to wait for a notebook to finish loading/running.
+TIMEOUT = int(os.environ.get("NOTEBOOK_TIMEOUT", "600"))
+
+TESTS = []
+
+
+def test(name):
+    def wrap(fn):
+        TESTS.append((name, fn))
+        return fn
+    return wrap
+
+
+def get(path, timeout=30):
+    req = urllib.request.Request(BASE_URL + path)
+    try:
+        with urllib.request.urlopen(req, timeout=timeout) as r:
+            return r.status, r.read().decode("utf-8", "replace")
+    except urllib.error.HTTPError as e:
+        return e.code, e.read().decode("utf-8", "replace")
+
+
+def index_json(timeout=30):
+    status, body = get("/pluto_export.json", timeout=timeout)
+    if status != 200:
+        return None
+    return json.loads(body)
+
+
+@test("t01 index page is served and lists notebooks")
+def t01():
+    status, body = get("/")
+    return (status == 200
+            and "Notebooks" in body
+            and "power" in body)
+
+
+@test("t02 pluto_export.json lists the bundled notebooks")
+def t02():
+    d = index_json()
+    if d is None:
+        return False
+    names = set(d.get("notebooks", {}))
+    return {"power.jl", "roc.jl", "gn-smoke-test.jl"} <= names
+
+
+@test("t03 the power notebook runs and is served")
+def t03():
+    # Wait for PlutoSliderServer to finish running the power notebook (it
+    # uses only the packaged Julia environment, so it needs no network).
+    deadline = time.time() + TIMEOUT
+    while time.time() < deadline:
+        d = index_json()
+        if d:
+            nb = d.get("notebooks", {}).get("power.jl")
+            if nb and nb.get("current_hash"):
+                break
+        time.sleep(5)
+    else:
+        return False
+
+    # It is exposed as a static export, <name>.html.
+    status, body = get("/power.html", timeout=60)
+    return status == 200 and len(body) > 1000
+
+
+def main():
+    passed = failed = 0
+    for name, fn in TESTS:
+        try:
+            ok = fn()
+        except Exception as e:  # noqa: BLE001
+            ok = False
+            print(f"FAIL {name}: exception {e!r}")
+        if ok:
+            passed += 1
+            print(f"PASS {name}")
+        else:
+            failed += 1
+            print(f"FAIL {name}")
+    print(f"== {passed} passed, {failed} failed ==")
+    return 1 if failed else 0
+
+
+if __name__ == "__main__":
+    raise SystemExit(main())
diff --git a/genenetwork/services/test/run_julia_notebook_container_tests.sh b/genenetwork/services/test/run_julia_notebook_container_tests.sh
new file mode 100755
index 0000000..068253a
--- /dev/null
+++ b/genenetwork/services/test/run_julia_notebook_container_tests.sh
@@ -0,0 +1,129 @@
+#!/bin/bash
+# Build the julia-notebook (PlutoSliderServer) system container, start it
+# through the run-guix-system-container daemon, wait for the web server,
+# and run the HTTP functional test suite (which includes the power
+# notebook test).
+#
+# Usage:
+#   test/run_julia_notebook_container_tests.sh [--keep]
+#
+# Environment overrides:
+#   base                checkout root (derived from this script)
+#   DAEMON              daemon address (default 127.0.0.1:8900)
+#   DAEMON_FINGERPRINT  daemon fingerprint (auto-detected by default)
+#   BASE_URL           PlutoSliderServer base url (default http://127.0.0.1:8890)
+#   SSH_PORT           container ssh port (default 2225)
+#   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:8900}"
+
+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/julia-notebook-container.scm"
+HTTP_TESTS="$HERE/julia_notebook_http_tests.py"
+RUN_GUIX_CONTAINER="${RUN_GUIX_CONTAINER:-$base/code/run-guix-container/bin/run-guix-container.rb}"
+CLIENT_KEY="${CLIENT_KEY:-$HOME/hello-container-key}"
+SSH_PORT="${SSH_PORT:-2225}"
+SSH_BIN="${SSH_BIN:-$(command -v ssh || echo /gnu/store/by018qdrlqk5akqkh7c9l6g1j2nijm2p-openssh-10.3p1/bin/ssh)}"
+export BASE_URL="${BASE_URL:-http://127.0.0.1:8890}"
+
+KEEP=0
+[ "${1:-}" = "--keep" ] && KEEP=1
+
+die() { echo "ERROR: $*" >&2; exit 1; }
+
+client() { "$RUN_GUIX_CONTAINER" --daemon "$DAEMON" --fingerprint "$DAEMON_FINGERPRINT" "$@"; }
+
+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"
+
+harvest_logs() {
+    local DEST="$HERE/logs/$(date +%Y%m%d-%H%M%S)-julia-notebook"
+    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/julia-notebook.log; do \
+         echo "===== $f"; cat "$f" 2>/dev/null; done; true' \
+        > "$DEST/varlog.txt" 2>/dev/null || echo "(log harvest failed)"
+    client --log "$HASH" > "$DEST/boot.log" 2>/dev/null || true
+    echo "logs saved: $DEST"
+}
+
+echo "== stopping any running container =="
+for pid in $(client --list 2>/dev/null | awk '$1 ~ /^[0-9]+$/ {print $1}'); do
+    client --kill "$pid" >/dev/null 2>&1
+done
+sleep 2
+
+echo "== building julia-notebook 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"
+
+echo "== starting container =="
+OUT=$(client "$HASH" 2>&1) || true
+PID=$(echo "$OUT" | awk '/^OK/ {print $2}')
+[ -n "$PID" ] || die "could not start container: $OUT"
+echo "running as PID $PID"
+
+cleanup() {
+    harvest_logs
+    if [ "$KEEP" = 1 ] || [ "${FAILED:-0}" -gt 0 ]; then
+        echo "container $PID left running"
+    else
+        echo "== stopping container =="
+        client --kill "$PID" >/dev/null 2>&1
+    fi
+}
+trap cleanup EXIT
+
+echo "== waiting for the web server =="
+UP=0
+for i in $(seq 1 40); 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 ] && echo "server is up" || echo "WARNING: server not up yet"
+
+echo "== HTTP functional tests (incl. power notebook) =="
+python3 "$HTTP_TESTS"
+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 'export PATH=/run/current-system/profile/bin:$PATH
+for tool in nginx herd; do command -v "$tool" >/dev/null && echo "OK  $tool"; done
+herd status julia-notebook 2>&1 | head -2'
+TOOLS_RC=$?
+
+echo
+if [ "$TEST_RC" = 0 ] && [ "$TOOLS_RC" = 0 ]; then
+    echo "== ALL JULIA-NOTEBOOK CONTAINER TESTS PASSED =="
+    FAILED=0
+    exit 0
+else
+    echo "== JULIA-NOTEBOOK CONTAINER TESTS FAILED (tests=$TEST_RC tools=$TOOLS_RC) =="
+    FAILED=1
+    exit 1
+fi