run_interface_tests_cron.sh 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. #!/usr/bin/env bash
  2. # =============================================================================
  3. # CFC 接口测试闭环(定时任务主体)
  4. # 流程:git pull 最新 -> 检测新增/变更需求 -> 跑冒烟(含回归对比) ->
  5. # 为新增接口补全用例并跑新测试 -> 输出测试用例完成情况(总数/已完成/未开始原因) ->
  6. # 提交测试结果 -> push 到远端(与开发团队闭环)
  7. #
  8. # 网络说明:原默认目标 192.168.16.251 已不可用(上游失效)。
  9. # 当前默认测试目标为 cfc.iwintrue.com(公网可达、后端在线)。
  10. # 本脚本设计为在宿主机用 Windows 任务计划程序每 30 分钟调用,
  11. # 不要在受限沙箱后台运行(后台网络被拦截)。
  12. # =============================================================================
  13. set -uo pipefail
  14. REPO_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  15. cd "$REPO_DIR" || exit 1
  16. LOG_DIR="$REPO_DIR/logs"
  17. mkdir -p "$LOG_DIR"
  18. # 防重入:若上轮仍在 25 分钟内,跳过本轮(避免 30 分钟间隔下长耗时测试重叠)
  19. LOCK="$LOG_DIR/.loop.lock"
  20. NOW=$(date +%s)
  21. if [ -f "$LOCK" ]; then
  22. LAST=$(cat "$LOCK" 2>/dev/null || echo 0)
  23. if [ $((NOW - LAST)) -lt 1500 ]; then
  24. echo "[$(date +%H:%M:%S)] 上轮仍在运行(锁 $(( (NOW - LAST) / 60 )) 分钟前),跳过本轮"
  25. exit 0
  26. fi
  27. fi
  28. echo "$NOW" > "$LOCK"
  29. trap 'rm -f "$LOCK"' EXIT
  30. BRANCH="${CFC_BRANCH:-cfclub}"
  31. BASE_URL="${CFC_BASE:-http://cfc.iwintrue.com}"
  32. HOST_HDR="${CFC_HOST:-cfc.iwintrue.com}"
  33. TIMEOUT="${CFC_TIMEOUT:-8}"
  34. TEST_SCRIPT="ops_interface_smoke.py"
  35. SCANNER="scan_frontend_endpoints.py"
  36. TS="$(date +%Y%m%d_%H%M%S)"
  37. RUN_DIR="$LOG_DIR/run_$TS"
  38. mkdir -p "$RUN_DIR"
  39. CUR_TSV="$RUN_DIR/results_$TS.tsv"
  40. ENDPOINTS_CUR="$RUN_DIR/endpoints_$TS.txt"
  41. EXTRA_CASES="$REPO_DIR/interface_test_extra_cases.tsv"
  42. BASELINE_EP="$REPO_DIR/interface_test_baseline_endpoints.txt"
  43. PREV_TSV="$REPO_DIR/interface_test_prev.tsv"
  44. TREND="$REPO_DIR/interface_test_trend.log"
  45. MANAGED="INTERFACE_TEST_REPORT.md interface_test_report.tsv cfc-frontend/INTERFACE_TEST_MATRIX.md ops_interface_smoke.py interface_test_bodies.tsv"
  46. log() { echo "[$(date +%H:%M:%S)] $*" | tee -a "$LOG_DIR/interface_cron.log"; }
  47. log "===== CFC 接口测试闭环 开始 $TS ====="
  48. log "REPO=$REPO_DIR BRANCH=$BRANCH BASE=$BASE_URL HOST=$HOST_HDR"
  49. # ---------------------------------------------------------------------------
  50. # 1) 拉取远端最新(含开发团队的修复/新需求)
  51. # 仅丢弃我们生成的报告类文件本地改动(每次都会重新生成),其余未提交改动用
  52. # --autostash 处理,避免破坏用户其他在途工作。
  53. # ---------------------------------------------------------------------------
  54. git checkout -- INTERFACE_TEST_REPORT.md interface_test_report.tsv cfc-frontend/INTERFACE_TEST_MATRIX.md 2>/dev/null
  55. if git pull --rebase --autostash origin "$BRANCH" >>"$LOG_DIR/interface_cron.log" 2>&1; then
  56. log "git pull 成功"
  57. else
  58. log "git pull 失败(可能本地有冲突或凭证问题),用当前代码继续"
  59. fi
  60. # ---------------------------------------------------------------------------
  61. # 2) 检测新增/变更需求(前端接口面变化)
  62. # ---------------------------------------------------------------------------
  63. python "$SCANNER" --out "$ENDPOINTS_CUR" 2>/dev/null
  64. NEWEP=""; RMVEP=""
  65. if [ -f "$BASELINE_EP" ]; then
  66. NEWEP=$(comm -13 <(sort "$BASELINE_EP") <(sort "$ENDPOINTS_CUR"))
  67. RMVEP=$(comm -23 <(sort "$BASELINE_EP") <(sort "$ENDPOINTS_CUR"))
  68. n_new=$(echo "$NEWEP" | grep -c . || true)
  69. n_rmv=$(echo "$RMVEP" | grep -c . || true)
  70. log "接口面变化:新增 $n_new 个,移除 $n_rmv 个"
  71. if [ "$n_new" -gt 0 ]; then
  72. : > "$EXTRA_CASES"
  73. while IFS= read -r line; do
  74. [ -z "$line" ] && continue
  75. m=$(echo "$line" | awk '{print $1}')
  76. p=$(echo "$line" | awk '{print $2}')
  77. printf 'auto\tauto\t%s\t%s\tfalse\tfalse\n' "$m" "$p" >> "$EXTRA_CASES"
  78. done <<< "$NEWEP"
  79. log "已为新增接口生成补全用例 -> $EXTRA_CASES"
  80. fi
  81. else
  82. log "首次运行:建立接口基线(不触发补全)"
  83. fi
  84. cp "$ENDPOINTS_CUR" "$BASELINE_EP"
  85. # ---------------------------------------------------------------------------
  86. # 3) 跑冒烟测试(前台调用,含登录态;新增接口通过 --extra 一并测试)
  87. # ---------------------------------------------------------------------------
  88. EXTRA_FLAG=""
  89. [ -s "$EXTRA_CASES" ] && EXTRA_FLAG="--extra $EXTRA_CASES"
  90. # 真实请求体映射:存在则注入带参冒烟,破除空体 45% 天花板;缺失则自动跳过
  91. BODY_FLAG=""
  92. [ -s "interface_test_bodies.tsv" ] && BODY_FLAG="--body-map interface_test_bodies.tsv"
  93. log "开始执行冒烟测试 $EXTRA_FLAG $BODY_FLAG"
  94. python "$TEST_SCRIPT" --base "$BASE_URL" --host "$HOST_HDR" \
  95. --timeout "$TIMEOUT" --auto-login --authed $EXTRA_FLAG $BODY_FLAG \
  96. --report "$CUR_TSV" > "$RUN_DIR/smoke_stdout.txt" 2>&1
  97. log "冒烟测试完成,结果 -> $CUR_TSV"
  98. # ---------------------------------------------------------------------------
  99. # 4) 汇总 + 回归对比 + 生成报告
  100. # ---------------------------------------------------------------------------
  101. python - "$CUR_TSV" "$PREV_TSV" "INTERFACE_TEST_REPORT.md" "$TS" "$TREND" <<'PY'
  102. import sys, collections, os
  103. cur_tsv, prev_tsv, rpt, ts, trend = sys.argv[1:6]
  104. def load(path):
  105. d = {}
  106. try:
  107. with open(path, encoding='utf-8') as f:
  108. next(f, None)
  109. for line in f:
  110. c = line.rstrip('\n').split('\t')
  111. if len(c) < 7: continue
  112. page, fn, method, path, http, biz, cat = c
  113. d[(method, path)] = (page, fn, http, biz, cat)
  114. except FileNotFoundError:
  115. pass
  116. return d
  117. cur = load(cur_tsv)
  118. prev = load(prev_tsv) if prev_tsv and os.path.exists(prev_tsv) else {}
  119. cats = collections.Counter(v[4] for v in cur.values())
  120. total = len(cur)
  121. ok = cats.get('BIZ_OK', 0)
  122. rate = ok * 100 // total if total else 0
  123. # 回归对比
  124. fixed = [] # 之前非 OK -> 现在 OK
  125. regressed = [] # 之前 OK -> 现在非 OK
  126. for k, v in cur.items():
  127. cat = v[4]
  128. if k in prev:
  129. pcat = prev[k][4]
  130. if pcat != 'BIZ_OK' and cat == 'BIZ_OK':
  131. fixed.append((k, pcat, cat))
  132. elif pcat == 'BIZ_OK' and cat != 'BIZ_OK':
  133. regressed.append((k, pcat, cat))
  134. def top(cat, n=20):
  135. return [(k[0], k[1], v[3], v[4]) for k, v in cur.items() if v[4] == cat][:n]
  136. lines = []
  137. lines.append(f"# CFC 接口冒烟测试报告 ({ts})\n")
  138. lines.append(f"- 目标: {os.environ.get('CFC_HOST', 'cfc.iwintrue.com')} 用例总数: **{total}**")
  139. lines.append(f"- 业务成功率(BIZ_OK): **{ok}/{total} = {rate}%**\n")
  140. lines.append("## 分类汇总")
  141. for k in ('BIZ_OK','BIZ_FAIL','AUTH_FAIL','NOT_FOUND','SERVER_ERR','NON_JSON','CONN_ERR'):
  142. if cats.get(k): lines.append(f"- {k}: {cats[k]}")
  143. lines.append("")
  144. if fixed:
  145. lines.append(f"## ✅ 回归修复({len(fixed)} 个,之前失败现已通过)")
  146. for (m,p),pc,_ in fixed[:30]:
  147. lines.append(f"- `{m} {p}` (原 {pc})")
  148. lines.append("")
  149. if regressed:
  150. lines.append(f"## ⚠️ 回归劣化({len(regressed)} 个,之前通过现失败)")
  151. for (m,p),_,c in regressed[:30]:
  152. lines.append(f"- `{m} {p}` (现 {c})")
  153. lines.append("")
  154. lines.append(f"## 需开发团队关注的契约缺口 (NOT_FOUND={cats.get('NOT_FOUND',0)})")
  155. for m,p,b,c in top('NOT_FOUND'):
  156. lines.append(f"- `{m} {p}` (biz={b}, {c})")
  157. lines.append("")
  158. lines.append(f"## 健壮性风险 (SERVER_ERR={cats.get('SERVER_ERR',0)})")
  159. for m,p,b,c in top('SERVER_ERR'):
  160. lines.append(f"- `{m} {p}` (http={b}, {c})")
  161. lines.append("")
  162. lines.append("> 说明:冒烟为空体请求,部分 500/400 属预期(缺必填参数)。NOT_FOUND 与回归劣化才需优先处理。")
  163. # ───────────────────────── 守卫:识别无效轮 ─────────────────────────
  164. # 上游宕机 / 网关抖动 / 登录失败 / 部分轮 不应被当作真实回归提交,亦不可污染
  165. # 回归基线(PREV_TSV)。命中则标 INVALID,由 bash 侧跳过 commit/push/基线更新。
  166. non_json = cats.get('NON_JSON', 0)
  167. server_err = cats.get('SERVER_ERR', 0)
  168. conn_err = cats.get('CONN_ERR', 0)
  169. http_502 = sum(1 for v in cur.values() if v[2] == '502') # v[2]=http 列
  170. nz_rate = (non_json * 100 // total) if total else 0
  171. NON_JSON_THRESHOLD = 20 # NON_JSON 占比 > 20% 视为上游风暴
  172. HTTP502_THRESHOLD = 10 # HTTP 502 数 > 10 视为网关抖动
  173. MIN_TOTAL = 400 # 用例数 < 400 视为部分轮(如登录失败跳过 auth 用例)
  174. login_failed = False
  175. _smoke_out = os.path.join(os.path.dirname(cur_tsv), 'smoke_stdout.txt')
  176. try:
  177. with open(_smoke_out, encoding='utf-8', errors='ignore') as _f:
  178. _so = _f.read()
  179. if ('父/师 token 均未取得' in _so) or ('未取到 token' in _so) or ('登录态接口将被跳过' in _so):
  180. login_failed = True
  181. except FileNotFoundError:
  182. pass
  183. reasons = []
  184. if login_failed:
  185. reasons.append('登录失败(auto-login 未取得 token,auth 用例被跳过)')
  186. if nz_rate > NON_JSON_THRESHOLD:
  187. reasons.append(f'NON_JSON 风暴({nz_rate}%)')
  188. if http_502 > HTTP502_THRESHOLD:
  189. reasons.append(f'HTTP502 网关抖动({http_502})')
  190. if total < MIN_TOTAL:
  191. reasons.append(f'用例数异常({total}<{MIN_TOTAL},疑似部分轮/中断)')
  192. INVALID = len(reasons) > 0
  193. if INVALID:
  194. banner = ("\n> ⚠️ **本轮判定 INVALID:结果不提交、不推送、不更新回归基线。**\n"
  195. f"> 原因:{';'.join(reasons)}\n"
  196. "> 处置:保留上次有效基线不变,待上游稳定后重跑即可;勿将此轮数值当作真实回归。\n")
  197. lines.append(banner)
  198. report = "\n".join(lines) + "\n"
  199. # 报告恒写入本轮运行目录(供排查);仅当有效时同步到仓库根(避免污染已提交产物)
  200. _run_report = os.path.join(os.path.dirname(cur_tsv), 'INTERFACE_TEST_REPORT.md')
  201. with open(_run_report, 'w', encoding='utf-8') as f:
  202. f.write(report)
  203. if not INVALID:
  204. with open(rpt, 'w', encoding='utf-8') as f:
  205. f.write(report)
  206. # 趋势日志(本地,不随提交推送;INVALID 也记录以留痕)
  207. with open(trend, 'a', encoding='utf-8') as f:
  208. f.write(f"{ts}\ttotal={total}\tBIZ_OK={ok}\trate={rate}%\t"
  209. f"fixed={len(fixed)}\tregressed={len(regressed)}\t"
  210. f"NOT_FOUND={cats.get('NOT_FOUND',0)}\tSERVER_ERR={cats.get('SERVER_ERR',0)}\t"
  211. f"NON_JSON={non_json}\tHTTP502={http_502}\tINVALID={1 if INVALID else 0}"
  212. + (f"({ '|'.join(reasons) })" if INVALID else "") + "\n")
  213. # 导出真实指标供 bash 判定(修复 rate/fixed/regressed 在子进程不可见导致
  214. # commit 失败 + 假通过率 ?%;bash 侧 set -u 下 unbound variable 亦会导致整条命令展开失败)
  215. _metrics_path = os.path.join(os.path.dirname(trend), 'interface_test_metrics.env')
  216. with open(_metrics_path, 'w', encoding='utf-8') as _mf:
  217. _mf.write(f"RATE={rate}\nFIXED_COUNT={len(fixed)}\nREGRESSED_COUNT={len(regressed)}\n"
  218. f"TOTAL={total}\nOK_COUNT={ok}\nNON_JSON_COUNT={non_json}\n"
  219. f"SERVER_ERR_COUNT={server_err}\nCONN_ERR_COUNT={conn_err}\n"
  220. f"HTTP_502_COUNT={http_502}\nINVALID={1 if INVALID else 0}\n"
  221. f"INVALID_REASON={'|'.join(reasons)}\n")
  222. print(report)
  223. print(f"\n[趋势] 已写入 {trend}")
  224. if INVALID:
  225. print(f"[守卫] INVALID 触发:{';'.join(reasons)} --> 跳过提交/推送/基线更新")
  226. PY
  227. # ---------------------------------------------------------------------------
  228. # 4.5) 测试用例完成情况检查(总数 / 已完成 / 未完成原因)
  229. # 统计自动化用例(test()) 与文档化用例(TC-xxxx),并标注未开始执行的原因,
  230. # 追加到报告与趋势日志,供人工与闭环跟踪。
  231. # ---------------------------------------------------------------------------
  232. python - "$CUR_TSV" "$RUN_DIR" "$REPO_DIR" "$TREND" <<'PY'
  233. import sys, os, re, glob, json
  234. cur_tsv, run_dir, repo_dir, trend = sys.argv[1:5]
  235. # —— 读取 step4 导出的指标(接口冒烟)——
  236. metrics = {}
  237. _mp = os.path.join(repo_dir, 'interface_test_metrics.env')
  238. try:
  239. for line in open(_mp, encoding='utf-8', errors='ignore'):
  240. line = line.strip()
  241. if '=' in line and not line.startswith('#'):
  242. k, v = line.split('=', 1)
  243. metrics[k.strip()] = v.strip()
  244. except FileNotFoundError:
  245. pass
  246. INVALID = metrics.get('INVALID', '0') == '1'
  247. OK_COUNT = int(metrics.get('OK_COUNT', '0') or 0)
  248. tests_dir = os.path.join(repo_dir, 'tests')
  249. # 1) 文档化用例 TC-xxxx
  250. doc_tcs = set()
  251. for f in glob.glob(os.path.join(tests_dir, 'test-cases', '*.md')):
  252. try:
  253. t = open(f, encoding='utf-8', errors='ignore').read()
  254. except Exception:
  255. continue
  256. for m in re.findall(r'TC-[A-Z]?-?\d+', t):
  257. doc_tcs.add(m)
  258. TOTAL_DOC = len(doc_tcs)
  259. # 2) 自动化用例 test()(按层级分类)
  260. specs = glob.glob(os.path.join(tests_dir, 'e2e', '**', '*.spec.js'), recursive=True)
  261. e2e_total = dom = roles = api = skip = 0
  262. for f in specs:
  263. try:
  264. src = open(f, encoding='utf-8', errors='ignore').read()
  265. except Exception:
  266. continue
  267. n = len(re.findall(r'^\s*test\s*\(', src, re.M))
  268. e2e_total += n
  269. s = (len(re.findall(r'test\s*\.\s*(?:skip|fixme)\s*\(', src))
  270. + len(re.findall(r'test\s*\.\s*describe\s*\.\s*skip\s*\(', src)))
  271. skip += s
  272. p = f.replace('\\', '/')
  273. if '/e2e/api/' in p:
  274. api += n
  275. elif '/e2e/roles/' in p:
  276. roles += n
  277. else:
  278. dom += n
  279. completed_runnable = e2e_total - skip
  280. # 3) API e2e 末次实跑(.last-run.json)
  281. api_pass = api_fixme = 0
  282. lr = os.path.join(tests_dir, 'test-results', '.last-run.json')
  283. if os.path.exists(lr):
  284. try:
  285. j = json.load(open(lr, encoding='utf-8'))
  286. if isinstance(j, dict):
  287. api_pass = int(j.get('passed', j.get('stats', {}).get('expected', 0) or 0))
  288. api_fixme = int(j.get('fixme', j.get('stats', {}).get('skipped', 0) or 0))
  289. except Exception:
  290. pass
  291. # 4) NOT_FOUND 端点数(从本轮 CUR_TSV 直接统计,避免依赖 env 未导出字段)
  292. nf = 0
  293. try:
  294. for line in open(cur_tsv, encoding='utf-8', errors='ignore'):
  295. c = line.rstrip('\n').split('\t')
  296. if len(c) >= 7 and c[6].strip() == 'NOT_FOUND':
  297. nf += 1
  298. except Exception:
  299. pass
  300. def pct(a, b):
  301. return f"{a * 100 // b}%" if b else "0%"
  302. reasons = []
  303. if dom > 0:
  304. reasons.append((dom, f"DOM 型 E2E 无法执行(CFC-ENV-001):{dom} 个用例已编写,但远程 cfc.iwintrue.com 为纯 Spring Boot API 后端、未部署 uni-app H5 / cfc-web 前端,浏览器型用例无法运行,未开始执行。"))
  305. if skip > 0:
  306. reasons.append((skip, f"已知缺陷/待上线导致跳过:{skip} 个用例被 test.skip/test.fixme 标记(能量发放 500-CFC-EN-001、昵称未持久化-CFC-FM-001 等),待后端修复后启用。"))
  307. if nf > 0:
  308. reasons.append((nf, f"新功能待部署后验证:接口冒烟发现 {nf} 个 NOT_FOUND 端点,对应前端页面尚未部署到测试环境,相关 E2E 暂挂起。"))
  309. if TOTAL_DOC > 0:
  310. reasons.append((TOTAL_DOC, f"文档化用例未全量自动化:{TOTAL_DOC} 条设计用例(TC-xxxx),核心主链路已落地 e2e,边缘/异常流仍以文档形式待补自动化。"))
  311. section = []
  312. section.append("\n## 测试用例完成情况")
  313. section.append(f"- 自动化用例总数(可执行 `test()`):**{e2e_total}**(api 层 {api} / 角色分包 {roles} / DOM 层 {dom})")
  314. section.append(f"- 文档化用例(设计层 TC-xxxx):**{TOTAL_DOC}**")
  315. section.append(f"- 已完成(自动化且未被跳过):**{completed_runnable}/{e2e_total} = {pct(completed_runnable, e2e_total)}**")
  316. section.append(f"- 已实跑通过:接口冒烟 BIZ_OK **{OK_COUNT}** 端点 + API e2e 末次 **{api_pass}** 通过 / {api_fixme} fixme")
  317. section.append("")
  318. section.append(f"### 未完成 / 未开始的原因(共 {len(reasons)} 类)")
  319. if reasons:
  320. for i, (c, t) in enumerate(reasons, 1):
  321. section.append(f"{i}. [{c} 项] {t}")
  322. else:
  323. section.append("- 无(全部用例均已落地并可执行)")
  324. section.append("")
  325. section.append("> 说明:自动化完成率按 `test()` 未被 skip/fixme 计;DOM 层受 CFC-ENV-001 限制需前端部署后执行,属环境阻塞而非用例缺失。")
  326. report_txt = "\n".join(section)
  327. # 追加到本轮运行目录报告(始终)
  328. _run_rpt = os.path.join(run_dir, 'INTERFACE_TEST_REPORT.md')
  329. try:
  330. _b = open(_run_rpt, encoding='utf-8', errors='ignore').read()
  331. open(_run_rpt, 'w', encoding='utf-8').write(_b.rstrip() + "\n\n" + report_txt)
  332. except FileNotFoundError:
  333. open(_run_rpt, 'w', encoding='utf-8').write(report_txt)
  334. # 仅有效轮才写回仓库根报告
  335. if not INVALID:
  336. _repo_rpt = os.path.join(repo_dir, 'INTERFACE_TEST_REPORT.md')
  337. try:
  338. _b = open(_repo_rpt, encoding='utf-8', errors='ignore').read()
  339. open(_repo_rpt, 'w', encoding='utf-8').write(_b.rstrip() + "\n\n" + report_txt)
  340. except FileNotFoundError:
  341. pass
  342. # JSON 供工具读取
  343. js = dict(e2e_total=e2e_total, api=api, roles=roles, dom=dom, skip=skip,
  344. completed_runnable=completed_runnable, doc_total=TOTAL_DOC,
  345. smoke_ok=OK_COUNT, api_pass=api_pass, api_fixme=api_fixme,
  346. reasons=[{"count": c, "reason": t} for c, t in reasons], invalid=INVALID)
  347. json.dump(js, open(os.path.join(run_dir, 'test_case_completion.json'), 'w', encoding='utf-8'),
  348. ensure_ascii=False, indent=2)
  349. if not INVALID:
  350. json.dump(js, open(os.path.join(repo_dir, 'test_case_completion.json'), 'w', encoding='utf-8'),
  351. ensure_ascii=False, indent=2)
  352. # 趋势日志
  353. with open(trend, 'a', encoding='utf-8') as f:
  354. f.write(f"\tTC_E2E={e2e_total}\tTC_DONE={completed_runnable}\tTC_DOC={TOTAL_DOC}\tTC_SKIP={skip}\tTC_DOM={dom}\n")
  355. # 导出指标给 bash(追加,供 commit 信息使用)
  356. with open(os.path.join(repo_dir, 'interface_test_metrics.env'), 'a', encoding='utf-8') as f:
  357. f.write(f"TC_E2E_TOTAL={e2e_total}\nTC_DONE={completed_runnable}\nTC_DOC={TOTAL_DOC}\nTC_SKIP={skip}\n")
  358. print(f"[测试用例] 总数(自动化)={e2e_total} 已完成(可运行)={completed_runnable} 文档化={TOTAL_DOC} 跳过={skip} | 原因 {len(reasons)} 类")
  359. PY
  360. # ---------------------------------------------------------------------------
  361. # 4.6) Playwright E2E 冒烟(独立于接口冒烟;失败不阻断主闭环)
  362. # 仅跑 api 层(playwright.config.api.js -> tests/e2e/api/*.spec.js),
  363. # 含 nonfunc-security / nonfunc-perf-compat 安全与非功能断言。
  364. # 结果独立成报告(固定名覆盖式),不并入 step4 接口冒烟报告,亦不阻断 commit/push。
  365. # ---------------------------------------------------------------------------
  366. PW_BIN="$REPO_DIR/tests/node_modules/.bin/playwright"
  367. PW_CFG="$REPO_DIR/tests/playwright.config.api.js"
  368. PW_RPT="$REPO_DIR/playwright_smoke_report.md"
  369. PW_LOG="$REPO_DIR/playwright_smoke.log"
  370. if [ -x "$PW_BIN" ]; then
  371. PW_RUN=("$PW_BIN")
  372. elif [ -f "$REPO_DIR/tests/node_modules/playwright/cli.js" ]; then
  373. PW_RUN=(node "$REPO_DIR/tests/node_modules/playwright/cli.js")
  374. else
  375. PW_RUN=()
  376. fi
  377. if [ "${#PW_RUN[@]}" -gt 0 ] && [ -f "$PW_CFG" ]; then
  378. log "开始执行 Playwright E2E 冒烟(api 层,独立、不阻断主闭环)"
  379. ( cd "$REPO_DIR/tests" && "${PW_RUN[@]}" test -c "$PW_CFG" --project=desktop --reporter=line \
  380. > "$PW_LOG" 2>&1 ) || true
  381. PW_PASS=$(grep -oE '[0-9]+ passed' "$PW_LOG" | grep -oE '[0-9]+' | head -1)
  382. PW_FAIL=$(grep -oE '[0-9]+ failed' "$PW_LOG" | grep -oE '[0-9]+' | head -1)
  383. PW_SKIP=$(grep -oE '[0-9]+ skipped' "$PW_LOG" | grep -oE '[0-9]+' | head -1)
  384. {
  385. echo ""
  386. echo "## Playwright E2E 冒烟(api 层,独立,不阻断主闭环)"
  387. echo "- 执行器: ${PW_RUN[*]} test -c playwright.config.api.js --project=desktop"
  388. echo "- 结果: **${PW_PASS:-0} passed / ${PW_FAIL:-0} failed / ${PW_SKIP:-0} skipped**"
  389. echo "- 覆盖: tests/e2e/api/*.spec.js(含 nonfunc-security 安全契约、nonfunc-perf-compat 性能兼容)"
  390. echo "- 说明: 红用例=已知后端缺口(权限提升/开放端点/上传未校验/缺鉴权过滤器),作持续告警;失败不阻断本闭环。"
  391. echo "- 明细: $PW_LOG"
  392. } > "$PW_RPT"
  393. log "Playwright E2E 冒烟完成: ${PW_PASS:-0} passed / ${PW_FAIL:-0} failed / ${PW_SKIP:-0} skipped(报告 $PW_RPT)"
  394. else
  395. log "⚠️ 未找到 Playwright 执行器或配置,跳过 E2E 冒烟"
  396. fi
  397. # ---------------------------------------------------------------------------
  398. # 5) 守卫判定:无效轮(宕机/NON_JSON 风暴/502 抖动/登录失败/部分轮)
  399. # 不提交、不推送、不更新回归基线,避免污染趋势与开发侧闭环
  400. # ---------------------------------------------------------------------------
  401. INVALID=0; INVALID_REASON=""
  402. RATE="?"; FIXED_COUNT=0; REGRESSED_COUNT=0
  403. METRICS_ENV="$REPO_DIR/interface_test_metrics.env"
  404. [ -f "$METRICS_ENV" ] && . "$METRICS_ENV"
  405. if [ "${INVALID:-0}" -eq 1 ]; then
  406. log "⚠️ 守卫触发 INVALID:本轮结果不提交 / 不推送 / 不更新回归基线"
  407. log " 原因: ${INVALID_REASON}"
  408. log " 处置: 保留 PREV_TSV 基线不变,待上游稳定后重跑即可"
  409. log " 排查: 本轮明细见 $RUN_DIR/results_$TS.tsv 与 $RUN_DIR/smoke_stdout.txt"
  410. log "===== 本轮结束(INVALID,已跳过推送)====="
  411. exit 0
  412. fi
  413. # ---------------------------------------------------------------------------
  414. # 6) 提交测试结果 + 补全用例(仅有效轮)
  415. # ---------------------------------------------------------------------------
  416. git add -f ops_interface_smoke.py
  417. # 强制纳入报告产物(防止未来 .gitignore 规则或路径规范导致漏提);
  418. # EXTRA_CASES 仅在存在且有内容时才加入,避免缺失路径使整个 git add 失败(旧逻辑缺陷)
  419. git add -f INTERFACE_TEST_REPORT.md interface_test_report.tsv cfc-frontend/INTERFACE_TEST_MATRIX.md interface_test_bodies.tsv
  420. [ -s "$EXTRA_CASES" ] && git add -f "$EXTRA_CASES"
  421. # 纳入 Playwright E2E 冒烟报告(仅当本轮实际执行过,PW_RPT 才存在)
  422. [ -f "$PW_RPT" ] && git add -f "$PW_RPT" "$PW_LOG"
  423. if git diff --cached --quiet; then
  424. log "无测试结果变更,跳过提交"
  425. else
  426. git commit -m "test(interface): 接口冒烟闭环 $(date '+%Y-%m-%d %H:%M') — 通过率 ${RATE}% (fix=${FIXED_COUNT} reg=${REGRESSED_COUNT}); 测试用例 ${TC_DONE:-?}/${TC_E2E_TOTAL:-?} 完成(文档化${TC_DOC:-?})" \
  427. >>"$LOG_DIR/interface_cron.log" 2>&1 && log "已提交测试结果(测试用例完成 ${TC_DONE:-?}/${TC_E2E_TOTAL:-?})"
  428. fi
  429. # ---------------------------------------------------------------------------
  430. # 7) 推送远端,与开发团队形成闭环
  431. # 增强:远端领先(非快进)时自动 rebase 后重试,避免测试结果不落远端。
  432. # 凭证/网络类错误不盲目重试,直接失败并留待下次运行。
  433. # ---------------------------------------------------------------------------
  434. PUSH_LOG="$RUN_DIR/push.log"
  435. MAX_PUSH_RETRY=2
  436. push_ok=0
  437. attempt=0
  438. while [ "$attempt" -lt $((MAX_PUSH_RETRY + 1)) ]; do
  439. attempt=$((attempt + 1))
  440. if git push origin "$BRANCH" >"$PUSH_LOG" 2>&1; then
  441. push_ok=1
  442. log "git push 成功 — 测试闭环已同步远端(第 ${attempt} 次尝试)"
  443. break
  444. fi
  445. # 仅在「远端领先/非快进」时自动 rebase 后重试
  446. if grep -qiE "non-fast-forward|fetch first|Updates were rejected|rejected" "$PUSH_LOG"; then
  447. log "git push 被拒(远端领先/非快进)。rebase 后重试(${attempt}/${MAX_PUSH_RETRY})"
  448. # rebase 期间用 --autostash 保护未提交改动(如事后刷新的 interface_test_report.tsv)
  449. if git pull --rebase --autostash origin "$BRANCH" >>"$LOG_DIR/interface_cron.log" 2>&1; then
  450. log "rebase 完成,重新尝试 push"
  451. else
  452. log "rebase 失败(可能存在冲突),中止 push 重试"
  453. break
  454. fi
  455. else
  456. # 凭证/网络等其他错误:不盲目重试,避免空耗与误报
  457. log "git push 失败(疑似凭证/网络,非非快进类): $(grep -iE 'error|fatal|timed out|could not|permission denied' "$PUSH_LOG" | head -3 | tr '\n' ' ')"
  458. break
  459. fi
  460. done
  461. if [ "$push_ok" -ne 1 ]; then
  462. log "git push 最终失败,本轮结果未同步远端(下次运行将重试)"
  463. fi
  464. # 更新回归基线(仅有效轮才覆盖,避免无效轮污染下一轮回归对比)
  465. cp "$CUR_TSV" "$PREV_TSV"
  466. # 同步仓库根目录的全量结果 TSV(供人工/工具读取,避免与报告一起变陈旧)
  467. cp "$CUR_TSV" "interface_test_report.tsv"
  468. log "已同步全量结果 -> interface_test_report.tsv"
  469. log "===== 本轮结束 ====="