smoke-test.sh 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #!/usr/bin/env bash
  2. # 冒烟测试:构建镜像 → 校验 dsh 版本 → 起容器 → basic auth 与进程检查
  3. #
  4. # Smoke test: build image → verify dsh version → run container → basic auth & process checks
  5. # Usage: smoke-test.sh <version>
  6. set -euo pipefail
  7. VERSION="$1"
  8. IMAGE="dsh-web:smoke"
  9. CID="dsh-smoke"
  10. cleanup() {
  11. # cleanup 失败不应掩盖主命令的退出码:bash EXIT trap 的函数返回值就是进程的最终
  12. # exit code(覆盖 `exit N` 的 N)。trap 启动时 $? = 触发 trap 的命令退出码,
  13. # 函数第一行捕获它,cleanup 内部失败用 warn 兜底,最后 return 这个 rc 把它冒出去
  14. #
  15. # Cleanup failures must not mask the main command's exit code: bash EXIT trap's
  16. # function return value becomes the process's final exit code (overriding `exit N`).
  17. # The trap fires with $? = the triggering command's exit code; we capture it on the
  18. # first line, swallow internal cleanup failures into a warn, and `return` it so it
  19. # propagates to Actions as the real failure rather than a silent success
  20. local rc=$?
  21. docker rm -f "$CID" >/dev/null 2>&1 || true
  22. if [ -n "${SMOKE_HOME:-}" ] && [ -d "$SMOKE_HOME" ]; then
  23. # 先放宽权限再递归删:容器内 uid 1000 (node) 写下的子文件可能只对本人可写,
  24. # runner (uid 1001) 在 sticky /tmp 下无法删;但若 SMOKE_PARENT 已经是 runner 自己的
  25. # 非 sticky 目录,权限放宽足以应对权限位差异
  26. #
  27. # Loosen perms first then recurse: container-internal uid 1000 (node) may have left
  28. # files only writable by themselves; on a sticky /tmp a uid-1001 runner can't drop
  29. # those. If SMOKE_PARENT is already a non-sticky runner-owned dir, chmod is enough.
  30. chmod -R u+w "$SMOKE_HOME" 2>/dev/null || true
  31. rm -rf "$SMOKE_HOME" 2>/dev/null || warn "无法清理 SMOKE_HOME=$SMOKE_HOME(容器子文件权限/sticky 导致),手动 rm 即可"
  32. fi
  33. return "$rc" # 关键:把主命令退出码冒出去
  34. # Critical: propagate the main command's exit code
  35. }
  36. warn() { printf 'WARN: %s\n' "$*" >&2; }
  37. trap cleanup EXIT
  38. cleanup
  39. echo "==> 构建镜像(DSH_VERSION=$VERSION)"
  40. docker build --build-arg DSH_VERSION="$VERSION" -t "$IMAGE" .
  41. echo "==> 校验 dsh --version"
  42. OUT="$(docker run --rm --entrypoint dsh "$IMAGE" --version)"
  43. echo " dsh --version => $OUT"
  44. grep -q "$VERSION" <<<"$OUT" || { echo "错误:版本不符(期望包含 $VERSION)" >&2; exit 1; }
  45. echo "==> 启动容器冒烟"
  46. # 预置假 x-cmd(x 可执行即跳过 entrypoint 首启 300s 下载)
  47. #
  48. # Pre-seed a fake x-cmd (an executable x skips the entrypoint's 300s first-run download)
  49. #
  50. # SMOKE_PARENT 选择:必须是非 sticky 目录。容器内 dsh/caddy 以 uid 1000 (node) 写入
  51. # bind mount 的子文件,runner 是 uid 1001;sticky /tmp + 跨 uid → cleanup 时 EPERM。
  52. # 优先用 GitHub Actions 提供的 RUNNER_TEMP(runner 独占,非 sticky),否则用
  53. # 系统 TMPDIR(部分发行版已重定向到 /run/user/<uid>),否则回退 $HOME
  54. # (Linux home 不 sticky,macOS 也只有 /tmp 有 sticky)。$HOME 兜底保留本地跑的可用性
  55. #
  56. # SMOKE_PARENT must be non-sticky: container dsh/caddy runs as uid 1000 (node) and
  57. # writes into the bind-mount, while the runner is uid 1001; sticky /tmp + cross-uid
  58. # triggers EPERM on cleanup. Prefer RUNNER_TEMP (runner-exclusive, non-sticky), then
  59. # TMPDIR (some distros point it at /run/user/<uid>), then $HOME (Linux home is not
  60. # sticky; macOS only has sticky on /tmp). $HOME fallback keeps local runs working.
  61. SMOKE_PARENT="${RUNNER_TEMP:-${TMPDIR:-$HOME}}"
  62. mkdir -p "$SMOKE_PARENT"
  63. SMOKE_HOME="$(mktemp -d -p "$SMOKE_PARENT" smoke-XXXXXX)"
  64. mkdir -p "$SMOKE_HOME/.x-cmd.root/bin"
  65. printf '#!/bin/sh\nexit 0\n' > "$SMOKE_HOME/.x-cmd.root/bin/x"
  66. docker run -d --name "$CID" \
  67. -v "$SMOKE_HOME:/home/node" \
  68. -e DSH_AUTH_USER=admin -e DSH_AUTH_PASSWORD=smoketest \
  69. -e DEEPSEEK_API_KEY=sk-dummy \
  70. -e DSH_TRUSTED_HOSTS=smoke.example.com \
  71. -p 127.0.0.1::3081 "$IMAGE" >/dev/null
  72. PORT="$(docker port "$CID" 3081 | head -n1 | sed 's/.*://')"
  73. echo " 容器 $CID 已启动,映射端口 $PORT"
  74. echo "==> 等待服务就绪(最多 240s)"
  75. # 仅 200 视为就绪:502(Caddy 已起但 dsh 未就绪)与 000(未监听)都继续等;
  76. # 401 是 Caddy basic auth 层拒绝、不经过反代,也不能代表 dsh 就绪
  77. #
  78. # Only 200 means ready: 502 (Caddy up but dsh not yet) and 000 (not listening) keep waiting;
  79. # 401 is rejected at the Caddy basic-auth layer without touching the proxy, so it proves nothing
  80. #
  81. # 阈值从 120s 提到 240s:0.1.2-rc.1 在 GitHub Actions runner 上首启需 ~150s
  82. # (x-cmd 80MB 下载 + dsh web bundles 懒加载),120s 反复误报上游慢。240s 留一倍余量,
  83. # 真挂的场景仍会超时但能区分「慢」vs「挂」
  84. #
  85. # Threshold raised 120s→240s: dsh 0.1.2-rc.1 first boot on GitHub Actions runner
  86. # takes ~150s (80MB x-cmd download + dsh web bundles lazy load). 120s kept misfiring
  87. # on upstream slowness; 240s leaves 1× headroom while still failing on real hangs
  88. READY=0
  89. LAST_CODE=000
  90. START_TS="$(date +%s)"
  91. for _ in $(seq 1 120); do
  92. CODE="$(curl -s -u admin:smoketest -o /dev/null -w '%{http_code}' "http://127.0.0.1:$PORT/" || true)"
  93. LAST_CODE="$CODE"
  94. if [ "$CODE" = "200" ]; then READY=1; break; fi
  95. sleep 2
  96. done
  97. if [ "$READY" != 1 ]; then
  98. ELAPSED=$(( $(date +%s) - START_TS ))
  99. echo "错误:服务 ${ELAPSED}s 内未就绪(最后一次 HTTP code=$LAST_CODE)" >&2
  100. docker logs "$CID" 2>&1 | tail -30 >&2
  101. exit 1
  102. fi
  103. echo "==> basic auth:无凭据应 401"
  104. CODE="$(curl -s -o /dev/null -w '%{http_code}' "http://127.0.0.1:$PORT/")"
  105. [ "$CODE" = "401" ] || { echo "错误:期望 401 实际 $CODE" >&2; exit 1; }
  106. echo "==> basic auth:带凭据应 200"
  107. CODE="$(curl -s -u admin:smoketest -o /dev/null -w '%{http_code}' "http://127.0.0.1:$PORT/")"
  108. [ "$CODE" = "200" ] || { echo "错误:期望 200 实际 $CODE" >&2; exit 1; }
  109. echo "==> 特权 API fence:伪造 Host 直连容器内 dsh 应 403(fence 拒绝未授权来源)"
  110. # 不经 Caddy 直连 127.0.0.1:3080,伪造浏览器 Host=evil.com——dsh 视其为非本机来源
  111. CODE="$(docker exec "$CID" curl -s -o /dev/null -w '%{http_code}' -X POST \
  112. -H 'Host: evil.com' -H 'Content-Type: application/json' -d '{}' \
  113. "http://127.0.0.1:3080/api")"
  114. [ "$CODE" = "403" ] || { echo "错误:fence 未拒绝伪造 Host(期望 403 实际 $CODE)" >&2; exit 1; }
  115. echo "==> 特权 API fence:经 Caddy 伪造 Host/Origin 应被改写为 loopback(非 403)"
  116. # 浏览器形态请求经 Caddy:Host/Origin 被改写为 127.0.0.1:3080,fence 通过;
  117. # 若 Caddyfile 的 header_up 两行失效,此处将返回 403——即改写链路回归
  118. CODE="$(curl -s -u admin:smoketest -o /dev/null -w '%{http_code}' -X POST \
  119. -H 'Host: evil.com' -H 'Origin: http://127.0.0.1:3080' \
  120. -H 'Content-Type: application/json' -d '{}' "http://127.0.0.1:$PORT/api")"
  121. [ "$CODE" = "400" ] || [ "$CODE" = "404" ] || { echo "错误:期望改写生效(400/404,fence 通过后坏 payload 的正常响应)实际 $CODE" >&2; exit 1; }
  122. echo "==> 进程检查:等待 dsh 与 caddy 均 RUNNING"
  123. # dsh 首启初始化(profile/bundles)慢于 Caddy,需轮询等待其越过 startsecs
  124. #
  125. # dsh's first-run init (profile/bundles) is slower than Caddy; poll until it passes startsecs
  126. READY=0
  127. for _ in $(seq 1 30); do
  128. STATUS="$(docker exec "$CID" supervisorctl status 2>/dev/null || true)"
  129. if grep -qE '^dsh[[:space:]]+RUNNING' <<<"$STATUS" && grep -qE '^caddy[[:space:]]+RUNNING' <<<"$STATUS"; then
  130. READY=1; break
  131. fi
  132. sleep 2
  133. done
  134. if [ "$READY" != 1 ]; then
  135. printf '错误:进程未全部 RUNNING\n%s\n' "$STATUS" >&2
  136. docker logs "$CID" 2>&1 | tail -30 >&2
  137. exit 1
  138. fi
  139. echo "==> trustedHosts 注入检查"
  140. # DSH_TRUSTED_HOSTS 在启动时注入 cordis.patch.yml(entrypoint 在 dsh 首启前预写,
  141. # dsh 的 initProfile 对已存在文件不覆盖);此处验证注入结果
  142. docker exec "$CID" sh -c 'grep -q "smoke.example.com" /home/node/.dsh/profiles/web/cordis.patch.yml' \
  143. || { echo "错误:trustedHosts 未注入 cordis.patch.yml" >&2; exit 1; }
  144. docker exec "$CID" sh -c 'yq ". | length" /home/node/.dsh/profiles/web/cordis.patch.yml | grep -q "^1$"' \
  145. || { echo "错误:cordis.patch.yml 应为 1 个条目" >&2; exit 1; }
  146. echo "==> 冒烟测试全部通过 ✓"