#!/bin/bash # Integration test script for LEMP WordPress deployment # Generated by Ansible set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Test configuration TEST_DOMAIN="${TEST_DOMAIN:-localhost}" TEST_PORT="${TEST_PORT:-8080}" INVENTORY_FILE="${INVENTORY_FILE:-inventory/docker.ini}" PLAYBOOK_DIR="${PLAYBOOK_DIR:-playbooks}" # Functions log_info() { echo -e "${GREEN}[INFO]${NC} $1" } log_warn() { echo -e "${YELLOW}[WARN]${NC} $1" } log_error() { echo -e "${RED}[ERROR]${NC} $1" } test_service() { local service=$1 local host=${2:-localhost} log_info "Testing $service service..." if ansible all -i "$INVENTORY_FILE" -m service -a "name=$service state=started" &>/dev/null; then log_info "✓ $service is running" return 0 else log_error "✗ $service is not running" return 1 fi } test_http_response() { local url=$1 local expected_code=${2:-200} log_info "Testing HTTP response for $url..." local response_code response_code=$(curl -s -o /dev/null -w "%{http_code}" "$url" || echo "000") if [[ "$response_code" == "$expected_code" ]]; then log_info "✓ HTTP $response_code response received" return 0 else log_error "✗ Expected HTTP $expected_code, got $response_code" return 1 fi } test_wordpress_installation() { local url="http://$TEST_DOMAIN:$TEST_PORT" log_info "Testing WordPress installation..." # Test homepage if curl -s "$url" | grep -q "WordPress\|wp-content" >/dev/null; then log_info "✓ WordPress homepage is accessible" else log_error "✗ WordPress homepage not accessible or not WordPress" return 1 fi # Test admin page if test_http_response "$url/wp-admin/" 302; then log_info "✓ WordPress admin redirects properly" else log_error "✗ WordPress admin not accessible" return 1 fi # Test WP-CLI if ansible all -i "$INVENTORY_FILE" -m shell -a "cd /var/www/html && wp core version" &>/dev/null; then log_info "✓ WP-CLI is working" else log_error "✗ WP-CLI is not working" return 1 fi } test_database_connection() { log_info "Testing database connection..." if ansible all -i "$INVENTORY_FILE" -m mysql_db -a "name=wordpress_db state=present" &>/dev/null; then log_info "✓ Database connection working" return 0 else log_error "✗ Database connection failed" return 1 fi } test_php_functionality() { log_info "Testing PHP functionality..." # Create test PHP file local test_script='' if ansible all -i "$INVENTORY_FILE" -m copy -a "content='$test_script' dest=/var/www/html/test-php.php" &>/dev/null; then if test_http_response "http://$TEST_DOMAIN:$TEST_PORT/test-php.php"; then log_info "✓ PHP is working" # Cleanup ansible all -i "$INVENTORY_FILE" -m file -a "path=/var/www/html/test-php.php state=absent" &>/dev/null return 0 else log_error "✗ PHP test failed" return 1 fi else log_error "✗ Could not create PHP test file" return 1 fi } test_ssl_configuration() { local url="https://$TEST_DOMAIN" log_info "Testing SSL configuration (if enabled)..." if curl -k -s "$url" >/dev/null 2>&1; then log_info "✓ SSL/HTTPS is working" # Test SSL certificate if echo | openssl s_client -connect "$TEST_DOMAIN:443" 2>/dev/null | openssl x509 -noout -dates >/dev/null 2>&1; then log_info "✓ SSL certificate is valid" else log_warn "! SSL certificate validation failed (might be self-signed)" fi else log_warn "! SSL/HTTPS not configured or not accessible" fi } run_security_tests() { log_info "Running security tests..." # Test for sensitive file exposure local sensitive_files=("wp-config.php" ".htaccess" "readme.html" "license.txt") for file in "${sensitive_files[@]}"; do if test_http_response "http://$TEST_DOMAIN:$TEST_PORT/$file" 403; then log_info "✓ $file is properly protected" else log_warn "! $file might be exposed" fi done # Test XMLRPC blocking if test_http_response "http://$TEST_DOMAIN:$TEST_PORT/xmlrpc.php" 403; then log_info "✓ XMLRPC is properly blocked" else log_warn "! XMLRPC might be accessible" fi } main() { log_info "Starting LEMP WordPress integration tests..." echo "=====================================================" local failed_tests=0 # Service tests for service in nginx mysql php*-fpm; do if ! test_service "$service"; then ((failed_tests++)) fi done # HTTP and WordPress tests if ! test_http_response "http://$TEST_DOMAIN:$TEST_PORT"; then ((failed_tests++)) fi if ! test_wordpress_installation; then ((failed_tests++)) fi if ! test_database_connection; then ((failed_tests++)) fi if ! test_php_functionality; then ((failed_tests++)) fi # Optional tests test_ssl_configuration run_security_tests echo "=====================================================" if [[ $failed_tests -eq 0 ]]; then log_info "All critical tests passed! ✓" exit 0 else log_error "$failed_tests critical test(s) failed! ✗" exit 1 fi } # Run tests main "$@"