From: Loic Dachary Date: Wed, 28 Oct 2015 18:25:40 +0000 (+0900) Subject: misc: reduce misc.sh verbosity X-Git-Tag: 1.1.0~775^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=62c26b35cd5ca464acd1fdedd3ad2df1f4643fae;p=teuthology.git 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 --- diff --git a/teuthology/misc.py b/teuthology/misc.py index 53caaa870..7b2e08ce4 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 71a534acb..af7b5960a 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 4715a7ad0..af4568fc8 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