about summary refs log tree commit diff
path: root/genenetwork/services/test/run_vm_tests.sh
blob: 7f0261a9a8d53e4638fdf423559af3ac57c36104 (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
#!/bin/bash
# Build the cgit VM disk image, boot it under qemu, wait for the web
# server, and run the HTTP functional test suite.
#
# Usage (from anywhere):
#   test/run_vm_tests.sh [--keep]
#
#   --keep   leave the VM running after the run (default: killed on
#            success, kept on failure for debugging)
#
# Environment overrides:
#   base    factory root (default: derived from this script)
#   BASE_URL   cgit base url (default http://127.0.0.1:8884)
#   SSH_PORT   forwarded ssh port (default 2227)
#   MEM        VM memory in MiB (default 1024)

set -u

HERE="$(cd "$(dirname "$0")" && pwd)"
# Layout note: the default base assumes the factory layout
# (<root>/code/<checkout>).  For a different layout (e.g. bare
# ~/guix/<checkout> on balg01) set the *_DIR variables directly:
#   GUIX_BIOINF, GN_MACHINES, GUIX_FORGE, RUN_GUIX_CONTAINER
base="${base:-$(cd "$HERE/../../../../.." && pwd)}"
export BASE_URL="${BASE_URL:-http://127.0.0.1:8884}"
SSH_PORT="${SSH_PORT:-2227}"
WEB_PORT="${WEB_PORT:-8884}"
MEM="${MEM:-1024}"

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}"
VM_SCM="$GN_MACHINES/vms/cgit-vm.scm"
CLIENT_KEY="$base/code/run-guix-container/etc/hello-container-key"
SSH_BIN="${SSH_BIN:-$(command -v ssh || echo /gnu/store/by018qdrlqk5akqkh7c9l6g1j2nijm2p-openssh-10.3p1/bin/ssh)}"
QEMU="${QEMU:-/gnu/store/5zxjbdd4d5vwln4bsvysbwj5k4j5pqh7-qemu-minimal-10.2.1/bin/qemu-system-x86_64}"

KEEP=0
[ "${1:-}" = "--keep" ] && KEEP=1

die() { echo "ERROR: $*" >&2; exit 1; }

# --- 1. build the VM image ---------------------------------------------
echo "== building cgit VM image =="
IMG=$(guix system image -t qcow2 \
      -L "$GUIX_BIOINF" -L "$GN_MACHINES" -L "$GUIX_FORGE" \
      "$VM_SCM" 2>/dev/null | tail -1)
[ -n "$IMG" ] || die "VM image build failed"
echo "built: $IMG"

DISK=/tmp/cgit-vm-test.qcow2
cp "$IMG" "$DISK" && chmod u+w "$DISK"

# --- 2. boot it under qemu ---------------------------------------------
echo "== booting VM (mem ${MEM}MiB, web -> :$WEB_PORT, ssh -> :$SSH_PORT) =="
# Use KVM when available (near-native speed), else fall back to TCG
# software emulation, which works fully unprivileged and just boots
# slower.
if [ -w /dev/kvm ]; then ACCEL="kvm"; else ACCEL="tcg,thread=multi"; fi

"$QEMU" -m "$MEM" \
        -drive file="$DISK",format=qcow2 \
        -netdev user,id=n0,hostfwd=tcp:127.0.0.1:${WEB_PORT}-:8883,hostfwd=tcp:127.0.0.1:${SSH_PORT}-:22 \
        -device virtio-net-pci,netdev=n0 \
        -nographic -accel "$ACCEL" \
        > /tmp/cgit-vm-qemu.log 2>&1 &
QEMU_PID=$!

harvest_logs() {
    local DEST="$HERE/logs/$(date +%Y%m%d-%H%M%S)-vm"
    mkdir -p "$DEST"
    echo "== harvesting VM logs into $DEST =="
    cp /tmp/cgit-vm-qemu.log "$DEST/qemu-console.log" 2>/dev/null
    "$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/nginx/*.log /var/log/fcgiwrap/*.log \
                  /var/log/anubis.log /var/log/cgit-seed.log /srv/git/seed-log.txt; \
         do echo "===== $f"; cat "$f" 2>/dev/null; done' \
        > "$DEST/varlog.txt" 2>/dev/null || echo "(log harvest failed; VM unreachable?)"
    echo "logs saved: $DEST"
}

cleanup() {
    harvest_logs
    kill "$QEMU_PID" 2>/dev/null
    if [ "$KEEP" = 1 ] || [ "${FAILED:-0}" -gt 0 ]; then
        echo "VM left running (pid $QEMU_PID; inspect via ssh -p $SSH_PORT
  -i $CLIENT_KEY root@127.0.0.1 or $BASE_URL)"
    fi
    rm -f "$DISK"
}
trap cleanup EXIT

# --- 3. wait for the web server ----------------------------------------
echo "== waiting for the web server =="
UP=0
for i in $(seq 1 60); 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 5
done
[ "$UP" = 1 ] || die "web server did not come up on $BASE_URL (qemu log: /tmp/cgit-vm-qemu.log)"
echo "web server is up"

# --- 4. run the tests ---------------------------------------------------
echo "== HTTP functional tests =="
python3 "$HERE/cgit_http_tests.py"
TEST_RC=$?

echo "== ssh smoke test =="
"$SSH_BIN" -p "$SSH_PORT" -o StrictHostKeyChecking=no \
    -o UserKnownHostsFile=/dev/null -i "$CLIENT_KEY" -o ConnectTimeout=10 \
    root@127.0.0.1 'echo SSH-OK; ls /srv/git; /run/current-system/profile/bin/herd status nginx 2>&1 | head -3; /run/current-system/profile/bin/herd status fcgiwrap 2>&1 | head -3'
SSH_RC=$?

echo
if [ "$TEST_RC" = 0 ] && [ "$SSH_RC" = 0 ]; then
    echo "== ALL VM TESTS PASSED =="
    FAILED=0
    exit 0
else
    echo "== VM TESTS FAILED (tests=$TEST_RC ssh=$SSH_RC) =="
    FAILED=1
    exit 1
fi