60061b
From 34ffcd1a463bd3c1d36ed2f33dd6335b35b38460 Mon Sep 17 00:00:00 2001
60061b
From: Hanna Reitz <hreitz@redhat.com>
60061b
Date: Fri, 4 Feb 2022 12:10:09 +0100
60061b
Subject: [PATCH 3/6] iotests.py: Add QemuStorageDaemon class
60061b
60061b
RH-Author: Hanna Reitz <hreitz@redhat.com>
60061b
RH-MergeRequest: 117: block/nbd: Handle AioContext changes
60061b
RH-Commit: [3/6] 754fe76bc5e8be57f4b78f176531014c4a12b044
60061b
RH-Bugzilla: 2035185
60061b
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
60061b
RH-Acked-by: Eric Blake <eblake@redhat.com>
60061b
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
60061b
60061b
This is a rather simple class that allows creating a QSD instance
60061b
running in the background and stopping it when no longer needed.
60061b
60061b
The __del__ handler is a safety net for when something goes so wrong in
60061b
a test that e.g. the tearDown() method is not called (e.g. setUp()
60061b
launches the QSD, but then launching a VM fails).  We do not want the
60061b
QSD to continue running after the test has failed, so __del__() will
60061b
take care to kill it.
60061b
60061b
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
60061b
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
60061b
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
60061b
(cherry picked from commit 091dc7b2b5553a529bff9a7bf9ad3bc85bc5bdcd)
60061b
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
60061b
---
60061b
 tests/qemu-iotests/iotests.py | 40 +++++++++++++++++++++++++++++++++++
60061b
 1 file changed, 40 insertions(+)
60061b
60061b
diff --git a/tests/qemu-iotests/iotests.py b/tests/qemu-iotests/iotests.py
60061b
index 83bfedb902..a51b5ce8cd 100644
60061b
--- a/tests/qemu-iotests/iotests.py
60061b
+++ b/tests/qemu-iotests/iotests.py
60061b
@@ -72,6 +72,8 @@
60061b
 qemu_prog = os.environ.get('QEMU_PROG', 'qemu')
60061b
 qemu_opts = os.environ.get('QEMU_OPTIONS', '').strip().split(' ')
60061b
 
60061b
+qsd_prog = os.environ.get('QSD_PROG', 'qemu-storage-daemon')
60061b
+
60061b
 gdb_qemu_env = os.environ.get('GDB_OPTIONS')
60061b
 qemu_gdb = []
60061b
 if gdb_qemu_env:
60061b
@@ -312,6 +314,44 @@ def cmd(self, cmd):
60061b
         return self._read_output()
60061b
 
60061b
 
60061b
+class QemuStorageDaemon:
60061b
+    def __init__(self, *args: str, instance_id: str = 'a'):
60061b
+        assert '--pidfile' not in args
60061b
+        self.pidfile = os.path.join(test_dir, f'qsd-{instance_id}-pid')
60061b
+        all_args = [qsd_prog] + list(args) + ['--pidfile', self.pidfile]
60061b
+
60061b
+        # Cannot use with here, we want the subprocess to stay around
60061b
+        # pylint: disable=consider-using-with
60061b
+        self._p = subprocess.Popen(all_args)
60061b
+        while not os.path.exists(self.pidfile):
60061b
+            if self._p.poll() is not None:
60061b
+                cmd = ' '.join(all_args)
60061b
+                raise RuntimeError(
60061b
+                    'qemu-storage-daemon terminated with exit code ' +
60061b
+                    f'{self._p.returncode}: {cmd}')
60061b
+
60061b
+            time.sleep(0.01)
60061b
+
60061b
+        with open(self.pidfile, encoding='utf-8') as f:
60061b
+            self._pid = int(f.read().strip())
60061b
+
60061b
+        assert self._pid == self._p.pid
60061b
+
60061b
+    def stop(self, kill_signal=15):
60061b
+        self._p.send_signal(kill_signal)
60061b
+        self._p.wait()
60061b
+        self._p = None
60061b
+
60061b
+        try:
60061b
+            os.remove(self.pidfile)
60061b
+        except OSError:
60061b
+            pass
60061b
+
60061b
+    def __del__(self):
60061b
+        if self._p is not None:
60061b
+            self.stop(kill_signal=9)
60061b
+
60061b
+
60061b
 def qemu_nbd(*args):
60061b
     '''Run qemu-nbd in daemon mode and return the parent's exit code'''
60061b
     return subprocess.call(qemu_nbd_args + ['--fork'] + list(args))
60061b
-- 
60061b
2.27.0
60061b