Files
taiji-AI-PAD/test_allocate_platform_agent.py
T

161 lines
6.8 KiB
Python

#!/usr/bin/env python3
"""测试分配平台 Agent 给租户的接口 - 验证 Bug 2 修复"""
import requests
import json
BASE_URL = "http://localhost:8002"
def test_allocate_platform_agent():
# 1. 渠道管理员登录
print("=" * 50)
print("1. 渠道管理员登录")
login_resp = requests.post(
f"{BASE_URL}/api/channel/auth/login",
json={"email": "66@66.com", "password": "66"}
)
print(f"状态码: {login_resp.status_code}")
print(f"响应: {json.dumps(login_resp.json(), indent=2, ensure_ascii=False)}")
if login_resp.status_code != 200:
print("登录失败!")
return
token = login_resp.json().get("token")
headers = {"Authorization": f"Bearer {token}"}
# 解析 JWT token 查看内容
import base64
parts = token.split(".")
if len(parts) >= 2:
payload = parts[1]
# 添加填充
payload += "=" * (4 - len(payload) % 4)
decoded = base64.urlsafe_b64decode(payload)
print(f"\nJWT Payload: {decoded.decode('utf-8')}")
# 2. 获取渠道下的租户列表
print("\n" + "=" * 50)
print("2. 获取渠道下的租户列表")
tenants_resp = requests.get(
f"{BASE_URL}/api/channel/tenants",
headers=headers
)
print(f"状态码: {tenants_resp.status_code}")
print(f"响应: {json.dumps(tenants_resp.json(), indent=2, ensure_ascii=False)}")
# 3. 查看可用的平台 Agent 模板
print("\n" + "=" * 50)
print("3. 查看可用的平台 Agent 模板")
templates_resp = requests.get(
f"{BASE_URL}/api/channel/available-platform-agents",
headers=headers
)
print(f"状态码: {templates_resp.status_code}")
print(f"响应: {json.dumps(templates_resp.json(), indent=2, ensure_ascii=False)}")
# 4. 查看渠道的平台 Agent 配额(分配前)
print("\n" + "=" * 50)
print("4. 查看渠道的平台 Agent 配额(分配前)")
quotas_resp = requests.get(
f"{BASE_URL}/api/channel/platform-agents",
headers=headers
)
print(f"状态码: {quotas_resp.status_code}")
quotas_data = quotas_resp.json()
print(f"响应: {json.dumps(quotas_data, indent=2, ensure_ascii=False)}")
# 找到 echo_agent 的配额
echo_agent_quota_before = None
for quota in quotas_data.get("data", {}).get("quotas", []):
if quota["templateName"] == "echo_agent":
echo_agent_quota_before = quota
print(f"\n[分配前] echo_agent 配额: podQuota={quota['podQuota']}, podUsed={quota['podUsed']}, podRemaining={quota['podRemaining']}")
break
if not echo_agent_quota_before:
print("\n渠道没有 echo_agent 配额,无法测试分配功能")
return
# 5. 如果有租户,尝试分配平台 Agent
if tenants_resp.status_code == 200:
tenants_data = tenants_resp.json()
tenants = tenants_data.get("data", {}).get("tenants", [])
# 找一个活跃的租户
active_tenant = None
for tenant in tenants:
if tenant["status"] == "active":
active_tenant = tenant
break
if active_tenant:
tenant_id = active_tenant["id"]
tenant_name = active_tenant["name"]
print("\n" + "=" * 50)
print(f"5. 分配 echo_agent 配额给租户 {tenant_name} ({tenant_id})")
allocate_resp = requests.post(
f"{BASE_URL}/api/channel/tenants/{tenant_id}/platform-agents",
headers=headers,
json={
"templateName": "echo_agent",
"podQuota": 2
}
)
print(f"状态码: {allocate_resp.status_code}")
print(f"响应: {json.dumps(allocate_resp.json(), indent=2, ensure_ascii=False)}")
if allocate_resp.status_code == 200:
# 6. 再次查看渠道的平台 Agent 配额(分配后)
print("\n" + "=" * 50)
print("6. 查看渠道的平台 Agent 配额(分配后)")
quotas_resp_after = requests.get(
f"{BASE_URL}/api/channel/platform-agents",
headers=headers
)
print(f"状态码: {quotas_resp_after.status_code}")
quotas_data_after = quotas_resp_after.json()
print(f"响应: {json.dumps(quotas_data_after, indent=2, ensure_ascii=False)}")
# 找到 echo_agent 的配额
echo_agent_quota_after = None
for quota in quotas_data_after.get("data", {}).get("quotas", []):
if quota["templateName"] == "echo_agent":
echo_agent_quota_after = quota
print(f"\n[分配后] echo_agent 配额: podQuota={quota['podQuota']}, podUsed={quota['podUsed']}, podRemaining={quota['podRemaining']}")
break
# 7. 验证 Bug 2 修复
print("\n" + "=" * 50)
print("7. 验证 Bug 2 修复结果")
if echo_agent_quota_before and echo_agent_quota_after:
pod_used_before = echo_agent_quota_before["podUsed"]
pod_used_after = echo_agent_quota_after["podUsed"]
expected_increase = 2 # 分配了 2 个 Pod
print(f"分配前 podUsed: {pod_used_before}")
print(f"分配后 podUsed: {pod_used_after}")
print(f"预期增加: {expected_increase}")
if pod_used_after == pod_used_before + expected_increase:
print("\n✅ Bug 2 修复成功!渠道的 podUsed 正确增加了分配给租户的配额数量")
else:
print(f"\n❌ Bug 2 修复失败!podUsed 应该从 {pod_used_before} 增加到 {pod_used_before + expected_increase},但实际是 {pod_used_after}")
# 8. 查看租户的平台 Agent 使用情况
print("\n" + "=" * 50)
print(f"8. 查看租户 {tenant_name} 的平台 Agent 使用情况")
tenant_usage_resp = requests.get(
f"{BASE_URL}/api/channel/tenants/{tenant_id}/platform-agents/usage",
headers=headers
)
print(f"状态码: {tenant_usage_resp.status_code}")
print(f"响应: {json.dumps(tenant_usage_resp.json(), indent=2, ensure_ascii=False)}")
else:
print("\n没有活跃的租户,跳过分配测试")
print("请先创建一个活跃的租户")
if __name__ == "__main__":
test_allocate_platform_agent()