-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathrun_agent_test.sh
More file actions
executable file
Β·442 lines (365 loc) Β· 13.3 KB
/
run_agent_test.sh
File metadata and controls
executable file
Β·442 lines (365 loc) Β· 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#!/bin/bash
#
# Run a single agent tutorial test
#
# This script runs the test for a single agent tutorial.
# It starts the agent, runs tests against it, then stops the agent.
#
# Usage:
# ./run_agent_test.sh <tutorial_path> # Run single tutorial test
# ./run_agent_test.sh --build-cli <tutorial_path> # Build CLI from source and run test
# ./run_agent_test.sh --view-logs <tutorial_path> # View logs for specific tutorial
# ./run_agent_test.sh --view-logs # View most recent agent logs
#
set -e # Exit on error
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Parse arguments
TUTORIAL_PATH=""
VIEW_LOGS=false
BUILD_CLI=false
for arg in "$@"; do
if [[ "$arg" == "--view-logs" ]]; then
VIEW_LOGS=true
elif [[ "$arg" == "--build-cli" ]]; then
BUILD_CLI=true
else
TUTORIAL_PATH="$arg"
fi
done
# Function to check prerequisites for running this test suite
check_prerequisites() {
# Check that we are in the examples/tutorials directory
if [[ "$PWD" != */examples/tutorials ]]; then
echo -e "${RED}β Please run this script from the examples/tutorials directory${NC}"
exit 1
fi
# Check if uv is available
if ! command -v uv &> /dev/null; then
echo -e "${RED}β uv is required but not installed${NC}"
echo "Please install uv: curl -LsSf https://astral.sh/uv/install.sh | sh"
exit 1
fi
echo -e "${GREEN}β
Prerequisites check passed${NC}"
}
# Function to wait for agent to be ready
wait_for_agent_ready() {
local name=$1
local logfile="/tmp/agentex-${name}.log"
local timeout=45 # seconds - increased to account for package installation time
local elapsed=0
echo -e "${YELLOW}β³ Waiting for ${name} agent to be ready...${NC}"
while [ $elapsed -lt $timeout ]; do
# Check if agent is successfully registered
if grep -q "Successfully registered agent" "$logfile" 2>/dev/null; then
# For temporal agents, also wait for workers to be ready
if [[ "$tutorial_path" == *"temporal"* ]]; then
# This is a temporal agent - wait for workers too
if grep -q "Running workers for task queue" "$logfile" 2>/dev/null; then
return 0
fi
else
return 0
fi
fi
sleep 1
((elapsed++))
done
echo -e "${RED}β Timeout waiting for ${name} agent to be ready${NC}"
echo -e "${YELLOW}π Agent logs:${NC}"
if [[ -f "$logfile" ]]; then
echo "----------------------------------------"
tail -50 "$logfile"
echo "----------------------------------------"
else
echo "β Log file not found: $logfile"
fi
return 1
}
# Function to start agent in background
start_agent() {
local tutorial_path=$1
local name=$(basename "$tutorial_path")
local logfile="/tmp/agentex-${name}.log"
echo -e "${YELLOW}π Starting ${name} agent...${NC}"
# Check if tutorial directory exists
if [[ ! -d "$tutorial_path" ]]; then
echo -e "${RED}β Tutorial directory not found: $tutorial_path${NC}"
return 1
fi
# Check if manifest exists
if [[ ! -f "$tutorial_path/manifest.yaml" ]]; then
echo -e "${RED}β Manifest not found: $tutorial_path/manifest.yaml${NC}"
return 1
fi
# Save current directory
local original_dir="$PWD"
# Change to tutorial directory
cd "$tutorial_path" || return 1
# Start the agent in background and capture PID
local manifest_path="$PWD/manifest.yaml" # Always use full path
if [ "$BUILD_CLI" = true ]; then
# Use wheel from dist directory at repo root
local wheel_file=$(ls /home/runner/work/*/*/dist/agentex_sdk-*.whl 2>/dev/null | head -n1)
if [[ -z "$wheel_file" ]]; then
echo -e "${RED}β No built wheel found in dist/agentex_sdk-*.whl${NC}"
echo -e "${YELLOW}π‘ Please build the local SDK first by running: uv build${NC}"
echo -e "${YELLOW}π‘ From the repo root directory${NC}"
cd "$original_dir"
return 1
fi
# Use the built wheel
uv run --with "$wheel_file" agentex agents run --manifest "$manifest_path" > "$logfile" 2>&1 &
else
uv run agentex agents run --manifest manifest.yaml > "$logfile" 2>&1 &
fi
local pid=$!
# Return to original directory
cd "$original_dir"
echo "$pid" > "/tmp/agentex-${name}.pid"
echo -e "${GREEN}β
${name} agent started (PID: $pid, logs: $logfile)${NC}"
# Wait for agent to be ready
if ! wait_for_agent_ready "$name"; then
kill -9 $pid 2>/dev/null
return 1
fi
return 0
}
# Helper function to view agent container logs
view_agent_logs() {
local tutorial_path=$1
# If tutorial path is provided, view logs for that specific tutorial
if [[ -n "$tutorial_path" ]]; then
local name=$(basename "$tutorial_path")
local logfile="/tmp/agentex-${name}.log"
echo -e "${YELLOW}π Viewing logs for ${name}...${NC}"
echo -e "${YELLOW}Log file: $logfile${NC}"
echo ""
if [[ ! -f "$logfile" ]]; then
echo -e "${RED}β Log file not found: $logfile${NC}"
return 1
fi
# Display the logs
tail -f "$logfile"
else
# No specific tutorial, find the most recent log file
local latest_log=$(ls -t /tmp/agentex-*.log 2>/dev/null | head -1)
if [[ -z "$latest_log" ]]; then
echo -e "${RED}β No agent log files found in /tmp/agentex-*.log${NC}"
echo -e "${YELLOW}Available log files:${NC}"
ls -lht /tmp/agentex-*.log 2>/dev/null || echo " (none)"
return 1
fi
echo -e "${YELLOW}π Viewing most recent agent logs...${NC}"
echo -e "${YELLOW}Log file: $latest_log${NC}"
echo ""
# Display the logs
tail -f "$latest_log"
fi
}
# Function to stop agent
stop_agent() {
local tutorial_path=$1
local name=$(basename "$tutorial_path")
local pidfile="/tmp/agentex-${name}.pid"
local logfile="/tmp/agentex-${name}.log"
echo -e "${YELLOW}π Stopping ${name} agent...${NC}"
# Check if PID file exists
if [[ ! -f "$pidfile" ]]; then
echo -e "${YELLOW}β οΈ No PID file found for ${name} agent${NC}"
return 0
fi
# Read PID from file
local pid=$(cat "$pidfile")
# Check if process is running and kill it
if kill -0 "$pid" 2>/dev/null; then
echo -e "${YELLOW}Stopping ${name} agent (PID: $pid)${NC}"
kill "$pid" 2>/dev/null || true
rm -f "$pidfile"
else
echo -e "${YELLOW}β οΈ ${name} agent was not running${NC}"
rm -f "$pidfile"
fi
echo -e "${GREEN}β
${name} agent stopped${NC}"
echo -e "${YELLOW}Logs available at: $logfile${NC}"
return 0
}
# Function to run test for a tutorial
run_test() {
local tutorial_path=$1
local name=$(basename "$tutorial_path")
echo -e "${YELLOW}π§ͺ Running tests for ${name}...${NC}"
# Check if tutorial directory exists
if [[ ! -d "$tutorial_path" ]]; then
echo -e "${RED}β Tutorial directory not found: $tutorial_path${NC}"
return 1
fi
# Check if test file exists
if [[ ! -f "$tutorial_path/tests/test_agent.py" ]]; then
echo -e "${RED}β Test file not found: $tutorial_path/tests/test_agent.py${NC}"
return 1
fi
# Save current directory
local original_dir="$PWD"
# Change to tutorial directory
cd "$tutorial_path" || return 1
# Determine pytest command - use built wheel if available (same wheel used to start agent)
local pytest_cmd="uv run pytest"
if [ "$BUILD_CLI" = true ]; then
local wheel_file=$(ls /home/runner/work/*/*/dist/agentex_sdk-*.whl 2>/dev/null | head -n1)
if [[ -z "$wheel_file" ]]; then
# Fallback for local development
wheel_file=$(ls "${SCRIPT_DIR}/../../dist/agentex_sdk-*.whl" 2>/dev/null | head -n1)
fi
if [[ -n "$wheel_file" ]]; then
pytest_cmd="uv run --with $wheel_file pytest"
fi
fi
# Run the tests with retry mechanism
local max_retries=5
local retry_count=0
local exit_code=1
while [ $retry_count -lt $max_retries ]; do
if [ $retry_count -gt 0 ]; then
echo -e "${YELLOW}π Retrying tests (attempt $((retry_count + 1))/$max_retries)...${NC}"
fi
# Stream pytest output directly in real-time
$pytest_cmd tests/test_agent.py -v -s
exit_code=$?
if [ $exit_code -eq 0 ]; then
break
else
retry_count=$((retry_count + 1))
if [ $retry_count -lt $max_retries ]; then
sleep 5
fi
fi
done
# Return to original directory
cd "$original_dir"
if [ $exit_code -eq 0 ]; then
echo -e "${GREEN}β
Tests passed for ${name}${NC}"
return 0
else
echo -e "${RED}β Tests failed for ${name}${NC}"
return 1
fi
}
# Function to execute test flow for a single tutorial
execute_tutorial_test() {
local tutorial=$1
echo ""
echo "================================================================================"
echo "Testing: $tutorial"
echo "================================================================================"
# Start the agent
if ! start_agent "$tutorial"; then
echo -e "${RED}β FAILED to start agent: $tutorial${NC}"
return 1
fi
# Run the tests
local test_passed=false
if run_test "$tutorial"; then
echo -e "${GREEN}β
PASSED: $tutorial${NC}"
test_passed=true
else
echo -e "${RED}β FAILED: $tutorial${NC}"
fi
# Stop the agent
stop_agent "$tutorial"
echo ""
if [ "$test_passed" = true ]; then
return 0
else
return 1
fi
}
# Function to check if built wheel is available
check_built_wheel() {
# Navigate to the repo root (two levels up from examples/tutorials)
local repo_root="../../"
local original_dir="$PWD"
cd "$repo_root" || {
echo -e "${RED}β Failed to navigate to repo root${NC}"
return 1
}
# Check if wheel exists in dist directory at repo root
local wheel_file=$(ls /home/runner/work/*/*/dist/agentex_sdk-*.whl 2>/dev/null | head -n1)
if [[ -z "$wheel_file" ]]; then
echo -e "${RED}β No built wheel found in dist/agentex_sdk-*.whl${NC}"
echo -e "${YELLOW}π‘ Please build the local SDK first by running: uv build${NC}"
echo -e "${YELLOW}π‘ From the repo root directory${NC}"
cd "$original_dir"
return 1
fi
# Test the wheel by running agentex --help
if ! uv run --with "$wheel_file" agentex --help >/dev/null 2>&1; then
echo -e "${RED}β Failed to run agentex with built wheel${NC}"
cd "$original_dir"
return 1
fi
cd "$original_dir"
return 0
}
# Main execution function
main() {
# Handle --view-logs flag
if [ "$VIEW_LOGS" = true ]; then
if [[ -n "$TUTORIAL_PATH" ]]; then
view_agent_logs "$TUTORIAL_PATH"
else
view_agent_logs
fi
exit 0
fi
# Require tutorial path
if [[ -z "$TUTORIAL_PATH" ]]; then
echo -e "${RED}β Error: Tutorial path is required${NC}"
echo ""
echo "Usage:"
echo " ./run_agent_test.sh <tutorial_path> # Run single tutorial test"
echo " ./run_agent_test.sh --build-cli <tutorial_path> # Build CLI from source and run test"
echo " ./run_agent_test.sh --view-logs <tutorial_path> # View logs for specific tutorial"
echo " ./run_agent_test.sh --view-logs # View most recent agent logs"
echo ""
echo "Examples:"
echo " ./run_agent_test.sh 00_sync/000_hello_acp"
echo " ./run_agent_test.sh --build-cli 00_sync/000_hello_acp"
exit 1
fi
echo "================================================================================"
echo "Running Tutorial Test: $TUTORIAL_PATH"
echo "================================================================================"
# Check prerequisites
check_prerequisites
echo ""
# Check built wheel if requested
if [ "$BUILD_CLI" = true ]; then
if ! check_built_wheel; then
echo -e "${RED}β Failed to find or verify built wheel${NC}"
exit 1
fi
echo ""
fi
# Execute the single tutorial test
if execute_tutorial_test "$TUTORIAL_PATH"; then
echo ""
echo "================================================================================"
echo -e "${GREEN}π Test passed for: $TUTORIAL_PATH${NC}"
echo "================================================================================"
exit 0
else
echo ""
echo "================================================================================"
echo -e "${RED}β Test failed for: $TUTORIAL_PATH${NC}"
echo "================================================================================"
exit 1
fi
}
# Run main function
main