Skip to content

Commit ffe31e2

Browse files
authored
test: add script to run DB integration tests locally (#653)
# Description Based on [`unit-tests.yml`](https://github.com/a2aproject/a2a-python/blob/main/.github/workflows/unit-tests.yml). Supports: - `--debug` to start DB(s) without running tests (for selective running in IDE) - `--mysql`, `--postgres` to use only one DB - `--stop` to stop after running with `--debug` From project root: ```bash ./scripts/run_integration_tests.sh ``` Re #652 to troubleshoot failures
1 parent 72216b9 commit ffe31e2

3 files changed

Lines changed: 133 additions & 0 deletions

File tree

.github/actions/spelling/allow.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Llm
6363
lstrips
6464
mikeas
6565
mockurl
66+
mysqladmin
6667
notif
6768
oauthoidc
6869
oidc
@@ -71,6 +72,7 @@ otherurl
7172
postgres
7273
POSTGRES
7374
postgresql
75+
proot
7476
protoc
7577
pyi
7678
pypistats

scripts/docker-compose.test.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
services:
2+
postgres:
3+
image: postgres:15-alpine
4+
environment:
5+
POSTGRES_USER: a2a
6+
POSTGRES_PASSWORD: a2a_password
7+
POSTGRES_DB: a2a_test
8+
ports:
9+
- "5432:5432"
10+
healthcheck:
11+
test: ["CMD-SHELL", "pg_isready"]
12+
interval: 10s
13+
timeout: 5s
14+
retries: 5
15+
16+
mysql:
17+
image: mysql:8.0
18+
environment:
19+
MYSQL_ROOT_PASSWORD: root
20+
MYSQL_DATABASE: a2a_test
21+
MYSQL_USER: a2a
22+
MYSQL_PASSWORD: a2a_password
23+
ports:
24+
- "3306:3306"
25+
healthcheck:
26+
test: ["CMD-SHELL", "mysqladmin ping -h localhost -u root -proot"]
27+
interval: 10s
28+
timeout: 5s
29+
retries: 5

scripts/run_integration_tests.sh

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# Get the directory of this script
5+
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
6+
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
7+
8+
# Docker compose file path
9+
COMPOSE_FILE="$SCRIPT_DIR/docker-compose.test.yml"
10+
11+
# Initialize variables
12+
DEBUG_MODE=false
13+
STOP_MODE=false
14+
SERVICES=()
15+
PYTEST_ARGS=()
16+
17+
# Parse arguments
18+
while [[ $# -gt 0 ]]; do
19+
case $1 in
20+
--debug)
21+
DEBUG_MODE=true
22+
shift
23+
;;
24+
--stop)
25+
STOP_MODE=true
26+
shift
27+
;;
28+
--postgres)
29+
SERVICES+=("postgres")
30+
shift
31+
;;
32+
--mysql)
33+
SERVICES+=("mysql")
34+
shift
35+
;;
36+
*)
37+
# Preserve other arguments for pytest
38+
PYTEST_ARGS+=("$1")
39+
shift
40+
;;
41+
esac
42+
done
43+
44+
# Handle --stop
45+
if [[ "$STOP_MODE" == "true" ]]; then
46+
echo "Stopping test databases..."
47+
docker compose -f "$COMPOSE_FILE" down
48+
exit 0
49+
fi
50+
51+
# Default to running both databases if none specified
52+
if [[ ${#SERVICES[@]} -eq 0 ]]; then
53+
SERVICES=("postgres" "mysql")
54+
fi
55+
56+
# Cleanup function to stop docker containers
57+
cleanup() {
58+
echo "Stopping test databases..."
59+
docker compose -f "$COMPOSE_FILE" down
60+
}
61+
62+
# Start the databases
63+
echo "Starting/Verifying databases: ${SERVICES[*]}..."
64+
docker compose -f "$COMPOSE_FILE" up -d --wait "${SERVICES[@]}"
65+
66+
# Set up environment variables based on active services
67+
# Only export DSNs for started services so tests skip missing ones
68+
for service in "${SERVICES[@]}"; do
69+
if [[ "$service" == "postgres" ]]; then
70+
export POSTGRES_TEST_DSN="postgresql+asyncpg://a2a:a2a_password@localhost:5432/a2a_test"
71+
elif [[ "$service" == "mysql" ]]; then
72+
export MYSQL_TEST_DSN="mysql+aiomysql://a2a:a2a_password@localhost:3306/a2a_test"
73+
fi
74+
done
75+
76+
# Handle --debug mode
77+
if [[ "$DEBUG_MODE" == "true" ]]; then
78+
echo "---------------------------------------------------"
79+
echo "Debug mode enabled. Databases are running."
80+
echo "You can connect to them using the following DSNs."
81+
echo ""
82+
echo "Run the following commands to set up your environment:"
83+
echo ""
84+
[[ -n "$POSTGRES_TEST_DSN" ]] && echo "export POSTGRES_TEST_DSN=\"$POSTGRES_TEST_DSN\""
85+
[[ -n "$MYSQL_TEST_DSN" ]] && echo "export MYSQL_TEST_DSN=\"$MYSQL_TEST_DSN\""
86+
echo ""
87+
echo "---------------------------------------------------"
88+
echo "Run ./scripts/run_integration_tests.sh --stop to shut databases down."
89+
exit 0
90+
fi
91+
92+
# Register cleanup trap for normal test run
93+
trap cleanup EXIT
94+
95+
# Run the tests
96+
echo "Running integration tests..."
97+
cd "$PROJECT_ROOT"
98+
99+
uv run --extra all pytest -v \
100+
tests/server/tasks/test_database_task_store.py \
101+
tests/server/tasks/test_database_push_notification_config_store.py \
102+
"${PYTEST_ARGS[@]}"

0 commit comments

Comments
 (0)