authelia/tests/precommit.sh

211 lines
6.2 KiB
Bash
Executable File

#!/bin/bash
# Manual test script for Authelia
# Run this to test your setup manually: ./test.sh
set -e
echo "🧪 Running Authelia test suite..."
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Parse command line arguments
CLEANUP=true
VERBOSE=false
for arg in "$@"; do
case $arg in
--no-cleanup)
CLEANUP=false
shift
;;
--verbose)
VERBOSE=true
shift
;;
*)
echo "Usage: $0 [--no-cleanup] [--verbose]"
echo " --no-cleanup: Keep containers running after tests"
echo " --verbose: Show detailed output"
exit 1
;;
esac
done
# Function to log messages
log() {
if [ "$VERBOSE" = true ]; then
echo -e "${BLUE}[DEBUG]${NC} $1"
fi
}
echo -e "${BLUE}🚀 Starting Authelia Development Environment Tests${NC}"
echo "======================================================"
# Step 1: Build all images
echo -e "${YELLOW}📦 Building Docker images...${NC}"
if [ "$VERBOSE" = true ]; then
docker-compose -f docker-compose.dev.yml build
else
docker-compose -f docker-compose.dev.yml build > /dev/null 2>&1
fi
if [ $? -ne 0 ]; then
echo -e "${RED}❌ Failed to build Docker images!${NC}"
exit 1
fi
echo -e "${GREEN}✓ Docker images built successfully${NC}"
# Step 2: Start services
echo -e "${YELLOW}🏃 Starting services...${NC}"
if [ "$VERBOSE" = true ]; then
docker-compose -f docker-compose.dev.yml up -d
else
docker-compose -f docker-compose.dev.yml up -d > /dev/null 2>&1
fi
if [ $? -ne 0 ]; then
echo -e "${RED}❌ Failed to start services!${NC}"
exit 1
fi
echo -e "${GREEN}✓ Services started${NC}"
# Step 3: Wait for services to be healthy
echo -e "${YELLOW}⏳ Waiting for services to be ready...${NC}"
sleep 15
# Primary Focus: Comprehensive Authelia Testing
echo -e "${BLUE}🔍 Testing Authelia Service (Primary Focus)${NC}"
echo "=============================================="
# Test Authelia health endpoint (most important)
echo " • Testing Authelia health endpoint..."
max_attempts=30
attempt=0
authelia_healthy=false
while [ $attempt -lt $max_attempts ]; do
if response=$(curl -f http://localhost:9091/api/health 2>/dev/null); then
if [ "$response" = '{"status":"OK"}' ]; then
authelia_healthy=true
break
fi
fi
attempt=$((attempt + 1))
sleep 2
done
if [ "$authelia_healthy" = false ]; then
echo -e "${RED}❌ Authelia health endpoint not responding after 60 seconds!${NC}"
echo "Authelia logs:"
docker-compose -f docker-compose.dev.yml logs authelia --tail 20
exit 1
fi
echo -e " ${GREEN}✓ Authelia health check: PASSED${NC}"
# Test Authelia web interface
echo " • Testing Authelia web interface..."
if curl -f http://localhost:9091/ >/dev/null 2>&1; then
echo -e " ${GREEN}✓ Authelia Web UI: ACCESSIBLE${NC}"
else
echo -e "${RED}❌ Authelia Web UI: NOT ACCESSIBLE${NC}"
exit 1
fi
# Test Authelia API endpoints
echo " • Testing Authelia API endpoints..."
if curl -f http://localhost:9091/api/configuration >/dev/null 2>&1; then
echo -e " ${GREEN}✓ Authelia API configuration endpoint: WORKING${NC}"
else
echo -e "${YELLOW}⚠ Authelia API configuration endpoint: LIMITED ACCESS (normal)${NC}"
fi
# Test Authelia service logs for errors
echo " • Checking Authelia service logs for errors..."
error_count=$(docker-compose -f docker-compose.dev.yml logs authelia | grep -i "error\|fatal\|panic" | grep -v "SMTP\|ntp" | wc -l)
if [ "$error_count" -eq 0 ]; then
echo -e " ${GREEN}✓ No critical errors in Authelia logs${NC}"
else
echo -e "${YELLOW}⚠ Found $error_count potential errors in logs (excluding SMTP/NTP)${NC}"
if [ "$VERBOSE" = true ]; then
echo "Recent errors:"
docker-compose -f docker-compose.dev.yml logs authelia | grep -i "error\|fatal\|panic" | grep -v "SMTP\|ntp" | tail -5
fi
fi
# Test Authelia container status
echo " • Checking Authelia container status..."
container_status=$(docker inspect authelia_dev_main --format='{{.State.Status}}' 2>/dev/null || echo "not_found")
if [ "$container_status" = "running" ]; then
echo -e " ${GREEN}✓ Authelia container: RUNNING${NC}"
else
echo -e "${RED}❌ Authelia container status: $container_status${NC}"
exit 1
fi
# Secondary: Test supporting infrastructure
echo ""
echo -e "${BLUE}🔧 Testing Supporting Infrastructure${NC}"
echo "====================================="
# Test LLDAP web interface (important for authentication)
echo " • Testing LLDAP web interface (authentication backend)..."
max_attempts=15
attempt=0
lldap_healthy=false
while [ $attempt -lt $max_attempts ]; do
if curl -f http://localhost:17170/health >/dev/null 2>&1; then
lldap_healthy=true
break
fi
attempt=$((attempt + 1))
sleep 2
done
if [ "$lldap_healthy" = true ]; then
echo -e " ${GREEN}✓ LLDAP Web UI: ACCESSIBLE${NC}"
else
echo -e "${YELLOW}⚠ LLDAP not accessible after 30 seconds (may affect auth)${NC}"
fi
# Display service status
echo ""
echo -e "${BLUE}📊 Service Status Overview:${NC}"
docker-compose -f docker-compose.dev.yml ps
echo ""
if [ "$authelia_healthy" = true ]; then
echo -e "${GREEN}🎉 Authelia Development Environment: FULLY FUNCTIONAL!${NC}"
else
echo -e "${RED}❌ Authelia Development Environment: ISSUES DETECTED${NC}"
exit 1
fi
echo ""
echo -e "${BLUE}🔗 Access URLs:${NC}"
echo " • Authelia: http://localhost:9091"
echo " • LLDAP Admin: http://localhost:17170"
echo " - Username: admin"
echo " - Password: /ETAToLiZPWo6QK171abAUqsa3WDpd9IgneZnTA4zU0="
echo ""
# Cleanup
if [ "$CLEANUP" = true ]; then
echo -e "${YELLOW}🧹 Cleaning up containers...${NC}"
if [ "$VERBOSE" = true ]; then
docker-compose -f docker-compose.dev.yml down
else
docker-compose -f docker-compose.dev.yml down > /dev/null 2>&1
fi
echo -e "${GREEN}✓ Cleanup completed${NC}"
else
echo -e "${YELLOW}🔧 Containers left running for inspection${NC}"
echo "To stop: docker-compose -f docker-compose.dev.yml down"
fi
echo -e "${GREEN}✅ Authelia functionality test completed successfully!${NC}"
exit 0