omnet
aien-dev/omnet
Brought by drakestapleton
omnet / integration / headscale / leak-check.sh
| 1 | #!/bin/sh |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # Test-only. Checks the DERP memory growth fix (omnet#8) in three steps: |
| 4 | # 1. the built Headscale embeds the tailscale.com version the patch pins; |
| 5 | # 2. upstream's own regression test (TestServerDupClientSendHistoryBounded) |
| 6 | # passes at that version: two connections sharing one node key take turns |
| 7 | # 5000 times and sendHistory stays no longer than the duplicate set; |
| 8 | # 3. negative control: the same test, laid over tailscale.com v1.102.0 (the |
| 9 | # version Headscale v0.29.6 ships), must FAIL. If it passes, this check |
| 10 | # cannot see the leak and the whole check fails. |
| 11 | # Usage: GO=/path/to/go sh integration/headscale/leak-check.sh [build/headscale] |
| 12 | # Needs network access to the Go module proxy the first time (module downloads). |
| 13 | # Removes only the temporary directory it creates. |
| 14 | set -eu |
| 15 | ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/../.." && pwd) |
| 16 | GO=${GO:-go} |
| 17 | HS=${1:-"$ROOT/build/headscale"} |
| 18 | # The module tests run on the toolchain Headscale is pinned to, not whatever |
| 19 | # GOTOOLCHAIN=auto would pick. |
| 20 | . "$ROOT/integration/headscale/headscale.pin" |
| 21 | export GOTOOLCHAIN=$HEADSCALE_GO_TOOLCHAIN GO111MODULE=on GOFLAGS=-mod=mod |
| 22 | TEST=TestServerDupClientSendHistoryBounded |
| 23 | OLD=v1.102.0 |
| 24 | |
| 25 | [ -x "$HS" ] || { printf 'NOT_RUN: %s is not a built headscale\n' "$HS" >&2; exit 77; } |
| 26 | |
| 27 | # The fixed version is read from the patch, never typed here. |
| 28 | FIXED=$(sed -n 's/^+[[:space:]]*tailscale\.com v\(1\.[0-9.]*\)$/v\1/p' \ |
| 29 | "$ROOT/integration/headscale/headscale.patch" | head -n 1) |
| 30 | [ -n "$FIXED" ] || { echo 'FAIL leak-check: no tailscale.com version found in headscale.patch' >&2; exit 1; } |
| 31 | |
| 32 | got=$("$GO" version -m "$HS" | awk '$1 == "dep" && $2 == "tailscale.com" { print $3 }') |
| 33 | [ "$got" = "$FIXED" ] || { |
| 34 | printf 'FAIL leak-check: %s embeds tailscale.com %s, patch pins %s\n' "$HS" "${got:-none}" "$FIXED" >&2 |
| 35 | exit 1 |
| 36 | } |
| 37 | printf 'PASS leak-check: built headscale embeds tailscale.com %s\n' "$FIXED" |
| 38 | |
| 39 | work=$(mktemp -d "${TMPDIR:-/tmp}/omnet-leak-check.XXXXXX") |
| 40 | trap 'rm -rf -- "$work"' EXIT HUP INT TERM |
| 41 | |
| 42 | run_test() { # version, replacement-module-dir-or-empty, log |
| 43 | mod="$work/mod-$1" |
| 44 | mkdir -p "$mod" |
| 45 | printf 'module omnetleakcheck\n\ngo 1.26\n\nrequire tailscale.com %s\n' "$1" > "$mod/go.mod" |
| 46 | [ -z "$2" ] || printf '\nreplace tailscale.com => %s\n' "$2" >> "$mod/go.mod" |
| 47 | printf 'package omnetleakcheck\n\nimport _ "tailscale.com/derp/derpserver"\n' > "$mod/x.go" |
| 48 | (cd "$mod" && "$GO" mod tidy >/dev/null 2>&1) || true |
| 49 | (cd "$mod" && "$GO" test -count=1 -run "^$TEST\$" tailscale.com/derp/derpserver) > "$3" 2>&1 |
| 50 | } |
| 51 | |
| 52 | if run_test "$FIXED" "" "$work/fixed.log" && grep -q '^ok' "$work/fixed.log"; then |
| 53 | printf 'PASS leak-check: %s at tailscale.com %s (sendHistory bounded)\n' "$TEST" "$FIXED" |
| 54 | else |
| 55 | cat "$work/fixed.log" >&2 |
| 56 | printf 'FAIL leak-check: %s at tailscale.com %s\n' "$TEST" "$FIXED" >&2 |
| 57 | exit 1 |
| 58 | fi |
| 59 | |
| 60 | # Negative control: copy the test function out of the fixed module's own test |
| 61 | # file into a private, writable copy of the old module (the module cache is |
| 62 | # read-only and Go refuses overlays inside it), and point a replace at it. |
| 63 | fixed_dir=$("$GO" mod download -json "tailscale.com@$FIXED" | sed -n 's/^[[:space:]]*"Dir": "\(.*\)",$/\1/p') |
| 64 | old_dir=$("$GO" mod download -json "tailscale.com@$OLD" | sed -n 's/^[[:space:]]*"Dir": "\(.*\)",$/\1/p') |
| 65 | [ -d "$fixed_dir" ] && [ -d "$old_dir" ] || { echo 'FAIL leak-check: module download failed' >&2; exit 1; } |
| 66 | cp -R "$old_dir" "$work/old-tailscale" |
| 67 | chmod -R u+w "$work/old-tailscale" |
| 68 | { |
| 69 | printf 'package derpserver\n\nimport (\n\t"testing"\n\n\t"tailscale.com/types/key"\n\t"tailscale.com/types/logger"\n)\n\n' |
| 70 | awk -v t="$TEST" '$0 ~ "^func " t "\\(" { on = 1 } on { print } on && /^}$/ { exit }' \ |
| 71 | "$fixed_dir/derp/derpserver/derpserver_test.go" |
| 72 | } > "$work/old-tailscale/derp/derpserver/omnet_control_test.go" |
| 73 | grep -q "^func $TEST(" "$work/old-tailscale/derp/derpserver/omnet_control_test.go" \ |
| 74 | || { echo 'FAIL leak-check: test not found in fixed module' >&2; exit 1; } |
| 75 | if run_test "$OLD" "$work/old-tailscale" "$work/old.log"; then |
| 76 | cat "$work/old.log" >&2 |
| 77 | printf 'FAIL leak-check: negative control passed at %s; the check cannot see the leak\n' "$OLD" >&2 |
| 78 | exit 1 |
| 79 | fi |
| 80 | if grep -q 'sendHistory len = ' "$work/old.log"; then |
| 81 | printf 'PASS leak-check: negative control FAILS at tailscale.com %s as required (%s)\n' \ |
| 82 | "$OLD" "$(grep -o 'sendHistory len = [0-9]*; want <= [0-9]*' "$work/old.log" | head -n 1)" |
| 83 | else |
| 84 | cat "$work/old.log" >&2 |
| 85 | printf 'FAIL leak-check: negative control at %s failed for another reason\n' "$OLD" >&2 |
| 86 | exit 1 |
| 87 | fi |