about summary refs log tree commit diff
path: root/genenetwork/services/test/run_julia_notebook_container_tests.sh
blob: 068253a5860a10dde352bdd4bc8185ea7d4a0085 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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