#!/bin/bash set -e echo "=== Route-Switcher Failover Test Script ===" echo # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Function to print colored output print_status() { echo -e "${GREEN}[INFO]${NC} $1" } print_warning() { echo -e "${YELLOW}[WARN]${NC} $1" } print_error() { echo -e "${RED}[ERROR]${NC} $1" } # Check if podman-compose is available if ! command -v podman-compose &> /dev/null; then print_error "podman-compose is not installed or not in PATH" exit 1 fi # Start the environment print_status "Starting test environment..." podman-compose up -d # Wait for containers to be ready print_status "Waiting for containers to initialize..." sleep 10 # Check if all containers are running print_status "Checking container status..." podman-compose ps # Set up initial default route for route-switcher (testing scenario only) # In production, this would be configured by the network admin print_status "Setting up initial default route via primary router..." podman-compose exec route-switcher ip route add default via 192.168.200.11 dev eth0 # Verify network connectivity print_status "Testing initial connectivity..." # Test from route-switcher to target (should use default route via primary) print_status "Testing connectivity via primary router..." podman-compose exec route-switcher ping -c 3 192.168.202.100 || { print_error "Primary connectivity test failed" podman-compose down exit 1 } # Test specific interface connectivity print_status "Testing connectivity via secondary interface..." podman-compose exec route-switcher ping -c 3 -I eth1 192.168.202.100 || { print_warning "Secondary interface connectivity test failed (might be expected initially)" } print_status "Initial connectivity tests passed!" # Show current routing table print_status "Current routing table in route-switcher:" podman-compose exec route-switcher ip route show echo print_warning "=== Starting Failover Test ===" print_status "Monitoring route-switcher logs (press Ctrl+C to stop monitoring)..." echo # Start monitoring logs in background podman-compose logs -f route-switcher & LOGS_PID=$! # Wait a bit for initial logs sleep 5 print_status "Simulating primary router failure by shutting down eth0..." podman-compose exec primary-router ip link set eth0 down print_status "Waiting for failover to occur..." sleep 15 # Check if failover happened print_status "Checking routing table after primary failure..." podman-compose exec route-switcher ip route show print_status "Testing connectivity after failover..." podman-compose exec route-switcher ping -c 3 192.168.202.100 || { print_warning "Connectivity test after failover failed (this might be expected during transition)" } print_status "Restoring primary router..." podman-compose exec primary-router ip link set eth0 up print_status "Waiting for failback (should take ~60 seconds of stable connection)..." sleep 70 print_status "Final routing table check:" podman-compose exec route-switcher ip route show print_status "Final connectivity test:" podman-compose exec route-switcher ping -c 3 192.168.202.100 # Stop monitoring logs kill $LOGS_PID 2>/dev/null || true echo print_status "=== Test Complete ===" print_status "To stop the environment: podman-compose down" print_status "To view logs: podman-compose logs route-switcher"