|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Create a temporary env file |
| 4 | +echo "Creating test env file..." |
| 5 | +cat > test.env << EOL |
| 6 | +FULL_NAMESPACE=TEST-MAINNET-ETH |
| 7 | +LOCAL_COLLECTOR_PORT=50051 |
| 8 | +EOL |
| 9 | + |
| 10 | +# Export required variables |
| 11 | +export FULL_NAMESPACE=TEST-MAINNET-ETH |
| 12 | + |
| 13 | +# Function to run the test |
| 14 | +run_test() { |
| 15 | + local nc_available=$1 |
| 16 | + local curl_available=$2 |
| 17 | + local expected_port_in_use=$3 |
| 18 | + local test_description=$4 |
| 19 | + |
| 20 | + echo "🧪 Test: $test_description" |
| 21 | + |
| 22 | + if [ "$nc_available" = "false" ]; then |
| 23 | + # Create functions that override command behavior for nc and netcat |
| 24 | + command() { |
| 25 | + if [ "$2" = "nc" ]; then |
| 26 | + return 1 |
| 27 | + elif [ "$2" = "netcat" ]; then |
| 28 | + return 1 |
| 29 | + fi |
| 30 | + builtin command "$@" |
| 31 | + } |
| 32 | + |
| 33 | + # Override the actual commands too |
| 34 | + nc() { return 1; } |
| 35 | + netcat() { return 1; } |
| 36 | + export -f command |
| 37 | + echo " Disabled nc/netcat check via function override" |
| 38 | + if [ "$curl_available" = "false" ]; then |
| 39 | + echo " Disabled curl check via function override" |
| 40 | + fi |
| 41 | + else |
| 42 | + # Restore normal command behavior |
| 43 | + unset -f command |
| 44 | + echo " Restored normal command behavior" |
| 45 | + fi |
| 46 | + |
| 47 | + if [ "$expected_port_in_use" = "true" ]; then |
| 48 | + echo " Starting dummy service on port 50051..." |
| 49 | + nc -l 50051 & |
| 50 | + NC_PID=$! |
| 51 | + sleep 1 |
| 52 | + fi |
| 53 | + |
| 54 | + echo " Running collector_test.sh..." |
| 55 | + ./collector_test.sh --env-file test.env |
| 56 | + TEST_RESULT=$? |
| 57 | + |
| 58 | + if [ "$expected_port_in_use" = "true" ]; then |
| 59 | + echo " Stopping dummy service..." |
| 60 | + kill $NC_PID 2>/dev/null |
| 61 | + fi |
| 62 | + |
| 63 | + echo " Test result code: $TEST_RESULT" |
| 64 | + |
| 65 | + # Verify expected exit codes |
| 66 | + if [ "$nc_available" = "false" ] && [ "$curl_available" = "false" ]; then |
| 67 | + # Should exit with 1 when no tools available |
| 68 | + if [ "$TEST_RESULT" -eq 1 ]; then |
| 69 | + echo " ✅ Correctly exited with code 1 when no tools available" |
| 70 | + return 0 |
| 71 | + else |
| 72 | + echo " ❌ Expected exit code 1 when no tools available, got $TEST_RESULT" |
| 73 | + return 1 |
| 74 | + fi |
| 75 | + elif [ "$expected_port_in_use" = "true" ] && [ "$TEST_RESULT" -eq 100 ]; then |
| 76 | + echo " ✅ Correctly detected running collector" |
| 77 | + return 0 |
| 78 | + elif [ "$expected_port_in_use" = "false" ] && [ "$TEST_RESULT" -eq 101 ]; then |
| 79 | + echo " ✅ Correctly found available port" |
| 80 | + return 0 |
| 81 | + else |
| 82 | + echo " ❌ Unexpected exit code $TEST_RESULT" |
| 83 | + return 1 |
| 84 | + fi |
| 85 | +} |
| 86 | + |
| 87 | +# Function to kill any process using our test port |
| 88 | +cleanup_port() { |
| 89 | + echo "Cleaning up port 50051..." |
| 90 | + lsof -ti:50051 | xargs kill -9 2>/dev/null || true |
| 91 | + sleep 1 |
| 92 | +} |
| 93 | + |
| 94 | +# Test scenarios |
| 95 | +echo "Running test scenarios..." |
| 96 | + |
| 97 | +# Initial cleanup |
| 98 | +cleanup_port |
| 99 | + |
| 100 | +echo "🧪 Testing with nc available..." |
| 101 | +# Test 1: With nc, port free (should exit 101) |
| 102 | +run_test "true" "true" "false" "With nc, port is free" |
| 103 | +cleanup_port |
| 104 | + |
| 105 | +# Test 2: With nc, port in use (should exit 100) |
| 106 | +run_test "true" "true" "true" "With nc, port is in use" |
| 107 | +cleanup_port |
| 108 | + |
| 109 | +echo "🧪 Testing with netcat fallback..." |
| 110 | +# Test 3: Without nc but with netcat, port free (should exit 101) |
| 111 | +run_test "false" "true" "false" "Without nc but with netcat, port is free" |
| 112 | +cleanup_port |
| 113 | + |
| 114 | +# Test 4: Without nc but with netcat, port in use (should exit 100) |
| 115 | +run_test "false" "true" "true" "Without nc but with netcat, port is in use" |
| 116 | +cleanup_port |
| 117 | + |
| 118 | +echo "🧪 Testing with bash fallback..." |
| 119 | +# Test 5: Without nc and netcat, port free (should exit 101) |
| 120 | +run_test "false" "false" "false" "Without nc/netcat, using bash fallback, port is free" |
| 121 | +cleanup_port |
| 122 | + |
| 123 | +# Test 6: Without nc and netcat, port in use (should exit 100) |
| 124 | +run_test "false" "false" "true" "Without nc/netcat, using bash fallback, port is in use" |
| 125 | +cleanup_port |
| 126 | + |
| 127 | +# Cleanup |
| 128 | +echo "Cleaning up..." |
| 129 | +rm -f test.env |
| 130 | +unset -f command # Ensure command function is unset |
| 131 | + |
| 132 | +echo "Tests completed!" |
0 commit comments