qemu-rust.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Alex Bennée" <alex.bennee@linaro.org>
To: qemu-devel@nongnu.org
Cc: "Peter Maydell" <peter.maydell@linaro.org>,
	"Philippe Mathieu-Daudé" <philmd@linaro.org>,
	"Michael S. Tsirkin" <mst@redhat.com>,
	"Daniel Henrique Barboza" <danielhb413@gmail.com>,
	"Pierrick Bouvier" <pierrick.bouvier@linaro.org>,
	"Harsh Prateek Bora" <harshpb@linux.ibm.com>,
	"Marcel Apfelbaum" <marcel.apfelbaum@gmail.com>,
	"Jiaxun Yang" <jiaxun.yang@flygoat.com>,
	"Halil Pasic" <pasic@linux.ibm.com>,
	"Christian Borntraeger" <borntraeger@linux.ibm.com>,
	qemu-ppc@nongnu.org,
	"Daniel Henrique Barboza" <dbarboza@ventanamicro.com>,
	"Alexandre Iooss" <erdnaxe@crans.org>,
	qemu-riscv@nongnu.org, "Thomas Huth" <huth@tuxfamily.org>,
	"Bernhard Beschow" <shentey@gmail.com>,
	"Thomas Huth" <thuth@redhat.com>,
	"Eric Farman" <farman@linux.ibm.com>,
	"Bin Meng" <bmeng.cn@gmail.com>,
	qemu-s390x@nongnu.org,
	"Niek Linnenbank" <nieklinnenbank@gmail.com>,
	qemu-arm@nongnu.org, "Alex Bennée" <alex.bennee@linaro.org>,
	"John Snow" <jsnow@redhat.com>,
	"Wainer dos Santos Moschetta" <wainersm@redhat.com>,
	"Cleber Rosa" <crosa@redhat.com>,
	qemu-rust@nongnu.org, "Nicholas Piggin" <npiggin@gmail.com>,
	"Daniel P. Berrangé" <berrange@redhat.com>,
	"Palmer Dabbelt" <palmer@dabbelt.com>,
	"Markus Armbruster" <armbru@redhat.com>,
	"Weiwei Li" <liwei1518@gmail.com>,
	"Mahmoud Mandour" <ma.mandourr@gmail.com>,
	"Mark Cave-Ayland" <mark.cave-ayland@ilande.co.uk>,
	"Liu Zhiwei" <zhiwei_liu@linux.alibaba.com>,
	"Aurelien Jarno" <aurelien@aurel32.net>,
	"Ani Sinha" <anisinha@redhat.com>,
	"Alistair Francis" <alistair.francis@wdc.com>,
	"Cédric Le Goater" <clg@redhat.com>
Subject: [PATCH 15/39] tests/functional: rewrite console handling to be bytewise
Date: Thu, 21 Nov 2024 16:57:42 +0000	[thread overview]
Message-ID: <20241121165806.476008-16-alex.bennee@linaro.org> (raw)
In-Reply-To: <20241121165806.476008-1-alex.bennee@linaro.org>

From: Daniel P. Berrangé <berrange@redhat.com>

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>
Message-Id: <20241121154218.1423005-16-berrange@redhat.com>
Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
---
 tests/functional/qemu_test/cmd.py | 79 +++++++++++++++++++++++++------
 1 file changed, 64 insertions(+), 15 deletions(-)

diff --git a/tests/functional/qemu_test/cmd.py b/tests/functional/qemu_test/cmd.py
index f6c4e4dda1..11c8334a7c 100644
--- a/tests/functional/qemu_test/cmd.py
+++ b/tests/functional/qemu_test/cmd.py
@@ -78,6 +78,54 @@ 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)
 
+# @test: functional test to fail if @failure is seen
+# @vm: the VM whose console to process
+# @success: a non-None string to look for
+# @failure: a string to look for that triggers test failure, or None
+#
+# Read up to 1 line of text from @vm, looking for @success
+# and optionally @failure.
+#
+# If @success or @failure are seen, immediately return True,
+# even if end of line is not yet seen. ie remainder of the
+# line is left unread.
+#
+# If end of line is seen, with neither @success or @failure
+# return False
+#
+# If @failure is seen, then mark @test as failed
+def _console_read_line_until_match(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 in msg:
+            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':
+            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
@@ -85,11 +133,22 @@ def _console_interaction(test, success_message, failure_message,
 
     if vm is None:
         vm = test.vm
-    console = vm.console_file
-    console_logger = logging.getLogger('console')
+
     test.log.debug(
         f"Console interaction: success_msg='{success_message}' " +
         f"failure_msg='{failure_message}' send_string='{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())
@@ -102,20 +161,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 in msg:
+        if _console_read_line_until_match(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,
-- 
2.39.5



  parent reply	other threads:[~2024-11-21 17:08 UTC|newest]

Thread overview: 78+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-21 16:57 [PATCH 00/39] maintainer updates for -rc2 pre-PR Alex Bennée
2024-11-21 16:57 ` [PATCH 01/39] tests/functional: fix mips64el test to honour workdir Alex Bennée
2024-11-21 16:57 ` [PATCH 02/39] tests/functional: automatically clean up scratch files after tests Alex Bennée
2024-11-21 16:57 ` [PATCH 03/39] tests/functional: remove "AVOCADO" from env variable name Alex Bennée
2024-11-21 16:57 ` [PATCH 04/39] tests/functional: remove todo wrt avocado.utils.wait_for Alex Bennée
2024-11-21 16:57 ` [PATCH 05/39] tests/functional: remove leftover :avocado: tags Alex Bennée
2024-11-21 16:57 ` [PATCH 06/39] tests/functional: remove obsolete reference to avocado bug Alex Bennée
2024-11-21 16:57 ` [PATCH 07/39] tests/functional: remove comments talking about avocado Alex Bennée
2024-11-21 19:03   ` Pierrick Bouvier
2024-11-21 16:57 ` [PATCH 08/39] tests/functional: honour self.workdir in ACPI bits tests Alex Bennée
2024-11-21 16:57 ` [PATCH 09/39] tests/functional: put QEMUMachine logs in testcase log directory Alex Bennée
2024-11-21 16:57 ` [PATCH 10/39] tests/functional: honour requested test VM name in QEMUMachine Alex Bennée
2024-11-21 16:57 ` [PATCH 11/39] tests/functional: enable debug logging for QEMUMachine Alex Bennée
2024-11-21 19:02   ` Thomas Huth
2024-11-21 16:57 ` [PATCH 12/39] tests/functional: logs details of console interaction operations Alex Bennée
2024-11-21 19:03   ` Thomas Huth
2024-11-21 16:57 ` [PATCH 13/39] tests/functional: don't try to wait for the empty string Alex Bennée
2024-11-21 19:04   ` Thomas Huth
2024-11-21 16:57 ` [PATCH 14/39] tests/functional: require non-NULL success_message for console wait Alex Bennée
2024-11-21 19:24   ` Thomas Huth
2024-11-21 16:57 ` Alex Bennée [this message]
2024-11-21 16:57 ` [PATCH 16/39] tests/functional: remove time.sleep usage from tuxrun tests Alex Bennée
2024-11-21 19:39   ` Thomas Huth
2024-11-21 16:57 ` [PATCH 17/39] tests/functional: add a QMP backdoor for debugging stalled tests Alex Bennée
2024-11-21 19:41   ` Thomas Huth
2024-11-21 16:57 ` [PATCH 18/39] tests/functional: avoid accessing log_filename on earlier failures Alex Bennée
2024-11-22 10:19   ` Thomas Huth
2024-11-21 16:57 ` [PATCH 19/39] tests/functional: Convert the Avocado aarch64 tuxrun tests Alex Bennée
2024-11-21 16:57 ` [PATCH 20/39] tests/functional: update the arm " Alex Bennée
2024-11-22 10:22   ` Thomas Huth
2024-11-21 16:57 ` [PATCH 21/39] tests/functional: update the aarch64 " Alex Bennée
2024-11-21 19:08   ` Thomas Huth
2024-11-21 16:57 ` [PATCH 22/39] tests/functional: update the i386 " Alex Bennée
2024-11-22 11:17   ` Thomas Huth
2024-11-21 16:57 ` [PATCH 23/39] tests/functional: add a m68k " Alex Bennée
2024-11-21 19:07   ` Thomas Huth
2024-11-22 10:16   ` Thomas Huth
2024-11-21 16:57 ` [PATCH 24/39] tests/functional: update the mips32 " Alex Bennée
2024-11-22 10:08   ` Thomas Huth
2024-11-21 16:57 ` [PATCH 25/39] tests/functional: update the mips32el " Alex Bennée
2024-11-22 10:09   ` Thomas Huth
2024-11-21 16:57 ` [PATCH 26/39] tests/functional: update the mips64 " Alex Bennée
2024-11-22 10:09   ` Thomas Huth
2024-11-21 16:57 ` [PATCH 27/39] tests/functional: update the mips64el " Alex Bennée
2024-11-22 10:11   ` Thomas Huth
2024-11-21 16:57 ` [PATCH 28/39] tests/functional: update the ppc32 " Alex Bennée
2024-11-22 10:42   ` Thomas Huth
2024-11-21 16:57 ` [PATCH 29/39] tests/functional: update the ppc64 " Alex Bennée
2024-11-22  9:52   ` Thomas Huth
2024-11-21 16:57 ` [PATCH 30/39] tests/functional: update the riscv32 " Alex Bennée
2024-11-22 10:58   ` Thomas Huth
2024-11-21 16:57 ` [PATCH 31/39] tests/functional: update the riscv64 " Alex Bennée
2024-11-22 11:02   ` Thomas Huth
2024-11-21 16:57 ` [PATCH 32/39] tests/functional: update the s390x " Alex Bennée
2024-11-22 11:00   ` Thomas Huth
2024-11-21 16:58 ` [PATCH 33/39] tests/functional: update the sparc64 " Alex Bennée
2024-11-22 11:02   ` Thomas Huth
2024-11-21 16:58 ` [PATCH 34/39] tests/functional: update the x86_64 " Alex Bennée
2024-11-22 11:15   ` Thomas Huth
2024-11-21 16:58 ` [PATCH 35/39] plugins: add missing export for qemu_plugin_num_vcpus Alex Bennée
2024-11-21 16:58 ` [PATCH 36/39] plugins: detect qemu plugin API symbols from header Alex Bennée
2024-11-21 16:58 ` [PATCH 37/39] plugins: eradicate qemu-plugins.symbols static file Alex Bennée
2024-11-21 16:58 ` [PATCH 38/39] rust/pl011: Fix range checks for device ID accesses Alex Bennée
2024-11-21 16:58 ` [PATCH 39/39] docs: explicitly permit a "commonly known identity" with SoB Alex Bennée
2024-11-26  4:55   ` Ani Sinha
2024-11-26 21:27   ` Michael S. Tsirkin
2024-11-21 17:19 ` [PATCH 00/39] maintainer updates for -rc2 pre-PR Peter Maydell
2024-11-21 17:31   ` Alex Bennée
2024-11-22 10:45     ` Thomas Huth
2024-11-23 15:29       ` Alex Bennée
2024-11-21 19:03 ` Cédric Le Goater
2024-11-21 19:10   ` Thomas Huth
2024-11-21 21:46     ` Cédric Le Goater
2024-11-22 10:47       ` Thomas Huth
2024-11-22 11:59         ` Cédric Le Goater
2024-11-22 12:32           ` Thomas Huth
2024-11-22 13:03             ` Cédric Le Goater
2024-11-22 10:14 ` Thomas Huth

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=20241121165806.476008-16-alex.bennee@linaro.org \
    --to=alex.bennee@linaro.org \
    --cc=alistair.francis@wdc.com \
    --cc=anisinha@redhat.com \
    --cc=armbru@redhat.com \
    --cc=aurelien@aurel32.net \
    --cc=berrange@redhat.com \
    --cc=bmeng.cn@gmail.com \
    --cc=borntraeger@linux.ibm.com \
    --cc=clg@redhat.com \
    --cc=crosa@redhat.com \
    --cc=danielhb413@gmail.com \
    --cc=dbarboza@ventanamicro.com \
    --cc=erdnaxe@crans.org \
    --cc=farman@linux.ibm.com \
    --cc=harshpb@linux.ibm.com \
    --cc=huth@tuxfamily.org \
    --cc=jiaxun.yang@flygoat.com \
    --cc=jsnow@redhat.com \
    --cc=liwei1518@gmail.com \
    --cc=ma.mandourr@gmail.com \
    --cc=marcel.apfelbaum@gmail.com \
    --cc=mark.cave-ayland@ilande.co.uk \
    --cc=mst@redhat.com \
    --cc=nieklinnenbank@gmail.com \
    --cc=npiggin@gmail.com \
    --cc=palmer@dabbelt.com \
    --cc=pasic@linux.ibm.com \
    --cc=peter.maydell@linaro.org \
    --cc=philmd@linaro.org \
    --cc=pierrick.bouvier@linaro.org \
    --cc=qemu-arm@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-ppc@nongnu.org \
    --cc=qemu-riscv@nongnu.org \
    --cc=qemu-rust@nongnu.org \
    --cc=qemu-s390x@nongnu.org \
    --cc=shentey@gmail.com \
    --cc=thuth@redhat.com \
    --cc=wainersm@redhat.com \
    --cc=zhiwei_liu@linux.alibaba.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).