]> git-server-git.apps.pok.os.sepia.ceph.com Git - teuthology.git/commitdiff
misc: reduce misc.sh verbosity 680/head
authorLoic Dachary <ldachary@redhat.com>
Wed, 28 Oct 2015 18:25:40 +0000 (03:25 +0900)
committerLoic Dachary <ldachary@redhat.com>
Wed, 28 Oct 2015 18:59:49 +0000 (03:59 +0900)
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 <ldachary@redhat.com>
teuthology/misc.py
teuthology/test/test_misc.py
tox.ini

index 53caaa87043297f855eb0328be42fae4008644e0..7b2e08ce41bd37658b18483d319dfce244fb0d11 100644 (file)
@@ -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,
index 71a534acbfb376a570965d0720e40750e515d292..af7b5960a98a104b50768538d7837b3c044cc7a1 100644 (file)
@@ -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 4715a7ad0cc26e548e132b58590d2f62b46436e7..af4568fc828f34fbea10d1ab216fbf1a0059691d 100644 (file)
--- a/tox.ini
+++ b/tox.ini
@@ -10,6 +10,7 @@ deps=
   mock
   fudge
   nose
+  pytest-capturelog
   pytest-cov==1.6
   coverage==3.7.1