Skip to content

Commit b96a08e

Browse files
authored
Merge pull request #145 from powerloom/142-docker-container-name-conflict-error
Fix: Fallback to TCP ping to test local collector port instead of `netcat`
2 parents 31186bb + bf89dbf commit b96a08e

File tree

2 files changed

+160
-15
lines changed

2 files changed

+160
-15
lines changed

collector_test.sh

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,28 @@ hosts=("localhost" "127.0.0.1" "0.0.0.0")
2929
test_ping=false
3030
test_namespace=false
3131

32-
# Test port connectivity
33-
if command -v nc &> /dev/null; then
34-
test_command="nc -z"
35-
for host in "${hosts[@]}"; do
36-
echo " ⏳ Testing ${host}:${LOCAL_COLLECTOR_PORT}"
37-
if ${test_command} "${host}" "${LOCAL_COLLECTOR_PORT}" 2>/dev/null; then
32+
# Test port connectivity using nc/netcat if available, otherwise use pure bash
33+
for host in "${hosts[@]}"; do
34+
echo " ⏳ Testing ${host}:${LOCAL_COLLECTOR_PORT}"
35+
36+
if command -v nc &> /dev/null; then
37+
if nc -z "${host}" "${LOCAL_COLLECTOR_PORT}" 2>/dev/null; then
3838
test_ping=true
3939
break
4040
fi
41-
done
42-
else
43-
test_command="curl -s --connect-timeout 5"
44-
for host in "${hosts[@]}"; do
45-
echo " ⏳ Testing ${host}:${LOCAL_COLLECTOR_PORT}"
46-
if $test_command "${host}:${LOCAL_COLLECTOR_PORT}" 2>/dev/null; then
41+
elif command -v netcat &> /dev/null; then
42+
if netcat -z "${host}" "${LOCAL_COLLECTOR_PORT}" 2>/dev/null; then
4743
test_ping=true
4844
break
4945
fi
50-
done
51-
fi
46+
else
47+
# Pure bash TCP connection test - available on all systems
48+
if timeout 1 bash -c "</dev/tcp/${host}/${LOCAL_COLLECTOR_PORT}" 2>/dev/null; then
49+
test_ping=true
50+
break
51+
fi
52+
fi
53+
done
5254

5355
# Test container status
5456
container_name="snapshotter-lite-local-collector-${FULL_NAMESPACE}"
@@ -66,7 +68,18 @@ if [ "$test_ping" = true ] && [ "$test_namespace" = true ]; then
6668
else
6769
echo "⚠️ No active collector found - searching for available ports..."
6870
for port in {50051..51050}; do
69-
if ! nc -z localhost $port 2>/dev/null; then
71+
port_is_free=false
72+
if [[ "$PORT_CHECK_CMD" == *"curl"* ]]; then
73+
if ! $PORT_CHECK_CMD "localhost:$port" 2>/dev/null; then
74+
port_is_free=true
75+
fi
76+
else
77+
if ! $PORT_CHECK_CMD "localhost" "$port" 2>/dev/null; then
78+
port_is_free=true
79+
fi
80+
fi
81+
82+
if [ "$port_is_free" = true ]; then
7083
echo "✅ Found available port: $port"
7184
sed -i".backup" "s/^LOCAL_COLLECTOR_PORT=.*/LOCAL_COLLECTOR_PORT=${port}/" "${ENV_FILE}"
7285
break

test_collector_check.sh

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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

Comments
 (0)