File size: 4,510 Bytes
9d8bf2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

# Automated test script for all OpenSpiel games in Docker
# Usage: ./test_docker_all_games.sh

set -e

# Colors for output
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Configuration
IMAGE_NAME="openspiel-env:latest"
CONTAINER_NAME="openspiel-test"
PORT=8000
HEALTH_CHECK_URL="http://localhost:${PORT}/health"
MAX_WAIT=30

# Games to test
GAMES=("catch" "tic_tac_toe" "kuhn_poker" "cliff_walking" "2048" "blackjack")

# Results tracking
declare -a RESULTS
PASSED=0
FAILED=0

echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}OpenSpiel Docker Integration Test${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""

# Function to cleanup containers
cleanup() {
    echo -e "${YELLOW}Cleaning up containers...${NC}"
    docker stop ${CONTAINER_NAME} 2>/dev/null || true
    docker rm ${CONTAINER_NAME} 2>/dev/null || true
}

# Function to wait for server health
wait_for_health() {
    local game=$1
    echo -e "  โณ Waiting for server to be ready..."

    for i in $(seq 1 $MAX_WAIT); do
        if curl -s -f ${HEALTH_CHECK_URL} > /dev/null 2>&1; then
            echo -e "  ${GREEN}โœ“${NC} Server ready (${i}s)"
            return 0
        fi
        sleep 1
    done

    echo -e "  ${RED}โœ—${NC} Server health check failed after ${MAX_WAIT}s"
    return 1
}

# Function to test a game
test_game() {
    local game=$1
    echo -e "\n${BLUE}โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”${NC}"
    echo -e "${BLUE}Testing: ${game}${NC}"
    echo -e "${BLUE}โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”${NC}"

    # Stop any existing container
    cleanup

    # Start container with game
    echo -e "  ๐Ÿณ Starting Docker container..."
    docker run -d \
        --name ${CONTAINER_NAME} \
        -p ${PORT}:8000 \
        -e OPENSPIEL_GAME=${game} \
        ${IMAGE_NAME} > /dev/null

    # Wait for server to be ready
    if ! wait_for_health ${game}; then
        echo -e "  ${RED}โœ— FAILED${NC} - Server did not start"
        RESULTS+=("${game}:FAILED:Server did not start")
        FAILED=$((FAILED + 1))
        cleanup
        return 1
    fi

    # Run Python client test
    echo -e "  ๐ŸŽฎ Running Python client test..."
    if NO_PROXY=localhost,127.0.0.1 HTTP_PROXY= HTTPS_PROXY= \
       PYTHONPATH=$PWD/src:$PYTHONPATH \
       python3 examples/openspiel_simple.py > /tmp/test_${game}.log 2>&1; then

        # Check if episode completed successfully
        if grep -q "Episode finished!" /tmp/test_${game}.log; then
            echo -e "  ${GREEN}โœ“ PASSED${NC} - Episode completed successfully"
            RESULTS+=("${game}:PASSED")
            PASSED=$((PASSED + 1))
        else
            echo -e "  ${RED}โœ— FAILED${NC} - Episode did not complete"
            RESULTS+=("${game}:FAILED:Episode incomplete")
            FAILED=$((FAILED + 1))
        fi
    else
        echo -e "  ${RED}โœ— FAILED${NC} - Python client error"
        RESULTS+=("${game}:FAILED:Client error")
        FAILED=$((FAILED + 1))
    fi

    # Cleanup
    cleanup
}

# Run tests for all games
for game in "${GAMES[@]}"; do
    test_game ${game}
done

# Print summary
echo -e "\n${BLUE}========================================${NC}"
echo -e "${BLUE}Test Summary${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""

for result in "${RESULTS[@]}"; do
    IFS=':' read -r game status message <<< "$result"
    if [ "$status" == "PASSED" ]; then
        echo -e "  ${GREEN}โœ“${NC} ${game}"
    else
        echo -e "  ${RED}โœ—${NC} ${game} - ${message}"
    fi
done

echo ""
echo -e "Total: ${PASSED} passed, ${FAILED} failed out of ${#GAMES[@]} games"
echo ""

# Exit with appropriate code
if [ $FAILED -eq 0 ]; then
    echo -e "${GREEN}========================================${NC}"
    echo -e "${GREEN}All tests PASSED! ๐ŸŽ‰${NC}"
    echo -e "${GREEN}========================================${NC}"
    exit 0
else
    echo -e "${RED}========================================${NC}"
    echo -e "${RED}Some tests FAILED${NC}"
    echo -e "${RED}========================================${NC}"
    exit 1
fi