Add interoperability test for py-libp2p and js-libp2p with enhanced logging

This commit is contained in:
paschal533
2025-06-09 00:11:19 +01:00
committed by acul71
parent 505d3b2a8f
commit 1507100632
13 changed files with 1632 additions and 5 deletions

View File

@ -0,0 +1,194 @@
#!/usr/bin/env pwsh
# run_test.ps1 - libp2p Interoperability Test Runner (PowerShell)
# Tests py-libp2p <-> js-libp2p ping communication
$ErrorActionPreference = "Stop"
# Colors for output
$Red = "`e[31m"
$Green = "`e[32m"
$Yellow = "`e[33m"
$Blue = "`e[34m"
$Cyan = "`e[36m"
$Reset = "`e[0m"
function Write-ColorOutput {
param([string]$Message, [string]$Color = $Reset)
Write-Host "${Color}${Message}${Reset}"
}
Write-ColorOutput "[CHECK] Checking prerequisites..." $Cyan
if (-not (Get-Command python -ErrorAction SilentlyContinue)) {
Write-ColorOutput "[ERROR] Python not found. Install Python 3.7+" $Red
exit 1
}
if (-not (Get-Command node -ErrorAction SilentlyContinue)) {
Write-ColorOutput "[ERROR] Node.js not found. Install Node.js 16+" $Red
exit 1
}
Write-ColorOutput "[CHECK] Checking port 8000..." $Blue
$portCheck = netstat -a -n -o | findstr :8000
if ($portCheck) {
Write-ColorOutput "[ERROR] Port 8000 in use. Free the port." $Red
Write-ColorOutput $portCheck $Yellow
exit 1
}
Write-ColorOutput "[DEBUG] Cleaning up Python processes..." $Blue
Get-Process -Name "python" -ErrorAction SilentlyContinue | Where-Object { $_.CommandLine -like "*ping.py*" } | Stop-Process -Force -ErrorAction SilentlyContinue
Write-ColorOutput "[PYTHON] Starting server on port 8000..." $Yellow
Set-Location -Path "py_node"
$pyLogFile = "py_server_8000.log"
$pyErrLogFile = "py_server_8000.log.err"
$pyDebugLogFile = "ping_debug.log"
if (Test-Path $pyLogFile) { Remove-Item $pyLogFile -Force -ErrorAction SilentlyContinue }
if (Test-Path $pyErrLogFile) { Remove-Item $pyErrLogFile -Force -ErrorAction SilentlyContinue }
if (Test-Path $pyDebugLogFile) { Remove-Item $pyDebugLogFile -Force -ErrorAction SilentlyContinue }
$pyProcess = Start-Process -FilePath "python" -ArgumentList "-u", "ping.py", "server", "--port", "8000" -NoNewWindow -PassThru -RedirectStandardOutput $pyLogFile -RedirectStandardError $pyErrLogFile
Write-ColorOutput "[DEBUG] Python server PID: $($pyProcess.Id)" $Blue
Write-ColorOutput "[DEBUG] Python logs: $((Get-Location).Path)\$pyLogFile, $((Get-Location).Path)\$pyErrLogFile, $((Get-Location).Path)\$pyDebugLogFile" $Blue
$timeoutSeconds = 20
$startTime = Get-Date
$serverStarted = $false
while (((Get-Date) - $startTime).TotalSeconds -lt $timeoutSeconds -and -not $serverStarted) {
if (Test-Path $pyLogFile) {
$content = Get-Content $pyLogFile -Raw -ErrorAction SilentlyContinue
if ($content -match "Server started|Listening") {
$serverStarted = $true
Write-ColorOutput "[OK] Python server started" $Green
}
}
if (Test-Path $pyErrLogFile) {
$errContent = Get-Content $pyErrLogFile -Raw -ErrorAction SilentlyContinue
if ($errContent) {
Write-ColorOutput "[DEBUG] Error log: $errContent" $Yellow
}
}
Start-Sleep -Milliseconds 500
}
if (-not $serverStarted) {
Write-ColorOutput "[ERROR] Python server failed to start" $Red
Write-ColorOutput "[DEBUG] Logs:" $Yellow
if (Test-Path $pyLogFile) { Get-Content $pyLogFile | Write-ColorOutput -Color $Yellow }
if (Test-Path $pyErrLogFile) { Get-Content $pyErrLogFile | Write-ColorOutput -Color $Yellow }
if (Test-Path $pyDebugLogFile) { Get-Content $pyDebugLogFile | Write-ColorOutput -Color $Yellow }
Write-ColorOutput "[DEBUG] Trying foreground run..." $Yellow
python -u ping.py server --port 8000
exit 1
}
# Extract Peer ID
$peerInfo = $null
if (Test-Path $pyLogFile) {
$content = Get-Content $pyLogFile -Raw
$peerIdPattern = "Peer ID:\s*([A-Za-z0-9]+)"
$peerIdMatch = [regex]::Match($content, $peerIdPattern)
if ($peerIdMatch.Success) {
$peerId = $peerIdMatch.Groups[1].Value
$peerInfo = @{
PeerId = $peerId
MultiAddr = "/ip4/127.0.0.1/tcp/8000/p2p/$peerId"
}
Write-ColorOutput "[OK] Peer ID: $peerId" $Cyan
Write-ColorOutput "[OK] MultiAddr: $($peerInfo.MultiAddr)" $Cyan
}
}
if (-not $peerInfo) {
Write-ColorOutput "[ERROR] Could not extract Peer ID" $Red
if (Test-Path $pyLogFile) { Get-Content $pyLogFile | Write-ColorOutput -Color $Yellow }
if (Test-Path $pyErrLogFile) { Get-Content $pyErrLogFile | Write-ColorOutput -Color $Yellow }
if (Test-Path $pyDebugLogFile) { Get-Content $pyDebugLogFile | Write-ColorOutput -Color $Yellow }
Stop-Process -Id $pyProcess.Id -Force -ErrorAction SilentlyContinue
exit 1
}
# Start JavaScript client
Write-ColorOutput "[JAVASCRIPT] Starting client..." $Yellow
Set-Location -Path "../js_node"
$jsLogFile = "test_js_client_to_py_server.log"
$jsErrLogFile = "test_js_client_to_py_server.log.err"
if (Test-Path $jsLogFile) { Remove-Item $jsLogFile -Force -ErrorAction SilentlyContinue }
if (Test-Path $jsErrLogFile) { Remove-Item $jsErrLogFile -Force -ErrorAction SilentlyContinue }
$jsProcess = Start-Process -FilePath "node" -ArgumentList "src/ping.js", "client", $peerInfo.MultiAddr, "3" -NoNewWindow -PassThru -RedirectStandardOutput $jsLogFile -RedirectStandardError $jsErrLogFile
Write-ColorOutput "[DEBUG] JavaScript client PID: $($jsProcess.Id)" $Blue
Write-ColorOutput "[DEBUG] Client logs: $((Get-Location).Path)\$jsLogFile, $((Get-Location).Path)\$jsErrLogFile" $Blue
# Wait for client to complete
$clientTimeout = 10
$clientStart = Get-Date
while (-not $jsProcess.HasExited -and (((Get-Date) - $clientStart).TotalSeconds -lt $clientTimeout)) {
Start-Sleep -Seconds 1
}
if (-not $jsProcess.HasExited) {
Write-ColorOutput "[DEBUG] JavaScript client did not exit, terminating..." $Yellow
Stop-Process -Id $jsProcess.Id -Force -ErrorAction SilentlyContinue
}
Write-ColorOutput "[CHECK] Results..." $Cyan
$success = $false
if (Test-Path $jsLogFile) {
$jsLogContent = Get-Content $jsLogFile -Raw -ErrorAction SilentlyContinue
if ($jsLogContent -match "successful|Ping.*successful") {
$success = $true
Write-ColorOutput "[SUCCESS] Ping test passed" $Green
} else {
Write-ColorOutput "[FAILED] No successful pings" $Red
Write-ColorOutput "[DEBUG] Client log path: $((Get-Location).Path)\$jsLogFile" $Yellow
Write-ColorOutput "Client log:" $Yellow
Write-ColorOutput $jsLogContent $Yellow
if (Test-Path $jsErrLogFile) {
Write-ColorOutput "[DEBUG] Client error log path: $((Get-Location).Path)\$jsErrLogFile" $Yellow
Write-ColorOutput "Client error log:" $Yellow
Get-Content $jsErrLogFile | Write-ColorOutput -Color $Yellow
}
Write-ColorOutput "[DEBUG] Python server log path: $((Get-Location).Path)\..\py_node\$pyLogFile" $Yellow
Write-ColorOutput "Python server log:" $Yellow
if (Test-Path "../py_node/$pyLogFile") {
$pyLogContent = Get-Content "../py_node/$pyLogFile" -Raw -ErrorAction SilentlyContinue
if ($pyLogContent) { Write-ColorOutput $pyLogContent $Yellow } else { Write-ColorOutput "Empty or inaccessible" $Yellow }
} else {
Write-ColorOutput "File not found" $Yellow
}
Write-ColorOutput "[DEBUG] Python server error log path: $((Get-Location).Path)\..\py_node\$pyErrLogFile" $Yellow
Write-ColorOutput "Python server error log:" $Yellow
if (Test-Path "../py_node/$pyErrLogFile") {
$pyErrLogContent = Get-Content "../py_node/$pyErrLogFile" -Raw -ErrorAction SilentlyContinue
if ($pyErrLogContent) { Write-ColorOutput $pyErrLogContent $Yellow } else { Write-ColorOutput "Empty or inaccessible" $Yellow }
} else {
Write-ColorOutput "File not found" $Yellow
}
Write-ColorOutput "[DEBUG] Python debug log path: $((Get-Location).Path)\..\py_node\$pyDebugLogFile" $Yellow
Write-ColorOutput "Python debug log:" $Yellow
if (Test-Path "../py_node/$pyDebugLogFile") {
$pyDebugLogContent = Get-Content "../py_node/$pyDebugLogFile" -Raw -ErrorAction SilentlyContinue
if ($pyDebugLogContent) { Write-ColorOutput $pyDebugLogContent $Yellow } else { Write-ColorOutput "Empty or inaccessible" $Yellow }
} else {
Write-ColorOutput "File not found" $Yellow
}
}
}
Write-ColorOutput "[CLEANUP] Stopping processes..." $Yellow
Stop-Process -Id $pyProcess.Id -Force -ErrorAction SilentlyContinue
Stop-Process -Id $jsProcess.Id -Force -ErrorAction SilentlyContinue
Set-Location -Path "../"
if ($success) {
Write-ColorOutput "[SUCCESS] Test completed" $Green
exit 0
} else {
Write-ColorOutput "[FAILED] Test failed" $Red
exit 1
}

View File

@ -0,0 +1,215 @@
#!/usr/bin/env bash
# run_test.sh - libp2p Interoperability Test Runner (Bash)
# Tests py-libp2p <-> js-libp2p ping communication
set -e
# Colors for output
RED='\033[31m'
GREEN='\033[32m'
YELLOW='\033[33m'
BLUE='\033[34m'
CYAN='\033[36m'
RESET='\033[0m'
write_color_output() {
local message="$1"
local color="${2:-$RESET}"
echo -e "${color}${message}${RESET}"
}
write_color_output "[CHECK] Checking prerequisites..." "$CYAN"
if ! command -v python3 &> /dev/null && ! command -v python &> /dev/null; then
write_color_output "[ERROR] Python not found. Install Python 3.7+" "$RED"
exit 1
fi
# Use python3 if available, otherwise python
PYTHON_CMD="python3"
if ! command -v python3 &> /dev/null; then
PYTHON_CMD="python"
fi
if ! command -v node &> /dev/null; then
write_color_output "[ERROR] Node.js not found. Install Node.js 16+" "$RED"
exit 1
fi
write_color_output "[CHECK] Checking port 8000..." "$BLUE"
if netstat -tuln 2>/dev/null | grep -q ":8000 " || ss -tuln 2>/dev/null | grep -q ":8000 "; then
write_color_output "[ERROR] Port 8000 in use. Free the port." "$RED"
if command -v netstat &> /dev/null; then
netstat -tuln | grep ":8000 " | write_color_output "$(cat)" "$YELLOW"
elif command -v ss &> /dev/null; then
ss -tuln | grep ":8000 " | write_color_output "$(cat)" "$YELLOW"
fi
exit 1
fi
write_color_output "[DEBUG] Cleaning up Python processes..." "$BLUE"
pkill -f "ping.py" 2>/dev/null || true
write_color_output "[PYTHON] Starting server on port 8000..." "$YELLOW"
cd py_node
PY_LOG_FILE="py_server_8000.log"
PY_ERR_LOG_FILE="py_server_8000.log.err"
PY_DEBUG_LOG_FILE="ping_debug.log"
rm -f "$PY_LOG_FILE" "$PY_ERR_LOG_FILE" "$PY_DEBUG_LOG_FILE"
$PYTHON_CMD -u ping.py server --port 8000 > "$PY_LOG_FILE" 2> "$PY_ERR_LOG_FILE" &
PY_PROCESS_PID=$!
write_color_output "[DEBUG] Python server PID: $PY_PROCESS_PID" "$BLUE"
write_color_output "[DEBUG] Python logs: $(pwd)/$PY_LOG_FILE, $(pwd)/$PY_ERR_LOG_FILE, $(pwd)/$PY_DEBUG_LOG_FILE" "$BLUE"
TIMEOUT_SECONDS=20
START_TIME=$(date +%s)
SERVER_STARTED=false
while [ $(($(date +%s) - START_TIME)) -lt $TIMEOUT_SECONDS ] && [ "$SERVER_STARTED" = false ]; do
if [ -f "$PY_LOG_FILE" ]; then
if grep -q "Server started\|Listening" "$PY_LOG_FILE" 2>/dev/null; then
SERVER_STARTED=true
write_color_output "[OK] Python server started" "$GREEN"
fi
fi
if [ -f "$PY_ERR_LOG_FILE" ] && [ -s "$PY_ERR_LOG_FILE" ]; then
ERR_CONTENT=$(cat "$PY_ERR_LOG_FILE" 2>/dev/null || true)
if [ -n "$ERR_CONTENT" ]; then
write_color_output "[DEBUG] Error log: $ERR_CONTENT" "$YELLOW"
fi
fi
sleep 0.5
done
if [ "$SERVER_STARTED" = false ]; then
write_color_output "[ERROR] Python server failed to start" "$RED"
write_color_output "[DEBUG] Logs:" "$YELLOW"
[ -f "$PY_LOG_FILE" ] && cat "$PY_LOG_FILE" | while read line; do write_color_output "$line" "$YELLOW"; done
[ -f "$PY_ERR_LOG_FILE" ] && cat "$PY_ERR_LOG_FILE" | while read line; do write_color_output "$line" "$YELLOW"; done
[ -f "$PY_DEBUG_LOG_FILE" ] && cat "$PY_DEBUG_LOG_FILE" | while read line; do write_color_output "$line" "$YELLOW"; done
write_color_output "[DEBUG] Trying foreground run..." "$YELLOW"
$PYTHON_CMD -u ping.py server --port 8000
exit 1
fi
# Extract Peer ID
PEER_ID=""
MULTI_ADDR=""
if [ -f "$PY_LOG_FILE" ]; then
CONTENT=$(cat "$PY_LOG_FILE" 2>/dev/null || true)
PEER_ID=$(echo "$CONTENT" | grep -oP "Peer ID:\s*\K[A-Za-z0-9]+" || true)
if [ -n "$PEER_ID" ]; then
MULTI_ADDR="/ip4/127.0.0.1/tcp/8000/p2p/$PEER_ID"
write_color_output "[OK] Peer ID: $PEER_ID" "$CYAN"
write_color_output "[OK] MultiAddr: $MULTI_ADDR" "$CYAN"
fi
fi
if [ -z "$PEER_ID" ]; then
write_color_output "[ERROR] Could not extract Peer ID" "$RED"
[ -f "$PY_LOG_FILE" ] && cat "$PY_LOG_FILE" | while read line; do write_color_output "$line" "$YELLOW"; done
[ -f "$PY_ERR_LOG_FILE" ] && cat "$PY_ERR_LOG_FILE" | while read line; do write_color_output "$line" "$YELLOW"; done
[ -f "$PY_DEBUG_LOG_FILE" ] && cat "$PY_DEBUG_LOG_FILE" | while read line; do write_color_output "$line" "$YELLOW"; done
kill $PY_PROCESS_PID 2>/dev/null || true
exit 1
fi
# Start JavaScript client
write_color_output "[JAVASCRIPT] Starting client..." "$YELLOW"
cd ../js_node
JS_LOG_FILE="test_js_client_to_py_server.log"
JS_ERR_LOG_FILE="test_js_client_to_py_server.log.err"
rm -f "$JS_LOG_FILE" "$JS_ERR_LOG_FILE"
node src/ping.js client "$MULTI_ADDR" 3 > "$JS_LOG_FILE" 2> "$JS_ERR_LOG_FILE" &
JS_PROCESS_PID=$!
write_color_output "[DEBUG] JavaScript client PID: $JS_PROCESS_PID" "$BLUE"
write_color_output "[DEBUG] Client logs: $(pwd)/$JS_LOG_FILE, $(pwd)/$JS_ERR_LOG_FILE" "$BLUE"
# Wait for client to complete
CLIENT_TIMEOUT=10
CLIENT_START=$(date +%s)
while kill -0 $JS_PROCESS_PID 2>/dev/null && [ $(($(date +%s) - CLIENT_START)) -lt $CLIENT_TIMEOUT ]; do
sleep 1
done
if kill -0 $JS_PROCESS_PID 2>/dev/null; then
write_color_output "[DEBUG] JavaScript client did not exit, terminating..." "$YELLOW"
kill $JS_PROCESS_PID 2>/dev/null || true
fi
write_color_output "[CHECK] Results..." "$CYAN"
SUCCESS=false
if [ -f "$JS_LOG_FILE" ]; then
JS_LOG_CONTENT=$(cat "$JS_LOG_FILE" 2>/dev/null || true)
if echo "$JS_LOG_CONTENT" | grep -q "successful\|Ping.*successful"; then
SUCCESS=true
write_color_output "[SUCCESS] Ping test passed" "$GREEN"
else
write_color_output "[FAILED] No successful pings" "$RED"
write_color_output "[DEBUG] Client log path: $(pwd)/$JS_LOG_FILE" "$YELLOW"
write_color_output "Client log:" "$YELLOW"
write_color_output "$JS_LOG_CONTENT" "$YELLOW"
if [ -f "$JS_ERR_LOG_FILE" ]; then
write_color_output "[DEBUG] Client error log path: $(pwd)/$JS_ERR_LOG_FILE" "$YELLOW"
write_color_output "Client error log:" "$YELLOW"
cat "$JS_ERR_LOG_FILE" | while read line; do write_color_output "$line" "$YELLOW"; done
fi
write_color_output "[DEBUG] Python server log path: $(pwd)/../py_node/$PY_LOG_FILE" "$YELLOW"
write_color_output "Python server log:" "$YELLOW"
if [ -f "../py_node/$PY_LOG_FILE" ]; then
PY_LOG_CONTENT=$(cat "../py_node/$PY_LOG_FILE" 2>/dev/null || true)
if [ -n "$PY_LOG_CONTENT" ]; then
write_color_output "$PY_LOG_CONTENT" "$YELLOW"
else
write_color_output "Empty or inaccessible" "$YELLOW"
fi
else
write_color_output "File not found" "$YELLOW"
fi
write_color_output "[DEBUG] Python server error log path: $(pwd)/../py_node/$PY_ERR_LOG_FILE" "$YELLOW"
write_color_output "Python server error log:" "$YELLOW"
if [ -f "../py_node/$PY_ERR_LOG_FILE" ]; then
PY_ERR_LOG_CONTENT=$(cat "../py_node/$PY_ERR_LOG_FILE" 2>/dev/null || true)
if [ -n "$PY_ERR_LOG_CONTENT" ]; then
write_color_output "$PY_ERR_LOG_CONTENT" "$YELLOW"
else
write_color_output "Empty or inaccessible" "$YELLOW"
fi
else
write_color_output "File not found" "$YELLOW"
fi
write_color_output "[DEBUG] Python debug log path: $(pwd)/../py_node/$PY_DEBUG_LOG_FILE" "$YELLOW"
write_color_output "Python debug log:" "$YELLOW"
if [ -f "../py_node/$PY_DEBUG_LOG_FILE" ]; then
PY_DEBUG_LOG_CONTENT=$(cat "../py_node/$PY_DEBUG_LOG_FILE" 2>/dev/null || true)
if [ -n "$PY_DEBUG_LOG_CONTENT" ]; then
write_color_output "$PY_DEBUG_LOG_CONTENT" "$YELLOW"
else
write_color_output "Empty or inaccessible" "$YELLOW"
fi
else
write_color_output "File not found" "$YELLOW"
fi
fi
fi
write_color_output "[CLEANUP] Stopping processes..." "$YELLOW"
kill $PY_PROCESS_PID 2>/dev/null || true
kill $JS_PROCESS_PID 2>/dev/null || true
cd ../
if [ "$SUCCESS" = true ]; then
write_color_output "[SUCCESS] Test completed" "$GREEN"
exit 0
else
write_color_output "[FAILED] Test failed" "$RED"
exit 1
fi