Files
ipass__ipass/tests/llm/e2e.sh
T
admin 5c307ba1aa iPass 山海关:Rust 动态模块版全量入库
- vanilla nginx + Rust 动态模块(取代 OpenResty+Lua),共享内存为运行期真源
- 三域名分流 18098 ssl:mdm 管理台 / lm LLM 代理 / *.dm 企业转发,共用 *.mayishangshu.cn 通配符证书(证书不入库)
- LLM 统一出口:同 Key 鉴权、上游 Key 替换、白名单、限流配额、SSE
- 回归夹具 tests/(网关 55 + LLM 34 = 89 断言,夹具 cp 覆盖重置)
- 文档:readme(16 章)+ CLAUDE.md + conf/tls/README(acme.sh 证书管理、hairpin 环回链路)
2026-09-11 15:37:09 +08:00

137 lines
7.4 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
# ============================================================================
# LLM 代理 · 本机回归
#
# 一般不用直接跑,走统一入口: bash tests/run.sh llm
# 直接跑需自行保证:假上游(tests/llm/upstream.py)已在 18999 监听,
# nginx 已按 tests/llm/nginx.conf.in 渲染并启动,且 data/ 用的是 fixtures 里
# 那两个固定文件(alice 白名单=openai、bob 白名单为空=不限)。
#
# ★ 核心:不只验网关自己的响应码,而是**读靶机实际收到的请求**——
# 「下游 Key 是否真被换成上游真 Key」只有看靶机才能证明。
#
# 可覆盖:GW / UPSTREAM_LOG / PY
# ============================================================================
set -u
HERE=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
RUN=${RUN:-$(cd "$HERE/.." && pwd)/.run/llm}
GW=${GW:-http://127.0.0.1:18090}
UP_LOG=${UPSTREAM_LOG:-$RUN/upstream.log}
PY=${PY:-python3}
# fixtures 里的固定 Key(见 fixtures/users.json)
ALICE_KEY=sk_alice_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
BOB_KEY=sk_bob_bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
pass=0; fail=0
ck() {
if [ "$2" = "$3" ]; then
printf " \033[32m✓\033[0m %s\n" "$1"; pass=$((pass+1))
else
printf " \033[31m✗\033[0m %s 期望=[%s] 实际=[%s]\n" "$1" "$2" "$3"; fail=$((fail+1))
fi
}
code() { curl -s -o /dev/null -w '%{http_code}' "$@"; }
jbody() { local out='{' sep=''; while [ $# -gt 1 ]; do out="$out$sep\"$1\":\"$2\""; sep=','; shift 2; done; printf '%s}' "$out"; }
# 靶机日志:取最后一条记录的某个字段
up_field() {
"$PY" -c "
import json, sys
last = None
try:
for l in open(sys.argv[1], encoding='utf-8'):
l = l.strip()
if l: last = l
except FileNotFoundError:
pass
print(json.loads(last).get(sys.argv[2], '') if last else '')
" "$UP_LOG" "$1" 2>/dev/null
}
up_count() { if [ -f "$UP_LOG" ]; then awk 'NF{n++} END{print n+0}' "$UP_LOG"; else echo 0; fi }
post() { curl -s -H "Authorization: Bearer $1" -X POST "$GW$2" -H 'Content-Type: application/json' -d "$3"; }
echo "【1】鉴权(OpenAI 兼容错误体)"
BODY=$(jbody model gpt-4o)
ck "无 Key → 401" 401 "$(code -X POST $GW/openai/v1/chat/completions)"
ck "错误 Key → 401" 401 "$(code -X POST $GW/openai/v1/chat/completions -H 'Authorization: Bearer sk_deadbeef')"
ck "错误体是 OpenAI 形状(含 error.type)" yes \
"$(curl -s -X POST $GW/openai/v1/chat/completions | grep -q '"error"' && echo yes || echo no)"
ck "路径缺 slot → 400" 400 "$(code -H "Authorization: Bearer $ALICE_KEY" $GW/)"
ck "未知上游 → 404" 404 "$(code -H "Authorization: Bearer $BOB_KEY" -X POST $GW/nope/v1/chat/completions)"
echo
echo "【2】上游解析与白名单"
ck "alice + openai(在白名单)→ 200" 200 "$(code -H "Authorization: Bearer $ALICE_KEY" -X POST $GW/openai/v1/chat/completions -H 'Content-Type: application/json' -d "$BODY")"
ck "响应体来自上游(含 pong)" yes "$(post "$ALICE_KEY" /openai/v1/chat/completions "$BODY" | grep -q pong && echo yes || echo no)"
ck "alice + other(不在白名单)→ 403" 403 "$(code -H "Authorization: Bearer $ALICE_KEY" -X POST $GW/other/v1/chat/completions -H 'Content-Type: application/json' -d "$BODY")"
ck "alice + downstream(已停用)→ 404" 404 "$(code -H "Authorization: Bearer $ALICE_KEY" -X POST $GW/downstream/v1/chat/completions -H 'Content-Type: application/json' -d "$BODY")"
ck "bob + other(空白名单=不限)→ 200" 200 "$(code -H "Authorization: Bearer $BOB_KEY" -X POST $GW/other/v1/chat/completions -H 'Content-Type: application/json' -d "$BODY")"
echo
echo "【3】★ Key 替换 / 路径 / 头(读靶机实际收到的请求)"
N0=$(up_count)
post "$ALICE_KEY" "/openai/v1/chat/completions?a=1" "$BODY" >/dev/null
ck "靶机恰好收到 1 次转发" 1 "$(( $(up_count) - N0 ))"
ck "★Authorization 已换成上游真 Key" "Bearer sk-UPSTREAM-REAL-SECRET" "$(up_field auth)"
ck "★下游 Key 未泄漏给上游" yes "$(up_field auth | grep -q 'sk_alice_' && echo leaked || echo yes)"
ck "Host 头 = 上游主机(非下游域名)" "127.0.0.1:18999" "$(up_field host)"
ck "★路径去 slot 且保留 query" "/v1/chat/completions?a=1" "$(up_field path)"
ck "请求体原样透传" "$BODY" "$(up_field body)"
echo
echo "【4】上游 base 带路径时的拼接"
post "$BOB_KEY" /other/v1/chat/completions "$BODY" >/dev/null
ck "★base=/llm + path 正确拼接" "/llm/v1/chat/completions" "$(up_field path)"
ck "用的是该上游自己的 Key" "Bearer sk-OTHER-UPSTREAM" "$(up_field auth)"
echo
echo "【5】★ 流式(SSE 不被缓冲)"
SSE=$(GW="$GW" KEY="$ALICE_KEY" "$PY" "$HERE/sse_test.py" 2>&1)
ck "Content-Type 是 text/event-stream" "text/event-stream" "$(echo "$SSE" | sed -n 's/^CT=//p')"
ck "收到 3 个 data 块" 3 "$(echo "$SSE" | sed -n 's/^CHUNKS=//p')"
FIRST=$(echo "$SSE" | sed -n 's/^FIRST=//p')
TOTAL=$(echo "$SSE" | sed -n 's/^TOTAL=//p')
ck "★首块 < 0.5s 到达(未被攒包)" yes "$(awk -v f="${FIRST:-9}" 'BEGIN{print (f<0.5)?"yes":"no"}')"
ck "总耗时 ≥ 1.0s(确实分了 3 次推)" yes "$(awk -v t="${TOTAL:-0}" 'BEGIN{print (t>=1.0)?"yes":"no"}')"
echo
echo "【6】★ 限流与每日配额(carol rpm=2、dave daily=2,见 fixtures/users.json)"
CAROL_KEY=sk_carol_cccccccccccccccccccccccccccccccccccccccccccccc
DAVE_KEY=sk_dave_dddddddddddddddddddddddddddddddddddddddddddddddd
cpost() { code -H "Authorization: Bearer $1" -X POST "$GW/other/v1/chat/completions" -H 'Content-Type: application/json' -d "$BODY"; }
ck "carol 第 1 发 → 200" 200 "$(cpost "$CAROL_KEY")"
ck "carol 第 2 发 → 200" 200 "$(cpost "$CAROL_KEY")"
ck "carol 第 3 发 → 429(rpm=2)" 429 "$(cpost "$CAROL_KEY")"
ck "429 是 rate_limit_error / rate_limit_exceeded" yes \
"$(post "$CAROL_KEY" /other/v1/chat/completions "$BODY" | grep -q 'rate_limit' && echo yes || echo no)"
ck "dave 第 1 发 → 200" 200 "$(cpost "$DAVE_KEY")"
ck "dave 第 2 发 → 200" 200 "$(cpost "$DAVE_KEY")"
ck "dave 第 3 发 → 429(daily=2)" 429 "$(cpost "$DAVE_KEY")"
ck "429 是 insufficient_quota" yes \
"$(post "$DAVE_KEY" /other/v1/chat/completions "$BODY" | grep -q 'insufficient_quota' && echo yes || echo no)"
ck "carol 被限后 alice 不受影响(按 Key 隔离)→ 200" 200 \
"$(code -H "Authorization: Bearer $ALICE_KEY" -X POST "$GW/openai/v1/chat/completions" -H 'Content-Type: application/json' -d "$BODY")"
if [ "${HAVE_TLS:-0}" = "1" ]; then
echo
echo "【7】★ HTTPS(18443 自签,模板 tests/llm/tls.conf.in)"
HTTPS_BASE=https://127.0.0.1:18443
ck "https + 错误 Key → 401(TLS 层下鉴权仍生效)" 401 \
"$(code -k -X POST $HTTPS_BASE/openai/v1/chat/completions -H 'Authorization: Bearer nope')"
ck "https + alice → 200" 200 \
"$(code -k -H "Authorization: Bearer $ALICE_KEY" -X POST $HTTPS_BASE/openai/v1/chat/completions -H 'Content-Type: application/json' -d "$BODY")"
ck "https 响应体来自上游" yes \
"$(curl -sk -H "Authorization: Bearer $ALICE_KEY" -X POST $HTTPS_BASE/openai/v1/chat/completions -H 'Content-Type: application/json' -d "$BODY" | grep -q pong && echo yes || echo no)"
else
echo
echo "【7】HTTPS:跳过(本机 nginx 无 http_ssl_module 或无 openssl)"
fi
echo
echo "============================================================"
printf " 通过 \033[32m%d\033[0m 失败 \033[31m%d\033[0m\n" "$pass" "$fail"
echo "============================================================"
[ "$fail" -eq 0 ] || exit 1