smoke-test.sh 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. # cookie 文件:0.1.2-rc.1+ 鉴权流程创建的临时 cookie,与 SMOKE_HOME 一起删即可
  22. # (cookie jar 在 SMOKE_PARENT 下,独立 mktemp 但跟着 SMOKE_HOME 的 rm -rf 也走
  23. # 因为 mktemp -p 选了 SMOKE_PARENT;不过保险起见显式删一次)
  24. [ -n "${SMOKE_COOKIE:-}" ] && [ -f "$SMOKE_COOKIE" ] && rm -f "$SMOKE_COOKIE" 2>/dev/null || true
  25. docker rm -f "$CID" >/dev/null 2>&1 || true
  26. if [ -n "${SMOKE_HOME:-}" ] && [ -d "$SMOKE_HOME" ]; then
  27. # 先放宽权限再递归删:容器内 uid 1000 (node) 写下的子文件可能只对本人可写,
  28. # runner (uid 1001) 在 sticky /tmp 下无法删;但若 SMOKE_PARENT 已经是 runner 自己的
  29. # 非 sticky 目录,权限放宽足以应对权限位差异
  30. #
  31. # Loosen perms first then recurse: container-internal uid 1000 (node) may have left
  32. # files only writable by themselves; on a sticky /tmp a uid-1001 runner can't drop
  33. # those. If SMOKE_PARENT is already a non-sticky runner-owned dir, chmod is enough.
  34. chmod -R u+w "$SMOKE_HOME" 2>/dev/null || true
  35. rm -rf "$SMOKE_HOME" 2>/dev/null || warn "无法清理 SMOKE_HOME=$SMOKE_HOME(容器子文件权限/sticky 导致),手动 rm 即可"
  36. fi
  37. return "$rc" # 关键:把主命令退出码冒出去
  38. # Critical: propagate the main command's exit code
  39. }
  40. warn() { printf 'WARN: %s\n' "$*" >&2; }
  41. trap cleanup EXIT
  42. cleanup
  43. echo "==> 构建镜像(DSH_VERSION=$VERSION)"
  44. docker build --build-arg DSH_VERSION="$VERSION" -t "$IMAGE" .
  45. echo "==> 校验 dsh --version"
  46. OUT="$(docker run --rm --entrypoint dsh "$IMAGE" --version)"
  47. echo " dsh --version => $OUT"
  48. grep -q "$VERSION" <<<"$OUT" || { echo "错误:版本不符(期望包含 $VERSION)" >&2; exit 1; }
  49. echo "==> 启动容器冒烟"
  50. # 预置假 x-cmd(x 可执行即跳过 entrypoint 首启 300s 下载)
  51. #
  52. # Pre-seed a fake x-cmd (an executable x skips the entrypoint's 300s first-run download)
  53. #
  54. # SMOKE_PARENT 选择:必须是非 sticky 目录。容器内 dsh/caddy 以 uid 1000 (node) 写入
  55. # bind mount 的子文件,runner 是 uid 1001;sticky /tmp + 跨 uid → cleanup 时 EPERM。
  56. # 优先用 GitHub Actions 提供的 RUNNER_TEMP(runner 独占,非 sticky),否则用
  57. # 系统 TMPDIR(部分发行版已重定向到 /run/user/<uid>),否则回退 $HOME
  58. # (Linux home 不 sticky,macOS 也只有 /tmp 有 sticky)。$HOME 兜底保留本地跑的可用性
  59. #
  60. # SMOKE_PARENT must be non-sticky: container dsh/caddy runs as uid 1000 (node) and
  61. # writes into the bind-mount, while the runner is uid 1001; sticky /tmp + cross-uid
  62. # triggers EPERM on cleanup. Prefer RUNNER_TEMP (runner-exclusive, non-sticky), then
  63. # TMPDIR (some distros point it at /run/user/<uid>), then $HOME (Linux home is not
  64. # sticky; macOS only has sticky on /tmp). $HOME fallback keeps local runs working.
  65. SMOKE_PARENT="${RUNNER_TEMP:-${TMPDIR:-$HOME}}"
  66. mkdir -p "$SMOKE_PARENT"
  67. SMOKE_HOME="$(mktemp -d -p "$SMOKE_PARENT" smoke-XXXXXX)"
  68. # mktemp 默认 0700。容器首启会把 bind mount 根 chown 成 node(uid 1000),但权限位不变:
  69. # runner(GitHub Actions 上是 uid 1001)便无法 traverse,即使 web-launch-url.txt 已生成
  70. # 也读不到(表现为 60s 超时 + `tail: Permission denied`)。放宽目录到 755 让 runner 能进入,
  71. # 文件本身仍是 0644(只读)。本地跑时 runner uid 恰好也是 1000,不触发,故 CI 才暴露。
  72. #
  73. # mktemp defaults to 0700. The container's first-run chowns the bind-mount root to
  74. # node (uid 1000) without changing mode bits, so the runner (uid 1001 on GitHub Actions)
  75. # cannot traverse it and cannot read web-launch-url.txt even once it exists (symptom:
  76. # 60 s timeout + `tail: Permission denied`). Loosen the directory to 755 so the runner
  77. # can enter; files stay 0644 (read-only). Local runs use uid 1000, so only CI exposes this.
  78. chmod 755 "$SMOKE_HOME"
  79. mkdir -p "$SMOKE_HOME/.x-cmd.root/bin"
  80. printf '#!/bin/sh\nexit 0\n' > "$SMOKE_HOME/.x-cmd.root/bin/x"
  81. docker run -d --name "$CID" \
  82. -v "$SMOKE_HOME:/home/node" \
  83. -e DSH_AUTH_USER=admin -e DSH_AUTH_PASSWORD=smoketest \
  84. -e DEEPSEEK_API_KEY=sk-dummy \
  85. -e DSH_TRUSTED_HOSTS=smoke.example.com \
  86. -p 127.0.0.1::3081 "$IMAGE" >/dev/null
  87. PORT="$(docker port "$CID" 3081 | head -n1 | sed 's/.*://')"
  88. echo " 容器 $CID 已启动,映射端口 $PORT"
  89. echo "==> 等待服务就绪(最多 240s)"
  90. # 等三件事之一:1) 200(≤0.1.1-rc.2 旧 caddy basic auth 通过)2) 401(0.1.2-rc.1+ 路径,
  91. # caddy 活,dsh 鉴权拒绝无 token)3) entrypoint 后台抓到的 web-launch-url.txt 出现
  92. # (dsh 启动后 5-30 秒内)。任一即就绪。502/000 继续等
  93. #
  94. # Wait for any of: 1) 200 (≤0.1.1-rc.2 old caddy basic-auth) 2) 401 (0.1.2-rc.1+,
  95. # caddy alive, dsh rejects no-token) 3) web-launch-url.txt written (5-30 s after dsh).
  96. # Any one means "ready". 502/000 keep waiting.
  97. READY=0
  98. LAST_CODE=000
  99. URL_FILE="$SMOKE_HOME/.dsh/web-launch-url.txt"
  100. START_TS="$(date +%s)"
  101. for _ in $(seq 1 120); do
  102. CODE="$(curl -s -o /dev/null -w '%{http_code}' "http://127.0.0.1:$PORT/" || true)"
  103. LAST_CODE="$CODE"
  104. if [ "$CODE" = "200" ] || [ "$CODE" = "401" ] || [ -s "$URL_FILE" ]; then
  105. READY=1; break
  106. fi
  107. sleep 2
  108. done
  109. if [ "$READY" != 1 ]; then
  110. ELAPSED=$(( $(date +%s) - START_TS ))
  111. echo "错误:服务 ${ELAPSED}s 内 Caddy 仍未监听(最后一次 HTTP code=$LAST_CODE,token 文件未出现)" >&2
  112. echo " (502/000 = 仍在启动;持续 502 = 上游启动慢/挂)" >&2
  113. docker logs "$CID" 2>&1 | tail -30 >&2
  114. exit 1
  115. fi
  116. # 版本分叉:检测 dsh 版本决定鉴权流程
  117. # dsh <0.1.2-rc.1 (e.g. 0.1.1-rc.2):用 caddy basic auth(DSH_AUTH_USER/PASSWORD)
  118. # dsh >=0.1.2-rc.1:用 token+cookie(dsh 0.1.2-rc.1 引入的一次性 token + 持久 cookie,
  119. # caddy 0.1.2-rc.1+ 不再 basic auth,DSH_AUTH_USER/PASSWORD 保留为兼容位但 caddy 不用)
  120. #
  121. # Version fork: pick the auth flow by dsh version
  122. # dsh <0.1.2-rc.1 (e.g. 0.1.1-rc.2): caddy basic auth
  123. # dsh >=0.1.2-rc.1: token + cookie (dsh 0.1.2-rc.1 one-time token + persistent cookie;
  124. # caddy 0.1.2-rc.1+ no longer basic-auths, DSH_AUTH_USER/PASSWORD are kept for compat)
  125. case "$VERSION" in
  126. 0.1.0-*|0.1.1-*)
  127. # 旧版鉴权:basic auth(dsh ≤0.1.1-rc.2)
  128. echo "==> 旧版鉴权:basic auth(dsh $VERSION)"
  129. echo "==> basic auth:无凭据应 401"
  130. CODE="$(curl -s -o /dev/null -w '%{http_code}' "http://127.0.0.1:$PORT/")"
  131. [ "$CODE" = "401" ] || { echo "错误:期望 401 实际 $CODE" >&2; exit 1; }
  132. echo "==> basic auth:带凭据应 200"
  133. CODE="$(curl -s -u admin:smoketest -o /dev/null -w '%{http_code}' "http://127.0.0.1:$PORT/")"
  134. [ "$CODE" = "200" ] || {
  135. echo "错误:带 admin:smoketest 凭据应返 200,实际 $CODE" >&2
  136. docker logs "$CID" 2>&1 | tail -30 >&2
  137. exit 1
  138. }
  139. # basic auth 路径:API fence 步骤用 basic auth
  140. AUTH_FLAGS=(-u admin:smoketest)
  141. ;;
  142. *)
  143. # 新版鉴权:token + cookie(dsh ≥0.1.2-rc.1)
  144. echo "==> 新版鉴权:token + cookie(dsh $VERSION)"
  145. # 等 token URL 文件(entrypoint 后台任务写入,dsh 启动后 1-30 秒内出现)
  146. # Wait for token URL file (entrypoint's background task writes it 1-30 s after dsh starts)
  147. for _ in $(seq 1 60); do
  148. [ -s "$URL_FILE" ] && break
  149. sleep 1
  150. done
  151. if [ ! -s "$URL_FILE" ]; then
  152. echo "错误:dsh 启动后 60s 内未在 $URL_FILE 捕获到 ?token= URL" >&2
  153. echo "--- 容器 web-server.log 后 30 行(dsh 启动日志 tee)---" >&2
  154. tail -30 "$SMOKE_HOME/.dsh/web-server.log" >&2 || true
  155. docker logs "$CID" 2>&1 | tail -30 >&2
  156. exit 1
  157. fi
  158. LAUNCH_URL="$(cat "$URL_FILE")"
  159. echo " 抓取到 dsh 启动 URL: $LAUNCH_URL"
  160. # URL_FILE 记录的是容器内 loopback 地址(dsh 强制 loopback bind),宿主机不能直连:
  161. # 必须把主机:端口改写到映射端口 $PORT,才能经 Caddy 走真实浏览器路径;token 原样保留。
  162. #
  163. # URL_FILE holds the container-internal loopback address (dsh hard-binds loopback);
  164. # the host cannot reach it directly. Rewrite host:port to the mapped port $PORT so the
  165. # request goes through Caddy like a real browser; keep the token value as-is.
  166. TOKEN="$(printf '%s' "$LAUNCH_URL" | sed -n 's#.*[?&]token=\([^&]*\).*#\1#p')"
  167. [ -n "$TOKEN" ] || { echo "错误:无法从 $LAUNCH_URL 解析 token" >&2; exit 1; }
  168. LAUNCH_URL="http://127.0.0.1:${PORT}/?token=${TOKEN}"
  169. # 一次性 token 换持久 cookie:GET ?token=XXX → 303 + Set-Cookie
  170. # Exchange one-time token for persistent cookie: GET ?token=XXX → 303 + Set-Cookie
  171. SMOKE_COOKIE="$(mktemp -p "$SMOKE_PARENT" smoke-cookie-XXXXXX)"
  172. CODE="$(curl -sS -c "$SMOKE_COOKIE" -o /dev/null -w '%{http_code}' "$LAUNCH_URL" || true)"
  173. if [ "$CODE" != "303" ]; then
  174. echo "错误:带 token 访问应返 303 重定向,实际 $CODE" >&2
  175. cat "$SMOKE_COOKIE" >&2 || true
  176. docker logs "$CID" 2>&1 | tail -20 >&2
  177. exit 1
  178. fi
  179. if ! grep -q "dsh" "$SMOKE_COOKIE" 2>/dev/null; then
  180. echo "错误:token 交换后未拿到 Set-Cookie(dsh 应发 HttpOnly cookie)" >&2
  181. cat "$SMOKE_COOKIE" >&2 || true
  182. exit 1
  183. fi
  184. # cookie 同时要供宿主机 curl 与容器内 docker exec curl 使用,而 cookie 文件是宿主
  185. # 路径、容器内不存在;统一抽成 Cookie header 字符串,两边都可用(HttpOnly cookie
  186. # 在 curl 的 cookie jar 里以 #HttpOnly_ 前缀记录,解析时需一并纳入)。
  187. #
  188. # The cookie must work for both the host curl and the in-container `docker exec curl`,
  189. # but the cookie jar is a host path that doesn't exist inside the container. Extract a
  190. # Cookie header string usable on both sides (HttpOnly cookies are stored with a
  191. # #HttpOnly_ prefix in curl's jar, so include them when parsing).
  192. COOKIE_HEADER="Cookie: $(awk 'NF && ($1 ~ /^#HttpOnly_/ || $0 !~ /^#/) {print $6"="$7}' "$SMOKE_COOKIE" | paste -sd'; ' -)"
  193. echo "==> 鉴权:带 cookie 应 200"
  194. CODE="$(curl -sS -H "$COOKIE_HEADER" -o /dev/null -w '%{http_code}' "http://127.0.0.1:$PORT/")"
  195. if [ "$CODE" != "200" ]; then
  196. echo "错误:带 cookie 应返 200,实际 $CODE" >&2
  197. echo "--- 诊断:直连容器内 dsh 3080(带 cookie)---" >&2
  198. docker exec "$CID" curl -sS -H "$COOKIE_HEADER" -o /dev/null -w ' HTTP %{http_code}\n' "http://127.0.0.1:3080/" >&2 || true
  199. docker logs "$CID" 2>&1 | tail -20 | sed 's/^/ /' >&2
  200. exit 1
  201. fi
  202. echo " 鉴权通过(cookie 持久化到 $SMOKE_HOME/.dsh/.credentials.yaml)"
  203. # 新版路径:API fence 步骤用 cookie(header 字符串,host/容器通用)
  204. #
  205. # New auth path: the fence step uses the cookie (a header string, valid on host and in-container)
  206. AUTH_FLAGS=(-H "$COOKIE_HEADER")
  207. ;;
  208. esac
  209. echo "==> 特权 API fence:伪造 Host 直连容器内 dsh 应 403(fence 拒绝未授权来源)"
  210. # 不经 Caddy 直连 127.0.0.1:3080,伪造浏览器 Host=evil.com——dsh 视其为非本机来源。
  211. # 此处用容器内 curl(不经 caddy,caddy 不参与;用 cookie/basic auth 都无法过 dsh 的
  212. # Host 来源检查——fence 拒绝非 loopback Host;仅 0.1.2-rc.1+ 需 cookie,0.1.1-rc.2 用 basic auth)
  213. #
  214. # Bypass Caddy: curl dsh 3080 directly from inside the container. dsh's fence rejects
  215. # non-loopback Host. Auth flavor: 0.1.1-rc.2 uses basic auth, 0.1.2-rc.1+ uses cookie.
  216. # Either way, the fake Host=evil.com is what we test, not auth.
  217. CODE="$(docker exec "$CID" curl -s "${AUTH_FLAGS[@]}" -o /dev/null -w '%{http_code}' -X POST \
  218. -H 'Host: evil.com' -H 'Content-Type: application/json' -d '{}' \
  219. "http://127.0.0.1:3080/api")"
  220. [ "$CODE" = "403" ] || { echo "错误:fence 未拒绝伪造 Host(期望 403 实际 $CODE)" >&2; exit 1; }
  221. echo "==> 特权 API fence:经 Caddy 伪造 Host/Origin 应被改写为 loopback(非 403)"
  222. # 浏览器形态请求经 Caddy:Host/Origin 被改写为 127.0.0.1:3080,fence 通过;
  223. # 若 Caddyfile 的 header_up 两行失效,此处将返回 403——即改写链路回归
  224. CODE="$(curl -s "${AUTH_FLAGS[@]}" -o /dev/null -w '%{http_code}' -X POST \
  225. -H 'Host: evil.com' -H 'Origin: http://127.0.0.1:3080' \
  226. -H 'Content-Type: application/json' -d '{}' "http://127.0.0.1:$PORT/api")"
  227. [ "$CODE" = "400" ] || [ "$CODE" = "404" ] || { echo "错误:期望改写生效(400/404,fence 通过后坏 payload 的正常响应)实际 $CODE" >&2; exit 1; }
  228. echo "==> 进程检查:等待 dsh 与 caddy 均 RUNNING"
  229. # dsh 首启初始化(profile/bundles)慢于 Caddy,需轮询等待其越过 startsecs
  230. #
  231. # dsh's first-run init (profile/bundles) is slower than Caddy; poll until it passes startsecs
  232. READY=0
  233. for _ in $(seq 1 30); do
  234. STATUS="$(docker exec "$CID" supervisorctl status 2>/dev/null || true)"
  235. if grep -qE '^dsh[[:space:]]+RUNNING' <<<"$STATUS" && grep -qE '^caddy[[:space:]]+RUNNING' <<<"$STATUS"; then
  236. READY=1; break
  237. fi
  238. sleep 2
  239. done
  240. if [ "$READY" != 1 ]; then
  241. printf '错误:进程未全部 RUNNING\n%s\n' "$STATUS" >&2
  242. docker logs "$CID" 2>&1 | tail -30 >&2
  243. exit 1
  244. fi
  245. echo "==> trustedHosts 注入检查"
  246. # DSH_TRUSTED_HOSTS 在启动时注入 cordis.patch.yml(entrypoint 在 dsh 首启前预写,
  247. # dsh 的 initProfile 对已存在文件不覆盖);此处验证注入结果
  248. #
  249. # 断言按语义(id=connection 的条目恰好 1 条、trustedHosts 恰为 smoke.example.com),
  250. # 不按「顶层条目总数 == 1」:dsh 可能在用户 patch 之外自行追加条目(插件启停记录等),
  251. # 总条目数断言会把这种正常追加误判成注入失败。此前 #19 只留下一句「应为 1 个条目」,
  252. # 日志里既没有实际条目数也没有文件内容,无从定位;本次同时补上失败现场 dump
  253. #
  254. # Assert on semantics (exactly one id=connection row whose trustedHosts is
  255. # smoke.example.com), not on the top-level row count: dsh may legitimately append
  256. # rows of its own (plugin enable/disable records), and a "total == 1" assert
  257. # misreads that as a failed injection. Issue #19 previously left only the message
  258. # "should be 1 entry" — no actual count, no file content; this also adds a
  259. # failure-scene dump.
  260. PATCH_PATH=/home/node/.dsh/profiles/web/cordis.patch.yml
  261. dump_patch() {
  262. {
  263. echo "--- 诊断:$PATCH_PATH ---"
  264. docker exec "$CID" sh -c 'ls -la /home/node/.dsh/profiles/web' || true
  265. echo "--- 文件内容(cat -n)---"
  266. docker exec "$CID" sh -c "cat -n '$PATCH_PATH'" || true
  267. echo "--- yq / jq 版本与条目数 ---"
  268. docker exec "$CID" sh -c "command -v yq; yq --version; jq --version; yq '. | length' '$PATCH_PATH'" || true
  269. echo "--- 容器日志(末 30 行)---"
  270. docker logs "$CID" 2>&1 | tail -30 || true
  271. } >&2
  272. }
  273. if ! docker exec "$CID" sh -c "grep -q 'smoke.example.com' '$PATCH_PATH'"; then
  274. echo "错误:trustedHosts 未注入 $PATCH_PATH" >&2
  275. dump_patch
  276. exit 1
  277. fi
  278. # id=connection 条目必须恰好 1 条(entrypoint 只写一条;重复=注入逻辑回归)
  279. #
  280. # Exactly one id=connection row (the entrypoint writes a single one; a duplicate means the injection regressed)
  281. CONN_ROWS="$(docker exec "$CID" sh -c "yq '[.[] | select(.id == \"connection\")] | length' '$PATCH_PATH'" 2>/dev/null || true)"
  282. if [ "$CONN_ROWS" != "1" ]; then
  283. echo "错误:$PATCH_PATH 中 id=connection 的条目应为 1 条,实际 [${CONN_ROWS:-查询失败}]" >&2
  284. dump_patch
  285. exit 1
  286. fi
  287. # connection.trustedHosts 必须恰为 smoke.example.com(注入值被 Caddy/其他层覆盖也算回归)
  288. #
  289. # connection.trustedHosts must be exactly smoke.example.com (an overwrite by Caddy or another layer is a regression too)
  290. CONN_HOSTS="$(docker exec "$CID" sh -c "yq -r '.[] | select(.id == \"connection\") | .config.trustedHosts // [] | join(\",\")' '$PATCH_PATH'" 2>/dev/null || true)"
  291. if [ "$CONN_HOSTS" != "smoke.example.com" ]; then
  292. echo "错误:connection.trustedHosts 应为 smoke.example.com,实际 [${CONN_HOSTS:-空/查询失败}]" >&2
  293. dump_patch
  294. exit 1
  295. fi
  296. # 始终打印「实际条目数 + 注入值」:条目数不是断言条件,但它是判断 dsh 是否追加过条目的关键证据
  297. #
  298. # Always print the real row count and injected value: the count is not an assert condition,
  299. # but it is the key evidence for whether a dsh version appended rows of its own
  300. PATCH_LEN="$(docker exec "$CID" sh -c "yq '. | length' '$PATCH_PATH'" 2>/dev/null || echo '?')"
  301. echo " 条目数 $PATCH_LEN(connection 1 条,trustedHosts=[$CONN_HOSTS])"
  302. echo "==> 冒烟测试全部通过 ✓"