[v6,2/4] tests/vm: add console_consume helper
diff mbox series

Message ID 20191031085306.28888-3-kraxel@redhat.com
State New
Headers show
Series
  • tests/vm: netbsd autoinstall
Related show

Commit Message

Gerd Hoffmann Oct. 31, 2019, 8:53 a.m. UTC
Helper function to read all console output.

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
---
 tests/vm/basevm.py | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

Comments

Philippe Mathieu-Daudé Oct. 31, 2019, 2:05 p.m. UTC | #1
On 10/31/19 9:53 AM, Gerd Hoffmann wrote:
> Helper function to read all console output.
> 
> Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
> ---
>   tests/vm/basevm.py | 19 +++++++++++++++++++
>   1 file changed, 19 insertions(+)
> 
> diff --git a/tests/vm/basevm.py b/tests/vm/basevm.py
> index 2929de23aa..086bfb2c66 100755
> --- a/tests/vm/basevm.py
> +++ b/tests/vm/basevm.py
> @@ -242,6 +242,25 @@ class BaseVM(object):
>               return False
>           return True
>   
> +    def console_consume(self):
> +        vm = self._guest
> +        output = ""
> +        vm.console_socket.setblocking(0)
> +        while True:
> +            try:
> +                chars = vm.console_socket.recv(1)
> +            except:
> +                break
> +            output += chars.decode("latin1")

I don't know enough decode(), but I'm very happy with the rest of
the patch, thanks a lot for fixing this long standing issue Gerd!

Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>

> +            if "\r" in output or "\n" in output:
> +                lines = re.split("[\r\n]", output)
> +                output = lines.pop()
> +                if self.debug:
> +                    self.console_log("\n".join(lines))
> +        if self.debug:
> +            self.console_log(output)
> +        vm.console_socket.setblocking(1)
> +
>       def console_send(self, command):
>           vm = self._guest
>           if self.debug:
>

Patch
diff mbox series

diff --git a/tests/vm/basevm.py b/tests/vm/basevm.py
index 2929de23aa..086bfb2c66 100755
--- a/tests/vm/basevm.py
+++ b/tests/vm/basevm.py
@@ -242,6 +242,25 @@  class BaseVM(object):
             return False
         return True
 
+    def console_consume(self):
+        vm = self._guest
+        output = ""
+        vm.console_socket.setblocking(0)
+        while True:
+            try:
+                chars = vm.console_socket.recv(1)
+            except:
+                break
+            output += chars.decode("latin1")
+            if "\r" in output or "\n" in output:
+                lines = re.split("[\r\n]", output)
+                output = lines.pop()
+                if self.debug:
+                    self.console_log("\n".join(lines))
+        if self.debug:
+            self.console_log(output)
+        vm.console_socket.setblocking(1)
+
     def console_send(self, command):
         vm = self._guest
         if self.debug: