Files
ipass__ipass/tests/run.sh
T
admin e38237dc4a feat(deploy): 新增 .11 一键更新脚本 + 上游多模型/模型发现/用量统计
功能(本轮 5 项需求的落地):
- 上游可挂多模型:llm-upstreams.json 每上游新增 models 清单(仅用于模型发现,不参与路由)
- 模型发现:GET /v1/models(OpenAI {object:list} 形状,网关本地应答)+ /api/v1/models + /api/v1/upstreams
- 用量统计:/api/admin/usage(管理台「用量统计」Tab)+ /api/v1/usage + /me 的 llm_usage 摘要
  口径:只统计真正转发出去的请求,401/403/404/429 不计;与配额计数 st: 刻意分开
- 账号展示名 display_name(纯展示,不进 ukey:)
- 修复 /v1 版本段重复拼接(llm::join_target)→ 上游 base 自带 /v1 时不再 404 空体
- 修复删账号未清用量键(stats::forget)

部署脚本 deploy/update-ipass.sh:
- 拉镜像 → tar 备份数据 → 删旧容器 → 按旧容器复刻端口/挂载/网络起新容器 → 自检
- 参数从 docker inspect 静态配置读取(docker port 对停止态容器返回空,会让端口静默回落)
- 端口按 host:container 去重(inspect 同一映射含 IPv4/IPv6 两项,重复 -p 会 address already in use)
- --bg 用 setsid/start_new_session 真正脱离会话(仅 nohup & 会被父进程组连带杀掉)
- 旧镜像自动打 rollback-<时间> 标签,收尾打印回滚命令

回归:bash tests/run.sh all → 网关 78 + LLM 75 = 153 条断言全绿。
文档:readme §12.4 / §15 / §16、CLAUDE.md 常用操作 + 排障速查已同步。
2026-09-12 10:09:56 +08:00

203 lines
6.9 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 # 只跑域名网关(55 条断言)
# 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"
rm -f "$RUN/upstream.log"
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)──"
UPSTREAM_LOG="$RUN/upstream.log" UPSTREAM_PORT=18999 "$PY" "$HERE/llm/upstream.py" \
>"$RUN/logs/upstream.out" 2>&1 &
up_pid=$!
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" \
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