droboticflow/scripts/smoke-test.sh

139 lines
4.6 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
if [[ $# -gt 0 ]]; then
API_BASE_URL="$1"
WORKER_BASE_URL="${2:-${WORKER_BASE_URL:-}}"
else
API_BASE_URL="${API_BASE_URL:-http://127.0.0.1:3001}"
WORKER_BASE_URL="${WORKER_BASE_URL:-http://127.0.0.1:3002}"
fi
json_get() {
local expression="$1"
python3 -c "import json,sys; print(${expression})"
}
api_curl() {
curl --retry 6 --retry-delay 2 --retry-connrefused --retry-all-errors -fsS "$@"
}
api_health="$(api_curl "$API_BASE_URL/api/health")"
printf 'api health: %s\n' "$api_health"
printf '%s' "$api_health" | grep -q '"status":"ok"'
project_payload="$(python3 - <<'PY'
import json
print(json.dumps({
"name": "Smoke Test Project",
"description": "Created by scripts/smoke-test.sh",
}))
PY
)"
project_response="$(api_curl -X POST "$API_BASE_URL/api/projects" -H 'Content-Type: application/json' -d "$project_payload")"
project_id="$(printf '%s' "$project_response" | json_get 'json.load(sys.stdin)["id"]')"
printf 'created project: %s\n' "$project_response"
workflow_payload="$(python3 - <<'PY'
import json
print(json.dumps({
"name": "Smoke Test Workflow",
"description": "End-to-end workflow created by smoke test",
"type": "batch",
"nodes": [
{
"id": "source-1",
"type": "operator",
"data": {
"label": "真实数据源",
"category": "IO",
"description": "Use SOURCE_TEXT to emit a real manifest artifact",
"inputs": [],
"outputs": [{"id": "out-1", "name": "manifest", "type": "json"}],
"hyperparams": [{"key": "SOURCE_TEXT", "value": "smoke-test-payload"}],
},
"position": {"x": 0, "y": 0},
},
{
"id": "upload-1",
"type": "operator",
"data": {
"label": "结果上传",
"category": "IO",
"description": "Persist artifacts produced by upstream nodes",
"inputs": [{"id": "in-1", "name": "manifest", "type": "json"}],
"outputs": [],
},
"position": {"x": 240, "y": 0},
},
],
"edges": [
{
"id": "edge-source-upload",
"source": "source-1",
"sourceHandle": "out-1",
"target": "upload-1",
"targetHandle": "in-1",
}
],
}))
PY
)"
workflow_response="$(api_curl -X POST "$API_BASE_URL/api/projects/$project_id/workflows" -H 'Content-Type: application/json' -d "$workflow_payload")"
workflow_id="$(printf '%s' "$workflow_response" | json_get 'json.load(sys.stdin)["id"]')"
printf 'created workflow: %s\n' "$workflow_response"
queued_run="$(api_curl -X POST "$API_BASE_URL/api/workflows/$workflow_id/run" -H 'Content-Type: application/json' -d '{"trigger":"manual"}')"
run_id="$(printf '%s' "$queued_run" | json_get 'json.load(sys.stdin)["id"]')"
printf 'queued run: %s\n' "$queued_run"
maybe_process_once() {
if [[ -n "${WORKER_BASE_URL}" ]] && api_curl "$WORKER_BASE_URL/worker-api/health" >/dev/null 2>&1; then
api_curl -X POST "$WORKER_BASE_URL/worker-api/process-once" >/dev/null
fi
}
if [[ -n "${WORKER_BASE_URL}" ]] && api_curl "$WORKER_BASE_URL/worker-api/health" >/dev/null 2>&1; then
worker_health="$(api_curl "$WORKER_BASE_URL/worker-api/health")"
printf 'worker health: %s\n' "$worker_health"
fi
deadline=$((SECONDS + 180))
final_run=""
final_status=""
while (( SECONDS < deadline )); do
maybe_process_once
final_run="$(api_curl "$API_BASE_URL/api/runs/$run_id")"
final_status="$(printf '%s' "$final_run" | json_get 'json.load(sys.stdin)["status"]')"
printf 'run status: %s\n' "$final_status"
case "$final_status" in
completed)
break
;;
failed|cancelled|waiting_for_audit)
printf 'unexpected terminal status: %s\n' "$final_run" >&2
exit 1
;;
esac
sleep 2
done
if [[ "$final_status" != "completed" ]]; then
printf 'run did not complete before timeout: %s\n' "$final_run" >&2
exit 1
fi
logs_response="$(api_curl "$API_BASE_URL/api/runs/$run_id/logs")"
artifacts_response="$(api_curl "$API_BASE_URL/api/runs/$run_id/artifacts")"
printf 'logs: %s\n' "$logs_response"
printf 'artifacts: %s\n' "$artifacts_response"
printf '%s' "$logs_response" | python3 -c 'import json, sys; payload = json.load(sys.stdin); items = payload.get("items", []);
if not items: raise SystemExit("expected at least one log entry")'
printf '%s' "$artifacts_response" | python3 -c 'import json, sys; payload = json.load(sys.stdin); items = payload.get("items", []);
if not items: raise SystemExit("expected at least one artifact")'