#!/bin/bash # ============================================================================ # 统一制品缓存 · 本机自测 # 验证:六类生态 MISS→HIT、Range(206)、PURGE、X-Cache-Status 头 # 前置:本机 nginx 已编译(NGINX_BIN),mock_upstream.py 由本脚本拉起 # ============================================================================ set -uo pipefail HERE=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) REPO=$(cd "$HERE/../.." && pwd) RUN="$HERE/.run" NGINX_BIN=${NGINX_BIN:-/Users/rong/Desktop/cache/build/nginx-1.30.4/objs/nginx} PORT=18083 UP_PORT=18999 PY=${PY:-python3} [ -x "$NGINX_BIN" ] || { echo "错误:nginx 不存在 $NGINX_BIN" >&2; exit 1; } mkdir -p "$RUN/cache" "$RUN/logs" # 每次跑起点一致:清空上一轮残留的缓存文件(否则「首次」会命中旧档) # # ⚠️ 用 `find … -delete` 而**不是** `rm -rf "$RUN/cache"`:本机有批量删除护栏, # 待删文件数超过阈值时整个删除会被**拒绝且不返回非零退出**,脚本(无 `set -e`) # 静默继续 → 上一轮副本存活 → 「首次 MISS」与「PURGE 后变 MISS」双双报 HIT。 # 这种假失败看起来像 nginx 缓存坏了,实际是本轮没打扫干净。实测踩过。 mkdir -p "$RUN/cache/go" "$RUN/cache/cargo" "$RUN/cache/pypi" \ "$RUN/cache/npm" "$RUN/cache/maven" "$RUN/cache/docker" find "$RUN/cache" -type f -delete >/dev/null 2>&1 # 自检:清完必须为空,否则后面的 MISS 断言全部不可信 if [ -n "$(find "$RUN/cache" -type f -print -quit 2>/dev/null)" ]; then echo "❌ 缓存目录没清干净($RUN/cache)——MISS/HIT 断言不可信,先修环境" >&2; exit 1 fi : > "$RUN/logs/error.log" : > "$RUN/upstream.log" render() { sed -e "s|@RUN@|$RUN|g" "$1" > "$2"; } # 端口探活:直到能建立连接再往下走(用 HEAD,不会写进上游请求日志)。 # 固定 sleep 在「刚 pkill 完又要立刻起」时不可靠,会偶发 502 → 断言误判。 wait_up() { # $1=port $2=最大尝试次数(默认 80,步长 0.1s) local port="$1" n="${2:-80}" i=0 until curl -s -o /dev/null -I --max-time 1 "http://127.0.0.1:$port/" 2>/dev/null; do i=$((i+1)); [ "$i" -ge "$n" ] && return 1; sleep 0.1 done return 0 } # ---- 起假上游 ---- pkill -f "tests/cache/mock_upstream.py" 2>/dev/null; sleep 0.3 UPSTREAM_LOG="$RUN/upstream.log" UPSTREAM_PORT=$UP_PORT "$PY" "$HERE/mock_upstream.py" >"$RUN/logs/upstream.out" 2>&1 & UP_PID=$! kill -0 "$UP_PID" 2>/dev/null || { echo "假上游启动失败,见 $RUN/logs/upstream.out" >&2; exit 1; } wait_up "$UP_PORT" || { echo "假上游端口 $UP_PORT 未就绪" >&2; kill "$UP_PID" 2>/dev/null; exit 1; } # ---- 起 nginx ---- render "$HERE/nginx.cache.local.conf" "$RUN/nginx.conf" "$NGINX_BIN" -p "$RUN" -c "$RUN/nginx.conf" -t 2>>"$RUN/logs/error.log" || { echo "nginx 配置测试失败" >&2; cat "$RUN/logs/error.log"; kill "$UP_PID"; exit 1; } "$NGINX_BIN" -p "$RUN" -c "$RUN/nginx.conf" wait_up "$PORT" || { echo "nginx 端口 $PORT 未就绪" >&2; cat "$RUN/logs/error.log"; kill "$UP_PID" 2>/dev/null; exit 1; } PASS=0; FAIL=0 chk() { # chk <期望子串> <实际> if [ "$3" = "$2" ]; then echo " ✅ $1"; PASS=$((PASS+1)); else echo " ❌ $1 期望[$2] 实际[$3]"; FAIL=$((FAIL+1)); fi } req() { # req [extra headers] -> 输出 "STATUS|XCACHE|BODY" local m="$1" p="$2"; shift 2 local out out=$(curl -s -o /tmp/cache_body -w "%{http_code}" -X "$m" "http://127.0.0.1:$PORT$p" "$@" -H "X-Test: 1" 2>/dev/null) local xc=$(curl -s -I -X "$m" "http://127.0.0.1:$PORT$p" "$@" 2>/dev/null | grep -i '^X-Cache-Status:' | tr -d '\r' | awk '{print $2}') # 重新取 body(上面 -I 不带 body);用首请求拿到的 body echo "$out|$xc|$(cat /tmp/cache_body)" } echo "── 六类生态 MISS→HIT ──" for eco in go cargo pypi npm maven docker; do # 首次请求:同一次抓取头+体(避免额外请求污染 MISS 判定) curl -s -D /tmp/h1 -o /tmp/b1 -w "%{http_code}" "http://127.0.0.1:$PORT/$eco/pkg-a" >/dev/null xc1=$(grep -i '^X-Cache-Status:' /tmp/h1 | tr -d '\r' | awk '{print $2}') b1=$(cat /tmp/b1) # 二次请求:HIT curl -s -D /tmp/h2 -o /tmp/b2 -w "%{http_code}" "http://127.0.0.1:$PORT/$eco/pkg-a" >/dev/null xc2=$(grep -i '^X-Cache-Status:' /tmp/h2 | tr -d '\r' | awk '{print $2}') b2=$(cat /tmp/b2) chk "$eco 首次 MISS" "MISS" "$xc1" chk "$eco 二次 HIT" "HIT" "$xc2" chk "$eco 内容一致" "$b1" "$b2" done echo "── Range(206 + 命中)──" # 首段 range 请求(未预热,应 MISS 并回填缓存;nginx 缓存完整对象后可按 range 服务) curl -s -D /tmp/hr1 -o /tmp/rr1 -w "%{http_code}" -r 0-15 "http://127.0.0.1:$PORT/go/biglayer" > /tmp/code_r code_r=$(cat /tmp/code_r) xc_r1=$(grep -i '^X-Cache-Status:' /tmp/hr1 | tr -d '\r' | awk '{print $2}') # 二次 range:命中缓存 curl -s -D /tmp/hr2 -o /dev/null -r 0-15 "http://127.0.0.1:$PORT/go/biglayer" >/dev/null xc_r2=$(grep -i '^X-Cache-Status:' /tmp/hr2 | tr -d '\r' | awk '{print $2}') chk "range 首请求 206" "206" "$code_r" chk "range 首请求 MISS" "MISS" "$xc_r1" chk "range 二次 HIT(缓存)" "HIT" "$xc_r2" echo "── PURGE(ipass 原生:删缓存文件,对应 POST /api/v1/cache/purge)──" # 开源 vanilla nginx 无 proxy_cache_purge 指令;ipass 的 purge 由管理 API # 删除缓存目录文件实现(无需第三方模块)。这里模拟同一动作。 # ⚠️ 同样用 find -delete 而非 `rm -rf "$RUN/cache/go"/*`:护栏会静默拦下 # 批量删除,届时文件还在、下一句仍读 HIT,「PURGE 失效」是假故障。 find "$RUN/cache/go" -type f -delete >/dev/null 2>&1 if [ -n "$(find "$RUN/cache/go" -type f -print -quit 2>/dev/null)" ]; then echo "❌ PURGE 没删掉文件($RUN/cache/go)——断言不可信" >&2; exit 1 fi xc_after=$(curl -s -D - -o /dev/null "http://127.0.0.1:$PORT/go/pkg-a" | grep -i '^X-Cache-Status:' | tr -d '\r' | awk '{print $2}') chk "PURGE(删文件)后变 MISS" "MISS" "$xc_after" echo "── 回源计数(HIT 不回源)──" before=$(grep -c "GET /go/pkg-a" "$RUN/upstream.log" 2>/dev/null || echo 0) # 再打两次,应全 HIT,不新增回源 curl -s -o /dev/null "http://127.0.0.1:$PORT/go/pkg-a" curl -s -o /dev/null "http://127.0.0.1:$PORT/go/pkg-a" after=$(grep -c "GET /go/pkg-a" "$RUN/upstream.log" 2>/dev/null || echo 0) chk "HIT 不回源(上游无新增)" "$before" "$after" # ---- 收尾 ---- "$NGINX_BIN" -p "$RUN" -c "$RUN/nginx.conf" -s stop >/dev/null 2>&1 kill "$UP_PID" 2>/dev/null echo echo "通过 $PASS / 失败 $FAIL" [ "$FAIL" -eq 0 ] && exit 0 || exit 1