-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathtest-rust
More file actions
executable file
·148 lines (128 loc) · 4.51 KB
/
test-rust
File metadata and controls
executable file
·148 lines (128 loc) · 4.51 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env bash
#
# Build, configure, and run the Oxen test suite.
#
# Usage: test-rust [--ffmpeg] [--keep] [nextest args...]
#
# --ffmpeg Enable the "ffmpeg" cargo feature for build and test commands.
# --keep Do not remove test data (in data/ox and data/test/runs)on cleanup--useful for
# debugging failed tests
# All subsequent arguments are forwarded to `cargo nextest run`.
# Check for --ffmpeg and --keep flags
FEATURE_ARGS=""
KEEP_DATA=false
while true; do
case "${1:-}" in
--ffmpeg) FEATURE_ARGS="-F ffmpeg"; shift ;;
--keep) KEEP_DATA=true; shift ;;
*) break ;;
esac
done
set -euo pipefail
# On macOS, SIP strips DYLD_LIBRARY_PATH from processes launched by system
# binaries (like /usr/bin/env bash), so cargo nextest can't find libpython.
# Re-derive the path from the active Python install if needed.
if [ "$(uname)" = "Darwin" ] && [ -z "${DYLD_LIBRARY_PATH:-}" ] && command -v python3 &>/dev/null; then
_pylib="$(python3 -c 'import sysconfig; print(sysconfig.get_config_var("LIBDIR"))' 2>/dev/null)"
if [ -n "$_pylib" ] && [ -d "$_pylib" ]; then
export DYLD_LIBRARY_PATH="$_pylib"
fi
fi
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
cd "$SCRIPT_DIR/.."
SERVER_PID=""
cleanup() {
if [ -n "$SERVER_PID" ] && kill -0 "$SERVER_PID" 2>/dev/null; then
echo "Stopping oxen-server (pid $SERVER_PID)..."
kill "$SERVER_PID"
# Set a background process to force-kill after 20s if server still hasn't exited
(sleep 20 && kill -9 "$SERVER_PID" 2>/dev/null) &
WATCHDOG=$!
# Wait for server to exit
wait "$SERVER_PID" 2>/dev/null || true
# Ensure the watchdog is killed, so it doesn't hang around when the server exits normally
kill "$WATCHDOG" 2>/dev/null || true
fi
if [ "$KEEP_DATA" = false ]; then
echo "==> Removing test data..."
rm -rf ./data/ox
rm -rf ./data/test/runs
if [ -n "${OXEN_TEST_RUN_DIR:-}" ]; then
rm -rf "$OXEN_TEST_RUN_DIR"
fi
else
echo "==> Keeping test data in data/ox and data/test/runs"
fi
}
trap cleanup EXIT INT TERM
# Ensure all prerequisites are installed
"$SCRIPT_DIR/install-prereqs"
# Build
echo "==> Building oxen (cargo build $FEATURE_ARGS)..."
# shellcheck disable=SC2086
cargo build $FEATURE_ARGS
# Verify ulimit
CURRENT_ULIMIT=$(ulimit -n)
DESIRED_ULIMIT=10240
if [ "$CURRENT_ULIMIT" -lt "$DESIRED_ULIMIT" ]; then
echo "==> ulimit -n is $CURRENT_ULIMIT (< $DESIRED_ULIMIT). Attempting to raise..."
ulimit -n "$DESIRED_ULIMIT" 2>/dev/null || {
echo "Warning: Could not raise ulimit -n to $DESIRED_ULIMIT. You may need to limit"
echo "the number of threads used by nextest to avoid running out of file descriptors."
}
fi
# Configure test user
if [ ! -d ./data/test/runs ] || [ ! -d ./data/test/config ]; then
echo "==> Configuring test user..."
mkdir -p ./data/test/runs
mkdir -p ./data/test/config
./target/debug/oxen-server add-user \
--email ox@oxen.ai \
--name ox \
--output data/test/config/user_config.toml
else
echo "==> Test user already configured, skipping."
fi
# Select port and start oxen-server
port_is_free() {
! lsof -i :"$1" -sTCP:LISTEN >/dev/null 2>&1
}
if [ -n "${OXEN_PORT:-}" ]; then
if ! port_is_free "$OXEN_PORT"; then
echo "ERROR: Something is already listening on port $OXEN_PORT. Stop it before running tests."
exit 1
fi
else
OXEN_PORT=""
for _ in $(seq 1 10); do
CANDIDATE=$(( RANDOM % 3001 + 3100 ))
if port_is_free "$CANDIDATE"; then
OXEN_PORT="$CANDIDATE"
break
fi
done
if [ -z "$OXEN_PORT" ]; then
echo "ERROR: Could not find an open port in range 3000-6000 after 10 attempts."
echo "Please supply OXEN_PORT directly, e.g.: OXEN_PORT=4567 $0 $*"
exit 1
fi
fi
export OXEN_TEST_HOST="localhost:${OXEN_PORT}"
echo "==> Starting oxen-server on port ${OXEN_PORT}..."
./target/debug/oxen-server start -p "${OXEN_PORT}" &
SERVER_PID=$!
wait_for_server() {
while true ; do
if curl -s -o /dev/null --fail "http://localhost:${OXEN_PORT}/api/health" 2>/dev/null; then
return 0
fi
sleep 0.1
done
}
echo "==> Waiting for oxen-server health check to pass..."
wait_for_server
# Run tests
echo "==> Running tests with 'cargo nextest run $FEATURE_ARGS $*' ..."
# shellcheck disable=SC2086
cargo nextest run $FEATURE_ARGS "$@"
# cleanup handled via the trapped cleanup function