From: Eric Blake <eblake@redhat.com>
To: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>,
Nir Soffer <nirsof@gmail.com>,
qemu-devel@nongnu.org, qemu-block@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>, Nir Soffer <nsoffer@redhat.com>,
Max Reitz <mreitz@redhat.com>
Subject: Re: [PATCH v2 2/4] iotests: Make qemu_nbd_popen() a contextmanager
Date: Tue, 28 Jul 2020 09:36:49 -0500 [thread overview]
Message-ID: <f36763b6-eac4-abf9-9563-7f8231422486@redhat.com> (raw)
In-Reply-To: <ac8994e8-7f06-0710-78ce-0596e9538e7d@virtuozzo.com>
On 7/28/20 8:42 AM, Vladimir Sementsov-Ogievskiy wrote:
> 28.07.2020 00:58, Nir Soffer wrote:
>> 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>
>> ---
>> +++ 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'''
>
> PEP8 (or some another PEP referenced in PEP8) asks to use """ for
> doc-strings
But at least './check 297' still passes, so we are consistent enough
with our current set of syntax checks to be acceptable.
>
>> + 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()
>
> why do we need try-finally? I think, the only possible exception is your
> "raise RuntimeError", and in this case the process is alredy dead, no
> need to kill it (and print the log message)
>
>> def compare_images(img1, img2, fmt1=imgfmt, fmt2=imgfmt):
>> '''Return True if two image files are identical'''
>>
>
> anyway:
> Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
>
Thanks; I trust your python review more than mine. Queuing for -rc2.
--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3226
Virtualization: qemu.org | libvirt.org
next prev parent reply other threads:[~2020-07-28 14:38 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-07-27 21:58 [PATCH v2 0/4] Fix convert to qcow2 compressed to NBD Nir Soffer
2020-07-27 21:58 ` [PATCH v2 1/4] block: nbd: Fix convert qcow2 compressed to nbd Nir Soffer
2020-07-28 13:15 ` Vladimir Sementsov-Ogievskiy
2020-07-28 14:30 ` Eric Blake
2020-07-28 14:28 ` Eric Blake
2020-07-27 21:58 ` [PATCH v2 2/4] iotests: Make qemu_nbd_popen() a contextmanager Nir Soffer
2020-07-28 13:42 ` Vladimir Sementsov-Ogievskiy
2020-07-28 14:36 ` Eric Blake [this message]
2020-07-28 16:05 ` Nir Soffer
2020-08-05 7:38 ` Vladimir Sementsov-Ogievskiy
2020-08-05 8:47 ` Nir Soffer
2020-07-27 21:58 ` [PATCH v2 3/4] iotests: Add more qemu_img helpers Nir Soffer
2020-07-28 13:50 ` Vladimir Sementsov-Ogievskiy
2020-07-28 16:34 ` Nir Soffer
2020-07-27 21:58 ` [PATCH v2 4/4] iotests: Test convert to qcow2 compressed to NBD Nir Soffer
2020-07-28 14:45 ` Eric Blake
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=f36763b6-eac4-abf9-9563-7f8231422486@redhat.com \
--to=eblake@redhat.com \
--cc=kwolf@redhat.com \
--cc=mreitz@redhat.com \
--cc=nirsof@gmail.com \
--cc=nsoffer@redhat.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=vsementsov@virtuozzo.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).