Files
zigp2p/docker-tests/run-tests.sh
2026-02-06 21:03:59 -03:00

217 lines
5.4 KiB
Bash
Executable File

#!/bin/bash
#
# P2P Cross-Machine Test Runner
#
# This script builds and runs the Docker-based P2P tests with various configurations.
#
# Usage:
# ./run-tests.sh # Run basic test (1 seeder, 3 leechers)
# ./run-tests.sh --leechers 10 # Run with 10 leechers
# ./run-tests.sh --file-size 100 # Run with 100MB file
# ./run-tests.sh --chaos # Run with network chaos (requires pumba)
# ./run-tests.sh --clean # Clean up everything
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Default values
LEECHER_COUNT=3
FILE_SIZE_MB=10
PIECE_SIZE_KB=256
ENABLE_CHAOS=false
CLEAN_ONLY=false
DEBUG_MODE=false
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
--leechers|-l)
LEECHER_COUNT="$2"
shift 2
;;
--file-size|-f)
FILE_SIZE_MB="$2"
shift 2
;;
--piece-size|-p)
PIECE_SIZE_KB="$2"
shift 2
;;
--chaos|-c)
ENABLE_CHAOS=true
shift
;;
--clean)
CLEAN_ONLY=true
shift
;;
--debug|-d)
DEBUG_MODE=true
shift
;;
--help|-h)
echo "P2P Cross-Machine Test Runner"
echo ""
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " --leechers, -l NUM Number of leecher nodes (default: 3)"
echo " --file-size, -f MB Test file size in MB (default: 10)"
echo " --piece-size, -p KB Piece size in KB (default: 256)"
echo " --chaos, -c Enable network chaos testing"
echo " --debug, -d Start debug monitor container"
echo " --clean Clean up containers and volumes"
echo " --help, -h Show this help"
exit 0
;;
*)
echo -e "${RED}Unknown option: $1${NC}"
exit 1
;;
esac
done
# Change to docker-tests directory
cd "$(dirname "$0")"
# Export environment variables for docker-compose
export LEECHER_COUNT
export FILE_SIZE_MB
export PIECE_SIZE_KB
print_header() {
echo ""
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE} $1${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""
}
print_status() {
echo -e "${GREEN}[✓]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[!]${NC} $1"
}
print_error() {
echo -e "${RED}[✗]${NC} $1"
}
# Clean up function
cleanup() {
print_header "Cleaning Up"
docker compose down -v --remove-orphans 2>/dev/null || true
docker compose --profile debug down -v 2>/dev/null || true
print_status "Containers stopped and removed"
print_status "Volumes cleaned"
}
# If clean only, just cleanup and exit
if [ "$CLEAN_ONLY" = true ]; then
cleanup
exit 0
fi
# Check Docker is available
if ! command -v docker &> /dev/null; then
print_error "Docker is not installed or not in PATH"
exit 1
fi
if ! docker info &> /dev/null; then
print_error "Docker daemon is not running"
exit 1
fi
print_header "P2P Cross-Machine Test"
echo "Configuration:"
echo " - Leecher count: $LEECHER_COUNT"
echo " - File size: ${FILE_SIZE_MB}MB"
echo " - Piece size: ${PIECE_SIZE_KB}KB"
echo " - Network chaos: $ENABLE_CHAOS"
echo " - Debug mode: $DEBUG_MODE"
echo ""
# Clean up any previous runs
print_status "Cleaning up previous runs..."
docker compose down -v --remove-orphans 2>/dev/null || true
# Build the images
print_header "Building Docker Images"
docker compose build --progress=plain
print_status "Docker images built successfully"
# Start the network
print_header "Starting P2P Network"
if [ "$DEBUG_MODE" = true ]; then
docker compose --profile debug up -d
print_status "Started with debug monitor"
print_warning "Debug: docker exec -it p2p-monitor bash"
else
docker compose up -d --scale leecher=$LEECHER_COUNT
fi
print_status "Seeder started"
print_status "$LEECHER_COUNT leecher(s) started"
# Wait for services to be healthy
print_header "Waiting for Services"
echo "Waiting for seeder to be healthy..."
timeout=60
while [ $timeout -gt 0 ]; do
if docker compose ps seeder | grep -q "healthy"; then
print_status "Seeder is healthy"
break
fi
echo -n "."
sleep 2
timeout=$((timeout - 2))
done
if [ $timeout -le 0 ]; then
print_error "Seeder failed to become healthy"
docker compose logs seeder
cleanup
exit 1
fi
# Follow logs
print_header "Test Logs"
echo "Following container logs (Ctrl+C to stop)..."
echo ""
# If chaos mode, add network delays
if [ "$ENABLE_CHAOS" = true ]; then
print_warning "Chaos mode enabled - this requires pumba to be installed"
if command -v pumba &> /dev/null; then
# Add 50ms delay with 10ms jitter to leechers
echo "Adding network chaos to leechers..."
pumba netem --duration 5m delay --time 50 --jitter 10 \
$(docker ps -q -f "name=leecher") &
else
print_warning "Pumba not found. Install with: brew install pumba"
print_warning "Continuing without network chaos..."
fi
fi
# Stream logs
docker compose logs -f --tail=100
# This won't be reached unless user Ctrl+C
echo ""
print_header "Test Complete"
echo "Run './run-tests.sh --clean' to clean up"