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