Skip to content

Commit

Permalink
Merge pull request #283 from grml/zeha/timeout
Browse files Browse the repository at this point in the history
tests/serial-console-connection: add timeout (default to 3min)
  • Loading branch information
zeha committed Aug 17, 2024
2 parents 4c31c18 + 235670b commit 4dfb0d3
Showing 1 changed file with 41 additions and 23 deletions.
64 changes: 41 additions & 23 deletions tests/serial-console-connection
Original file line number Diff line number Diff line change
@@ -1,30 +1,43 @@
#!/usr/bin/env python
import argparse
import pexpect
import serial
import sys
import time
from pexpect import fdpexpect, TIMEOUT

parser = argparse.ArgumentParser(description='Connect to serial console ' +
'to execute stuff')
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)')
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('--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')

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")


def print_ser_lines(ser):
Expand Down Expand Up @@ -74,15 +87,19 @@ 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))
login(ser, hostname, user, password)
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)
break

if success:
write_ser_line(ser, "")
Expand All @@ -101,5 +118,6 @@ def main():
if not success:
sys.exit(1)


if __name__ == "__main__":
main()

0 comments on commit 4dfb0d3

Please sign in to comment.