From 6239c0546ab0d88f88929666d079e49a8d637658 Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Fri, 9 Dec 2022 16:07:27 +0100 Subject: [PATCH] test.expect(): Add support for cmd keyword argument expect now takes a cmd argument, which defaults to None, and which runs the command over the given connection before it takes to waiting for a matching regex to be sent. Signed-off-by: Jan Lindemann --- src/python/devtest/os/test/test.py | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/python/devtest/os/test/test.py b/src/python/devtest/os/test/test.py index 0166a15..045050c 100644 --- a/src/python/devtest/os/test/test.py +++ b/src/python/devtest/os/test/test.py @@ -8,11 +8,15 @@ from ..misc import * from ..Connection import Connection from ..Connections import Connections -async def expect(conn, regex=None, subject=None, act_timeout=0.1, total_timeout=None): # export +async def expect(conn, cmd=None, regex=None, subject=None, act_timeout=0.1, total_timeout=None): # export # regex is a keyword-argument so that other arguments to support other # checks can be added later if regex is None: raise Exception("passed empty pattern to test.expect()") + if cmd is not None: + if isinstance(cmd, str) and not cmd.endswith('\n'): + cmd += '\n' + await conn.write(cmd) deadline = None if total_timeout is not None: deadline = time.time() + total_timeout @@ -66,16 +70,29 @@ async def cmds_exec(console, cmds, log_act=None, echo_cmd=False): # export cmdline = c[0] e = None if len(c) < 2 else c[1] timeout = 200 if len(c) < 3 else c[2] - rr = await cmd_exec(console, cmdline, act_timeout=1, log_act=log_act, echo_cmd=echo_cmd) - if rr is None: - return 'Failed to run command "{}"'.format(cmdline) - if e is not None: - if find_match(rr, e): + if cmdline is None: + if e is None: continue slog(DEBUG, 'Waiting for: "{}"'.format(e)) rr = await expect(console, regex=e, act_timeout=timeout) if rr is None: return 'Timed out waiting for: "{}"'.format(e) + elif isinstance(cmdline, str): + rr = await cmd_exec(console, cmdline, act_timeout=1, log_act=log_act, echo_cmd=echo_cmd) + if rr is None: + return 'Failed to run command "{}"'.format(cmdline) + if e is not None: + if find_match(rr, e): + continue + slog(DEBUG, 'Waiting for: "{}"'.format(e)) + rr = await expect(console, regex=e, act_timeout=timeout) + if rr is None: + return 'Timed out waiting for: "{}"'.format(e) + else: + rr = cmdline(console, *c[1:]) + if ret is not None: + return ret + return None async def grep_log(conn, regex, log_glob='/var/log/messages', act_timeout=0.1, total_timeout=None): # export