浏览代码

【修复,脚本】冒烟测试 cleanup 与 HTTP 超时

run 34124461509 在 dsh 0.1.2-rc.1 冒烟测试失败,日志最后是:

    rm: cannot remove '/tmp/tmp.0ixdeCMLmf': Operation not permitted
    Error: Process completed with exit code 1.

但这是次生错误:真错误是「服务 120s 内未就绪」(dsh 0.1.2-rc.1 首启需 ~150s,
x-cmd 80MB 下载 + dsh web bundles 懒加载)。

三层修复:

1. **mktemp -d 路径**:从默认 /tmp 改到 RUNNER_TEMP → TMPDIR → HOME,
   都是非 sticky 目录,避免容器内 uid 1000 (node) 写下的子文件被 runner
   (uid 1001) 在 sticky /tmp 下无法清理 (EPERM)

2. **HTTP 超时 120s → 240s**:120s 反复误报上游启动慢;240s 留一倍余量,
   真挂的场景仍超时但能区分「慢」vs「挂」。失败信息加上
   「最后一次 HTTP code=...」便于排查

3. **cleanup 防御性保留主退出码**:trap 函数第一行捕获 $?,最后
   return 出去,即使内部 rm 失败也不污染 exit code (虽然实测当前
   bash EXIT trap + exit N 不被覆盖,显式保留是 future-proof)
PJ568 2 月之前
父节点
当前提交
33a8aa2d1a
共有 1 个文件被更改,包括 54 次插入 和 5 次删除
  1. 54 5
      scripts/smoke-test.sh

+ 54 - 5
scripts/smoke-test.sh

@@ -10,9 +10,32 @@ IMAGE="dsh-web:smoke"
 CID="dsh-smoke"
 
 cleanup() {
+  # cleanup 失败不应掩盖主命令的退出码:bash EXIT trap 的函数返回值就是进程的最终
+  # exit code(覆盖 `exit N` 的 N)。trap 启动时 $? = 触发 trap 的命令退出码,
+  # 函数第一行捕获它,cleanup 内部失败用 warn 兜底,最后 return 这个 rc 把它冒出去
+  #
+  # Cleanup failures must not mask the main command's exit code: bash EXIT trap's
+  # function return value becomes the process's final exit code (overriding `exit N`).
+  # The trap fires with $? = the triggering command's exit code; we capture it on the
+  # first line, swallow internal cleanup failures into a warn, and `return` it so it
+  # propagates to Actions as the real failure rather than a silent success
+  local rc=$?
   docker rm -f "$CID" >/dev/null 2>&1 || true
-  [ -n "${SMOKE_HOME:-}" ] && rm -rf "$SMOKE_HOME" || true
+  if [ -n "${SMOKE_HOME:-}" ] && [ -d "$SMOKE_HOME" ]; then
+    # 先放宽权限再递归删:容器内 uid 1000 (node) 写下的子文件可能只对本人可写,
+    # runner (uid 1001) 在 sticky /tmp 下无法删;但若 SMOKE_PARENT 已经是 runner 自己的
+    # 非 sticky 目录,权限放宽足以应对权限位差异
+    #
+    # Loosen perms first then recurse: container-internal uid 1000 (node) may have left
+    # files only writable by themselves; on a sticky /tmp a uid-1001 runner can't drop
+    # those. If SMOKE_PARENT is already a non-sticky runner-owned dir, chmod is enough.
+    chmod -R u+w "$SMOKE_HOME" 2>/dev/null || true
+    rm -rf "$SMOKE_HOME" 2>/dev/null || warn "无法清理 SMOKE_HOME=$SMOKE_HOME(容器子文件权限/sticky 导致),手动 rm 即可"
+  fi
+  return "$rc"  # 关键:把主命令退出码冒出去
+                 # Critical: propagate the main command's exit code
 }
+warn() { printf 'WARN: %s\n' "$*" >&2; }
 trap cleanup EXIT
 cleanup
 
@@ -28,7 +51,21 @@ echo "==> 启动容器冒烟"
 # 预置假 x-cmd(x 可执行即跳过 entrypoint 首启 300s 下载)
 #
 # Pre-seed a fake x-cmd (an executable x skips the entrypoint's 300s first-run download)
-SMOKE_HOME="$(mktemp -d)"
+#
+# SMOKE_PARENT 选择:必须是非 sticky 目录。容器内 dsh/caddy 以 uid 1000 (node) 写入
+# bind mount 的子文件,runner 是 uid 1001;sticky /tmp + 跨 uid → cleanup 时 EPERM。
+# 优先用 GitHub Actions 提供的 RUNNER_TEMP(runner 独占,非 sticky),否则用
+# 系统 TMPDIR(部分发行版已重定向到 /run/user/<uid>),否则回退 $HOME
+# (Linux home 不 sticky,macOS 也只有 /tmp 有 sticky)。$HOME 兜底保留本地跑的可用性
+#
+# SMOKE_PARENT must be non-sticky: container dsh/caddy runs as uid 1000 (node) and
+# writes into the bind-mount, while the runner is uid 1001; sticky /tmp + cross-uid
+# triggers EPERM on cleanup. Prefer RUNNER_TEMP (runner-exclusive, non-sticky), then
+# TMPDIR (some distros point it at /run/user/<uid>), then $HOME (Linux home is not
+# sticky; macOS only has sticky on /tmp). $HOME fallback keeps local runs working.
+SMOKE_PARENT="${RUNNER_TEMP:-${TMPDIR:-$HOME}}"
+mkdir -p "$SMOKE_PARENT"
+SMOKE_HOME="$(mktemp -d -p "$SMOKE_PARENT" smoke-XXXXXX)"
 mkdir -p "$SMOKE_HOME/.x-cmd.root/bin"
 printf '#!/bin/sh\nexit 0\n' > "$SMOKE_HOME/.x-cmd.root/bin/x"
 docker run -d --name "$CID" \
@@ -40,20 +77,32 @@ docker run -d --name "$CID" \
 PORT="$(docker port "$CID" 3081 | head -n1 | sed 's/.*://')"
 echo "    容器 $CID 已启动,映射端口 $PORT"
 
-echo "==> 等待服务就绪(最多 120s)"
+echo "==> 等待服务就绪(最多 240s)"
 # 仅 200 视为就绪:502(Caddy 已起但 dsh 未就绪)与 000(未监听)都继续等;
 # 401 是 Caddy basic auth 层拒绝、不经过反代,也不能代表 dsh 就绪
 #
 # Only 200 means ready: 502 (Caddy up but dsh not yet) and 000 (not listening) keep waiting;
 # 401 is rejected at the Caddy basic-auth layer without touching the proxy, so it proves nothing
+#
+# 阈值从 120s 提到 240s:0.1.2-rc.1 在 GitHub Actions runner 上首启需 ~150s
+# (x-cmd 80MB 下载 + dsh web bundles 懒加载),120s 反复误报上游慢。240s 留一倍余量,
+# 真挂的场景仍会超时但能区分「慢」vs「挂」
+#
+# Threshold raised 120s→240s: dsh 0.1.2-rc.1 first boot on GitHub Actions runner
+# takes ~150s (80MB x-cmd download + dsh web bundles lazy load). 120s kept misfiring
+# on upstream slowness; 240s leaves 1× headroom while still failing on real hangs
 READY=0
-for _ in $(seq 1 60); do
+LAST_CODE=000
+START_TS="$(date +%s)"
+for _ in $(seq 1 120); do
   CODE="$(curl -s -u admin:smoketest -o /dev/null -w '%{http_code}' "http://127.0.0.1:$PORT/" || true)"
+  LAST_CODE="$CODE"
   if [ "$CODE" = "200" ]; then READY=1; break; fi
   sleep 2
 done
 if [ "$READY" != 1 ]; then
-  echo "错误:服务 120s 内未就绪" >&2
+  ELAPSED=$(( $(date +%s) - START_TS ))
+  echo "错误:服务 ${ELAPSED}s 内未就绪(最后一次 HTTP code=$LAST_CODE)" >&2
   docker logs "$CID" 2>&1 | tail -30 >&2
   exit 1
 fi