iotests.py: Add qemu_img_pipe_and_status()

This function will be used by the next patch, which intends to check
both the exit code and qemu-img's output.

Signed-off-by: Max Reitz <mreitz@redhat.com>
Message-Id: <20200625125548.870061-5-mreitz@redhat.com>
Reviewed-by: Maxim Levitsky <mlevitsk@redhat.com>
[mreitz: Rebased on 49438972b8]
Signed-off-by: Max Reitz <mreitz@redhat.com>
This commit is contained in:
Max Reitz 2020-06-25 14:55:33 +02:00
parent dc4ab02919
commit d849acab41
1 changed files with 20 additions and 20 deletions

View File

@ -29,7 +29,7 @@ import struct
import subprocess import subprocess
import sys import sys
from typing import (Any, Callable, Dict, Iterable, from typing import (Any, Callable, Dict, Iterable,
List, Optional, Sequence, TypeVar) List, Optional, Sequence, Tuple, TypeVar)
import unittest import unittest
# pylint: disable=import-error, wrong-import-position # pylint: disable=import-error, wrong-import-position
@ -90,15 +90,24 @@ luks_default_secret_object = 'secret,id=keysec0,data=' + \
luks_default_key_secret_opt = 'key-secret=keysec0' luks_default_key_secret_opt = 'key-secret=keysec0'
def qemu_img(*args): def qemu_img_pipe_and_status(*args: str) -> Tuple[str, int]:
'''Run qemu-img and return the exit code''' """
devnull = open('/dev/null', 'r+') Run qemu-img and return both its output and its exit code
exitcode = subprocess.call(qemu_img_args + list(args), """
stdin=devnull, stdout=devnull) subp = subprocess.Popen(qemu_img_args + list(args),
if exitcode < 0: stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True)
output = subp.communicate()[0]
if subp.returncode < 0:
sys.stderr.write('qemu-img received signal %i: %s\n' sys.stderr.write('qemu-img received signal %i: %s\n'
% (-exitcode, ' '.join(qemu_img_args + list(args)))) % (-subp.returncode,
return exitcode ' '.join(qemu_img_args + list(args))))
return (output, subp.returncode)
def qemu_img(*args: str) -> int:
'''Run qemu-img and return the exit code'''
return qemu_img_pipe_and_status(*args)[1]
def ordered_qmp(qmsg, conv_keys=True): def ordered_qmp(qmsg, conv_keys=True):
# Dictionaries are not ordered prior to 3.6, therefore: # Dictionaries are not ordered prior to 3.6, therefore:
@ -140,18 +149,9 @@ def qemu_img_verbose(*args):
% (-exitcode, ' '.join(qemu_img_args + list(args)))) % (-exitcode, ' '.join(qemu_img_args + list(args))))
return exitcode return exitcode
def qemu_img_pipe(*args): def qemu_img_pipe(*args: str) -> str:
'''Run qemu-img and return its output''' '''Run qemu-img and return its output'''
subp = subprocess.Popen(qemu_img_args + list(args), return qemu_img_pipe_and_status(*args)[0]
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True)
output = subp.communicate()[0]
if subp.returncode < 0:
sys.stderr.write('qemu-img received signal %i: %s\n'
% (-subp.returncode,
' '.join(qemu_img_args + list(args))))
return output
def qemu_img_log(*args): def qemu_img_log(*args):
result = qemu_img_pipe(*args) result = qemu_img_pipe(*args)