rts-commander / tests /docker-test.sh
Luigi's picture
deploy(web): full clean snapshot with app code and assets
12d64f8
#!/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