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