#!/bin/bash # 创建 Agent 测试是否正常工作,然后删除 # 使用: AGENT_MANAGER_URL=http://localhost:8000 ./scripts/test_create_delete_agent.sh # 或: ./scripts/test_create_delete_agent.sh http://your-manager:8000 set -e BASE_URL="${1:-${AGENT_MANAGER_URL:-http://localhost:8000}}" AGENT_NAME="test-echo-$(date +%s)" echo "==========================================" echo "Agent 创建/删除测试" echo "==========================================" echo "Manager URL: $BASE_URL" echo "Agent 名称: $AGENT_NAME" echo "" # 1. 创建 Agent (echo_agent 无需额外 config) echo ">>> 1. 创建 Agent (template=echo_agent)..." CREATE_RESP=$(curl -s -w "\n%{http_code}" -X POST "${BASE_URL}/agents" \ -H "Content-Type: application/json" \ -d "{ \"name\": \"${AGENT_NAME}\", \"template\": \"echo_agent\", \"config\": {} }") HTTP_BODY=$(echo "$CREATE_RESP" | head -n -1) HTTP_CODE=$(echo "$CREATE_RESP" | tail -n 1) if [ "$HTTP_CODE" != "200" ]; then echo "创建失败 HTTP $HTTP_CODE" echo "$HTTP_BODY" | python3 -m json.tool 2>/dev/null || echo "$HTTP_BODY" exit 1 fi echo "创建成功" echo "$HTTP_BODY" | python3 -m json.tool 2>/dev/null || echo "$HTTP_BODY" echo "" # 从响应中取 pod_ip 或 access_info POD_IP=$(echo "$HTTP_BODY" | python3 -c "import sys,json; d=json.load(sys.stdin); print(d.get('pod_ip','') or (d.get('access_info',{}) or {}).get('pod_url','').split('//')[-1].split(':')[0])" 2>/dev/null || true) EXTERNAL_IP=$(echo "$HTTP_BODY" | python3 -c "import sys,json; d=json.load(sys.stdin); a=d.get('access_info',{}); print(a.get('external_ip','') or a.get('ip_url','').split('//')[-1].split(':')[0] if isinstance(a,dict) else '')" 2>/dev/null || true) SERVICE_PORT=$(echo "$HTTP_BODY" | python3 -c "import sys,json; d=json.load(sys.stdin); print(d.get('service_port', 8000) or 8000)" 2>/dev/null || echo "8000") # 2. 等待 Pod 就绪并测活 echo ">>> 2. 等待 Pod 就绪并测试健康..." for i in 1 2 3 4 5 6 7 8 9 10; do STATUS_RESP=$(curl -s -w "\n%{http_code}" "${BASE_URL}/agents/${AGENT_NAME}/status") STATUS_BODY=$(echo "$STATUS_RESP" | head -n -1) STATUS_CODE=$(echo "$STATUS_RESP" | tail -n 1) if [ "$STATUS_CODE" != "200" ]; then sleep 3 continue fi STATUS=$(echo "$STATUS_BODY" | python3 -c "import sys,json; d=json.load(sys.stdin); print(d.get('status',''))" 2>/dev/null || true) if [ "$STATUS" = "Running" ]; then break fi sleep 3 done if [ "$STATUS" != "Running" ]; then echo "Pod 未在预期内变为 Running,当前 status: $STATUS" echo "继续尝试访问 Agent 健康端点..." fi # 尝试访问 Agent:优先外网 IP:80,否则 pod_ip:service_port AGENT_URL="" if [ -n "$EXTERNAL_IP" ]; then AGENT_URL="http://${EXTERNAL_IP}:80" elif [ -n "$POD_IP" ]; then AGENT_URL="http://${POD_IP}:${SERVICE_PORT}" fi if [ -n "$AGENT_URL" ]; then echo "访问 Agent: $AGENT_URL/health" if curl -sf --connect-timeout 10 "${AGENT_URL}/health" > /dev/null; then echo "Agent 健康检查通过" else echo "健康检查失败(可能 LoadBalancer 未就绪或网络不可达)" fi # 尝试根路径 if curl -sf --connect-timeout 5 "${AGENT_URL}/" > /dev/null; then echo "Agent 根路径可访问" fi else echo "未获取到 Pod IP 或外网 IP,跳过 Agent 端点测试" fi echo "" echo ">>> 3. 删除 Agent..." DEL_RESP=$(curl -s -w "\n%{http_code}" -X DELETE "${BASE_URL}/agents/${AGENT_NAME}") DEL_BODY=$(echo "$DEL_RESP" | head -n -1) DEL_CODE=$(echo "$DEL_RESP" | tail -n 1) if [ "$DEL_CODE" = "200" ] || [ "$DEL_CODE" = "204" ]; then echo "删除成功" echo "$DEL_BODY" | python3 -m json.tool 2>/dev/null || echo "$DEL_BODY" else echo "删除返回 HTTP $DEL_CODE" echo "$DEL_BODY" exit 1 fi echo "" echo "==========================================" echo "测试完成: 创建 -> 检查状态 -> 删除 均成功" echo "=========================================="