Spaces:
Sleeping
Sleeping
File size: 6,033 Bytes
12d64f8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
#!/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
|