CalejoControl/scripts/run-mock-tests.sh

338 lines
8.5 KiB
Bash
Executable File

#!/bin/bash
# Automated test runner for mock SCADA and optimizer services
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored output
print_status() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Function to wait for services
wait_for_services() {
print_status "Waiting for mock services to be ready..."
max_wait=60
start_time=$(date +%s)
while true; do
current_time=$(date +%s)
elapsed=$((current_time - start_time))
if [ $elapsed -ge $max_wait ]; then
print_error "Services not ready within $max_wait seconds"
return 1
fi
# Check if all services are responding
scada_ready=$(curl -s http://localhost:8081/health | grep -q "healthy" && echo "yes" || echo "no")
optimizer_ready=$(curl -s http://localhost:8082/health | grep -q "healthy" && echo "yes" || echo "no")
calejo_ready=$(curl -s http://localhost:8080/health > /dev/null && echo "yes" || echo "no")
if [ "$scada_ready" = "yes" ] && [ "$optimizer_ready" = "yes" ] && [ "$calejo_ready" = "yes" ]; then
print_success "All services are ready!"
return 0
fi
echo " Waiting... ($elapsed/$max_wait seconds)"
sleep 5
done
}
# Function to run specific test categories
run_unit_tests() {
print_status "Running unit tests..."
if python -m pytest tests/unit/ -v --tb=short; then
print_success "Unit tests passed"
return 0
else
print_error "Unit tests failed"
return 1
fi
}
run_integration_tests() {
print_status "Running integration tests..."
if python -m pytest tests/integration/test_mock_services.py -v --tb=short; then
print_success "Integration tests passed"
return 0
else
print_error "Integration tests failed"
return 1
fi
}
run_all_tests() {
print_status "Running all tests..."
if python -m pytest tests/ -v --tb=short; then
print_success "All tests passed"
return 0
else
print_error "Some tests failed"
return 1
fi
}
run_health_checks() {
print_status "Running health checks..."
services=(
"Calejo Control Adapter:8080"
"Mock SCADA:8081"
"Mock Optimizer:8082"
)
all_healthy=true
for service in "${services[@]}"; do
name="${service%:*}"
port="${service#*:}"
if curl -s "http://localhost:$port/health" > /dev/null; then
print_success "$name is healthy"
else
print_error "$name is not responding"
all_healthy=false
fi
done
if [ "$all_healthy" = "true" ]; then
print_success "All health checks passed"
return 0
else
print_error "Some health checks failed"
return 1
fi
}
run_api_tests() {
print_status "Running API tests..."
# Test SCADA API
print_status "Testing SCADA API..."
if curl -s http://localhost:8081/api/v1/data | python -m json.tool > /dev/null 2>&1; then
print_success "SCADA API is accessible"
else
print_error "SCADA API test failed"
return 1
fi
# Test Optimizer API
print_status "Testing Optimizer API..."
if curl -s http://localhost:8082/api/v1/models | python -m json.tool > /dev/null 2>&1; then
print_success "Optimizer API is accessible"
else
print_error "Optimizer API test failed"
return 1
fi
# Test Calejo API
print_status "Testing Calejo API..."
if curl -s http://localhost:8080/health > /dev/null; then
print_success "Calejo API is accessible"
else
print_error "Calejo API test failed"
return 1
fi
print_success "All API tests passed"
return 0
}
run_end_to_end_test() {
print_status "Running end-to-end test..."
# This simulates a complete workflow
print_status "1. Getting SCADA data..."
scada_data=$(curl -s http://localhost:8081/api/v1/data)
if [ $? -eq 0 ]; then
print_success "SCADA data retrieved"
else
print_error "Failed to get SCADA data"
return 1
fi
print_status "2. Running optimization..."
optimization_result=$(curl -s -X POST http://localhost:8082/api/v1/optimize/energy_optimization \
-H "Content-Type: application/json" \
-d '{"power_load": 450, "time_of_day": 14, "production_rate": 95}')
if [ $? -eq 0 ]; then
print_success "Optimization completed"
else
print_error "Optimization failed"
return 1
fi
print_status "3. Testing equipment control..."
control_result=$(curl -s -X POST http://localhost:8081/api/v1/control/pump_1 \
-H "Content-Type: application/json" \
-d '{"command": "START"}')
if [ $? -eq 0 ]; then
print_success "Equipment control successful"
else
print_error "Equipment control failed"
return 1
fi
print_success "End-to-end test completed successfully"
return 0
}
# Function to display usage
usage() {
echo "Usage: $0 [options]"
echo ""
echo "Options:"
echo " --health Run health checks only"
echo " --api Run API tests only"
echo " --unit Run unit tests only"
echo " --integration Run integration tests only"
echo " --e2e Run end-to-end test only"
echo " --all Run all tests (default)"
echo " --wait-only Only wait for services, don't run tests"
echo " -h, --help Show this help message"
echo ""
echo "Examples:"
echo " $0 # Run all tests"
echo " $0 --health # Run health checks"
echo " $0 --api --e2e # Run API and end-to-end tests"
}
# Parse command line arguments
HEALTH_ONLY=false
API_ONLY=false
UNIT_ONLY=false
INTEGRATION_ONLY=false
E2E_ONLY=false
ALL_TESTS=true
WAIT_ONLY=false
while [[ $# -gt 0 ]]; do
case $1 in
--health)
HEALTH_ONLY=true
ALL_TESTS=false
shift
;;
--api)
API_ONLY=true
ALL_TESTS=false
shift
;;
--unit)
UNIT_ONLY=true
ALL_TESTS=false
shift
;;
--integration)
INTEGRATION_ONLY=true
ALL_TESTS=false
shift
;;
--e2e)
E2E_ONLY=true
ALL_TESTS=false
shift
;;
--wait-only)
WAIT_ONLY=true
ALL_TESTS=false
shift
;;
-h|--help)
usage
exit 0
;;
*)
print_error "Unknown option: $1"
usage
exit 1
;;
esac
done
# Main execution
print_status "Starting automated tests for mock deployment..."
# Wait for services
if ! wait_for_services; then
print_error "Cannot proceed with tests - services not available"
exit 1
fi
if [ "$WAIT_ONLY" = "true" ]; then
print_success "Services are ready - exiting as requested"
exit 0
fi
# Run tests based on options
overall_result=0
if [ "$HEALTH_ONLY" = "true" ] || [ "$ALL_TESTS" = "true" ]; then
if ! run_health_checks; then
overall_result=1
fi
fi
if [ "$API_ONLY" = "true" ] || [ "$ALL_TESTS" = "true" ]; then
if ! run_api_tests; then
overall_result=1
fi
fi
if [ "$UNIT_ONLY" = "true" ] || [ "$ALL_TESTS" = "true" ]; then
if ! run_unit_tests; then
overall_result=1
fi
fi
if [ "$INTEGRATION_ONLY" = "true" ] || [ "$ALL_TESTS" = "true" ]; then
if ! run_integration_tests; then
overall_result=1
fi
fi
if [ "$E2E_ONLY" = "true" ] || [ "$ALL_TESTS" = "true" ]; then
if ! run_end_to_end_test; then
overall_result=1
fi
fi
# Final result
if [ $overall_result -eq 0 ]; then
echo ""
print_success "🎉 All tests completed successfully!"
echo ""
echo "📊 Test Summary:"
echo " ✅ Health checks passed"
echo " ✅ API tests passed"
echo " ✅ Unit tests passed"
echo " ✅ Integration tests passed"
echo " ✅ End-to-end tests passed"
echo ""
echo "🚀 Mock deployment is ready for development!"
else
echo ""
print_error "❌ Some tests failed. Please check the logs above."
exit 1
fi