about summary refs log tree commit diff
path: root/nixos/lib
diff options
context:
space:
mode:
authorJacek Galowicz <jacek@galowicz.de>2024-01-26 08:27:06 +0100
committerGitHub <noreply@github.com>2024-01-26 08:27:06 +0100
commitd6318f4a861a19cb28f3ba96b7d1342587e5e294 (patch)
tree95b6a753efd40654864de42dde93655155f210d2 /nixos/lib
parent7785103869b8c4d5493fe47c4f13c6791f6c5bd5 (diff)
parent1196ae6e6bdc3070402fd0875f8f5b69cb4c4a12 (diff)
downloadnixlib-d6318f4a861a19cb28f3ba96b7d1342587e5e294.tar
nixlib-d6318f4a861a19cb28f3ba96b7d1342587e5e294.tar.gz
nixlib-d6318f4a861a19cb28f3ba96b7d1342587e5e294.tar.bz2
nixlib-d6318f4a861a19cb28f3ba96b7d1342587e5e294.tar.lz
nixlib-d6318f4a861a19cb28f3ba96b7d1342587e5e294.tar.xz
nixlib-d6318f4a861a19cb28f3ba96b7d1342587e5e294.tar.zst
nixlib-d6318f4a861a19cb28f3ba96b7d1342587e5e294.zip
Merge pull request #262772 from RaitoBezarius/qemu-vm/wait-for-event
nixos/lib/test-driver: add `wait_for_qmp_event`
Diffstat (limited to 'nixos/lib')
-rw-r--r--nixos/lib/test-driver/test_driver/machine.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/nixos/lib/test-driver/test_driver/machine.py b/nixos/lib/test-driver/test_driver/machine.py
index da60b669fa27..f526325b8a2c 100644
--- a/nixos/lib/test-driver/test_driver/machine.py
+++ b/nixos/lib/test-driver/test_driver/machine.py
@@ -768,6 +768,30 @@ class Machine:
             self.booted = False
             self.connected = False
 
+    def wait_for_qmp_event(self, event_filter: Callable[[dict[str, Any]], bool], timeout: int = 60 * 10) -> dict[str, Any]:
+        """
+        Wait for a QMP event which you can filter with the `event_filter` function.
+        The function takes as an input a dictionary of the event and if it returns True, we return that event,
+        if it does not, we wait for the next event and retry.
+
+        It will skip all events received in the meantime, if you want to keep them,
+        you have to do the bookkeeping yourself and store them somewhere.
+
+        By default, it will wait up to 10 minutes, `timeout` is in seconds.
+        """
+        if self.qmp_client is None:
+            raise RuntimeError('QMP API is not ready yet, is the VM ready?')
+
+        start = time.time()
+        while True:
+            evt = self.qmp_client.wait_for_event(timeout=timeout)
+            if event_filter(evt):
+                return evt
+
+            elapsed = time.time() - start
+            if elapsed >= timeout:
+                raise TimeoutError
+
     def get_tty_text(self, tty: str) -> str:
         status, output = self.execute(
             f"fold -w$(stty -F /dev/tty{tty} size | "