From c660bdef1dabd152fe2e205bdc67a0164e1fc88e Mon Sep 17 00:00:00 2001 From: Timothy Day Date: Fri, 22 Sep 2023 03:29:41 +0000 Subject: [PATCH] LU-16694 tests: rewrite socket client, server in python Rewrite socketclient and socketserver in python, rather than perl. This should bring us closer to dropping perl requirements from Lustre packages. Test-Parameters: trivial testlist=sanity env=ONLY=54a,ONLY_REPEAT=100 Signed-off-by: Timothy Day Change-Id: I1c03e13993af147ce7d487b8d66320f4f99eb676 Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/52478 Tested-by: jenkins Tested-by: Maloo Reviewed-by: Andreas Dilger Reviewed-by: Feng Lei Reviewed-by: Oleg Drokin --- lustre/tests/sanity.sh | 8 ++-- lustre/tests/socketclient | 68 +++++++++++++++++++++++++++------ lustre/tests/socketserver | 97 +++++++++++++++++++++++++++++++++-------------- 3 files changed, 127 insertions(+), 46 deletions(-) diff --git a/lustre/tests/sanity.sh b/lustre/tests/sanity.sh index 6c23488..0c819a7 100755 --- a/lustre/tests/sanity.sh +++ b/lustre/tests/sanity.sh @@ -6339,15 +6339,13 @@ test_53() { run_test 53 "verify that MDS and OSTs agree on pre-creation ====" test_54a() { - LANG=C perl -MSocket -e ';' || skip "no Socket perl module installed" - - LANG=C $SOCKETSERVER $DIR/socket || + $SOCKETSERVER $DIR/socket || error "$SOCKETSERVER $DIR/socket failed: $?" - LANG=C $SOCKETCLIENT $DIR/socket || + $SOCKETCLIENT $DIR/socket || error "$SOCKETCLIENT $DIR/socket failed: $?" unlink $DIR/socket || error "unlink $DIR/socket failed: $?" } -run_test 54a "unix domain socket test ==========================" +run_test 54a "unix domain socket test" test_54b() { f="$DIR/f54b" diff --git a/lustre/tests/socketclient b/lustre/tests/socketclient index 5e6e659..1b629f0 100755 --- a/lustre/tests/socketclient +++ b/lustre/tests/socketclient @@ -1,12 +1,56 @@ -#!/usr/bin/perl -w -use Socket; -use strict; -my ($rendezvous, $line); - -$rendezvous = shift || <@ARGV>; -socket(SOCK, AF_UNIX, SOCK_STREAM, 0) || die "socket: $!"; -connect(SOCK, sockaddr_un($rendezvous)) || die "connect: $!"; -while (defined($line = )) { - print $line; -} -exit; +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0 + +# +# This file is part of Lustre, http://www.lustre.org/ +# +# lustre/tests/socketclient +# +# Domain socket client, used to test that domain sockets +# work on lustre. +# +# Rewrite in Python: Timothy Day +# + +import os +import socket +import sys + + +# Define a function for logging messages +def logmsg(msg): + print(f"{sys.argv[0]} {os.getpid()}: {msg} at {os.popen('date').read().strip()}") + + +# Get the socket path from the command-line argument, +# or ask the user to input one +if len(sys.argv) > 1: + socket_path = sys.argv[1] +else: + socket_path = input("Enter socket path: ") + +try: + # Create a Unix domain socket + sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + + # Connect to the specified rendezvous point + sock.connect(socket_path) + + logmsg(f"connection on {socket_path}") + + while True: + # Receive data from the socket in chunks of 1024 bytes + data = sock.recv(1024) + + # If there's no more data to read, exit the loop + if not data: + break + + # Decode the data as UTF-8 and print it to the standard output + print("Message:", data.decode('utf-8'), end='') + +except socket.error as e: + logmsg(f"Socket error: {e}") + +finally: + sock.close() diff --git a/lustre/tests/socketserver b/lustre/tests/socketserver index e2fd66a..1a6be76 100755 --- a/lustre/tests/socketserver +++ b/lustre/tests/socketserver @@ -1,29 +1,68 @@ -#! /usr/bin/perl -w -use strict; -use Socket; - -BEGIN { $ENV{PATH} = '/usr/ucb:/bin' } -sub logmsg { print "$0 $$: @_ at ", scalar localtime, "\n" } - -my $NAME = <@ARGV>; -my $uaddr = sockaddr_un($NAME); - -socket(Server,AF_UNIX,SOCK_STREAM,0) || die "socket: $!"; -unlink($NAME); -bind (Server, $uaddr) || die "bind: $!"; -listen(Server,SOMAXCONN) || die "listen: $!"; - -logmsg "server started on $NAME"; - -my $rc = fork(); -if ($rc > 0) { #parent - exit(); -} elsif ($rc < 0) { # error - logmsg "fork failed: $rc"; - exit(); -} - -accept(Client,Server); -logmsg "connection on $NAME"; -print Client "from server\n"; -close Client; +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0 + +# +# This file is part of Lustre, http://www.lustre.org/ +# +# lustre/tests/socketserver +# +# Domain socket server, used to test that domain sockets +# work on lustre. +# +# Rewrite in Python: Timothy Day +# + +import os +import socket +import sys + + +# Define a function for logging messages +def logmsg(msg): + print(f"{sys.argv[0]} {os.getpid()}: {msg} at {os.popen('date').read().strip()}") + + +# Get the socket path from the command-line argument, +# or ask the user to input one +if len(sys.argv) > 1: + socket_path = sys.argv[1] +else: + socket_path = input("Enter socket path: ") + +try: + # Create a Unix domain socket with a specified file path + sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + + # Bind the socket to the Unix domain socket file + sock.bind(socket_path) + + # Listen for incoming connections on the socket + sock.listen(socket.SOMAXCONN) + + # Fork a child process to handle the connection + pid = os.fork() + + if pid > 0: + # Parent process + logmsg(f"server started on {socket_path}") + sys.exit(0) + elif pid < 0: + # Fork failed + logmsg(f"fork failed: {pid}") + sys.exit(1) + + # Accept an incoming connection + client_sock, _ = sock.accept() + + logmsg(f"connection on {socket_path}") + + # Send a message to the client and close + # the client socket + client_sock.send(b"This is a message from the server!\n") + client_sock.close() + +except socket.error as e: + logmsg(f"Socket error: {e}") + +finally: + sock.close() -- 1.8.3.1