Team Ai
Apppublic

openenv/atari_env

sourceHugging Faceupdated 6mo agoView on Hugging Face
3likes
test_atari_docker.sh334 linesDownload Raw Back to root
1#!/bin/bash2# Comprehensive Docker test for Atari environment3# Tests: Build, Start, Health, Reset, Step, State, Cleanup4 5set -e  # Exit on error6 7# Colors for output8RED='\033[0;31m'9GREEN='\033[0;32m'10YELLOW='\033[1;33m'11BLUE='\033[0;34m'12NC='\033[0m' # No Color13 14# Configuration15IMAGE_NAME="atari-env"16IMAGE_TAG="test"17CONTAINER_NAME="atari-env-test"18PORT="8765"  # Use non-standard port to avoid conflicts19HEALTH_RETRIES=3020HEALTH_DELAY=221 22# Cleanup function23cleanup() {24    echo -e "\n${BLUE}Cleaning up...${NC}"25    docker stop ${CONTAINER_NAME} 2>/dev/null || true26    docker rm ${CONTAINER_NAME} 2>/dev/null || true27    echo -e "${GREEN}✓${NC} Cleanup complete"28}29 30# Set trap to cleanup on exit31trap cleanup EXIT32 33# Header34echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"35echo "  ATARI ENVIRONMENT DOCKER TEST"36echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"37echo ""38 39# Check prerequisites40echo -e "${BLUE}Checking prerequisites...${NC}"41if ! command -v docker &> /dev/null; then42    echo -e "${RED}✗${NC} Docker is not installed"43    exit 144fi45echo -e "${GREEN}✓${NC} Docker is installed"46 47if ! command -v curl &> /dev/null; then48    echo -e "${RED}✗${NC} curl is not installed"49    exit 150fi51echo -e "${GREEN}✓${NC} curl is installed"52 53# Check if we're in the right directory54if [ ! -f "envs/atari_env/server/Dockerfile" ]; then55    echo -e "${RED}✗${NC} Must run from OpenEnv root directory"56    exit 157fi58echo -e "${GREEN}✓${NC} In correct directory"59 60# Step 1: Build Docker image61echo ""62echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"63echo -e "${BLUE}STEP 1: Building Docker Image${NC}"64echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"65 66echo "Building ${IMAGE_NAME}:${IMAGE_TAG}..."67if docker build -f envs/atari_env/server/Dockerfile -t ${IMAGE_NAME}:${IMAGE_TAG} . 2>&1 | tee /tmp/atari_build.log | tail -n 20; then68    echo -e "${GREEN}✓${NC} Docker image built successfully"69else70    echo -e "${RED}✗${NC} Docker build failed"71    echo "See /tmp/atari_build.log for full output"72    exit 173fi74 75# Check image exists76if docker image inspect ${IMAGE_NAME}:${IMAGE_TAG} &> /dev/null; then77    IMAGE_SIZE=$(docker image inspect ${IMAGE_NAME}:${IMAGE_TAG} --format='{{.Size}}' | awk '{print $1/1024/1024}')78    echo -e "${GREEN}✓${NC} Image size: ${IMAGE_SIZE} MB"79else80    echo -e "${RED}✗${NC} Image not found after build"81    exit 182fi83 84# Step 2: Start container85echo ""86echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"87echo -e "${BLUE}STEP 2: Starting Container${NC}"88echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"89 90# Clean up any existing container91docker rm -f ${CONTAINER_NAME} 2>/dev/null || true92 93echo "Starting container on port ${PORT}..."94docker run -d \95    --name ${CONTAINER_NAME} \96    -p ${PORT}:8000 \97    -e ATARI_GAME=pong \98    -e ATARI_OBS_TYPE=ram \99    -e ATARI_FRAMESKIP=4 \100    ${IMAGE_NAME}:${IMAGE_TAG}101 102if [ $? -eq 0 ]; then103    echo -e "${GREEN}✓${NC} Container started: ${CONTAINER_NAME}"104else105    echo -e "${RED}✗${NC} Failed to start container"106    exit 1107fi108 109# Wait for container to be running110sleep 2111if docker ps | grep -q ${CONTAINER_NAME}; then112    echo -e "${GREEN}✓${NC} Container is running"113else114    echo -e "${RED}✗${NC} Container is not running"115    docker logs ${CONTAINER_NAME}116    exit 1117fi118 119# Step 3: Wait for health check120echo ""121echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"122echo -e "${BLUE}STEP 3: Waiting for Server${NC}"123echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"124 125echo "Waiting for server to be ready (timeout: ${HEALTH_RETRIES}s)..."126for i in $(seq 1 ${HEALTH_RETRIES}); do127    if curl -s http://localhost:${PORT}/health > /dev/null 2>&1; then128        echo -e "${GREEN}✓${NC} Server is ready (${i}s)"129        break130    fi131 132    if [ $i -eq ${HEALTH_RETRIES} ]; then133        echo -e "${RED}✗${NC} Server did not become ready in time"134        echo "Container logs:"135        docker logs ${CONTAINER_NAME}136        exit 1137    fi138 139    echo -n "."140    sleep ${HEALTH_DELAY}141done142 143# Step 4: Test health endpoint144echo ""145echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"146echo -e "${BLUE}STEP 4: Testing Health Endpoint${NC}"147echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"148 149HEALTH_RESPONSE=$(curl -s http://localhost:${PORT}/health)150echo "Response: ${HEALTH_RESPONSE}"151 152if echo "${HEALTH_RESPONSE}" | grep -q "healthy"; then153    echo -e "${GREEN}✓${NC} Health endpoint working"154else155    echo -e "${RED}✗${NC} Health endpoint failed"156    exit 1157fi158 159# Step 5: Test reset endpoint160echo ""161echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"162echo -e "${BLUE}STEP 5: Testing Reset Endpoint${NC}"163echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"164 165RESET_RESPONSE=$(curl -s -X POST http://localhost:${PORT}/reset -H "Content-Type: application/json" -d '{}')166 167if [ -z "${RESET_RESPONSE}" ]; then168    echo -e "${RED}✗${NC} Reset endpoint returned empty response"169    docker logs ${CONTAINER_NAME} | tail -20170    exit 1171fi172 173echo "Response (first 200 chars): ${RESET_RESPONSE:0:200}..."174 175# Check if response contains expected fields176if echo "${RESET_RESPONSE}" | grep -q "observation" && \177   echo "${RESET_RESPONSE}" | grep -q "screen" && \178   echo "${RESET_RESPONSE}" | grep -q "legal_actions"; then179    echo -e "${GREEN}✓${NC} Reset endpoint working"180 181    # Extract some info182    SCREEN_LEN=$(echo "${RESET_RESPONSE}" | grep -o '"screen":\[[^]]*\]' | wc -c)183    echo "  Screen data length: ${SCREEN_LEN} chars"184else185    echo -e "${RED}✗${NC} Reset response missing required fields"186    echo "Full response: ${RESET_RESPONSE}"187    exit 1188fi189 190# Step 6: Test step endpoint191echo ""192echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"193echo -e "${BLUE}STEP 6: Testing Step Endpoint${NC}"194echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"195 196STEP_PAYLOAD='{"action": {"action_id": 0, "game_name": "pong"}}'197STEP_RESPONSE=$(curl -s -X POST http://localhost:${PORT}/step -H "Content-Type: application/json" -d "${STEP_PAYLOAD}")198 199if [ -z "${STEP_RESPONSE}" ]; then200    echo -e "${RED}✗${NC} Step endpoint returned empty response"201    docker logs ${CONTAINER_NAME} | tail -20202    exit 1203fi204 205echo "Response (first 200 chars): ${STEP_RESPONSE:0:200}..."206 207# Check if response contains expected fields208if echo "${STEP_RESPONSE}" | grep -q "observation" && \209   echo "${STEP_RESPONSE}" | grep -q "reward" && \210   echo "${STEP_RESPONSE}" | grep -q "done"; then211    echo -e "${GREEN}✓${NC} Step endpoint working"212 213    # Extract reward and done214    REWARD=$(echo "${STEP_RESPONSE}" | grep -o '"reward":[^,}]*' | cut -d: -f2)215    DONE=$(echo "${STEP_RESPONSE}" | grep -o '"done":[^,}]*' | cut -d: -f2)216    echo "  Reward: ${REWARD}"217    echo "  Done: ${DONE}"218else219    echo -e "${RED}✗${NC} Step response missing required fields"220    echo "Full response: ${STEP_RESPONSE}"221    exit 1222fi223 224# Step 7: Test state endpoint225echo ""226echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"227echo -e "${BLUE}STEP 7: Testing State Endpoint${NC}"228echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"229 230STATE_RESPONSE=$(curl -s http://localhost:${PORT}/state)231 232if [ -z "${STATE_RESPONSE}" ]; then233    echo -e "${RED}✗${NC} State endpoint returned empty response"234    docker logs ${CONTAINER_NAME} | tail -20235    exit 1236fi237 238echo "Response: ${STATE_RESPONSE}"239 240# Check if response contains expected fields241if echo "${STATE_RESPONSE}" | grep -q "episode_id" && \242   echo "${STATE_RESPONSE}" | grep -q "step_count" && \243   echo "${STATE_RESPONSE}" | grep -q "game_name"; then244    echo -e "${GREEN}✓${NC} State endpoint working"245 246    # Extract info247    GAME_NAME=$(echo "${STATE_RESPONSE}" | grep -o '"game_name":"[^"]*"' | cut -d'"' -f4)248    STEP_COUNT=$(echo "${STATE_RESPONSE}" | grep -o '"step_count":[^,}]*' | cut -d: -f2)249    echo "  Game: ${GAME_NAME}"250    echo "  Steps: ${STEP_COUNT}"251else252    echo -e "${RED}✗${NC} State response missing required fields"253    echo "Full response: ${STATE_RESPONSE}"254    exit 1255fi256 257# Step 8: Test multiple steps258echo ""259echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"260echo -e "${BLUE}STEP 8: Testing Multiple Steps${NC}"261echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"262 263echo "Taking 10 steps..."264TOTAL_REWARD=0265for i in {1..10}; do266    ACTION_ID=$((RANDOM % 3))  # Random action 0-2267    STEP_PAYLOAD="{\"action\": {\"action_id\": ${ACTION_ID}, \"game_name\": \"pong\"}}"268    STEP_RESPONSE=$(curl -s -X POST http://localhost:${PORT}/step -H "Content-Type: application/json" -d "${STEP_PAYLOAD}")269 270    if ! echo "${STEP_RESPONSE}" | grep -q "observation"; then271        echo -e "${RED}✗${NC} Step ${i} failed"272        exit 1273    fi274 275    REWARD=$(echo "${STEP_RESPONSE}" | grep -o '"reward":[^,}]*' | cut -d: -f2 | sed 's/null/0/')276    DONE=$(echo "${STEP_RESPONSE}" | grep -o '"done":[^,}]*' | cut -d: -f2)277 278    echo "  Step ${i}: action=${ACTION_ID}, reward=${REWARD}, done=${DONE}"279 280    if [ "${DONE}" = "true" ]; then281        echo "  Episode completed early at step ${i}"282        break283    fi284done285 286echo -e "${GREEN}✓${NC} Multiple steps completed successfully"287 288# Step 9: Check container logs for errors289echo ""290echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"291echo -e "${BLUE}STEP 9: Checking Container Logs${NC}"292echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"293 294LOGS=$(docker logs ${CONTAINER_NAME} 2>&1)295 296if echo "${LOGS}" | grep -i "error" | grep -v "LoggerMode.Error"; then297    echo -e "${YELLOW}⚠${NC}  Found errors in logs:"298    echo "${LOGS}" | grep -i "error" | head -5299else300    echo -e "${GREEN}✓${NC} No errors in container logs"301fi302 303if echo "${LOGS}" | grep -i "exception"; then304    echo -e "${RED}✗${NC} Found exceptions in logs:"305    echo "${LOGS}" | grep -i "exception" | head -5306    exit 1307else308    echo -e "${GREEN}✓${NC} No exceptions in container logs"309fi310 311# Final Summary312echo ""313echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"314echo -e "${GREEN}✅ ALL DOCKER TESTS PASSED${NC}"315echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"316echo ""317echo "Summary:"318echo "  ✓ Docker image built successfully"319echo "  ✓ Container started and ran"320echo "  ✓ Health endpoint working"321echo "  ✓ Reset endpoint working"322echo "  ✓ Step endpoint working"323echo "  ✓ State endpoint working"324echo "  ✓ Multiple steps working"325echo "  ✓ No errors or exceptions"326echo ""327echo "Image: ${IMAGE_NAME}:${IMAGE_TAG}"328echo "Container: ${CONTAINER_NAME}"329echo "Port: ${PORT}"330echo ""331echo "To keep container running: docker start ${CONTAINER_NAME}"332echo "To view logs: docker logs ${CONTAINER_NAME}"333echo ""334