| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- #!/bin/bash
- #
- # Docker Test Suite for BamBuddy
- # Runs build verification, unit tests, and integration tests in Docker
- #
- set -e
- # Configuration
- PORT=${PORT:-8000}
- # Colors for output
- RED='\033[0;31m'
- GREEN='\033[0;32m'
- YELLOW='\033[1;33m'
- BLUE='\033[0;34m'
- NC='\033[0m' # No Color
- # Track results
- TESTS_PASSED=0
- TESTS_FAILED=0
- FAILED_TESTS=""
- print_header() {
- echo ""
- echo -e "${BLUE}========================================${NC}"
- echo -e "${BLUE} $1${NC}"
- echo -e "${BLUE}========================================${NC}"
- }
- print_success() {
- echo -e "${GREEN}✓ $1${NC}"
- TESTS_PASSED=$((TESTS_PASSED + 1))
- }
- print_failure() {
- echo -e "${RED}✗ $1${NC}"
- TESTS_FAILED=$((TESTS_FAILED + 1))
- FAILED_TESTS="${FAILED_TESTS}\n - $1"
- }
- print_info() {
- echo -e "${YELLOW}→ $1${NC}"
- }
- cleanup() {
- print_info "Cleaning up test containers..."
- sudo docker compose -f docker-compose.test.yml down -v --remove-orphans 2>/dev/null || true
- sudo docker compose down -v --remove-orphans 2>/dev/null || true
- }
- # Cleanup on exit
- trap cleanup EXIT
- # Parse arguments
- RUN_BUILD=true
- RUN_BACKEND=true
- RUN_FRONTEND=true
- RUN_INTEGRATION=true
- while [[ $# -gt 0 ]]; do
- case $1 in
- --build-only)
- RUN_BACKEND=false
- RUN_FRONTEND=false
- RUN_INTEGRATION=false
- shift
- ;;
- --backend-only)
- RUN_BUILD=false
- RUN_FRONTEND=false
- RUN_INTEGRATION=false
- shift
- ;;
- --frontend-only)
- RUN_BUILD=false
- RUN_BACKEND=false
- RUN_INTEGRATION=false
- shift
- ;;
- --integration-only)
- RUN_BUILD=false
- RUN_BACKEND=false
- RUN_FRONTEND=false
- shift
- ;;
- --skip-build)
- RUN_BUILD=false
- shift
- ;;
- --skip-integration)
- RUN_INTEGRATION=false
- shift
- ;;
- -h|--help)
- echo "Usage: $0 [OPTIONS]"
- echo ""
- echo "Options:"
- echo " --build-only Only run build test"
- echo " --backend-only Only run backend tests"
- echo " --frontend-only Only run frontend tests"
- echo " --integration-only Only run integration tests"
- echo " --skip-build Skip build test"
- echo " --skip-integration Skip integration tests"
- echo " -h, --help Show this help"
- exit 0
- ;;
- *)
- echo "Unknown option: $1"
- exit 1
- ;;
- esac
- done
- print_header "BamBuddy Docker Test Suite"
- # ============================================
- # Test 1: Docker Build
- # ============================================
- if [ "$RUN_BUILD" = true ]; then
- print_header "Test 1: Docker Build"
- print_info "Building production Docker image..."
- if sudo docker build -t bambuddy:test . --pull --no-cache --progress=plain; then
- print_success "Production image builds successfully"
- # Verify image has expected labels/structure
- print_info "Verifying image structure..."
- if sudo docker run --rm bambuddy:test python -c "import backend.app.main; print('Backend imports OK')"; then
- print_success "Backend module imports correctly"
- else
- print_failure "Backend module import failed"
- fi
- if sudo docker run --rm bambuddy:test test -d /app/static; then
- print_success "Static files directory exists"
- else
- print_failure "Static files directory missing"
- fi
- else
- print_failure "Production image build failed"
- fi
- fi
- # ============================================
- # Test 2: Backend Unit Tests
- # ============================================
- if [ "$RUN_BACKEND" = true ]; then
- print_header "Test 2: Backend Unit Tests"
- print_info "Building backend test image..."
- if sudo docker compose -f docker-compose.test.yml build backend-test --pull --no-cache --progress=plain; then
- print_info "Running backend tests..."
- if sudo docker compose -f docker-compose.test.yml run --rm backend-test; then
- print_success "Backend unit tests passed"
- else
- print_failure "Backend unit tests failed"
- fi
- else
- print_failure "Backend test image build failed"
- fi
- fi
- # ============================================
- # Test 3: Frontend Unit Tests
- # ============================================
- if [ "$RUN_FRONTEND" = true ]; then
- print_header "Test 3: Frontend Unit Tests"
- print_info "Building frontend test image..."
- if sudo docker compose -f docker-compose.test.yml build frontend-test --pull --no-cache --progress=plain; then
- print_info "Running frontend tests..."
- if sudo docker compose -f docker-compose.test.yml run --rm frontend-test; then
- print_success "Frontend unit tests passed"
- else
- print_failure "Frontend unit tests failed"
- fi
- else
- print_failure "Frontend test image build failed"
- fi
- fi
- # ============================================
- # Test 4: Integration Tests
- # ============================================
- if [ "$RUN_INTEGRATION" = true ]; then
- print_header "Test 4: Integration Tests"
- print_info "Building integration container..."
- # Build the integration container first to ensure latest code
- if ! sudo docker compose -f docker-compose.test.yml build integration --pull --no-cache --progress=plain; then
- print_failure "Integration container build failed"
- else
- print_info "Starting application container..."
- # Start the integration container
- sudo docker compose -f docker-compose.test.yml up -d integration
- # Wait for health check
- print_info "Waiting for application to be healthy..."
- RETRIES=30
- while [ $RETRIES -gt 0 ]; do
- if sudo docker compose -f docker-compose.test.yml ps integration | grep -q "healthy"; then
- break
- fi
- sleep 2
- ((RETRIES--))
- done
- if [ $RETRIES -eq 0 ]; then
- print_failure "Application failed to become healthy"
- sudo docker compose -f docker-compose.test.yml logs integration
- else
- print_success "Application is healthy"
- # Run basic health checks
- print_info "Running integration tests..."
- # Test health endpoint
- HEALTH_RESPONSE=$(sudo docker compose -f docker-compose.test.yml exec -T integration curl -s http://localhost:${PORT}/health)
- if echo "$HEALTH_RESPONSE" | grep -q "healthy"; then
- print_success "Health endpoint responds correctly"
- else
- print_failure "Health endpoint check failed"
- fi
- # Test API endpoints
- API_RESPONSE=$(sudo docker compose -f docker-compose.test.yml exec -T integration curl -s http://localhost:${PORT}/api/v1/settings)
- if echo "$API_RESPONSE" | grep -q "settings"; then
- print_success "Settings API endpoint responds"
- else
- # Settings might return empty, which is OK
- print_success "Settings API endpoint accessible"
- fi
- # Test static files
- STATIC_RESPONSE=$(sudo docker compose -f docker-compose.test.yml exec -T integration curl -s -o /dev/null -w "%{http_code}" http://localhost:${PORT}/)
- if [ "$STATIC_RESPONSE" = "200" ]; then
- print_success "Static files served correctly"
- else
- print_failure "Static files not served (HTTP $STATIC_RESPONSE)"
- fi
- # Run pytest integration tests if they exist
- if sudo docker compose -f docker-compose.test.yml run --rm integration-test-runner 2>/dev/null; then
- print_success "Integration test suite passed"
- else
- print_info "No Docker-specific integration tests found (this is OK)"
- fi
- fi
- fi
- # Cleanup integration containers
- sudo docker compose -f docker-compose.test.yml down -v
- fi
- # ============================================
- # Summary
- # ============================================
- print_header "Test Summary"
- echo ""
- echo -e "Tests Passed: ${GREEN}${TESTS_PASSED}${NC}"
- echo -e "Tests Failed: ${RED}${TESTS_FAILED}${NC}"
- if [ $TESTS_FAILED -gt 0 ]; then
- echo ""
- echo -e "${RED}Failed tests:${NC}"
- echo -e "$FAILED_TESTS"
- echo ""
- exit 1
- else
- echo ""
- echo -e "${GREEN}All tests passed!${NC}"
- echo ""
- exit 0
- fi
|