cryptospore / rpms / qemu-kvm

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