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