Skip to content

Commit 9ab28ed

Browse files
committed
Add test container setup and teardown scripts
- Introduced `setup-test-containers.sh` for streamlined startup of test containers, including health checks and environment variable loading. - Added `teardown-test-containers.sh` for simplified container shutdown, with options to remove volumes. - Enhanced user feedback with color-coded messages for better visibility during test setup and teardown processes.
1 parent 61d72a5 commit 9ab28ed

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

tests/setup-test-containers.sh

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/bin/bash
2+
# Test Container Startup Script
3+
# Smart startup - checks if already running, handles port conflicts automatically
4+
5+
set -e
6+
7+
# Colors
8+
RED='\033[0;31m'
9+
GREEN='\033[0;32m'
10+
BLUE='\033[0;34m'
11+
YELLOW='\033[1;33m'
12+
NC='\033[0m'
13+
14+
print_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
15+
print_success() { echo -e "${GREEN}[SUCCESS]${NC} $1"; }
16+
print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1"; }
17+
print_error() { echo -e "${RED}[ERROR]${NC} $1"; }
18+
19+
# Navigate to backend directory
20+
SCRIPT_DIR="$(dirname "$0")"
21+
cd "$SCRIPT_DIR/../backends/advanced" || exit 1
22+
23+
# Load environment variables for tests
24+
if [ -f "$SCRIPT_DIR/setup/.env.test" ]; then
25+
print_info "Loading test environment..."
26+
set -a
27+
source "$SCRIPT_DIR/setup/.env.test"
28+
set +a
29+
fi
30+
31+
# Load HF_TOKEN from speaker-recognition service if available
32+
SPEAKER_ENV="$SCRIPT_DIR/../extras/speaker-recognition/.env"
33+
if [ -f "$SPEAKER_ENV" ] && [ -z "$HF_TOKEN" ]; then
34+
print_info "Loading HF_TOKEN from speaker-recognition..."
35+
set -a
36+
source "$SPEAKER_ENV"
37+
set +a
38+
fi
39+
40+
# Use unique project name
41+
export COMPOSE_PROJECT_NAME="advanced-backend-test"
42+
43+
# Configuration
44+
FRESH_BUILD="${FRESH_BUILD:-false}" # Set to true for clean rebuild with volume removal
45+
46+
# Check if containers are already running and healthy
47+
if [ "$FRESH_BUILD" = "false" ]; then
48+
if curl -s http://localhost:8001/health > /dev/null 2>&1; then
49+
print_success "Test containers already running and healthy"
50+
print_info "Backend: http://localhost:8001"
51+
print_info "To force rebuild: FRESH_BUILD=true ./setup-test-containers.sh"
52+
exit 0
53+
fi
54+
fi
55+
56+
# Clean up any existing test containers from ANY project name to avoid port conflicts
57+
print_info "Cleaning up any existing test containers..."
58+
docker compose -f docker-compose-test.yml down 2>/dev/null || true
59+
COMPOSE_PROJECT_NAME=advanced docker compose -f docker-compose-test.yml down 2>/dev/null || true
60+
61+
# Remove any stale "Created" containers that might be holding ports
62+
docker ps -a --filter "name=backend-test" --filter "status=created" --format "{{.Names}}" | xargs -r docker rm -f 2>/dev/null || true
63+
64+
# Fresh build - remove everything and rebuild
65+
if [ "$FRESH_BUILD" = "true" ]; then
66+
print_info "Fresh build requested - removing volumes and rebuilding images..."
67+
docker compose -f docker-compose-test.yml down -v 2>/dev/null || true
68+
69+
# Start with build flag
70+
print_info "Building and starting test containers..."
71+
docker compose -f docker-compose-test.yml up -d --build --wait
72+
73+
print_success "Fresh build complete!"
74+
else
75+
# Normal startup
76+
print_info "Starting test containers..."
77+
docker compose -f docker-compose-test.yml up -d --wait
78+
79+
print_success "Containers started!"
80+
fi
81+
82+
print_success "All services ready!"
83+
print_info "Backend: http://localhost:8001"
84+
print_info "MongoDB: localhost:27018"
85+
print_info "Redis: localhost:6380"
86+
print_info "Qdrant: localhost:6337"

tests/teardown-test-containers.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/bash
2+
# Test Container Teardown Script
3+
# Simplified - just uses docker compose down
4+
5+
set -e
6+
7+
# Colors
8+
RED='\033[0;31m'
9+
GREEN='\033[0;32m'
10+
BLUE='\033[0;34m'
11+
YELLOW='\033[1;33m'
12+
NC='\033[0m'
13+
14+
print_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
15+
print_success() { echo -e "${GREEN}[SUCCESS]${NC} $1"; }
16+
print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1"; }
17+
18+
# Navigate to backend directory
19+
cd "$(dirname "$0")/../backends/advanced" || exit 1
20+
21+
# Use unique project name
22+
export COMPOSE_PROJECT_NAME="advanced-backend-test"
23+
24+
if [ "${REMOVE_VOLUMES:-false}" = "true" ]; then
25+
print_info "Stopping containers and removing volumes..."
26+
docker compose -f docker-compose-test.yml down -v
27+
print_success "Containers and volumes removed"
28+
else
29+
print_info "Stopping containers (keeping volumes)..."
30+
docker compose -f docker-compose-test.yml down
31+
print_success "Containers stopped (volumes preserved)"
32+
print_warning "To remove volumes: REMOVE_VOLUMES=true ./teardown-test-containers.sh"
33+
fi

0 commit comments

Comments
 (0)