From 62c26b35cd5ca464acd1fdedd3ad2df1f4643fae Mon Sep 17 00:00:00 2001 From: Loic Dachary Date: Thu, 29 Oct 2015 03:25:40 +0900 Subject: [PATCH] misc: reduce misc.sh verbosity Only shows the first 128 characters of the output when the command succeeds and show all of it only when it fails. Signed-off-by: Loic Dachary --- teuthology/misc.py | 10 ++++++++-- teuthology/test/test_misc.py | 14 ++++++++++++++ tox.ini | 1 + 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/teuthology/misc.py b/teuthology/misc.py index 53caaa8704..7b2e08ce41 100644 --- a/teuthology/misc.py +++ b/teuthology/misc.py @@ -1299,7 +1299,7 @@ def is_in_dict(searchkey, searchval, d): return searchval == val -def sh(command): +def sh(command, log_limit=128): """ Run the shell command and return the output in ascii (stderr and stdout). If the command fails, raise an exception. The command @@ -1314,8 +1314,14 @@ def sh(command): shell=True) output = proc.communicate()[0] if output.strip(): - log.debug(command + " output " + str(output)) + if len(output) > log_limit: + log.debug(command + " output " + str(output)[:log_limit] + + "... (truncated to the first " + str(log_limit) + + " characters)") + else: + log.debug(command + " output " + str(output)) if proc.returncode != 0: + log.debug(command + " failed with " + str(output)) raise subprocess.CalledProcessError( returncode=proc.returncode, cmd=command, diff --git a/teuthology/test/test_misc.py b/teuthology/test/test_misc.py index 71a534acbf..af7b5960a9 100644 --- a/teuthology/test/test_misc.py +++ b/teuthology/test/test_misc.py @@ -11,6 +11,20 @@ import pytest class FakeRemote(object): pass +def test_sh_normal(caplog): + assert misc.sh("echo ABC") == "ABC\n" + assert "truncated" not in caplog.text() + +def test_sh_truncate(caplog): + assert misc.sh("echo -n AB ; echo C", 2) == "ABC\n" + assert "truncated" in caplog.text() + assert "ABC" not in caplog.text() + +def test_sh_fail(caplog): + with pytest.raises(Exception) as excinfo: + misc.sh("echo -n AB ; echo C ; exit 111", 2) == "ABC\n" + assert excinfo.value.returncode == 111 + assert "failed with ABC" in caplog.text() def test_wait_until_osds_up(): ctx = argparse.Namespace() diff --git a/tox.ini b/tox.ini index 4715a7ad0c..af4568fc82 100644 --- a/tox.ini +++ b/tox.ini @@ -10,6 +10,7 @@ deps= mock fudge nose + pytest-capturelog pytest-cov==1.6 coverage==3.7.1 -- 2.39.5