Files
ipass__ipass/tests/run.sh
T
admin 1bc316e014 fix(stats): 「上次落盘」记录移入共享内存(1.0.19)
1.0.18 首次线上验证暴露:error.log 里有 10 次 [stats] 落盘(来自 6#0/18#0/23#0
多个 worker),GET /api/admin/stats/persist 却报「本进程还没落过」。

根因:LAST_DUMP_AT 是每 worker 的进程内静态量。maybe_dump() 挂在记账路径上,
而记账每 worker 各算各的 → 落盘时刻也就每 worker 一份;接口落到没落过盘的
worker 就报 0。与 1.0.18 已修过的 spr:last(上次恢复)是同一类错误。

修法:发布到共享内存 spd:last(publish_dump 取 max)。一次解决两件事:
* 上报一致 —— 所有 worker 报同一份;
* 节流收敛 —— maybe_dump 新增慢路径查它,把「8 个 worker 各写一遍同一个文件」
  收敛成「全局每间隔一次」。快路径仍是原子比较,热路径代价不变。

顺带:
- 新增回归断言「状态页报告的上次落盘时刻非空」(套件开 2 worker,能暴露该分歧)
- deploy/agent.sh 加 stats-persist / stats-dump 子命令(线上观测口)
- 统一文档里过期的断言条数(90/111/160/179/196/262/298 → 实际值)
- 记录审计容量巡检 LAST_PURGE_AT/PURGING 同为每 worker 静态量(另案)

回归:bash tests/run.sh all → 299 条全绿(网关 102 + LLM 197)。
2026-09-14 20:05:51 +08:00

236 lines
8.8 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# ============================================================================
# 本机回归 · 统一入口
#
# bash tests/run.sh # 跑全部(域名网关 + LLM 代理)
# bash tests/run.sh gateway # 只跑域名网关(102 条断言)
# bash tests/run.sh llm # 只跑 LLM 代理
#
# 前置:本机 nginx 已编译好(默认 /Users/xs/Desktop/workspace/iPass/build/ngxbuild/nginx-1.30.4/objs/nginx),
# Rust 模块由本脚本自动 cargo build。
# 可覆盖:NGINX_BIN / NGINX_BUILD_DIR / PY
#
# ⚠️ 全程使用 tests/.run/ 下的独立 data 目录,**绝不碰生产 data/**。
# nginx.conf 由 .in 模板渲染(占位符 @MODULE@ / @REPO@ / @RUN@),
# 所以 nginx 跑在哪个路径、模块编到哪都不写死。
# ============================================================================
set -uo pipefail
HERE=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
REPO=$(cd "$HERE/.." && pwd)
NGINX_BIN=${NGINX_BIN:-/Users/xs/Desktop/workspace/iPass/build/ngxbuild/nginx-1.30.4/objs/nginx}
NGINX_BUILD_DIR=${NGINX_BUILD_DIR:-/Users/xs/Desktop/workspace/iPass/build/ngxbuild/nginx-1.30.4/objs}
MODULE="$REPO/rust/target/debug/libipass_gateway.dylib"
# python3:优先隔离的托管运行时,其次系统的
PY=${PY:-}
if [ -z "$PY" ]; then
for c in /Users/xs/.workbuddy/binaries/python/versions/3.13.12/bin/python3 python3; do
if command -v "$c" >/dev/null 2>&1; then PY=$c; break; fi
done
fi
[ -n "$PY" ] || { echo "错误:找不到 python3" >&2; exit 1; }
# ---------------------------------------------------------------------------
# 工具
# ---------------------------------------------------------------------------
# sed 替换串里的 & 和 | 需要转义,否则路径里的这些字符会炸
esc() { printf '%s' "$1" | sed -e 's/[&|]/\\&/g'; }
# render <模板> <输出> <RUN 目录>
render() {
sed -e "s|@MODULE@|$(esc "$MODULE")|g" \
-e "s|@REPO@|$(esc "$REPO")|g" \
-e "s|@RUN@|$(esc "$3")|g" "$1" > "$2"
}
ngx_stop() {
if [ -f "$1/nginx.conf" ]; then
"$NGINX_BIN" -p "$1" -c "$1/nginx.conf" -s stop >/dev/null 2>&1
fi
sleep 1
return 0
}
ngx_start() { "$NGINX_BIN" -p "$1" -c "$1/nginx.conf"; }
# wait_tcp <host> <port>
wait_tcp() {
local i
for i in $(seq 1 60); do
"$PY" -c 'import socket, sys
s = socket.socket(); s.settimeout(0.2)
try:
s.connect((sys.argv[1], int(sys.argv[2]))); sys.exit(0)
except Exception:
sys.exit(1)' "$1" "$2" && return 0
sleep 0.1
done
return 1
}
build() {
echo "── 编译 Rust 模块 ──"
( cd "$REPO/rust" && NGINX_BUILD_DIR="$NGINX_BUILD_DIR" cargo build ) || return 1
}
# ---------------------------------------------------------------------------
# 套件:域名网关
# ---------------------------------------------------------------------------
suite_gateway() {
local RUN="$HERE/.run/gateway"
mkdir -p "$RUN/data" "$RUN/logs"
render "$HERE/gateway/nginx.conf.in" "$RUN/nginx.conf" "$RUN"
ngx_stop "$RUN"
# 夹具覆盖式播种:保证每次跑起点一致(合成数据,非生产 data)。
# 用 cp 而非 rm —— 批量删除会被安全钩子拦截,导致脏数据残留、断言失败。
cp "$HERE/gateway/fixtures/users.json" "$RUN/data/users.json"
cp "$HERE/gateway/fixtures/domains.json" "$RUN/data/domains.json"
: > "$RUN/cookie.jar"
: > "$RUN/logs/error.log"
echo "── 启动网关(18080 转发 / 18081 靶机 / 18082 管理)──"
if ! ngx_start "$RUN"; then
echo "nginx 启动失败,见 $RUN/logs/error.log" >&2; return 1
fi
wait_tcp 127.0.0.1 18082 || { echo "管理端口未就绪" >&2; ngx_stop "$RUN"; return 1; }
echo
ADMIN="http://localhost:18082" GW="http://127.0.0.1:18080" \
JAR="$RUN/cookie.jar" RUN="$RUN" PY="$PY" \
bash "$HERE/gateway/e2e.sh"
local rc=$?
ngx_stop "$RUN"
return $rc
}
# ---------------------------------------------------------------------------
# 套件:LLM 代理
# ---------------------------------------------------------------------------
suite_llm() {
local RUN="$HERE/.run/llm"
local up_pid=""
mkdir -p "$RUN/data" "$RUN/logs"
# 夹具覆盖式播种,保证每次跑的起点一致(这两个文件是合成数据,不是生产 data)
cp "$HERE/llm/fixtures/users.json" "$RUN/data/users.json"
cp "$HERE/llm/fixtures/llm-upstreams.json" "$RUN/data/llm-upstreams.json"
cp "$HERE/llm/fixtures/api-proxies.json" "$RUN/data/api-proxies.json"
rm -f "$RUN/upstream.log"
# 把上一轮的统计快照**挪开**(而不是删除),保证本轮从 0 起算。
#
# 为什么不删:本机装了 safe-delete 钩子 —— 同一个 agent turn 里累计删除超过
# 50 个文件后,后续所有删除都被静默拦掉(可见日志 SAFE_DELETE_BULK_CONFIRM_REQUIRED)。
# `rm -rf` 与 python 的 shutil.rmtree / os.remove 都撞在这上面。
# 症状很有欺骗性:清理「没报错但没生效」,于是上一轮的 ust:/ud:/uh: 被启动时的
# restore() 恢复进本轮 —— carol 用量期望 2 变成 6、「上游健康初始状态」期望
# unknown 变成 ok(看起来像统计功能有 bug,实际是测试间的状态泄漏)。
# `mv` 是 rename、不是删除,在沙箱下稳定生效;留档也便于比对。
if [ -s "$RUN/data/llm-stats.json" ]; then
mkdir -p "$RUN/stale"
mv "$RUN/data/llm-stats.json" "$RUN/stale/llm-stats.$(date +%s).json"
fi
render "$HERE/llm/nginx.conf.in" "$RUN/nginx.conf" "$RUN"
# TLS 夹具:本机 nginx 带 http_ssl_module 且有 openssl 时,生成自签证书 +
# 渲染 443 server(@RUN@/tls/llm.conf,nginx.conf.in 已 include 该目录)。
# 任一条件不满足就留空目录 → include 空匹配,HTTPS 组断言自动跳过。
HAVE_TLS=0
mkdir -p "$RUN/tls"
rm -f "$RUN"/tls/*.pem "$RUN"/tls/*.conf
if command -v openssl >/dev/null 2>&1 \
&& "$NGINX_BIN" -V 2>&1 | grep -q http_ssl_module; then
if openssl req -x509 -newkey rsa:2048 -nodes -days 30 \
-subj "/CN=llm.local" -addext "subjectAltName=DNS:llm.local" \
-keyout "$RUN/tls/key.pem" -out "$RUN/tls/cert.pem" >/dev/null 2>&1; then
render "$HERE/llm/tls.conf.in" "$RUN/tls/llm.conf" "$RUN"
HAVE_TLS=1
fi
fi
ngx_stop "$RUN"
: > "$RUN/logs/error.log"
echo "── 启动假上游(18999)与 LLM 网关(18090)──"
# 清掉**本项目自己**残留的靶机进程(如上次调试用 run_in_background 起的)。
# 否则新靶机 bind 失败、旧靶机继续服务但日志写到别的目录——
# 表现就是「请求全 200 但 UP_LOG 全空」,25 条断言集体假失败(2026-09-12 实测)。
pkill -f "tests/llm/upstream.py" 2>/dev/null
for i in $(seq 1 20); do
"$PY" -c 'import socket, sys
s = socket.socket(); s.settimeout(0.2)
try:
s.connect(("127.0.0.1", 18999)); sys.exit(0)
except Exception:
sys.exit(1)' && sleep 0.2 || break
done
UPSTREAM_LOG="$RUN/upstream.log" UPSTREAM_PORT=18999 UPSTREAM_FAIL_AUTH="acct-bad" "$PY" "$HERE/llm/upstream.py" \
>"$RUN/logs/upstream.out" 2>&1 &
up_pid=$!
# wait_tcp 可能被残留进程「骗绿」——必须确认**本进程**还活着(bind 失败会立刻退出)
if ! kill -0 "$up_pid" 2>/dev/null; then
echo "假上游启动即退出(多半是 18999 被非本项目的进程占用),见 $RUN/logs/upstream.out" >&2
return 1
fi
if ! wait_tcp 127.0.0.1 18999; then
echo "假上游未就绪,见 $RUN/logs/upstream.out" >&2
kill "$up_pid" 2>/dev/null; return 1
fi
if ! ngx_start "$RUN"; then
echo "nginx 启动失败,见 $RUN/logs/error.log" >&2
kill "$up_pid" 2>/dev/null; return 1
fi
if ! wait_tcp 127.0.0.1 18090; then
echo "LLM 端口未就绪" >&2
ngx_stop "$RUN"; kill "$up_pid" 2>/dev/null; return 1
fi
wait_tcp 127.0.0.1 18091 || { echo "管理端口未就绪" >&2; ngx_stop "$RUN"; kill "$up_pid" 2>/dev/null; return 1; }
echo
GW="http://127.0.0.1:18090" API="http://127.0.0.1:18091" \
UPSTREAM_LOG="$RUN/upstream.log" RUN="$RUN" PY="$PY" HAVE_TLS="$HAVE_TLS" \
NGINX_BIN="$NGINX_BIN" \
bash "$HERE/llm/e2e.sh"
local rc=$?
ngx_stop "$RUN"
kill "$up_pid" 2>/dev/null
wait "$up_pid" 2>/dev/null
return $rc
}
# ---------------------------------------------------------------------------
# main
# ---------------------------------------------------------------------------
WHICH=${1:-all}
rc=0
rc2=0
build || { echo "编译失败,先修 Rust 再跑回归" >&2; exit 1; }
echo
case "$WHICH" in
gateway) suite_gateway; rc=$? ;;
llm) suite_llm; rc=$? ;;
all|"")
suite_gateway; rc=$?
echo
suite_llm; rc2=$?
[ "$rc2" -ne 0 ] && rc=$rc2
;;
*) echo "用法: bash tests/run.sh [gateway|llm|all]" >&2; exit 2 ;;
esac
echo
if [ "$rc" -eq 0 ]; then
echo "全部通过"
else
echo "有断言失败(退出码 ${rc:-0})"
fi
exit $rc