-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathtest.sh
More file actions
executable file
·214 lines (189 loc) · 6.62 KB
/
Copy pathtest.sh
File metadata and controls
executable file
·214 lines (189 loc) · 6.62 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "$0")" && pwd)"
cd "$ROOT_DIR"
# Git exports repository-local variables while invoking hooks. They must not
# leak into pytest: tests that create temporary repositories would otherwise
# keep operating on the outer worktree. The runner is already anchored at the
# backend directory, so normal Git discovery remains available after scrubbing.
while IFS= read -r git_env_name; do
[[ -n "$git_env_name" ]] && unset "$git_env_name"
done < <(git rev-parse --local-env-vars 2>/dev/null || true)
PYTHON_BIN="${PYTHON:-}"
if [[ -z "$PYTHON_BIN" ]]; then
if [[ -x ".venv/bin/python" ]]; then
PYTHON_BIN=".venv/bin/python"
else
PYTHON_BIN="python3"
fi
fi
export ENCRYPTION_SECRET="omi_ZwB2ZNqB2HHpMK6wStk7sTpavJiPTFg7gXUHnc4tFABPU6pZ2c2DKgehtfgi4RZv"
export OPENAI_API_KEY="test-openai-key-not-real"
export BACKEND_PYTEST_TIMING_SUMMARY="${BACKEND_PYTEST_TIMING_SUMMARY:-1}"
export BACKEND_FAST_UNIT_WARN_SECONDS="${BACKEND_FAST_UNIT_WARN_SECONDS:-0.1}"
export BACKEND_FAST_UNIT_FAIL_SECONDS="${BACKEND_FAST_UNIT_FAIL_SECONDS:-0.12}"
# The file-isolated runner already parallelizes pytest processes. Letting each process
# start a native BLAS/OpenMP pool oversubscribes the machine and makes process CPU time
# depend on which test first initializes NumPy. Keep one native worker per pytest process;
# callers can still override a setting when intentionally exercising parallel kernels.
export OMP_NUM_THREADS="${OMP_NUM_THREADS:-1}"
export OPENBLAS_NUM_THREADS="${OPENBLAS_NUM_THREADS:-1}"
export MKL_NUM_THREADS="${MKL_NUM_THREADS:-1}"
export VECLIB_MAXIMUM_THREADS="${VECLIB_MAXIMUM_THREADS:-1}"
export NUMEXPR_NUM_THREADS="${NUMEXPR_NUM_THREADS:-1}"
export BLIS_NUM_THREADS="${BLIS_NUM_THREADS:-1}"
pytest_args=(-v)
marker_expr="${BACKEND_PYTEST_MARK_EXPR:-not integration and not slow}"
if [[ -n "$marker_expr" ]]; then
pytest_args+=(-m "$marker_expr")
fi
use_file_isolation="${BACKEND_PYTEST_FILE_ISOLATION:-1}"
if [[ "$use_file_isolation" != "1" && "$use_file_isolation" != "true" && "${BACKEND_PYTEST_XDIST:-auto}" != "0" && "${BACKEND_PYTEST_XDIST:-auto}" != "false" ]]; then
if "$PYTHON_BIN" -c "import xdist" >/dev/null 2>&1; then
workers="${BACKEND_PYTEST_WORKERS:-auto}"
pytest_args+=(-n "$workers" --dist=loadfile)
else
echo "pytest-xdist is not installed; running backend unit tests serially."
fi
fi
test_list_file="${BACKEND_UNIT_TEST_FILE_LIST:-}"
generated_test_list=""
if [[ -z "$test_list_file" ]]; then
generated_test_list="$(mktemp)"
trap '[[ -z "${generated_test_list:-}" ]] || rm -f "$generated_test_list"' EXIT
"$PYTHON_BIN" scripts/select_backend_unit_tests.py --all --output "$generated_test_list"
test_list_file="$generated_test_list"
fi
if [[ ! -f "$test_list_file" ]]; then
echo "BACKEND_UNIT_TEST_FILE_LIST does not exist: $test_list_file" >&2
exit 1
fi
selected_tests=()
while IFS= read -r test_path; do
if [[ -n "$test_path" && "$test_path" != testing/e2e/* ]]; then
selected_tests+=("$test_path")
fi
done < "$test_list_file"
if [[ ${#selected_tests[@]} -eq 0 ]]; then
echo "No backend unit tests selected."
exit 0
fi
echo "Running ${#selected_tests[@]} backend unit test file(s)."
if [[ "$use_file_isolation" == "1" || "$use_file_isolation" == "true" ]]; then
worker_count="${BACKEND_PYTEST_WORKERS:-auto}"
if [[ "$worker_count" == "auto" ]]; then
if command -v getconf >/dev/null 2>&1; then
worker_count="$(getconf _NPROCESSORS_ONLN 2>/dev/null || echo 4)"
elif command -v sysctl >/dev/null 2>&1; then
worker_count="$(sysctl -n hw.ncpu 2>/dev/null || echo 4)"
else
worker_count="4"
fi
fi
active_pids=()
failed_test_paths=()
failed=0
status_dir="$(mktemp -d)"
test_index=0
active_pid_count() {
set +u
local count="${#active_pids[@]}"
set -u
echo "$count"
}
reap_finished_children() {
local running_pids
local pid
local still_active=()
local current_pids=()
running_pids="$(jobs -pr || true)"
set +u
current_pids=("${active_pids[@]}")
for pid in "${current_pids[@]}"; do
if [[ $'\n'"$running_pids"$'\n' == *$'\n'"$pid"$'\n'* ]]; then
still_active+=("$pid")
else
if ! wait "$pid"; then
failed=1
fi
fi
done
set -u
set +u
active_pids=("${still_active[@]}")
set -u
}
for test_path in "${selected_tests[@]}"; do
status_file="$status_dir/$test_index.status"
test_index=$((test_index + 1))
(
echo "::group::$test_path"
set +e
"$PYTHON_BIN" -m pytest "${pytest_args[@]}" "$test_path"
status=$?
set -e
if [[ "$status" -eq 5 && -n "$marker_expr" ]]; then
echo "No tests matched marker expression for $test_path; treating as skipped."
status=0
fi
printf '%s\t%s\n' "$status" "$test_path" > "$status_file"
if [[ "$status" -ne 0 ]]; then
echo "::error title=Backend unit file failed::$test_path exited with status $status"
fi
echo "::endgroup::"
exit 0
) &
active_pids+=("$!")
while [[ "$(active_pid_count)" -ge "$worker_count" ]]; do
reap_finished_children
if [[ "$(active_pid_count)" -lt "$worker_count" ]]; then
break
fi
oldest_pid="${active_pids[0]}"
active_pids=("${active_pids[@]:1}")
if ! wait "$oldest_pid"; then
failed=1
fi
done
done
reap_finished_children
set +u
for pid in "${active_pids[@]}"; do
if ! wait "$pid"; then
failed=1
fi
done
set -u
for status_file in "$status_dir"/*.status; do
[[ -e "$status_file" ]] || continue
IFS=$'\t' read -r status test_path < "$status_file"
if [[ "$status" -ne 0 ]]; then
echo "Backend unit test file failed: $test_path (status $status)"
failed_test_paths+=("$test_path")
failed=1
fi
done
if [[ "$failed" -ne 0 ]]; then
rerun_list="/tmp/omi-backend-unit-failures.txt"
echo
echo "Backend unit suite failed."
echo "Reproduce only the failed file(s) with the same test.sh runner and timing guard:"
printf ' : > %q\n' "$rerun_list"
for test_path in "${failed_test_paths[@]}"; do
printf ' echo %q >> %q\n' "$test_path" "$rerun_list"
done
printf ' BACKEND_UNIT_TEST_FILE_LIST=%q bash test.sh\n' "$rerun_list"
echo "Do not use bare pytest for fast-unit timing failures; it omits test.sh's guard settings."
fi
rm -rf "$status_dir"
exit "$failed"
fi
set +e
"$PYTHON_BIN" -m pytest "${pytest_args[@]}" "${selected_tests[@]}"
status=$?
set -e
if [[ "$status" -eq 5 && -n "$marker_expr" ]]; then
echo "No tests matched marker expression for selected files; treating as skipped."
exit 0
fi
exit "$status"