thebeanogamer / rpms / qemu-kvm

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