1be5c7
From 12f596b66d577eb92f154fadf734d058dd0756d6 Mon Sep 17 00:00:00 2001
1be5c7
From: Hanna Reitz <hreitz@redhat.com>
1be5c7
Date: Wed, 16 Feb 2022 11:53:54 +0100
1be5c7
Subject: [PATCH 23/24] iotests: Allow using QMP with the QSD
1be5c7
1be5c7
RH-Author: Hanna Reitz <hreitz@redhat.com>
1be5c7
RH-MergeRequest: 189: block: Make bdrv_refresh_limits() non-recursive
1be5c7
RH-Commit: [2/3] 55bee4690a2e02d3be9f2bd68f2d244d0a36743b
1be5c7
RH-Bugzilla: 2072932
1be5c7
RH-Acked-by: Eric Blake <eblake@redhat.com>
1be5c7
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
1be5c7
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
1be5c7
1be5c7
Add a parameter to optionally open a QMP connection when creating a
1be5c7
QemuStorageDaemon instance.
1be5c7
1be5c7
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
1be5c7
Message-Id: <20220216105355.30729-3-hreitz@redhat.com>
1be5c7
Reviewed-by: Eric Blake <eblake@redhat.com>
1be5c7
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
1be5c7
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
1be5c7
(cherry picked from commit ec88eed8d14088b36a3495710368b8d1a3c33420)
1be5c7
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
1be5c7
---
1be5c7
 tests/qemu-iotests/iotests.py | 32 +++++++++++++++++++++++++++++++-
1be5c7
 1 file changed, 31 insertions(+), 1 deletion(-)
1be5c7
1be5c7
diff --git a/tests/qemu-iotests/iotests.py b/tests/qemu-iotests/iotests.py
1be5c7
index a51b5ce8cd..2ef493755c 100644
1be5c7
--- a/tests/qemu-iotests/iotests.py
1be5c7
+++ b/tests/qemu-iotests/iotests.py
1be5c7
@@ -38,6 +38,7 @@
1be5c7
 
1be5c7
 from qemu.machine import qtest
1be5c7
 from qemu.qmp import QMPMessage
1be5c7
+from qemu.aqmp.legacy import QEMUMonitorProtocol
1be5c7
 
1be5c7
 # Use this logger for logging messages directly from the iotests module
1be5c7
 logger = logging.getLogger('qemu.iotests')
1be5c7
@@ -315,14 +316,30 @@ def cmd(self, cmd):
1be5c7
 
1be5c7
 
1be5c7
 class QemuStorageDaemon:
1be5c7
-    def __init__(self, *args: str, instance_id: str = 'a'):
1be5c7
+    _qmp: Optional[QEMUMonitorProtocol] = None
1be5c7
+    _qmpsock: Optional[str] = None
1be5c7
+    # Python < 3.8 would complain if this type were not a string literal
1be5c7
+    # (importing `annotations` from `__future__` would work; but not on <= 3.6)
1be5c7
+    _p: 'Optional[subprocess.Popen[bytes]]' = None
1be5c7
+
1be5c7
+    def __init__(self, *args: str, instance_id: str = 'a', qmp: bool = False):
1be5c7
         assert '--pidfile' not in args
1be5c7
         self.pidfile = os.path.join(test_dir, f'qsd-{instance_id}-pid')
1be5c7
         all_args = [qsd_prog] + list(args) + ['--pidfile', self.pidfile]
1be5c7
 
1be5c7
+        if qmp:
1be5c7
+            self._qmpsock = os.path.join(sock_dir, f'qsd-{instance_id}.sock')
1be5c7
+            all_args += ['--chardev',
1be5c7
+                         f'socket,id=qmp-sock,path={self._qmpsock}',
1be5c7
+                         '--monitor', 'qmp-sock']
1be5c7
+
1be5c7
+            self._qmp = QEMUMonitorProtocol(self._qmpsock, server=True)
1be5c7
+
1be5c7
         # Cannot use with here, we want the subprocess to stay around
1be5c7
         # pylint: disable=consider-using-with
1be5c7
         self._p = subprocess.Popen(all_args)
1be5c7
+        if self._qmp is not None:
1be5c7
+            self._qmp.accept()
1be5c7
         while not os.path.exists(self.pidfile):
1be5c7
             if self._p.poll() is not None:
1be5c7
                 cmd = ' '.join(all_args)
1be5c7
@@ -337,11 +354,24 @@ def __init__(self, *args: str, instance_id: str = 'a'):
1be5c7
 
1be5c7
         assert self._pid == self._p.pid
1be5c7
 
1be5c7
+    def qmp(self, cmd: str, args: Optional[Dict[str, object]] = None) \
1be5c7
+            -> QMPMessage:
1be5c7
+        assert self._qmp is not None
1be5c7
+        return self._qmp.cmd(cmd, args)
1be5c7
+
1be5c7
     def stop(self, kill_signal=15):
1be5c7
         self._p.send_signal(kill_signal)
1be5c7
         self._p.wait()
1be5c7
         self._p = None
1be5c7
 
1be5c7
+        if self._qmp:
1be5c7
+            self._qmp.close()
1be5c7
+
1be5c7
+        if self._qmpsock is not None:
1be5c7
+            try:
1be5c7
+                os.remove(self._qmpsock)
1be5c7
+            except OSError:
1be5c7
+                pass
1be5c7
         try:
1be5c7
             os.remove(self.pidfile)
1be5c7
         except OSError:
1be5c7
-- 
1be5c7
2.35.3
1be5c7