Files
bakery-ia/test_docker_simple.py
2025-08-08 09:08:41 +02:00

151 lines
5.0 KiB
Python

#!/usr/bin/env python3
"""
Simple Docker Services Test
Tests that docker-compose can start services without external dependencies
"""
import os
import sys
import subprocess
import time
from pathlib import Path
def run_command(cmd, timeout=300):
"""Run a shell command with timeout"""
try:
print(f"Running: {cmd}")
result = subprocess.run(
cmd,
shell=True,
timeout=timeout,
capture_output=True,
text=True
)
return result
except subprocess.TimeoutExpired:
print(f"Command timed out after {timeout} seconds")
return None
except Exception as e:
print(f"Error running command: {e}")
return None
def test_infrastructure_services():
"""Test starting just infrastructure services"""
print("🏗️ Testing infrastructure services...")
try:
# Stop any existing containers
print("Cleaning up existing containers...")
run_command("docker compose down", timeout=120)
# Start only infrastructure services
infra_services = "redis rabbitmq auth-db data-db"
cmd = f"docker compose up -d {infra_services}"
result = run_command(cmd, timeout=300)
if result and result.returncode == 0:
print("✅ Infrastructure services started")
# Wait a bit for services to initialize
print("Waiting 30 seconds for services to initialize...")
time.sleep(30)
# Check container status
status_result = run_command("docker compose ps", timeout=30)
if status_result and status_result.stdout:
print("Container status:")
print(status_result.stdout)
# Try to start one application service
print("Testing application service startup...")
app_result = run_command("docker compose up -d auth-service", timeout=180)
if app_result and app_result.returncode == 0:
print("✅ Auth service started successfully")
# Wait for it to initialize
time.sleep(20)
# Check health with curl
health_result = run_command("curl -f http://localhost:8001/health", timeout=10)
if health_result and health_result.returncode == 0:
print("✅ Auth service is healthy!")
return True
else:
print("⚠️ Auth service started but health check failed")
# Show logs for debugging
logs_result = run_command("docker compose logs --tail=20 auth-service", timeout=30)
if logs_result and logs_result.stdout:
print("Auth service logs:")
print(logs_result.stdout)
return False
else:
print("❌ Failed to start auth service")
if app_result and app_result.stderr:
print(f"Error: {app_result.stderr}")
return False
else:
print("❌ Failed to start infrastructure services")
if result and result.stderr:
print(f"Error: {result.stderr}")
return False
except Exception as e:
print(f"❌ Error during infrastructure test: {e}")
return False
def show_final_status():
"""Show final container status"""
print("\n📊 Final container status:")
result = run_command("docker compose ps", timeout=30)
if result and result.stdout:
print(result.stdout)
def cleanup():
"""Clean up containers"""
print("\n🧹 Cleaning up containers...")
run_command("docker compose down", timeout=180)
print("✅ Cleanup completed")
def main():
"""Main test function"""
print("🔧 SIMPLE DOCKER SERVICES TEST")
print("=" * 40)
base_path = Path(__file__).parent
os.chdir(base_path)
success = False
try:
# Test infrastructure services
success = test_infrastructure_services()
# Show current status
show_final_status()
except KeyboardInterrupt:
print("\n⚠️ Test interrupted by user")
except Exception as e:
print(f"\n❌ Unexpected error: {e}")
finally:
# Always cleanup
cleanup()
# Final result
print("\n" + "=" * 40)
if success:
print("🎉 DOCKER SERVICES TEST PASSED!")
print("✅ Services can start and respond to health checks")
print("💡 Your docker-compose setup is working correctly")
print("🚀 You can now run: docker compose up -d")
return 0
else:
print("❌ DOCKER SERVICES TEST FAILED")
print("⚠️ Some issues were found with service startup")
print("💡 Check the logs above for details")
return 1
if __name__ == "__main__":
exit_code = main()
sys.exit(exit_code)