qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: "Daniel P. Berrangé" <berrange@redhat.com>, qemu-devel@nongnu.org
Cc: "Thomas Huth" <thuth@redhat.com>,
	"Ani Sinha" <anisinha@redhat.com>,
	"Alex Bennée" <alex.bennee@linaro.org>,
	"Philippe Mathieu-Daudé" <philmd@linaro.org>,
	"Peter Maydell" <peter.maydell@linaro.org>,
	"Cédric Le Goater" <clg@redhat.com>
Subject: Re: [PATCH 13/15] tests/functional: rewrite console handling to be bytewise
Date: Tue, 19 Nov 2024 18:11:10 +0100	[thread overview]
Message-ID: <beb4abeb-299a-4d4a-a253-3e65a41edcc9@redhat.com> (raw)
In-Reply-To: <20241119150519.1123365-14-berrange@redhat.com>

On 11/19/24 16:05, Daniel P. Berrangé wrote:
> The console interaction that waits for predicted strings uses
> readline(), and thus is only capable of waiting for strings
> that are followed by a newline.
> 
> This is inconvenient when needing to match on some things,
> particularly login prompts, or shell prompts, causing tests
> to use time.sleep(...) instead, which is unreliable.
> 
> Switch to reading the console 1 byte at a time, comparing
> against the success/failure messages until we see a match,
> regardless of whether a newline is encountered.
> 
> The success/failure comparisons are done with the python bytes
> type, rather than strings, to avoid the problem of needing to
> decode partially received multibyte utf8 characters.
> 
> Heavily inspired by a patch proposed by Cédric, but written
> again to work in bytes, rather than strings.
> 
> Co-developed-by: Cédric Le Goater <clg@redhat.com>
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
>   tests/functional/qemu_test/cmd.py | 63 +++++++++++++++++++++++--------
>   1 file changed, 48 insertions(+), 15 deletions(-)
> 
> diff --git a/tests/functional/qemu_test/cmd.py b/tests/functional/qemu_test/cmd.py
> index 76a48064cd..91267a087f 100644
> --- a/tests/functional/qemu_test/cmd.py
> +++ b/tests/functional/qemu_test/cmd.py
> @@ -78,15 +78,58 @@ def run_cmd(args):
>   def is_readable_executable_file(path):
>       return os.path.isfile(path) and os.access(path, os.R_OK | os.X_OK)
>   
> +def _console_readline(test, vm, success, failure):
> +    msg = bytes([])
> +    done = False
> +    while True:
> +        c = vm.console_socket.recv(1)
> +        if c is None:
> +            done = True
> +            test.fail(
> +                f"EOF in console, expected '{success}'")
> +            break
> +        msg += c
> +
> +        if success is None or success in msg:

As an optimization, you could use msg.endswith(success) and 
msg.endswith(failure), which would avoid the most blatant cases of 
O(n^2) behavior.

More important, I think "if success is None" should not be here, because 
it will exit after one char.  Instead...

> +            done = True
> +            break
> +        if failure and failure in msg:
> +            done = True
> +            vm.console_socket.close()
> +            test.fail(
> +                f"'{failure}' found in console, expected '{success}'")
> +
> +        if c == b'\n':

Here you can put

                done = success is None

Paolo

> +            break
> +
> +    console_logger = logging.getLogger('console')
> +    try:
> +        console_logger.debug(msg.decode().strip())
> +    except:
> +        console_logger.debug(msg)
> +
> +    return done
> +
>   def _console_interaction(test, success_message, failure_message,
>                            send_string, keep_sending=False, vm=None):
>       assert not keep_sending or send_string
>       if vm is None:
>           vm = test.vm
> -    console = vm.console_file
> -    console_logger = logging.getLogger('console')
> +
>       test.log.debug(f"Console interaction success:'{success_message}' " +
>                      f"failure:'{failure_message}' send:'{send_string}'")
> +
> +    # We'll process console in bytes, to avoid having to
> +    # deal with unicode decode errors from receiving
> +    # partial utf8 byte sequences
> +    success_message_b = None
> +    if success_message is not None:
> +        success_message_b = success_message.encode()
> +
> +    failure_message_b = None
> +    if failure_message is not None:
> +        failure_message_b = failure_message.encode()
> +
>       while True:
>           if send_string:
>               vm.console_socket.sendall(send_string.encode())
> @@ -99,20 +142,10 @@ def _console_interaction(test, success_message, failure_message,
>                   break
>               continue
>   
> -        try:
> -            msg = console.readline().decode().strip()
> -        except UnicodeDecodeError:
> -            msg = None
> -        if not msg:
> -            continue
> -        console_logger.debug(msg)
> -        if success_message is None or success_message in msg:
> +        if _console_readline(test, vm,
> +                             success_message_b,
> +                             failure_message_b):
>               break
> -        if failure_message and failure_message in msg:
> -            console.close()
> -            fail = 'Failure message found in console: "%s". Expected: "%s"' % \
> -                    (failure_message, success_message)
> -            test.fail(fail)
>   
>   def interrupt_interactive_console_until_pattern(test, success_message,
>                                                   failure_message=None,



  reply	other threads:[~2024-11-19 17:11 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-19 15:05 [PATCH 00/15] test/functional: improve functional test debugging & fix tuxrun Daniel P. Berrangé
2024-11-19 15:05 ` [PATCH 01/15] tests/functional: fix mips64el test to honour workdir Daniel P. Berrangé
2024-11-19 15:35   ` Alex Bennée
2024-11-19 17:08   ` Philippe Mathieu-Daudé
2024-11-19 15:05 ` [PATCH 02/15] tests/functional: automatically clean up scratch files after tests Daniel P. Berrangé
2024-11-19 16:21   ` Alex Bennée
2024-11-19 17:28     ` Thomas Huth
2024-11-19 15:05 ` [PATCH 03/15] tests/functional: remove "AVOCADO" from env variable name Daniel P. Berrangé
2024-11-19 16:22   ` Alex Bennée
2024-11-19 17:09   ` Philippe Mathieu-Daudé
2024-11-19 15:05 ` [PATCH 04/15] tests/functional: remove todo wrt avocado.utils.wait_for Daniel P. Berrangé
2024-11-19 16:37   ` Alex Bennée
2024-11-19 15:05 ` [PATCH 05/15] tests/functional: remove leftover :avocado: tags Daniel P. Berrangé
2024-11-19 16:37   ` Alex Bennée
2024-11-19 15:05 ` [PATCH 06/15] tests/functional: remove obsolete reference to avocado bug Daniel P. Berrangé
2024-11-19 16:39   ` Alex Bennée
2024-11-19 15:05 ` [PATCH 07/15] tests/functional: remove comments talking about avocado Daniel P. Berrangé
2024-11-19 17:32   ` Thomas Huth
2024-11-19 15:05 ` [PATCH 08/15] tests/functional: honour self.workdir in ACPI bits tests Daniel P. Berrangé
2024-11-19 17:09   ` Alex Bennée
2024-11-19 15:05 ` [PATCH 09/15] tests/functional: put QEMUMachine logs in testcase log directory Daniel P. Berrangé
2024-11-19 17:10   ` Alex Bennée
2024-11-19 15:05 ` [PATCH 10/15] tests/functional: honour requested test VM name in QEMUMachine Daniel P. Berrangé
2024-11-19 17:40   ` Thomas Huth
2024-11-19 15:05 ` [PATCH 11/15] tests/functional: enable debug logging for QEMUMachine Daniel P. Berrangé
2024-11-21  6:52   ` Thomas Huth
2024-11-19 15:05 ` [PATCH 12/15] tests/functional: logs details of console interaction operations Daniel P. Berrangé
2024-11-21  6:58   ` Thomas Huth
2024-11-19 15:05 ` [PATCH 13/15] tests/functional: rewrite console handling to be bytewise Daniel P. Berrangé
2024-11-19 17:11   ` Paolo Bonzini [this message]
2024-11-19 18:54     ` Daniel P. Berrangé
2024-11-19 19:26       ` Paolo Bonzini
2024-11-19 15:05 ` [PATCH 14/15] tests/functional: remove time.sleep usage from tuxrun tests Daniel P. Berrangé
2024-11-19 15:05 ` [PATCH 15/15] tests/functional: add a QMP backdoor for debugging stalled tests Daniel P. Berrangé
2024-11-21  7:01   ` Thomas Huth
2024-11-19 17:59 ` [PATCH 00/15] test/functional: improve functional test debugging & fix tuxrun Cédric Le Goater

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=beb4abeb-299a-4d4a-a253-3e65a41edcc9@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=alex.bennee@linaro.org \
    --cc=anisinha@redhat.com \
    --cc=berrange@redhat.com \
    --cc=clg@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=philmd@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=thuth@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).