Whamcloud - gitweb
LU-16694 tests: rewrite socket client, server in python 78/52478/2
authorTimothy Day <timday@amazon.com>
Fri, 22 Sep 2023 03:29:41 +0000 (03:29 +0000)
committerOleg Drokin <green@whamcloud.com>
Mon, 8 Apr 2024 15:37:12 +0000 (15:37 +0000)
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 <timday@amazon.com>
Change-Id: I1c03e13993af147ce7d487b8d66320f4f99eb676
Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/52478
Tested-by: jenkins <devops@whamcloud.com>
Tested-by: Maloo <maloo@whamcloud.com>
Reviewed-by: Andreas Dilger <adilger@whamcloud.com>
Reviewed-by: Feng Lei <flei@whamcloud.com>
Reviewed-by: Oleg Drokin <green@whamcloud.com>
lustre/tests/sanity.sh
lustre/tests/socketclient
lustre/tests/socketserver

index 6c23488..0c819a7 100755 (executable)
@@ -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"
index 5e6e659..1b629f0 100755 (executable)
@@ -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 = <SOCK>)) {
-       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 <timday@amazon.com>
+#
+
+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()
index e2fd66a..1a6be76 100755 (executable)
@@ -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 <timday@amazon.com>
+#
+
+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()