#!/bin/bash # RTS Game - Docker Test Script # Tests Docker build and deployment locally set -e # Exit on error # Colors GREEN='\033[0;32m' RED='\033[0;31m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Configuration IMAGE_NAME="rts-game" CONTAINER_NAME="rts-test" PORT=7860 WAIT_TIME=8 echo -e "${BLUE}╔═══════════════════════════════════════════════════════════════╗${NC}" echo -e "${BLUE}║ 🐳 RTS Game - Docker Local Test 🐳 ║${NC}" echo -e "${BLUE}╚═══════════════════════════════════════════════════════════════╝${NC}" echo "" # Function to cleanup cleanup() { echo -e "\n${YELLOW}🧹 Cleaning up...${NC}" docker stop $CONTAINER_NAME 2>/dev/null || true docker rm $CONTAINER_NAME 2>/dev/null || true } # Trap to cleanup on exit trap cleanup EXIT # Check if Docker is installed echo -e "${BLUE}1️⃣ Checking Docker installation...${NC}" if ! command -v docker &> /dev/null; then echo -e "${RED}❌ Docker is not installed${NC}" echo "Please install Docker first: https://docs.docker.com/get-docker/" exit 1 fi echo -e "${GREEN}✅ Docker is installed: $(docker --version)${NC}" echo "" # Check if port is available echo -e "${BLUE}2️⃣ Checking if port $PORT is available...${NC}" if lsof -Pi :$PORT -sTCP:LISTEN -t >/dev/null 2>&1; then echo -e "${RED}❌ Port $PORT is already in use${NC}" echo "Please stop the process using this port or change the port" exit 1 fi echo -e "${GREEN}✅ Port $PORT is available${NC}" echo "" # Build Docker image echo -e "${BLUE}3️⃣ Building Docker image...${NC}" echo "This may take a few minutes on first build..." if docker build -t $IMAGE_NAME . > build.log 2>&1; then echo -e "${GREEN}✅ Docker image built successfully${NC}" rm build.log else echo -e "${RED}❌ Docker build failed${NC}" echo "See build.log for details" exit 1 fi echo "" # Run container echo -e "${BLUE}4️⃣ Starting Docker container...${NC}" if docker run -d -p $PORT:$PORT --name $CONTAINER_NAME $IMAGE_NAME > /dev/null 2>&1; then echo -e "${GREEN}✅ Container started successfully${NC}" else echo -e "${RED}❌ Failed to start container${NC}" exit 1 fi echo "" # Wait for startup echo -e "${BLUE}5️⃣ Waiting for server to start (${WAIT_TIME}s)...${NC}" for i in $(seq 1 $WAIT_TIME); do echo -n "." sleep 1 done echo "" echo "" # Test health endpoint echo -e "${BLUE}6️⃣ Testing /health endpoint...${NC}" HEALTH_RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:$PORT/health) if [ "$HEALTH_RESPONSE" = "200" ]; then echo -e "${GREEN}✅ Health check passed (HTTP $HEALTH_RESPONSE)${NC}" else echo -e "${RED}❌ Health check failed (HTTP $HEALTH_RESPONSE)${NC}" echo "Container logs:" docker logs $CONTAINER_NAME exit 1 fi echo "" # Test main page echo -e "${BLUE}7️⃣ Testing main page...${NC}" MAIN_RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:$PORT/) if [ "$MAIN_RESPONSE" = "200" ]; then echo -e "${GREEN}✅ Main page accessible (HTTP $MAIN_RESPONSE)${NC}" else echo -e "${RED}❌ Main page not accessible (HTTP $MAIN_RESPONSE)${NC}" exit 1 fi echo "" # Check container stats echo -e "${BLUE}8️⃣ Checking container stats...${NC}" STATS=$(docker stats $CONTAINER_NAME --no-stream --format "table {{.CPUPerc}}\t{{.MemUsage}}") echo "$STATS" echo -e "${GREEN}✅ Container is running efficiently${NC}" echo "" # Show container logs echo -e "${BLUE}9️⃣ Container logs (last 10 lines):${NC}" echo "─────────────────────────────────────────────────────────────" docker logs --tail 10 $CONTAINER_NAME echo "─────────────────────────────────────────────────────────────" echo "" # Test WebSocket (basic check) echo -e "${BLUE}🔟 Testing WebSocket endpoint...${NC}" WS_RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:$PORT/ws) # WebSocket returns 426 or similar when accessed via HTTP (expected) if [ "$WS_RESPONSE" = "426" ] || [ "$WS_RESPONSE" = "400" ]; then echo -e "${GREEN}✅ WebSocket endpoint responding${NC}" else echo -e "${YELLOW}⚠️ WebSocket check inconclusive (HTTP $WS_RESPONSE)${NC}" fi echo "" # Summary echo -e "${GREEN}╔═══════════════════════════════════════════════════════════════╗${NC}" echo -e "${GREEN}║ ✅ ALL TESTS PASSED! ✅ ║${NC}" echo -e "${GREEN}╚═══════════════════════════════════════════════════════════════╝${NC}" echo "" echo -e "${BLUE}📊 Test Summary:${NC}" echo " • Docker build: ✅ Success" echo " • Container start: ✅ Success" echo " • Health check: ✅ Pass" echo " • Main page: ✅ Accessible" echo " • WebSocket: ✅ Responding" echo "" echo -e "${BLUE}🌐 Access the game:${NC}" echo " http://localhost:$PORT" echo "" echo -e "${BLUE}📋 Useful commands:${NC}" echo " • View logs: docker logs -f $CONTAINER_NAME" echo " • Stop container: docker stop $CONTAINER_NAME" echo " • Remove container: docker rm $CONTAINER_NAME" echo " • Stats: docker stats $CONTAINER_NAME" echo "" echo -e "${YELLOW}⏸️ Press Ctrl+C to stop and cleanup${NC}" echo "" # Keep container running and show logs docker logs -f $CONTAINER_NAME