diff --git a/cmd/nettools/client/batch/batch.go b/cmd/nettools/client/batch/batch.go index 7049c9547..ed0cbbf99 100644 --- a/cmd/nettools/client/batch/batch.go +++ b/cmd/nettools/client/batch/batch.go @@ -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 { diff --git a/test/Makefile b/test/Makefile index 81a1d4a38..1690e4df3 100644 --- a/test/Makefile +++ b/test/Makefile @@ -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 diff --git a/test/scripts/check-nettools-server.sh b/test/scripts/check-nettools-server.sh index 99ccba2c3..2002d7773 100644 --- a/test/scripts/check-nettools-server.sh +++ b/test/scripts/check-nettools-server.sh @@ -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