157 lines
7.1 KiB
Python
157 lines
7.1 KiB
Python
#!/usr/bin/env python3
|
|
"""远光智炼平台 - API功能测试脚本"""
|
|
import json
|
|
import time
|
|
import requests
|
|
import sys
|
|
from datetime import datetime
|
|
|
|
BASE_URL = "http://localhost:17861/modelTF"
|
|
results = []
|
|
|
|
def login(username="admin", password="admin123"):
|
|
resp = requests.post(f"{BASE_URL}/login", json={"username": username, "password": password})
|
|
data = resp.json()
|
|
if data.get("code") == 0:
|
|
return data["data"]["token"]
|
|
return None
|
|
|
|
def test_api(name, method="GET", url="", json_body=None, headers=None):
|
|
try:
|
|
h = {"Content-Type": "application/json"}
|
|
if headers:
|
|
h.update(headers)
|
|
resp = requests.request(method, f"{BASE_URL}{url}", json=json_body, headers=h, timeout=30)
|
|
data = resp.json()
|
|
code = data.get("code", -1)
|
|
msg = data.get("message", "")
|
|
d = data.get("data")
|
|
if isinstance(d, list):
|
|
data_desc = f"{len(d)} items"
|
|
elif isinstance(d, dict):
|
|
data_desc = f"obj({len(d)} keys)"
|
|
elif isinstance(d, str):
|
|
data_desc = f"str({len(d)})"
|
|
elif d is None:
|
|
data_desc = "null"
|
|
else:
|
|
data_desc = str(type(d).__name__)
|
|
|
|
status = "PASS" if code == 0 else f"FAIL(code={code})"
|
|
results.append({
|
|
"name": name, "method": method, "url": url,
|
|
"status": status, "message": msg, "data_desc": data_desc
|
|
})
|
|
return data
|
|
except Exception as e:
|
|
results.append({
|
|
"name": name, "method": method, "url": url,
|
|
"status": f"ERROR", "message": str(e)[:120], "data_desc": "-"
|
|
})
|
|
return None
|
|
|
|
# ==================== 登录获取 token ====================
|
|
token = login()
|
|
if not token:
|
|
print("FAILED: Cannot login, server may not be running")
|
|
sys.exit(1)
|
|
auth_headers = {"Authorization": f"Bearer {token}"}
|
|
print(f"Login OK, token: {token}")
|
|
|
|
# ==================== 1. 基础 ====================
|
|
test_api("health", url="/health")
|
|
test_api("system-info", url="/system-info", headers=auth_headers)
|
|
test_api("me", url="/me", headers=auth_headers)
|
|
test_api("dashboard/overview", url="/dashboard/overview", headers=auth_headers)
|
|
test_api("dashboard/stats", url="/dashboard/stats", headers=auth_headers)
|
|
|
|
# ==================== 2. 用户管理 ====================
|
|
test_api("users-list", url="/users", headers=auth_headers)
|
|
test_api("users-create", method="POST", url="/users",
|
|
json_body={"username": f"test_u_{int(time.time())}", "password": "Test1234!", "display_name": "Test User", "role": "viewer"},
|
|
headers=auth_headers)
|
|
test_api("users-change-password", method="POST", url="/users/me/password",
|
|
json_body={"old_password": "admin123", "new_password": "admin123"},
|
|
headers=auth_headers)
|
|
|
|
# ==================== 3. 模型管理 ====================
|
|
test_api("model-manage-list", url="/model-manage", headers=auth_headers)
|
|
test_api("model-manage-local", url="/model-manage/local-models", headers=auth_headers)
|
|
test_api("model-manage-trained", url="/model-manage/trained-models", headers=auth_headers)
|
|
test_api("model-manage-export-jobs", url="/model-manage/export-jobs", headers=auth_headers)
|
|
test_api("model-manage-create", method="POST", url="/model-manage",
|
|
json_body={"name": f"test_model_{int(time.time())}", "source": "local", "model_path": "/tmp/test", "description": "test"},
|
|
headers=auth_headers)
|
|
|
|
# ==================== 4. 数据集管理 ====================
|
|
test_api("dataset-list", url="/dataset-manage", headers=auth_headers)
|
|
ds_data = test_api("dataset-create", method="POST", url="/dataset-manage",
|
|
json_body={"name": f"test_ds_{int(time.time())}", "description": "test dataset"},
|
|
headers=auth_headers)
|
|
|
|
# ==================== 5. 模型训练 ====================
|
|
test_api("fine-tune-list", url="/fine-tune", headers=auth_headers)
|
|
test_api("fine-tune-check-name", url="/fine-tune/check-name?name=test_task", headers=auth_headers)
|
|
test_api("fine-tune-preflight", method="POST", url="/fine-tune/preflight",
|
|
json_body={"model_id": "m_test", "dataset_id": "ds_test", "epochs": 1},
|
|
headers=auth_headers)
|
|
|
|
# ==================== 6. 模型评测 ====================
|
|
test_api("model-eval-list", url="/model-eval", headers=auth_headers)
|
|
test_api("dimension-list", url="/dimension", headers=auth_headers)
|
|
|
|
# ==================== 7. 模型推理/对比 ====================
|
|
test_api("model-compare-list", url="/model-compare", headers=auth_headers)
|
|
test_api("model-chat-local-status", url="/model-chat/local/status", headers=auth_headers)
|
|
|
|
# ==================== 8. 数据处理 ====================
|
|
test_api("data-process-list", url="/data-process", headers=auth_headers)
|
|
|
|
# ==================== 9. 算力节点 ====================
|
|
test_api("compute-nodes", url="/compute/nodes", headers=auth_headers)
|
|
test_api("compute-gpus", url="/compute/gpus", headers=auth_headers)
|
|
test_api("compute-queue", url="/compute/queue", headers=auth_headers)
|
|
|
|
# ==================== 10. 治理/审计 ====================
|
|
test_api("log-files", url="/log-files", headers=auth_headers)
|
|
test_api("training-log-files", url="/training-log-files", headers=auth_headers)
|
|
test_api("web-log", method="POST", url="/web-log",
|
|
json_body={"level": "info", "message": "test log entry"},
|
|
headers=auth_headers)
|
|
|
|
# ==================== 11. 数据转换 ====================
|
|
test_api("data-convert-list", url="/data-convert", headers=auth_headers)
|
|
|
|
# ==================== 12. 错误处理测试 ====================
|
|
test_api("error-404", url="/nonexistent-endpoint", headers=auth_headers)
|
|
test_api("error-unauthorized", url="/users") # no auth header
|
|
|
|
# ==================== 13. 权限测试 - 普通用户 ====================
|
|
viewer_token = login("viewer", "viewer123")
|
|
if viewer_token:
|
|
viewer_headers = {"Authorization": f"Bearer {viewer_token}"}
|
|
test_api("viewer-login", url="/me", headers=viewer_headers)
|
|
test_api("viewer-users-list-denied", url="/users", headers=viewer_headers)
|
|
test_api("viewer-fine-tune-list", url="/fine-tune", headers=viewer_headers)
|
|
else:
|
|
results.append({"name": "viewer-login", "method": "POST", "url": "/login", "status": "SKIP", "message": "viewer user not found", "data_desc": "-"})
|
|
|
|
# ==================== 输出结果 ====================
|
|
pass_count = sum(1 for r in results if r["status"] == "PASS")
|
|
fail_count = sum(1 for r in results if "FAIL" in r["status"])
|
|
error_count = sum(1 for r in results if r["status"] == "ERROR")
|
|
skip_count = sum(1 for r in results if r["status"] == "SKIP")
|
|
|
|
print(f"\n{'='*80}")
|
|
print(f"TEST SUMMARY: PASS={pass_count} FAIL={fail_count} ERROR={error_count} SKIP={skip_count} TOTAL={len(results)}")
|
|
print(f"{'='*80}\n")
|
|
|
|
for r in results:
|
|
status_icon = "[OK]" if r["status"] == "PASS" else "[XX]" if r["status"] in ("FAIL", "ERROR") or "FAIL" in r["status"] else "[--]"
|
|
print(f"{status_icon} {r['name']:40s} {r['method']:6s} {r['status']:20s} {r['data_desc']:20s} {r['message'][:60]}")
|
|
|
|
# 保存 JSON 结果
|
|
with open("test_results.json", "w", encoding="utf-8") as f:
|
|
json.dump(results, f, ensure_ascii=False, indent=2)
|
|
print(f"\nResults saved to test_results.json")
|