From c530e15cb3de0a7bd67875474a6fd7b4bd00f59b Mon Sep 17 00:00:00 2001 From: Chris Hofstaedtler Date: Sat, 17 Aug 2024 16:04:40 +0200 Subject: [PATCH 1/4] tests/serial-console-connection: add timeout (default to 3min) --- tests/serial-console-connection | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/serial-console-connection b/tests/serial-console-connection index 97c623b2..7ba0c486 100755 --- a/tests/serial-console-connection +++ b/tests/serial-console-connection @@ -18,6 +18,8 @@ parser.add_argument('--user', default="root", help='user name to use for login (default: root)') parser.add_argument('--password', default="grml", help='password for login (default: grml)') +parser.add_argument('--timeout', default="180", type=int, + help='Maximum time for finding the login prompt (seconds)') parser.add_argument('--tries', default="12", type=int, help='Number of retries for finding the login prompt') parser.add_argument('--poweroff', action="store_true", default=False, @@ -74,6 +76,7 @@ def main(): ser.flushOutput() success = False + ts_start = time.time() for i in range(args.tries): try: print("Logging into %s via serial console [try %s]" % (port, i)) @@ -83,6 +86,9 @@ def main(): except Exception as except_inst: print("Login failure (try %s):" % (i, ), except_inst, file=sys.stderr) time.sleep(5) + if time.time() - ts_start > args.timeout: + print("Timeout reached waiting for login prompt", file=sys.stderr) + break if success: write_ser_line(ser, "") From e129b3c465f8a7744edd63fa0e1d66f4648d758f Mon Sep 17 00:00:00 2001 From: Chris Hofstaedtler Date: Sat, 17 Aug 2024 16:05:22 +0200 Subject: [PATCH 2/4] tests/serial-console-connection: automatically format defaults in --help --- tests/serial-console-connection | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/serial-console-connection b/tests/serial-console-connection index 7ba0c486..e21b64b6 100755 --- a/tests/serial-console-connection +++ b/tests/serial-console-connection @@ -6,20 +6,20 @@ import sys import time from pexpect import fdpexpect, TIMEOUT -parser = argparse.ArgumentParser(description='Connect to serial console ' + - 'to execute stuff') +parser = argparse.ArgumentParser( + description='Connect to serial console to execute stuff', + formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('--port', required=True, help='serial console device to connect ' + 'to (e.g. /dev/pts/X)') parser.add_argument('--hostname', default="buster", - help='hostname of the system for login process ' + - '(default: buster)') + help='hostname of the system for login process') parser.add_argument('--user', default="root", - help='user name to use for login (default: root)') + help='user name to use for login') parser.add_argument('--password', default="grml", - help='password for login (default: grml)') + help='password for login') parser.add_argument('--timeout', default="180", type=int, - help='Maximum time for finding the login prompt (seconds)') + help='Maximum time for finding the login prompt, in seconds') parser.add_argument('--tries', default="12", type=int, help='Number of retries for finding the login prompt') parser.add_argument('--poweroff', action="store_true", default=False, From 0e7cffd2773b3655895b57f53f7ec746bd505eaf Mon Sep 17 00:00:00 2001 From: Chris Hofstaedtler Date: Sat, 17 Aug 2024 16:07:03 +0200 Subject: [PATCH 3/4] tests/serial-console-connection: remove unused imports --- tests/serial-console-connection | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/serial-console-connection b/tests/serial-console-connection index e21b64b6..2cb63ad7 100755 --- a/tests/serial-console-connection +++ b/tests/serial-console-connection @@ -1,10 +1,9 @@ #!/usr/bin/env python import argparse -import pexpect import serial import sys import time -from pexpect import fdpexpect, TIMEOUT +from pexpect import fdpexpect parser = argparse.ArgumentParser( description='Connect to serial console to execute stuff', From 235670bd2058d17e2086b4d60e067b3d33fb4086 Mon Sep 17 00:00:00 2001 From: Chris Hofstaedtler Date: Sat, 17 Aug 2024 16:08:24 +0200 Subject: [PATCH 4/4] tests/serial-console-connection: reformat using black --- tests/serial-console-connection | 55 ++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/tests/serial-console-connection b/tests/serial-console-connection index 2cb63ad7..b44299a3 100755 --- a/tests/serial-console-connection +++ b/tests/serial-console-connection @@ -6,26 +6,38 @@ import time from pexpect import fdpexpect parser = argparse.ArgumentParser( - description='Connect to serial console to execute stuff', - formatter_class=argparse.ArgumentDefaultsHelpFormatter) -parser.add_argument('--port', required=True, - help='serial console device to connect ' + - 'to (e.g. /dev/pts/X)') -parser.add_argument('--hostname', default="buster", - help='hostname of the system for login process') -parser.add_argument('--user', default="root", - help='user name to use for login') -parser.add_argument('--password', default="grml", - help='password for login') -parser.add_argument('--timeout', default="180", type=int, - help='Maximum time for finding the login prompt, in seconds') -parser.add_argument('--tries', default="12", type=int, - help='Number of retries for finding the login prompt') -parser.add_argument('--poweroff', action="store_true", default=False, - help='send "poweroff" command after all other commands') -parser.add_argument('command', nargs='+', - help='command to execute after logging in') - + description="Connect to serial console to execute stuff", + formatter_class=argparse.ArgumentDefaultsHelpFormatter, +) +parser.add_argument( + "--port", + required=True, + help="serial console device to connect " + "to (e.g. /dev/pts/X)", +) +parser.add_argument( + "--hostname", default="buster", help="hostname of the system for login process" +) +parser.add_argument("--user", default="root", help="user name to use for login") +parser.add_argument("--password", default="grml", help="password for login") +parser.add_argument( + "--timeout", + default="180", + type=int, + help="Maximum time for finding the login prompt, in seconds", +) +parser.add_argument( + "--tries", + default="12", + type=int, + help="Number of retries for finding the login prompt", +) +parser.add_argument( + "--poweroff", + action="store_true", + default=False, + help='send "poweroff" command after all other commands', +) +parser.add_argument("command", nargs="+", help="command to execute after logging in") def print_ser_lines(ser): @@ -83,7 +95,7 @@ def main(): success = True break except Exception as except_inst: - print("Login failure (try %s):" % (i, ), except_inst, file=sys.stderr) + print("Login failure (try %s):" % (i,), except_inst, file=sys.stderr) time.sleep(5) if time.time() - ts_start > args.timeout: print("Timeout reached waiting for login prompt", file=sys.stderr) @@ -106,5 +118,6 @@ def main(): if not success: sys.exit(1) + if __name__ == "__main__": main()