#!/bin/bash # Paths to scripts MAIN_SCRIPT="./main.sh" BROKER_SCRIPT="./broker.sh" # Temporary files for output and logging TEMP_OUTPUT="temp_output.json" LOG_FILE="test_harness.log" NC_PORT=8080 # Port for netcat or HTTP server if needed # Function to log test results log_result() { local test_name="$1" local result="$2" echo "$test_name: $result" | tee -a "$LOG_FILE" } # Function to clear the port if it's already in use clear_port() { if lsof -i :$NC_PORT &>/dev/null; then echo "Clearing processes using port $NC_PORT..." fuser -k $NC_PORT/tcp fi } # Test 1: Check main.sh produces JSON output test_main_output() { echo "Running test_main_output..." bash "$MAIN_SCRIPT" > "$TEMP_OUTPUT" if jq empty "$TEMP_OUTPUT" &>/dev/null; then log_result "Test 1 - main.sh JSON Output" "PASS" else log_result "Test 1 - main.sh JSON Output" "FAIL" fi } # Test 2: Test broker.sh with JSON data and different protocols (simplified) test_broker_basic() { echo "Running test_broker_basic..." json_data=$(cat "$TEMP_OUTPUT") # Assuming the broker script sends data, we only check if it runs without error bash "$BROKER_SCRIPT" -o http -s "localhost:$NC_PORT" "$json_data" if [ $? -eq 0 ]; then log_result "Test 2 - broker.sh HTTP protocol" "PASS" else log_result "Test 2 - broker.sh HTTP protocol" "FAIL" fi } # Test 3: Error handling for missing JSON data in broker.sh test_broker_error_handling() { echo "Running test_broker_error_handling..." # Run broker.sh without JSON data bash "$BROKER_SCRIPT" -o http -s "localhost:$NC_PORT" > /dev/null 2>&1 if [ $? -ne 0 ]; then log_result "Test 3 - broker.sh Missing JSON Data" "PASS" else log_result "Test 3 - broker.sh Missing JSON Data" "FAIL" fi } # Cleanup function to remove temporary files cleanup() { rm -f "$TEMP_OUTPUT" } # Run tests echo "Starting test harness for main.sh and broker.sh" | tee "$LOG_FILE" test_main_output test_broker_basic test_broker_error_handling cleanup echo "Tests completed. Results are logged in $LOG_FILE."