- 16 个业务模块,856 个测试用例,40 个 E2E 测试 - 单一 React 19 前端 + PlatformSwitcher 二合一切换 - Flask 后端支持 scenario/dcloud_module/dcloud_e2e/dcloud_all 四种任务类型 - E2E 测试面板:API 全链路(8条) + UI 点击(3条) + 分步测试(3个) - SSE 实时日志流 + 飞书告警 + 定时调度 - 完整还原原 ToB 平台 TestPanel E2E 模块
282 lines
9.5 KiB
Python
282 lines
9.5 KiB
Python
import os
|
|
import sys
|
|
import json
|
|
import queue
|
|
import signal
|
|
import threading
|
|
import subprocess
|
|
import time
|
|
import re
|
|
from datetime import datetime
|
|
|
|
import requests
|
|
|
|
from backend.database import tasks_db, reports_db, save_db
|
|
|
|
PROJECT_ROOT = os.path.dirname(os.path.dirname(__file__))
|
|
REPORTS_DIR = os.path.join(PROJECT_ROOT, "reports")
|
|
SCREENSHOTS_DIR = os.path.join(PROJECT_ROOT, "screenshots")
|
|
LARK_WEBHOOK = "https://open.feishu.cn/open-apis/bot/v2/hook/d75c14ad-d782-489e-8a99-81b511ee4abd"
|
|
|
|
log_queues = {}
|
|
process_pids = {}
|
|
|
|
os.makedirs(REPORTS_DIR, exist_ok=True)
|
|
os.makedirs(SCREENSHOTS_DIR, exist_ok=True)
|
|
|
|
|
|
def _resolve_python():
|
|
candidates = [
|
|
os.path.join(PROJECT_ROOT, ".venv311", "bin", "python3"),
|
|
os.path.join(PROJECT_ROOT, ".venv", "bin", "python3"),
|
|
os.path.join(PROJECT_ROOT, "venv", "bin", "python"),
|
|
]
|
|
for p in candidates:
|
|
if os.path.exists(p):
|
|
return p
|
|
return sys.executable
|
|
|
|
|
|
def send_alerts(report):
|
|
task_id = report.get("task_id")
|
|
task = tasks_db.get(task_id, {})
|
|
channels = task.get("alert_channels", [])
|
|
|
|
if "lark" in channels:
|
|
rule = task.get("alert_rule", "always")
|
|
if rule == "only_on_fail" and report["result"] == "PASS":
|
|
return
|
|
try:
|
|
status_color = "green" if report["result"] == "PASS" else "red"
|
|
status_text = "PASS" if report["result"] == "PASS" else "FAIL"
|
|
card = {
|
|
"config": {"wide_screen_mode": True},
|
|
"header": {
|
|
"title": {"tag": "plain_text", "content": f"Unified Test Report: {report['task_name']}"},
|
|
"template": status_color,
|
|
},
|
|
"elements": [
|
|
{
|
|
"tag": "div",
|
|
"fields": [
|
|
{"is_short": True, "text": {"tag": "lark_md", "content": f"**Status:** {status_text}"}},
|
|
{"is_short": True, "text": {"tag": "lark_md", "content": f"**Product:** {report['product']}"}},
|
|
{"is_short": True, "text": {"tag": "lark_md", "content": f"**Env:** {task.get('env', 'PROD')}"}},
|
|
{"is_short": True, "text": {"tag": "lark_md", "content": f"**Pass/Total:** {report['pass']}/{report['total_runs']}"}},
|
|
],
|
|
},
|
|
{"tag": "hr"},
|
|
{
|
|
"tag": "action",
|
|
"actions": [
|
|
{
|
|
"tag": "button",
|
|
"text": {"tag": "plain_text", "content": "View Report"},
|
|
"type": "primary",
|
|
"url": "http://127.0.0.1:5001/#/tasks",
|
|
}
|
|
],
|
|
},
|
|
],
|
|
}
|
|
requests.post(LARK_WEBHOOK, json={"msg_type": "interactive", "card": card}, timeout=5)
|
|
except Exception as e:
|
|
print(f"Feishu alert failed: {e}")
|
|
|
|
|
|
def run_task_process(task):
|
|
task_id = task["id"]
|
|
task["status"] = "running"
|
|
task["started_at"] = datetime.now().isoformat()
|
|
save_db()
|
|
|
|
q = log_queues.get(task_id)
|
|
logs_all = []
|
|
|
|
def push(msg, level="INFO"):
|
|
entry = {"ts": datetime.now().strftime("%H:%M:%S"), "level": level, "msg": msg}
|
|
logs_all.append(entry)
|
|
if q:
|
|
q.put(json.dumps(entry))
|
|
|
|
run_limit = int(task.get("run_count", 1))
|
|
retry_count = int(task.get("retry_count", 1)) if task.get("retry_on_fail") else 0
|
|
retry_delay = int(task.get("retry_delay", 5))
|
|
|
|
env = os.environ.copy()
|
|
env["ROBOGO_SCREENSHOTS_DIR"] = os.path.abspath(SCREENSHOTS_DIR)
|
|
env["ROBOGO_REPORTS_DIR"] = os.path.abspath(REPORTS_DIR)
|
|
env["ROBOGO_TASK_ID"] = task_id
|
|
env["ROBOGO_ENV"] = task.get("env", "PROD")
|
|
env["ROBOGO_SCOPE"] = task.get("scope", "all")
|
|
env["AUTH_ACCOUNT"] = task.get("account", "")
|
|
env["AUTH_PASSWORD"] = task.get("password", "")
|
|
env["ROBOGO_USER"] = task.get("account", "")
|
|
env["ROBOGO_PWD"] = task.get("password", "")
|
|
env["HEADLESS"] = task.get("headless", "true")
|
|
|
|
runner_type = task.get("runner", "script")
|
|
|
|
total_pass, total_fail = 0, 0
|
|
current_run = 0
|
|
planned_runs = run_limit
|
|
last_run_success = None
|
|
is_retry_run = False
|
|
|
|
push(f"Task started — Env: {env['ROBOGO_ENV']} | Scope: {env['ROBOGO_SCOPE']} | Runner: {runner_type}", "INFO")
|
|
|
|
python_bin = _resolve_python()
|
|
|
|
while current_run < run_limit:
|
|
current_run += 1
|
|
push(f"Run {current_run}/{planned_runs}...", "INFO")
|
|
|
|
try:
|
|
if runner_type == "pytest":
|
|
cmd = _build_pytest_cmd(task)
|
|
if cmd is None:
|
|
push(f"Unknown module: {task.get('module')}", "ERROR")
|
|
last_run_success = False
|
|
total_fail += 1
|
|
break
|
|
proc = subprocess.Popen(
|
|
cmd,
|
|
cwd=PROJECT_ROOT,
|
|
stdout=subprocess.PIPE,
|
|
stderr=subprocess.STDOUT,
|
|
text=True,
|
|
bufsize=1,
|
|
env=env,
|
|
preexec_fn=os.setsid if os.name != "nt" else None,
|
|
)
|
|
else:
|
|
if not task.get("entry"):
|
|
push("No entry script configured", "ERROR")
|
|
last_run_success = False
|
|
total_fail += 1
|
|
break
|
|
proc = subprocess.Popen(
|
|
[python_bin, task["entry"]],
|
|
stdout=subprocess.PIPE,
|
|
stderr=subprocess.STDOUT,
|
|
text=True,
|
|
bufsize=1,
|
|
env=env,
|
|
preexec_fn=os.setsid if os.name != "nt" else None,
|
|
)
|
|
|
|
process_pids[task_id] = proc.pid
|
|
|
|
for line in proc.stdout:
|
|
push(line.rstrip(), "INFO")
|
|
proc.wait()
|
|
process_pids.pop(task_id, None)
|
|
|
|
if proc.returncode == 0:
|
|
last_run_success = True
|
|
if is_retry_run:
|
|
total_fail = max(0, total_fail - 1)
|
|
total_pass += 1
|
|
push(f"Retry run {current_run} passed (overrides previous failure)", "SUCCESS")
|
|
else:
|
|
total_pass += 1
|
|
push(f"Run {current_run} passed", "SUCCESS")
|
|
is_retry_run = False
|
|
else:
|
|
last_run_success = False
|
|
total_fail += 1
|
|
push(f"Run {current_run} failed", "ERROR")
|
|
if retry_count > 0:
|
|
push(f"Retrying ({retry_count} left), waiting {retry_delay}s...", "WARN")
|
|
time.sleep(retry_delay)
|
|
retry_count -= 1
|
|
run_limit += 1
|
|
is_retry_run = True
|
|
else:
|
|
is_retry_run = False
|
|
except Exception as e:
|
|
push(f"System error: {e}", "ERROR")
|
|
last_run_success = False
|
|
total_fail += 1
|
|
is_retry_run = False
|
|
|
|
finished_at = datetime.now().isoformat()
|
|
final_result = "PASS" if last_run_success else "FAIL"
|
|
report = {
|
|
"task_id": task_id,
|
|
"task_name": task["name"],
|
|
"product": task["product"],
|
|
"total_runs": planned_runs,
|
|
"pass": total_pass,
|
|
"fail": total_fail,
|
|
"started_at": task["started_at"],
|
|
"finished_at": finished_at,
|
|
"result": final_result,
|
|
}
|
|
|
|
log_file = os.path.join(REPORTS_DIR, f"{task_id}.json")
|
|
try:
|
|
with open(log_file, "w", encoding="utf-8") as f:
|
|
json.dump({"logs": logs_all}, f, ensure_ascii=False)
|
|
except Exception:
|
|
pass
|
|
|
|
reports_db[task_id] = report
|
|
task["status"] = "pass" if final_result == "PASS" else "fail"
|
|
task["finished_at"] = finished_at
|
|
task["report_id"] = task_id
|
|
|
|
save_db()
|
|
send_alerts(report)
|
|
push("__DONE__", "DONE")
|
|
|
|
|
|
def _build_pytest_cmd(task):
|
|
module = task.get("module")
|
|
test_marker = task.get("test_marker", "")
|
|
test_file = task.get("test_file", "")
|
|
|
|
if test_file:
|
|
return [_resolve_python(), "-m", "pytest", test_file, "-v", "--tb=short", "--color=no"]
|
|
|
|
if test_marker:
|
|
return [_resolve_python(), "-m", "pytest", "tests/", "-m", test_marker, "-v", "--tb=short", "--color=no"]
|
|
|
|
if module:
|
|
test_dir = os.path.join(PROJECT_ROOT, "tests", module)
|
|
if not os.path.exists(test_dir):
|
|
return None
|
|
return [_resolve_python(), "-m", "pytest", str(test_dir), "-v", "--tb=short", "--color=no"]
|
|
|
|
return [_resolve_python(), "-m", "pytest", "tests/", "-v", "--tb=short", "--color=no"]
|
|
|
|
|
|
def stream_pytest(module, test=None):
|
|
"""Stream pytest output via SSE for the API server"""
|
|
cmd = _build_pytest_cmd({"module": module, "test_file": test})
|
|
if cmd is None:
|
|
return None
|
|
return cmd
|
|
|
|
|
|
def stop_task(tid):
|
|
pid = process_pids.get(tid)
|
|
task = tasks_db.get(tid)
|
|
|
|
if task:
|
|
if task.get("schedule_type") == "continuous":
|
|
task["schedule_type"] = "once"
|
|
task["status"] = "fail"
|
|
task["finished_at"] = datetime.now().isoformat()
|
|
|
|
if pid:
|
|
try:
|
|
os.killpg(os.getpgid(pid), signal.SIGKILL)
|
|
except Exception:
|
|
try:
|
|
os.kill(pid, signal.SIGKILL)
|
|
except Exception:
|
|
pass
|
|
|
|
process_pids.pop(tid, None)
|
|
save_db() |