Skip to content

Commit

Permalink
Merge pull request #845 from projectcalico/smc-trunc-syscall-log
Browse files Browse the repository at this point in the history
Truncate long stdin/stout/stderr output from failing commands.
  • Loading branch information
alexwlchan committed Oct 19, 2015
2 parents 2552440 + 9c24c0c commit 42f545f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
17 changes: 16 additions & 1 deletion calico/felix/futils.py
Expand Up @@ -67,6 +67,8 @@
MAX_CONCURRENT_CALLS = 32
_call_semaphore = gevent.lock.Semaphore(MAX_CONCURRENT_CALLS)

DEFAULT_TRUNC_LENGTH = 1000


class FailedSystemCall(Exception):
def __init__(self, message, args, retcode, stdout, stderr, input=None):
Expand All @@ -84,7 +86,20 @@ def __str__(self):
" stderr : %s\n"
" input : %s\n" %
(self.message, self.retcode, self.args,
self.stdout, self.stderr, self.input))
safe_truncate(self.stdout),
safe_truncate(self.stderr),
safe_truncate(self.input)))


def safe_truncate(s, max_len=DEFAULT_TRUNC_LENGTH):
if s is None:
return s
if not isinstance(s, basestring):
s = str(s)
snip = "...<snip>..."
if len(s) > max_len:
s = s[:(max_len+1)//2] + snip + s[-(max_len//2):]
return s


def call_silent(args):
Expand Down
16 changes: 16 additions & 0 deletions calico/felix/test/test_futils.py
Expand Up @@ -125,6 +125,22 @@ def test_uniquely_shorten(self):
"%r but got %r" %
(inp, length, exp, output))

def test_safe_truncate(self):
self.assert_safe_truncate("foobarbazb", 10, "foobarbazb")
# Yes, this gets longer, which is silly. However, there's no point
# making the code complicated to handle this case that should never be
# hit.
self.assert_safe_truncate("foobarbazb", 9, "fooba...<snip>...bazb")
self.assert_safe_truncate(None, 9, None)
self.assert_safe_truncate(1234, 9, "1234")

def assert_safe_truncate(self, s, length, expected):
result = futils.safe_truncate(s, length)
self.assertEqual(result, expected,
"Expected %r to be truncated as %r but got %r" %
(s, expected, result))


class TestStats(unittest.TestCase):
def setUp(self):
futils._registered_diags = []
Expand Down

0 comments on commit 42f545f

Please sign in to comment.