From a59f1db48bedd3b72a58e4d5193eb17c413aa4d0 Mon Sep 17 00:00:00 2001 From: Nir Soffer Date: Feb 26 2018 19:08:58 +0000 Subject: Show stderr when sanlock command fail When util.sanlock() fail, we show now also the command stderr, allowing assertions about sanlock behavior, and easier debugging. Signed-off-by: Nir Soffer --- diff --git a/tests/daemon_test.py b/tests/daemon_test.py index 9697658..84afacb 100644 --- a/tests/daemon_test.py +++ b/tests/daemon_test.py @@ -5,7 +5,6 @@ Test sanlock client operations. import io import signal import struct -import subprocess import pytest @@ -38,7 +37,7 @@ def test_start_after_kill(): def test_client_failure(): # No daemon is running, client must fail - with pytest.raises(subprocess.CalledProcessError) as e: + with pytest.raises(util.CommandError) as e: util.sanlock("client", "status") assert e.value.returncode == 1 diff --git a/tests/util.py b/tests/util.py index 157624a..9bd35fb 100644 --- a/tests/util.py +++ b/tests/util.py @@ -16,6 +16,20 @@ class TimeoutExpired(Exception): """ Raised when timeout expired """ +class CommandError(Exception): + msg = ("Command {self.cmd} failed with returncode={self.returncode}, " + "stdout={self.stdout!r}, stderr={self.stderr!r}") + + def __init__(self, cmd, returncode, stdout, stderr): + self.cmd = cmd + self.returncode = returncode + self.stdout = stdout + self.stderr = stderr + + def __str__(self): + return self.msg.format(self=self) + + def start_daemon(): cmd = [SANLOCK, "daemon", # no fork and print all logging to stderr @@ -57,11 +71,15 @@ def wait_for_daemon(timeout): def sanlock(*args): """ Run sanlock returning the process stdout, or raising - subprocess.CalledProcessError on failures. + util.CommandError on failures. """ cmd = [SANLOCK] cmd.extend(args) - return subprocess.check_output(cmd) + p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + out, err = p.communicate() + if p.returncode: + raise CommandError(cmd, p.returncode, out, err) + return out def wait_for_termination(p, timeout):