#!/bin/bash # # run_integration_tests.sh - Integration test script for mod_reqin_log # # This script runs integration tests for the mod_reqin_log module. # It requires: # - Apache HTTPD with the module loaded # - A running socket consumer # set -e SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" SOCKET_PATH="/tmp/mod_reqin_log.sock" LOG_FILE="/tmp/mod_reqin_log_test.log" APACHE_URL="${APACHE_URL:-http://localhost}" # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Counters TESTS_RUN=0 TESTS_PASSED=0 TESTS_FAILED=0 log_info() { echo -e "${YELLOW}[INFO]${NC} $1" } log_pass() { echo -e "${GREEN}[PASS]${NC} $1" ((TESTS_PASSED++)) } log_fail() { echo -e "${RED}[FAIL]${NC} $1" ((TESTS_FAILED++)) } cleanup() { log_info "Cleaning up..." rm -f "$LOG_FILE" rm -f "$SOCKET_PATH" } trap cleanup EXIT # Check prerequisites check_prerequisites() { log_info "Checking prerequisites..." if ! command -v curl &> /dev/null; then echo "Error: curl is required but not installed." exit 1 fi if ! command -v python3 &> /dev/null; then echo "Error: python3 is required but not installed." exit 1 fi } # Start socket consumer start_consumer() { log_info "Starting socket consumer..." python3 "$SCRIPT_DIR/socket_consumer.py" "$SOCKET_PATH" -o "$LOG_FILE" & CONSUMER_PID=$! sleep 2 if ! kill -0 $CONSUMER_PID 2>/dev/null; then echo "Error: Failed to start socket consumer" exit 1 fi } # Stop socket consumer stop_consumer() { log_info "Stopping socket consumer..." if [ -n "$CONSUMER_PID" ]; then kill $CONSUMER_PID 2>/dev/null || true wait $CONSUMER_PID 2>/dev/null || true fi } # Test: Basic request logging test_basic_logging() { ((TESTS_RUN++)) log_info "Test: Basic request logging" curl -s "$APACHE_URL/" > /dev/null sleep 1 if grep -q '"method":"GET"' "$LOG_FILE" 2>/dev/null; then log_pass "Basic logging test" else log_fail "Basic logging test - No GET method found in logs" fi } # Test: Custom header logging test_custom_headers() { ((TESTS_RUN++)) log_info "Test: Custom header logging" curl -s -H "X-Request-Id: test-12345" "$APACHE_URL/" > /dev/null sleep 1 if grep -q '"header_X-Request-Id":"test-12345"' "$LOG_FILE" 2>/dev/null; then log_pass "Custom header logging test" else log_fail "Custom header logging test - X-Request-Id not found in logs" fi } # Test: Multiple headers test_multiple_headers() { ((TESTS_RUN++)) log_info "Test: Multiple headers" curl -s \ -H "X-Request-Id: req-abc" \ -H "X-Trace-Id: trace-xyz" \ "$APACHE_URL/" > /dev/null sleep 1 local found_request_id=$(grep -c '"header_X-Request-Id":"req-abc"' "$LOG_FILE" 2>/dev/null || echo 0) local found_trace_id=$(grep -c '"header_X-Trace-Id":"trace-xyz"' "$LOG_FILE" 2>/dev/null || echo 0) if [ "$found_request_id" -gt 0 ] && [ "$found_trace_id" -gt 0 ]; then log_pass "Multiple headers test" else log_fail "Multiple headers test - Not all headers found" fi } # Test: JSON format validation test_json_format() { ((TESTS_RUN++)) log_info "Test: JSON format validation" curl -s "$APACHE_URL/" > /dev/null sleep 1 # Get last line and validate JSON local last_line=$(tail -1 "$LOG_FILE" 2>/dev/null | sed 's/^\[.*\] //') if echo "$last_line" | python3 -m json.tool > /dev/null 2>&1; then log_pass "JSON format validation test" else log_fail "JSON format validation test - Invalid JSON format" fi } # Test: Required fields presence test_required_fields() { ((TESTS_RUN++)) log_info "Test: Required fields presence" curl -s "$APACHE_URL/" > /dev/null sleep 1 local last_line=$(tail -1 "$LOG_FILE" 2>/dev/null | sed 's/^\[.*\] //') local required_fields=("time" "timestamp" "src_ip" "dst_ip" "method" "path" "host") local all_present=true for field in "${required_fields[@]}"; do if ! echo "$last_line" | grep -q "\"$field\":"; then all_present=false break fi done if $all_present; then log_pass "Required fields presence test" else log_fail "Required fields presence test - Missing required fields" fi } # Test: HTTP method variations test_method_variations() { ((TESTS_RUN++)) log_info "Test: HTTP method variations" curl -s -X POST "$APACHE_URL/" > /dev/null curl -s -X PUT "$APACHE_URL/" > /dev/null curl -s -X DELETE "$APACHE_URL/" > /dev/null sleep 1 local found_post=$(grep -c '"method":"POST"' "$LOG_FILE" 2>/dev/null || echo 0) local found_put=$(grep -c '"method":"PUT"' "$LOG_FILE" 2>/dev/null || echo 0) local found_delete=$(grep -c '"method":"DELETE"' "$LOG_FILE" 2>/dev/null || echo 0) if [ "$found_post" -gt 0 ] && [ "$found_put" -gt 0 ] && [ "$found_delete" -gt 0 ]; then log_pass "HTTP method variations test" else log_fail "HTTP method variations test - Not all methods found" fi } # Test: Path logging test_path_logging() { ((TESTS_RUN++)) log_info "Test: Path logging" curl -s "$APACHE_URL/api/users" > /dev/null curl -s "$APACHE_URL/foo/bar/baz" > /dev/null sleep 1 local found_api=$(grep -c '"path":"/api/users"' "$LOG_FILE" 2>/dev/null || echo 0) local found_foo=$(grep -c '"path":"/foo/bar/baz"' "$LOG_FILE" 2>/dev/null || echo 0) if [ "$found_api" -gt 0 ] && [ "$found_foo" -gt 0 ]; then log_pass "Path logging test" else log_fail "Path logging test - Not all paths found" fi } # Main test runner main() { echo "========================================" echo "mod_reqin_log Integration Tests" echo "========================================" echo "" check_prerequisites start_consumer # Give Apache time to connect to socket sleep 2 # Run tests test_basic_logging test_custom_headers test_multiple_headers test_json_format test_required_fields test_method_variations test_path_logging # Stop consumer stop_consumer # Summary echo "" echo "========================================" echo "Test Summary" echo "========================================" echo "Tests run: $TESTS_RUN" echo -e "Tests passed: ${GREEN}$TESTS_PASSED${NC}" echo -e "Tests failed: ${RED}$TESTS_FAILED${NC}" echo "" if [ $TESTS_FAILED -gt 0 ]; then exit 1 fi echo -e "${GREEN}All tests passed!${NC}" exit 0 } main "$@"