diff mbox series

[v1,1/4] tests/functional: Introduce _console_read()

Message ID 20241112062806.838717-2-clg@redhat.com (mailing list archive)
State New
Headers show
Series tests/functional: Finish conversion of Aspeed tests | expand

Commit Message

Cédric Le Goater Nov. 12, 2024, 6:28 a.m. UTC
Interaction with the console has been a problem in our avocado
tests. In some cases, the expected string does not match in the
output, causing the test to fail with a timeout. These were worked
around by sleeping before reading the console and even with SSH
connections in some places.

To fix, process the console output char by char and not with
readline. This routine was largely inspired by console_wait() in
tests/vm/basevm.py.

Signed-off-by: Cédric Le Goater <clg@redhat.com>
---
 tests/functional/qemu_test/cmd.py | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

Comments

Thomas Huth Nov. 12, 2024, 7:11 a.m. UTC | #1
On 12/11/2024 07.28, Cédric Le Goater wrote:
> Interaction with the console has been a problem in our avocado
> tests. In some cases, the expected string does not match in the
> output, causing the test to fail with a timeout. These were worked
> around by sleeping before reading the console and even with SSH
> connections in some places.
> 
> To fix, process the console output char by char and not with
> readline. This routine was largely inspired by console_wait() in
> tests/vm/basevm.py.
> 
> Signed-off-by: Cédric Le Goater <clg@redhat.com>
> ---
>   tests/functional/qemu_test/cmd.py | 17 ++++++++++++++++-
>   1 file changed, 16 insertions(+), 1 deletion(-)
> 
> diff --git a/tests/functional/qemu_test/cmd.py b/tests/functional/qemu_test/cmd.py
> index cbabb1ceed3c..bb39857e6cae 100644
> --- a/tests/functional/qemu_test/cmd.py
> +++ b/tests/functional/qemu_test/cmd.py
> @@ -12,6 +12,7 @@
>   # later.  See the COPYING file in the top-level directory.
>   
>   import logging
> +import re
>   import os
>   import os.path
>   import subprocess
> @@ -78,6 +79,20 @@ 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_read(vm, expect, expectalt = None):
> +    output = ""
> +    while True:
> +        data = vm.console_socket.recv(1)
> +        if not data:
> +            break
> +        output += data.decode("latin1")
> +        if expect in output:
> +            break
> +        if "\r" in output or "\n" in output:
> +            lines = re.split("[\r\n]", output)
> +            output = lines.pop()
> +    return output
> +

The idea looks promising, but I just realized that this is breaking the 
console.log:

$ cat 
tests/functional/arm/test_arm_aspeed.AST2x00Machine.test_arm_ast2500_evb_buildroot/console.log 
  | wc -l
12

Without your patch, the log is way more verbose:

$ cat 
tests/functional/arm/test_arm_aspeed.AST2x00Machine.test_arm_ast2500_evb_buildroot/console.log 
  | wc -l
232

Could you please have another look?

  Thanks,
   Thomas
Cédric Le Goater Nov. 12, 2024, 8:01 a.m. UTC | #2
On 11/12/24 08:11, Thomas Huth wrote:
> On 12/11/2024 07.28, Cédric Le Goater wrote:
>> Interaction with the console has been a problem in our avocado
>> tests. In some cases, the expected string does not match in the
>> output, causing the test to fail with a timeout. These were worked
>> around by sleeping before reading the console and even with SSH
>> connections in some places.
>>
>> To fix, process the console output char by char and not with
>> readline. This routine was largely inspired by console_wait() in
>> tests/vm/basevm.py.
>>
>> Signed-off-by: Cédric Le Goater <clg@redhat.com>
>> ---
>>   tests/functional/qemu_test/cmd.py | 17 ++++++++++++++++-
>>   1 file changed, 16 insertions(+), 1 deletion(-)
>>
>> diff --git a/tests/functional/qemu_test/cmd.py b/tests/functional/qemu_test/cmd.py
>> index cbabb1ceed3c..bb39857e6cae 100644
>> --- a/tests/functional/qemu_test/cmd.py
>> +++ b/tests/functional/qemu_test/cmd.py
>> @@ -12,6 +12,7 @@
>>   # later.  See the COPYING file in the top-level directory.
>>   import logging
>> +import re
>>   import os
>>   import os.path
>>   import subprocess
>> @@ -78,6 +79,20 @@ 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_read(vm, expect, expectalt = None):
>> +    output = ""
>> +    while True:
>> +        data = vm.console_socket.recv(1)
>> +        if not data:
>> +            break
>> +        output += data.decode("latin1")
>> +        if expect in output:
>> +            break
>> +        if "\r" in output or "\n" in output:
>> +            lines = re.split("[\r\n]", output)
>> +            output = lines.pop()
>> +    return output
>> +
> 
> The idea looks promising, but I just realized that this is breaking the console.log:
> 
> $ cat tests/functional/arm/test_arm_aspeed.AST2x00Machine.test_arm_ast2500_evb_buildroot/console.log  | wc -l
> 12
> 
> Without your patch, the log is way more verbose:
> 
> $ cat tests/functional/arm/test_arm_aspeed.AST2x00Machine.test_arm_ast2500_evb_buildroot/console.log  | wc -l
> 232
> 
> Could you please have another look?

sure. I can add a log.debug.


Thanks,

C.



>
diff mbox series

Patch

diff --git a/tests/functional/qemu_test/cmd.py b/tests/functional/qemu_test/cmd.py
index cbabb1ceed3c..bb39857e6cae 100644
--- a/tests/functional/qemu_test/cmd.py
+++ b/tests/functional/qemu_test/cmd.py
@@ -12,6 +12,7 @@ 
 # later.  See the COPYING file in the top-level directory.
 
 import logging
+import re
 import os
 import os.path
 import subprocess
@@ -78,6 +79,20 @@  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_read(vm, expect, expectalt = None):
+    output = ""
+    while True:
+        data = vm.console_socket.recv(1)
+        if not data:
+            break
+        output += data.decode("latin1")
+        if expect in output:
+            break
+        if "\r" in output or "\n" in output:
+            lines = re.split("[\r\n]", output)
+            output = lines.pop()
+    return output
+
 def _console_interaction(test, success_message, failure_message,
                          send_string, keep_sending=False, vm=None):
     assert not keep_sending or send_string
@@ -98,7 +113,7 @@  def _console_interaction(test, success_message, failure_message,
             continue
 
         try:
-            msg = console.readline().decode().strip()
+            msg = _console_read(vm, success_message)
         except UnicodeDecodeError:
             msg = None
         if not msg: