148 lines
4.8 KiB
Python
148 lines
4.8 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
DMS合规性测试工具 - API服务器功能测试脚本
|
||
测试Docker化的API服务器是否正常工作
|
||
"""
|
||
|
||
import requests
|
||
import json
|
||
import time
|
||
import sys
|
||
from pathlib import Path
|
||
|
||
def test_health_check(api_server):
|
||
"""测试健康检查端点"""
|
||
print(f"[测试] 健康检查端点...")
|
||
try:
|
||
response = requests.get(f"{api_server}/", timeout=10)
|
||
if response.status_code == 200:
|
||
result = response.json()
|
||
print(f"[成功] 健康检查通过: {result}")
|
||
return True
|
||
else:
|
||
print(f"[失败] 健康检查失败: {response.status_code}")
|
||
return False
|
||
except Exception as e:
|
||
print(f"[错误] 健康检查异常: {e}")
|
||
return False
|
||
|
||
def test_with_mock_api(api_server):
|
||
"""使用模拟API进行测试"""
|
||
print(f"[测试] 使用模拟API配置进行测试...")
|
||
|
||
# 使用一个简单的测试配置,不依赖外部API
|
||
test_config = {
|
||
"base-url": "https://httpbin.org/", # 使用httpbin作为测试目标
|
||
"dms": "./assets/doc/dms/domain.json",
|
||
"stages-dir": "./custom_stages",
|
||
"custom-test-cases-dir": "./custom_testcases",
|
||
"verbose": True,
|
||
"output": "./test_reports/",
|
||
"format": "json",
|
||
"generate-pdf": False, # 暂时不生成PDF
|
||
"strictness-level": "CRITICAL",
|
||
"ignore-ssl": True
|
||
}
|
||
|
||
print(f"[信息] 测试配置:")
|
||
print(json.dumps(test_config, indent=2, ensure_ascii=False))
|
||
|
||
try:
|
||
print(f"[信息] 发送测试请求...")
|
||
start_time = time.time()
|
||
|
||
response = requests.post(
|
||
f"{api_server}/run",
|
||
json=test_config,
|
||
headers={"Content-Type": "application/json"},
|
||
timeout=60 # 1分钟超时
|
||
)
|
||
|
||
end_time = time.time()
|
||
duration = end_time - start_time
|
||
|
||
print(f"[信息] 请求完成,耗时: {duration:.2f}秒")
|
||
print(f"[信息] HTTP状态码: {response.status_code}")
|
||
|
||
if response.status_code == 200:
|
||
print(f"[成功] API服务器响应正常")
|
||
try:
|
||
result = response.json()
|
||
print(f"[信息] 响应状态: {result.get('status', '未知')}")
|
||
if 'message' in result:
|
||
print(f"[信息] 响应消息: {result['message']}")
|
||
return True
|
||
except:
|
||
print(f"[信息] 响应内容(非JSON): {response.text[:200]}...")
|
||
return True
|
||
else:
|
||
print(f"[失败] API服务器响应异常")
|
||
print(f"[错误] 响应内容: {response.text[:200]}...")
|
||
return False
|
||
|
||
except requests.exceptions.Timeout:
|
||
print(f"[错误] 请求超时(1分钟)")
|
||
return False
|
||
except Exception as e:
|
||
print(f"[错误] 请求异常: {e}")
|
||
return False
|
||
|
||
def test_history_viewer(history_server):
|
||
"""测试历史查看器"""
|
||
print(f"[测试] 历史查看器端点...")
|
||
try:
|
||
response = requests.get(f"{history_server}/", timeout=10)
|
||
if response.status_code == 200:
|
||
print(f"[成功] 历史查看器响应正常")
|
||
return True
|
||
else:
|
||
print(f"[失败] 历史查看器响应异常: {response.status_code}")
|
||
return False
|
||
except Exception as e:
|
||
print(f"[错误] 历史查看器异常: {e}")
|
||
return False
|
||
|
||
def main():
|
||
print("=== DMS合规性测试工具 - API服务器功能测试 ===")
|
||
|
||
api_server = "http://localhost:5050"
|
||
history_server = "http://localhost:5051"
|
||
|
||
print(f"[信息] API服务器: {api_server}")
|
||
print(f"[信息] 历史查看器: {history_server}")
|
||
|
||
# 测试结果
|
||
results = []
|
||
|
||
# 1. 测试API服务器健康检查
|
||
results.append(("API服务器健康检查", test_health_check(api_server)))
|
||
|
||
# 2. 测试历史查看器
|
||
results.append(("历史查看器", test_history_viewer(history_server)))
|
||
|
||
# 3. 测试API服务器功能(使用模拟API)
|
||
results.append(("API服务器功能测试", test_with_mock_api(api_server)))
|
||
|
||
# 显示测试结果摘要
|
||
print(f"\n=== 测试结果摘要 ===")
|
||
passed = 0
|
||
total = len(results)
|
||
|
||
for test_name, result in results:
|
||
status = "✅ 通过" if result else "❌ 失败"
|
||
print(f"{status} {test_name}")
|
||
if result:
|
||
passed += 1
|
||
|
||
print(f"\n总计: {passed}/{total} 测试通过")
|
||
|
||
if passed == total:
|
||
print(f"[成功] 所有测试通过!Docker化的API服务器工作正常!")
|
||
return 0
|
||
else:
|
||
print(f"[警告] 部分测试失败,请检查服务状态")
|
||
return 1
|
||
|
||
if __name__ == "__main__":
|
||
sys.exit(main())
|