#!/bin/bash # Test integration script - validates if the frontend and backend work together # Specifically designed to test if the production environment setup will work # Get the directory containing this script SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" PARENT_DIR="$(dirname "$SCRIPT_DIR")" TEST_LOG="$SCRIPT_DIR/test-integration.log" # Colors for output GREEN='\033[0;32m' YELLOW='\033[0;33m' RED='\033[0;31m' NC='\033[0m' # No Color # Create or clear log file > "$TEST_LOG" echo -e "${YELLOW}Starting MYP integration test environment...${NC}" | tee -a "$TEST_LOG" echo "Test directory: $SCRIPT_DIR" | tee -a "$TEST_LOG" echo "Parent directory: $PARENT_DIR" | tee -a "$TEST_LOG" echo "Log file: $TEST_LOG" | tee -a "$TEST_LOG" # Function to run docker compose (handles both docker-compose and docker compose syntax) run_docker_compose() { if command -v docker-compose &> /dev/null; then docker-compose "$@" elif command -v docker &> /dev/null && docker compose version &> /dev/null; then docker compose "$@" else echo -e "${RED}Error: Neither docker-compose nor docker compose is available${NC}" | tee -a "$TEST_LOG" exit 1 fi } # Stop any existing test environment (cleanup) echo -e "${YELLOW}Cleaning up any existing test environment...${NC}" | tee -a "$TEST_LOG" run_docker_compose -f "$SCRIPT_DIR/test-env.yml" down >> "$TEST_LOG" 2>&1 # Start the test environment echo -e "${YELLOW}Starting test environment with docker-compose...${NC}" | tee -a "$TEST_LOG" run_docker_compose -f "$SCRIPT_DIR/test-env.yml" up -d >> "$TEST_LOG" 2>&1 if [ $? -ne 0 ]; then echo -e "${RED}Failed to start test environment.${NC}" | tee -a "$TEST_LOG" echo "Check the log file for details: $TEST_LOG" | tee -a "$TEST_LOG" exit 1 fi # Check if external backend is available echo -e "${YELLOW}Checking if external backend is available...${NC}" | tee -a "$TEST_LOG" max_attempts=10 attempt=1 backend_ready=false while [ $attempt -le $max_attempts ] && [ "$backend_ready" = "false" ]; do echo "Checking backend readiness (attempt $attempt/$max_attempts)..." | tee -a "$TEST_LOG" if curl -s http://192.168.0.105:5000/api/health &> /dev/null; then backend_ready=true echo -e "${GREEN}External backend is available!${NC}" | tee -a "$TEST_LOG" else echo "External backend not available yet, waiting..." | tee -a "$TEST_LOG" sleep 2 attempt=$((attempt+1)) fi done if [ "$backend_ready" = "false" ]; then echo -e "${RED}External backend at 192.168.0.105:5000 is not available after $max_attempts attempts${NC}" | tee -a "$TEST_LOG" echo -e "${RED}Please make sure the backend is running on the separate Raspberry Pi${NC}" | tee -a "$TEST_LOG" # Cleanup run_docker_compose -f "$SCRIPT_DIR/test-env.yml" down >> "$TEST_LOG" 2>&1 exit 1 fi # Wait for frontend to be ready echo -e "${YELLOW}Waiting for frontend to be ready...${NC}" | tee -a "$TEST_LOG" max_attempts=30 attempt=1 frontend_ready=false while [ $attempt -le $max_attempts ] && [ "$frontend_ready" = "false" ]; do echo "Checking frontend readiness (attempt $attempt/$max_attempts)..." | tee -a "$TEST_LOG" if curl -s http://127.0.0.1:3000 &> /dev/null; then frontend_ready=true echo -e "${GREEN}Frontend is ready!${NC}" | tee -a "$TEST_LOG" else echo "Frontend not ready yet, waiting..." | tee -a "$TEST_LOG" sleep 2 attempt=$((attempt+1)) fi done if [ "$frontend_ready" = "false" ]; then echo -e "${RED}Frontend failed to start properly after $max_attempts attempts${NC}" | tee -a "$TEST_LOG" echo "Logs from frontend container:" | tee -a "$TEST_LOG" run_docker_compose -f "$SCRIPT_DIR/test-env.yml" logs frontend-test >> "$TEST_LOG" 2>&1 # Cleanup run_docker_compose -f "$SCRIPT_DIR/test-env.yml" down >> "$TEST_LOG" 2>&1 exit 1 fi # Perform basic integration tests echo -e "${YELLOW}Performing basic integration tests...${NC}" | tee -a "$TEST_LOG" # Test 1: Frontend can fetch API data (printers endpoint) echo "Test 1: Frontend can fetch data from backend API..." | tee -a "$TEST_LOG" frontend_container_id=$(docker ps -qf "name=myp-frontend-test") if [ -z "$frontend_container_id" ]; then echo -e "${RED}Failed to find frontend container${NC}" | tee -a "$TEST_LOG" run_docker_compose -f "$SCRIPT_DIR/test-env.yml" down >> "$TEST_LOG" 2>&1 exit 1 fi # Run a simple test inside the frontend container to check API connectivity api_test_result=$(docker exec $frontend_container_id curl -s http://192.168.0.105:5000/api/health) if [[ "$api_test_result" == *"healthy"* ]]; then echo -e "${GREEN}Test 1 PASSED: Frontend can connect to backend API${NC}" | tee -a "$TEST_LOG" else echo -e "${RED}Test 1 FAILED: Frontend cannot connect to backend API${NC}" | tee -a "$TEST_LOG" echo "API response: $api_test_result" | tee -a "$TEST_LOG" # Don't exit, continue with other tests fi # Test 2: Frontend serves HTML content echo "Test 2: Frontend serves valid HTML content..." | tee -a "$TEST_LOG" frontend_html=$(curl -s http://127.0.0.1:3000) if [[ "$frontend_html" == *""* ]]; then echo -e "${GREEN}Test 2 PASSED: Frontend serves valid HTML${NC}" | tee -a "$TEST_LOG" else echo -e "${RED}Test 2 FAILED: Frontend does not serve valid HTML${NC}" | tee -a "$TEST_LOG" # Don't exit, continue with other tests fi # All tests completed echo -e "${GREEN}Integration tests completed${NC}" | tee -a "$TEST_LOG" # Ask if the environment should be kept running or shutdown echo -e "${YELLOW}Test environment is running at:${NC}" | tee -a "$TEST_LOG" echo "Frontend: http://127.0.0.1:3000" | tee -a "$TEST_LOG" echo "Backend: http://192.168.0.105:5000 (external Raspberry Pi)" | tee -a "$TEST_LOG" echo "" | tee -a "$TEST_LOG" read -p "Do you want to keep the test environment running? (y/n): " keep_running if [[ "$keep_running" != "y" && "$keep_running" != "Y" ]]; then echo -e "${YELLOW}Shutting down test environment...${NC}" | tee -a "$TEST_LOG" run_docker_compose -f "$SCRIPT_DIR/test-env.yml" down >> "$TEST_LOG" 2>&1 echo -e "${GREEN}Test environment has been shut down${NC}" | tee -a "$TEST_LOG" else echo -e "${GREEN}Test environment is still running${NC}" | tee -a "$TEST_LOG" echo "To stop it later, run: docker-compose -f $SCRIPT_DIR/test-env.yml down" | tee -a "$TEST_LOG" fi echo -e "${GREEN}Integration testing completed. See log for details: $TEST_LOG${NC}"