Add few helpers for running external commands: - CommandFailed: exception, keeping all the info related to a failed command, and providing a useful error message. (Unfortunately subprocess.CalledProcessError does not).
- run(): run a command collecting output from the underlying process stdout and stderr, returning the command output or raising CommandFailed. These helpers will be used by new qemu-nbd tests. And later can be used to cleanup helpers for running qemu-* tools in iotests.py. Signed-off-by: Nir Soffer <nir...@gmail.com> --- tests/qemu-iotests/iotests.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/qemu-iotests/iotests.py b/tests/qemu-iotests/iotests.py index b25d48a91b..0f8abf99cb 100644 --- a/tests/qemu-iotests/iotests.py +++ b/tests/qemu-iotests/iotests.py @@ -64,6 +64,24 @@ luks_default_secret_object = 'secret,id=keysec0,data=' + \ os.environ['IMGKEYSECRET'] luks_default_key_secret_opt = 'key-secret=keysec0' +class CommandFailed(Exception): + + def __init__(self, cmd, rc, out, err): + self.cmd = cmd + self.rc = rc + self.out = out + self.err = err + + def __str__(self): + return ("Command {self.cmd} failed: rc={self.rc}, out={self.out!r}, " + "err={self.err!r}").format(self=self) + +def run(*args): + p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + out, err = p.communicate() + if p.returncode != 0: + raise CommandFailed(args, p.returncode, out, err) + return out def qemu_img(*args): '''Run qemu-img and return the exit code''' -- 2.14.3