- 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 模块
496 lines
16 KiB
Python
496 lines
16 KiB
Python
"""
|
||
数据生成模块API辅助函数
|
||
|
||
提供真实创建、查询、删除等操作的API封装
|
||
"""
|
||
|
||
import os
|
||
from typing import Optional
|
||
from tests.logger import get_logger
|
||
|
||
logger = get_logger(__name__)
|
||
|
||
|
||
def api_upload_file(client, file_path: str) -> Optional[str]:
|
||
"""上传文件
|
||
|
||
Args:
|
||
client: API客户端
|
||
file_path: 文件路径
|
||
|
||
Returns:
|
||
文件URL,上传失败返回None
|
||
"""
|
||
if not os.path.exists(file_path):
|
||
logger.error(f"文件不存在: {file_path}")
|
||
return None
|
||
|
||
logger.info(f"开始上传文件: {file_path}")
|
||
|
||
try:
|
||
# 使用正确的API路径:/api/generationApi/file/upload
|
||
response = client.upload_file("/api/generationApi/file/upload", file_path)
|
||
|
||
logger.info(f"上传响应: status={response.status_code}")
|
||
|
||
if response.status_code == 200:
|
||
data = response.json()
|
||
logger.info(f"上传响应数据: {data}")
|
||
|
||
if isinstance(data, dict) and "data" in data:
|
||
url = data["data"].get("url")
|
||
if url:
|
||
logger.info(f"文件上传成功: {url}")
|
||
return url
|
||
logger.warning(f"上传响应中未找到URL: {data}")
|
||
else:
|
||
logger.error(f"文件上传失败: {response.status_code} - {response.text}")
|
||
except Exception as e:
|
||
logger.error(f"文件上传异常: {e}")
|
||
|
||
return None
|
||
|
||
|
||
def api_get_workflows(client, page_num: int = 1, page_size: int = 20) -> list:
|
||
"""获取工作流列表
|
||
|
||
Args:
|
||
client: API客户端
|
||
page_num: 页码
|
||
page_size: 每页数量
|
||
|
||
Returns:
|
||
工作流列表
|
||
"""
|
||
response = client.post(
|
||
"/api/generationApi/comfyWorkflow/list",
|
||
json={"pageNum": page_num, "pageSize": page_size},
|
||
)
|
||
if response.status_code == 200:
|
||
data = response.json()
|
||
logger.info(
|
||
f"工作流列表API响应: {list(data.keys()) if isinstance(data, dict) else 'is list'}"
|
||
)
|
||
|
||
if isinstance(data, list):
|
||
logger.info(f"返回列表格式,长度: {len(data)}")
|
||
return data
|
||
elif isinstance(data, dict) and "data" in data:
|
||
workflows = data["data"] if isinstance(data["data"], list) else []
|
||
logger.info(f"返回data字段,长度: {len(workflows)}")
|
||
return workflows
|
||
elif isinstance(data, dict) and "list" in data:
|
||
workflows = data["list"] if isinstance(data["list"], list) else []
|
||
logger.info(f"返回list字段,长度: {len(workflows)}")
|
||
return workflows
|
||
else:
|
||
logger.warning(f"获取工作流列表失败: {response.status_code}")
|
||
return []
|
||
|
||
|
||
def api_get_first_workflow_id(client) -> Optional[str]:
|
||
"""获取第一个工作流的ID
|
||
|
||
Args:
|
||
client: API客户端
|
||
|
||
Returns:
|
||
工作流ID,如果没有工作流返回None
|
||
"""
|
||
response = client.post(
|
||
"/api/generationApi/comfyWorkflow/list", json={"pageNum": 1, "pageSize": 10}
|
||
)
|
||
|
||
if response.status_code == 200:
|
||
data = response.json()
|
||
logger.info(f"工作流列表API响应: {list(data.keys())}")
|
||
|
||
# 处理响应数据结构
|
||
if isinstance(data, dict):
|
||
# 新格式:{"status": 0, "data": {"list": [...], "total": 5}}
|
||
if "data" in data and isinstance(data["data"], dict):
|
||
workflow_list = data["data"].get("list", [])
|
||
if isinstance(workflow_list, list) and len(workflow_list) > 0:
|
||
workflow = workflow_list[0]
|
||
workflow_id = workflow.get("id")
|
||
if workflow_id:
|
||
logger.info(
|
||
f"找到工作流: {workflow.get('name')} (ID: {workflow_id})"
|
||
)
|
||
return str(workflow_id)
|
||
|
||
# 旧格式:{"data": [...]}
|
||
elif "data" in data and isinstance(data["data"], list):
|
||
if len(data["data"]) > 0:
|
||
workflow = data["data"][0]
|
||
workflow_id = workflow.get("id") or workflow.get("_id")
|
||
if workflow_id:
|
||
logger.info(
|
||
f"找到工作流: {workflow.get('name')} (ID: {workflow_id})"
|
||
)
|
||
return str(workflow_id)
|
||
|
||
# 直接返回列表
|
||
elif isinstance(data, list) and len(data) > 0:
|
||
workflow = data[0]
|
||
workflow_id = workflow.get("id") or workflow.get("_id")
|
||
if workflow_id:
|
||
logger.info(f"找到工作流: {workflow.get('name')} (ID: {workflow_id})")
|
||
return str(workflow_id)
|
||
|
||
logger.info("没有找到工作流")
|
||
else:
|
||
logger.error(f"获取工作流列表失败: {response.status_code}")
|
||
|
||
return None
|
||
|
||
|
||
def api_create_task(
|
||
client, task_name: str, workflow_id: str, **kwargs
|
||
) -> Optional[str]:
|
||
"""创建数据生成任务
|
||
|
||
Args:
|
||
client: API客户端
|
||
task_name: 任务名称
|
||
workflow_id: 工作流ID
|
||
**kwargs: 其他参数
|
||
- prompt: 提示词(默认:test)
|
||
- prompt_list: 提示词列表(默认:["test"])
|
||
- batch_size: 批量大小(默认:1)
|
||
- instance_count: GPU数量(默认:1)
|
||
- sku_id: SKU ID(默认:a10690ab-816a-e350-0f00-b6f318ee86ee)
|
||
- queue_name: 队列名称(默认:fattest-elastic)
|
||
- queue_type: 队列类型(默认:elastic)
|
||
- cache_path: 缓存路径(默认:fattest/test)
|
||
|
||
Returns:
|
||
任务ID,创建失败返回None
|
||
"""
|
||
# 必需参数
|
||
task_data = {
|
||
"name": task_name,
|
||
"workflow_id": int(workflow_id), # API期望整数
|
||
"batch_size": kwargs.get("batch_size", 1),
|
||
"instance_count": kwargs.get("instance_count", 1),
|
||
"cache_path": kwargs.get("cache_path", f"fattest/test_{task_name}"),
|
||
"prompt_list": kwargs.get("prompt_list", [kwargs.get("prompt", "test")]),
|
||
"expansion_param": {
|
||
"prompt": kwargs.get("prompt", "test"),
|
||
"system_prompt": kwargs.get("system_prompt", ""),
|
||
},
|
||
"resource_param": {
|
||
"sku_id": kwargs.get("sku_id", "a10690ab-816a-e350-0f00-b6f318ee86ee"),
|
||
"queue_name": kwargs.get("queue_name", "fattest-elastic"),
|
||
"queue_type": kwargs.get("queue_type", "elastic"),
|
||
},
|
||
}
|
||
|
||
logger.info(f"创建任务请求数据: workflow_id={workflow_id}, name={task_name}")
|
||
|
||
response = client.post("/api/generationApi/imgGeneration/task", json=task_data)
|
||
|
||
if response.status_code in [200, 201]:
|
||
data = response.json()
|
||
logger.info(f"创建任务响应: {data}")
|
||
|
||
# 尝试从响应中提取任务ID
|
||
if isinstance(data, dict):
|
||
# 新格式:{"data": {"task_id": "..."}}
|
||
if "data" in data and isinstance(data["data"], dict):
|
||
task_id = data["data"].get("task_id")
|
||
if task_id:
|
||
logger.info(f"创建任务成功: {task_name} (ID: {task_id})")
|
||
return str(task_id)
|
||
|
||
# 旧格式:{"taskId": "..."} 或 {"id": "..."}
|
||
task_id = data.get("taskId") or data.get("id")
|
||
if task_id:
|
||
logger.info(f"创建任务成功: {task_name} (ID: {task_id})")
|
||
return str(task_id)
|
||
|
||
logger.warning(f"创建任务响应中未找到任务ID: {data}")
|
||
else:
|
||
logger.error(f"创建任务失败: {response.status_code} - {response.text}")
|
||
|
||
return None
|
||
|
||
|
||
def api_delete_task(client, task_id: str) -> bool:
|
||
"""删除数据生成任务
|
||
|
||
Args:
|
||
client: API客户端
|
||
task_id: 任务ID
|
||
|
||
Returns:
|
||
是否删除成功
|
||
"""
|
||
response = client.post(
|
||
f"/api/generationApi/imgGeneration/task/{task_id}/delete", json={}
|
||
)
|
||
|
||
if response.status_code == 200:
|
||
logger.info(f"删除任务成功: {task_id}")
|
||
return True
|
||
else:
|
||
logger.warning(f"删除任务失败: {response.status_code} - {response.text}")
|
||
return False
|
||
|
||
|
||
def api_get_tasks(client, page_num: int = 1, page_size: int = 20) -> list:
|
||
"""获取任务列表
|
||
|
||
Args:
|
||
client: API客户端
|
||
page_num: 页码
|
||
page_size: 每页数量
|
||
|
||
Returns:
|
||
任务列表
|
||
"""
|
||
response = client.post(
|
||
"/api/generationApi/imgGeneration/tasks",
|
||
json={"pageNum": page_num, "pageSize": page_size},
|
||
)
|
||
|
||
if response.status_code == 200:
|
||
data = response.json()
|
||
if isinstance(data, list):
|
||
return data
|
||
elif isinstance(data, dict) and "list" in data:
|
||
return data["list"] if isinstance(data["list"], list) else []
|
||
elif isinstance(data, dict) and "data" in data:
|
||
if isinstance(data["data"], dict) and "list" in data["data"]:
|
||
return (
|
||
data["data"]["list"]
|
||
if isinstance(data["data"]["list"], list)
|
||
else []
|
||
)
|
||
elif isinstance(data["data"], list):
|
||
return data["data"]
|
||
return []
|
||
|
||
|
||
def api_create_workflow(
|
||
client, name: str, description: str = "", **kwargs
|
||
) -> Optional[str]:
|
||
"""创建工作流
|
||
|
||
Args:
|
||
client: API客户端
|
||
name: 工作流名称
|
||
description: 描述
|
||
**kwargs: 其他参数
|
||
- workflow_url: 工作流文件URL
|
||
- workflow_file: 工作流文件路径(会自动上传)
|
||
- workflow_id: 现有工作流ID(克隆)
|
||
- width: 宽度
|
||
- height: 高度
|
||
|
||
Returns:
|
||
工作流ID,创建失败返回None
|
||
"""
|
||
workflow_url = kwargs.get("workflow_url")
|
||
workflow_file = kwargs.get("workflow_file")
|
||
workflow_id = kwargs.get("workflow_id")
|
||
|
||
logger.info(
|
||
f"创建工作流: name={name}, workflow_file={workflow_file}, workflow_id={workflow_id}"
|
||
)
|
||
|
||
# 方案A:如果有workflow_id,直接使用(克隆现有工作流)
|
||
if workflow_id:
|
||
logger.info(f"使用现有工作流ID克隆: {workflow_id}")
|
||
workflow_data = {
|
||
"name": name,
|
||
"description": description,
|
||
"workflow_id": workflow_id,
|
||
}
|
||
# 方案B:如果有workflow_file,先上传
|
||
elif workflow_file and not workflow_url:
|
||
logger.info(f"开始上传工作流文件: {workflow_file}")
|
||
workflow_url = api_upload_file(client, workflow_file)
|
||
if not workflow_url:
|
||
logger.error("工作流文件上传失败")
|
||
return None
|
||
logger.info(f"文件上传成功,获得URL: {workflow_url}")
|
||
|
||
# 构造完整URL(可能需要)
|
||
if not workflow_url.startswith("http"):
|
||
base_url = client.base_url
|
||
workflow_url = f"{base_url}{workflow_url}"
|
||
logger.info(f"转换为完整URL: {workflow_url}")
|
||
|
||
workflow_data = {
|
||
"name": name,
|
||
"description": description,
|
||
"workflow_url": workflow_url,
|
||
}
|
||
# 方案C:如果已经有workflow_url
|
||
elif workflow_url:
|
||
# 构造完整URL(可能需要)
|
||
if not workflow_url.startswith("http"):
|
||
base_url = client.base_url
|
||
workflow_url = f"{base_url}{workflow_url}"
|
||
logger.info(f"转换为完整URL: {workflow_url}")
|
||
|
||
workflow_data = {
|
||
"name": name,
|
||
"description": description,
|
||
"workflow_url": workflow_url,
|
||
}
|
||
else:
|
||
logger.error("创建工作流需要workflow_url、workflow_file或workflow_id")
|
||
return None
|
||
|
||
# 添加可选参数
|
||
if kwargs.get("width"):
|
||
workflow_data["width"] = kwargs["width"]
|
||
if kwargs.get("height"):
|
||
workflow_data["height"] = kwargs["height"]
|
||
|
||
logger.info(f"创建工作流请求数据: {workflow_data}")
|
||
|
||
response = client.post(
|
||
"/api/generationApi/comfyWorkflow/create", json=workflow_data
|
||
)
|
||
|
||
if response.status_code in [200, 201]:
|
||
data = response.json()
|
||
# 安全地获取工作流ID
|
||
workflow_id = data.get("id") or data.get("workflowId") or data.get("workflow_id")
|
||
if not workflow_id and isinstance(data.get("data"), dict):
|
||
workflow_id = data["data"].get("id") or data["data"].get("workflow_id")
|
||
|
||
if workflow_id:
|
||
logger.info(f"创建工作流成功: {name} (ID: {workflow_id})")
|
||
return workflow_id
|
||
else:
|
||
logger.warning(f"创建工作流响应中未找到ID: {data}")
|
||
else:
|
||
logger.error(f"创建工作流失败: {response.status_code} - {response.text}")
|
||
|
||
return None
|
||
|
||
|
||
def api_get_task_detail(client, task_id: str) -> Optional[dict]:
|
||
"""获取任务详情
|
||
|
||
Args:
|
||
client: API客户端
|
||
task_id: 任务ID
|
||
|
||
Returns:
|
||
任务详情字典,失败返回None
|
||
"""
|
||
try:
|
||
response = client.post(
|
||
f"/api/generationApi/imgGeneration/task/{task_id}/detail", json={}
|
||
)
|
||
if response.status_code == 200:
|
||
data = response.json()
|
||
if isinstance(data, dict) and "data" in data:
|
||
logger.info(f"获取任务详情成功: {task_id}")
|
||
return data["data"]
|
||
return data
|
||
else:
|
||
logger.warning(f"获取任务详情失败: {response.status_code}")
|
||
except Exception as e:
|
||
logger.error(f"获取任务详情异常: {e}")
|
||
return None
|
||
|
||
|
||
def api_stop_task(client, task_id: str) -> bool:
|
||
"""停止数据生成任务
|
||
|
||
Args:
|
||
client: API客户端
|
||
task_id: 任务ID
|
||
|
||
Returns:
|
||
是否停止成功
|
||
"""
|
||
try:
|
||
response = client.post(
|
||
f"/api/generationApi/imgGeneration/task/{task_id}/stop", json={}
|
||
)
|
||
if response.status_code == 200:
|
||
logger.info(f"停止任务成功: {task_id}")
|
||
return True
|
||
else:
|
||
logger.warning(f"停止任务失败: {response.status_code} - {response.text}")
|
||
except Exception as e:
|
||
logger.error(f"停止任务异常: {e}")
|
||
return False
|
||
|
||
|
||
def api_score_task(client, task_id: str) -> bool:
|
||
"""触发任务图片评测
|
||
|
||
Args:
|
||
client: API客户端
|
||
task_id: 任务ID
|
||
|
||
Returns:
|
||
是否触发成功
|
||
"""
|
||
try:
|
||
response = client.post(
|
||
f"/api/generationApi/imgGeneration/task/{task_id}/score", json={}
|
||
)
|
||
if response.status_code == 200:
|
||
logger.info(f"触发评测成功: {task_id}")
|
||
return True
|
||
else:
|
||
logger.warning(f"触发评测失败: {response.status_code} - {response.text}")
|
||
except Exception as e:
|
||
logger.error(f"触发评测异常: {e}")
|
||
return False
|
||
|
||
|
||
def api_get_task_pods(client, task_id: str) -> list:
|
||
"""获取任务Pod列表
|
||
|
||
Args:
|
||
client: API客户端
|
||
task_id: 任务ID
|
||
|
||
Returns:
|
||
Pod列表
|
||
"""
|
||
try:
|
||
response = client.get(f"/api/generationApi/imgGeneration/task/{task_id}/pods")
|
||
if response.status_code == 200:
|
||
data = response.json()
|
||
if isinstance(data, dict) and "data" in data:
|
||
return data["data"] if isinstance(data["data"], list) else []
|
||
elif isinstance(data, list):
|
||
return data
|
||
else:
|
||
logger.warning(f"获取Pod列表失败: {response.status_code}")
|
||
except Exception as e:
|
||
logger.error(f"获取Pod列表异常: {e}")
|
||
return []
|
||
|
||
|
||
def api_delete_workflow(client, workflow_id: str) -> bool:
|
||
"""删除工作流
|
||
|
||
Args:
|
||
client: API客户端
|
||
workflow_id: 工作流ID
|
||
|
||
Returns:
|
||
是否删除成功
|
||
"""
|
||
# 注意:需要确认实际的删除API路径
|
||
response = client.delete(f"/api/generationApi/comfyWorkflow/{workflow_id}")
|
||
|
||
if response.status_code == 200:
|
||
logger.info(f"删除工作流成功: {workflow_id}")
|
||
return True
|
||
else:
|
||
logger.warning(f"删除工作流失败: {response.status_code} - {response.text}")
|
||
return False
|