#!/bin/bash # Script untuk test Redis functionality di Docker environment # Usage: ./docker-test-redis.sh [dev|prod] 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 # Default values ENVIRONMENT="dev" # Parse arguments while [[ $# -gt 0 ]]; do case $1 in dev|development) ENVIRONMENT="dev" shift ;; prod|production|staging) ENVIRONMENT="prod" shift ;; *) echo "Unknown option $1" echo "Usage: $0 [dev|prod]" exit 1 ;; esac done # 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 get compose file get_compose_file() { if [[ $ENVIRONMENT == "dev" ]]; then COMPOSE_FILE="docker-compose.yml" else COMPOSE_FILE="docker-compose.prod.yml" fi } # Function to check if containers are running check_containers() { if ! docker-compose -f "$COMPOSE_FILE" ps | grep -q "redis.*Up"; then print_error "Redis container is not running!" exit 1 fi if ! docker-compose -f "$COMPOSE_FILE" ps | grep -q "app.*Up"; then print_error "App container is not running!" exit 1 fi } # Function to test PHP Redis extension test_redis_extension() { print_status "Testing PHP Redis extension..." if docker-compose -f "$COMPOSE_FILE" exec -T app php -m | grep -q "redis"; then print_success "PHP Redis extension is installed" else print_error "PHP Redis extension is NOT installed" return 1 fi } # Function to test Redis server connection test_redis_connection() { print_status "Testing Redis server connection..." # Test direct connection to Redis container if docker-compose -f "$COMPOSE_FILE" exec -T redis redis-cli ping | grep -q "PONG"; then print_success "Redis server is responding" else print_error "Redis server is not responding" return 1 fi # Test connection from PHP if docker-compose -f "$COMPOSE_FILE" exec -T app php -r " try { \$redis = new Redis(); \$redis->connect('redis', 6379); echo 'OK'; } catch (Exception \$e) { echo 'FAILED: ' . \$e->getMessage(); } " | grep -q "OK"; then print_success "PHP Redis connection working" else print_error "PHP Redis connection failed" return 1 fi } # Function to test Laravel cache with Redis test_laravel_cache() { print_status "Testing Laravel cache with Redis..." # Test cache clear if docker-compose -f "$COMPOSE_FILE" exec -T app php artisan cache:clear > /dev/null 2>&1; then print_success "Laravel cache clear working" else print_warning "Laravel cache clear failed" fi # Test cache set/get local test_key="test_$(date +%s)" local test_value="redis_test_value" if docker-compose -f "$COMPOSE_FILE" exec -T app php artisan tinker --execute=" Cache::put('$test_key', '$test_value', 60); echo Cache::get('$test_key'); " | grep -q "$test_value"; then print_success "Laravel cache operations working" else print_error "Laravel cache operations failed" return 1 fi } # Function to test Redis session storage test_redis_sessions() { print_status "Testing Redis session configuration..." # Check session driver in config local session_driver=$(docker-compose -f "$COMPOSE_FILE" exec -T app php -r "echo config('session.driver');") if [[ "$session_driver" == "redis" ]]; then print_success "Laravel sessions configured to use Redis" else print_warning "Laravel sessions not using Redis (current: $session_driver)" fi } # Function to test Redis queue configuration test_redis_queue() { print_status "Testing Redis queue configuration..." # Check queue driver in config local queue_driver=$(docker-compose -f "$COMPOSE_FILE" exec -T app php -r "echo config('queue.default');") if [[ "$queue_driver" == "redis" ]]; then print_success "Laravel queue configured to use Redis" else print_warning "Laravel queue not using Redis (current: $queue_driver)" fi } # Function to show Redis info show_redis_info() { print_status "Redis server information:" echo "" docker-compose -f "$COMPOSE_FILE" exec redis redis-cli info server | head -10 echo "" print_status "Redis memory usage:" docker-compose -f "$COMPOSE_FILE" exec redis redis-cli info memory | grep used_memory_human echo "" print_status "Redis connected clients:" docker-compose -f "$COMPOSE_FILE" exec redis redis-cli info clients | grep connected_clients } # Function to show Laravel configuration show_laravel_config() { print_status "Laravel Redis configuration:" echo "" print_status "Cache driver:" docker-compose -f "$COMPOSE_FILE" exec -T app php -r "echo 'CACHE_DRIVER=' . config('cache.default') . PHP_EOL;" print_status "Session driver:" docker-compose -f "$COMPOSE_FILE" exec -T app php -r "echo 'SESSION_DRIVER=' . config('session.driver') . PHP_EOL;" print_status "Queue driver:" docker-compose -f "$COMPOSE_FILE" exec -T app php -r "echo 'QUEUE_CONNECTION=' . config('queue.default') . PHP_EOL;" print_status "Redis host:" docker-compose -f "$COMPOSE_FILE" exec -T app php -r "echo 'REDIS_HOST=' . config('database.redis.default.host') . PHP_EOL;" } # Main execution echo "================================================" print_status "🔴 Redis Functionality Test" print_status "Environment: $ENVIRONMENT" echo "================================================" echo "" # Get compose file get_compose_file # Check prerequisites check_containers # Run tests print_status "Running Redis tests..." echo "" test_redis_extension && echo "" test_redis_connection && echo "" test_laravel_cache && echo "" test_redis_sessions && echo "" test_redis_queue && echo "" # Show information show_redis_info echo "" show_laravel_config echo "" print_success "✅ Redis functionality test completed!" print_status "🔧 Troubleshooting commands:" echo " - Redis logs: docker-compose -f $COMPOSE_FILE logs redis" echo " - App logs: docker-compose -f $COMPOSE_FILE logs app" echo " - Redis CLI: docker-compose -f $COMPOSE_FILE exec redis redis-cli" echo " - Test cache: docker-compose -f $COMPOSE_FILE exec app php artisan cache:clear"