Skip to content

Commit

Permalink
Merge pull request #1302 from spidernet-io/fix-check-net-tool-server
Browse files Browse the repository at this point in the history
Fix check net tool server process
  • Loading branch information
weizhoublue authored Apr 12, 2024
2 parents b0f59e0 + 6991e14 commit 03aa272
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 38 deletions.
8 changes: 7 additions & 1 deletion cmd/nettools/client/batch/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@ func Batch(ctx context.Context, config flag.Config) error {
}

func tcp(ctx context.Context, config flag.Config) error {
addrStr := fmt.Sprintf("%s:%s", *config.Addr, *config.TcpPort)
ip := net.ParseIP(*config.Addr)
var addrStr string
if ip.To4() == nil {
addrStr = fmt.Sprintf("[%s]:%s", *config.Addr, *config.TcpPort)
} else {
addrStr = fmt.Sprintf("%s:%s", *config.Addr, *config.TcpPort)
}
dialer := net.Dialer{Timeout: 3 * time.Second}
conn, err := dialer.DialContext(ctx, "tcp", addrStr)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ run_nettool_server: load_nettool_image
echo "run nettool server container"
{ docker run -itd --name $(NETTOOLS_SERVER_A) --network kind $(NETTOOLS_IMAGE) $(NETTOOLS_SERVER_BIN) -protocol $(MOD) -tcpPort $(TCP_PORT) --udpPort $(UDP_PORT) -webPort $(WEB_PORT) & } || { echo "failed to run nettools server a"; exit 1; }; \
{ docker run -itd --name $(NETTOOLS_SERVER_B) --network kind $(NETTOOLS_IMAGE) $(NETTOOLS_SERVER_BIN) -protocol $(MOD) -tcpPort $(TCP_PORT) --udpPort $(UDP_PORT) -webPort $(WEB_PORT) & } || { echo "failed to run nettools server b"; exit 1; }; \
sleep 6
make check_netttool_server

# kind load nettool image
Expand Down
80 changes: 43 additions & 37 deletions test/scripts/check-nettools-server.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,54 @@
## SPDX-License-Identifier: Apache-2.0
## Copyright Authors of Spider

set -x
checkNettoolsServer() {
containerName=$1

TCP_HELLO="TCP Server Say hello"
UDP_HELLO="UDP Server Say hello"
WEB_HELLO="WebSocket Server Say hello"
echo "E2E_IP_FAMILY=$E2E_IP_FAMILY"

checkNettoolsServer() {
server_name=$1

if [[ ${E2E_IP_FAMILY} == "ipv4" ]]; then SERVER_IP=$(docker inspect ${server_name} | grep -w IPAddress | grep -E -o '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | tr -d '\n'); fi
if [ "${E2E_IP_FAMILY}" == "ipv6" ] || [ "${E2E_IP_FAMILY}" == "dual" ]; then
v6=$(docker inspect "${server_name}" | grep -w GlobalIPv6Address | sed 1d | awk '{print $2}' | tr -d '",' | tr -d '\n')
SERVER_IP=[${v6}]
fi
RESULT=$(mktemp)

"${CLIENT}" -addr "${SERVER_IP}" -protocol all -tcpPort "${TCP_PORT}" -udpPort "${UDP_PORT}" -webPort "${WEB_PORT}" > "${RESULT}" 2>&1 &

server="bad"

for i in {0..10}; do
if grep -e "${TCP_HELLO}" -e "${UDP_HELLO}" -e "${WEB_HELLO}" "${RESULT}"; then
echo "server is ok"
server="ok"
break
else
echo "some connect not ready, wait..."
sleep 2s
if [[ ${E2E_IP_FAMILY} == "ipv4" ]] || [[ ${E2E_IP_FAMILY} == "dual" ]]; then
serverIPv4=$(docker inspect "$containerName" -f '{{.NetworkSettings.Networks.kind.IPAddress}}')
if [[ -z "$serverIPv4" ]]; then
echo "Error: IPv4 address not found for container: $containerName"
exit 1
else
echo "find IPv4 address: $serverIPv4"
eip=$(ip r get "$serverIPv4" | awk '/src/ { print $5 }')
"${CLIENT}" -addr "${serverIPv4}" -protocol all -tcpPort "${TCP_PORT}" -udpPort "${UDP_PORT}" -webPort "${WEB_PORT}" -eip $eip -batch true
if [ $? -ne 0 ]; then
print_error "failed to check net tool server: $containerName"
exit 1
fi
print_green "success to check net tools server: $containerName"
fi
fi
done

if [[ ${server} == "bad" ]]; then echo 'time out to wait server:"${server_name}" ready'; exit 1; fi

if [[ ${E2E_IP_FAMILY} == "ipv6" ]] || [[ ${E2E_IP_FAMILY} == "dual" ]]; then
serverIPv6=$(docker inspect "$containerName" -f '{{.NetworkSettings.Networks.kind.GlobalIPv6Address}}')
if [[ -z "$serverIPv6" ]]; then
echo "Error: IPv6 address not found for container: $containerName"
exit 1
else
echo "find IPv6 address: $serverIPv6"
eip=$(ip r get "$serverIPv6" | awk '{for(i=1;i<=NF;i++) if ($i=="src") print $(i+1)}')

"${CLIENT}" -addr "${serverIPv6}" -protocol all -tcpPort "${TCP_PORT}" -udpPort "${UDP_PORT}" -webPort "${WEB_PORT}" -eip $eip -batch true
if [ $? -ne 0 ]; then
print_error "failed to check net tool server: $containerName"
exit 1
fi
print_green "success to check net tools server: $containerName"
fi
fi
}

print_green() {
echo -e "\033[0;32m$1\033[0m"
}

print_error() {
echo -e "\033[0;31m$1\033[0m"
}

checkNettoolsServer ${NETTOOLS_SERVER_A}
checkNettoolsServer ${NETTOOLS_SERVER_B}

echo "server is ok, delete the test-client process"
ps=$(pgrep -f "${CLIENT}" | tr '\n' ' ')
if [[ -n $ps ]]; then
for p in $ps; do
set +m kill "$p" 2>&1 >/dev/null
done
fi

0 comments on commit 03aa272

Please sign in to comment.