aboutsummaryrefslogtreecommitdiff
path: root/tests/qemu-iotests/iotests.py
diff options
context:
space:
mode:
authorNir Soffer <nirsof@gmail.com>2020-07-28 00:58:44 +0300
committerEric Blake <eblake@redhat.com>2020-07-28 09:54:43 -0500
commitb7719bcad2e92bab5aae3166fa9011f127e6ee49 (patch)
treee0b2b32df18a5cdb90e4ac047ae768984e04485f /tests/qemu-iotests/iotests.py
parenta2b333c01880f56056d50c238834d62e32001e54 (diff)
iotests: Make qemu_nbd_popen() a contextmanager
Instead of duplicating the code to wait until the server is ready and remember to terminate the server and wait for it, make it possible to use like this: with qemu_nbd_popen('-k', sock, image): # Access image via qemu-nbd socket... Only test 264 used this helper, but I had to modify the output since it did not consistently when starting and stopping qemu-nbd. Signed-off-by: Nir Soffer <nsoffer@redhat.com> Message-Id: <20200727215846.395443-3-nsoffer@redhat.com> Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com> Signed-off-by: Eric Blake <eblake@redhat.com>
Diffstat (limited to 'tests/qemu-iotests/iotests.py')
-rw-r--r--tests/qemu-iotests/iotests.py28
1 files changed, 26 insertions, 2 deletions
diff --git a/tests/qemu-iotests/iotests.py b/tests/qemu-iotests/iotests.py
index 3590ed78a0..8f79668435 100644
--- a/tests/qemu-iotests/iotests.py
+++ b/tests/qemu-iotests/iotests.py
@@ -28,10 +28,13 @@ import signal
import struct
import subprocess
import sys
+import time
from typing import (Any, Callable, Dict, Iterable,
List, Optional, Sequence, Tuple, TypeVar)
import unittest
+from contextlib import contextmanager
+
# pylint: disable=import-error, wrong-import-position
sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'python'))
from qemu import qtest
@@ -270,9 +273,30 @@ def qemu_nbd_early_pipe(*args):
return subp.returncode, output if subp.returncode else ''
+@contextmanager
def qemu_nbd_popen(*args):
- '''Run qemu-nbd in daemon mode and return the parent's exit code'''
- return subprocess.Popen(qemu_nbd_args + ['--persistent'] + list(args))
+ '''Context manager running qemu-nbd within the context'''
+ pid_file = file_path("pid")
+
+ cmd = list(qemu_nbd_args)
+ cmd.extend(('--persistent', '--pid-file', pid_file))
+ cmd.extend(args)
+
+ log('Start NBD server')
+ p = subprocess.Popen(cmd)
+ try:
+ while not os.path.exists(pid_file):
+ if p.poll() is not None:
+ raise RuntimeError(
+ "qemu-nbd terminated with exit code {}: {}"
+ .format(p.returncode, ' '.join(cmd)))
+
+ time.sleep(0.01)
+ yield
+ finally:
+ log('Kill NBD server')
+ p.kill()
+ p.wait()
def compare_images(img1, img2, fmt1=imgfmt, fmt2=imgfmt):
'''Return True if two image files are identical'''