Skip to content

Commit

Permalink
Send OSError message to Sentry. Refs #3
Browse files Browse the repository at this point in the history
  • Loading branch information
hltbra committed Dec 6, 2016
1 parent 9b6c6df commit aa1f4ea
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
current
=======

* Send OSError message to Sentry (refs https://github.com/Yipit/cron-sentry/issues/3)


0.6.4
=====
Expand Down
16 changes: 9 additions & 7 deletions cron_sentry/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,15 @@ def run(self):
with TemporaryFile() as stderr:
try:
exit_status = call(self.command, stdout=stdout, stderr=stderr)
except OSError:
exit_status = 1

last_lines_stdout = self._get_last_lines(stdout)
last_lines_stderr = self._get_last_lines(stderr)

if exit_status > 0:
except OSError as exc:
last_lines_stdout = ''
last_lines_stderr = unicode(exc)
exit_status = 127 # http://www.tldp.org/LDP/abs/html/exitcodes.html
else:
last_lines_stdout = self._get_last_lines(stdout)
last_lines_stderr = self._get_last_lines(stderr)

if exit_status != 0:
elapsed = int((time() - start) * 1000)
self.report_fail(exit_status, last_lines_stdout, last_lines_stderr, elapsed)

Expand Down
30 changes: 26 additions & 4 deletions tests/test_command_reporter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import errno
import os

import mock
import sys

from cron_sentry.runner import CommandReporter, DEFAULT_STRING_MAX_LENGTH, run, parser


Expand All @@ -13,14 +17,32 @@ def test_command_reporter_accepts_parameters(ClientMock):
assert client.captureMessage.called


@mock.patch('cron_sentry.runner.sys')
@mock.patch('cron_sentry.runner.Client')
def test_command_reporter_catches_invalid_commands(ClientMock):
reporter = CommandReporter(['command-does-not-exists'], 'http://testdsn', DEFAULT_STRING_MAX_LENGTH)
def test_command_reporter_catches_invalid_commands(ClientMock, sys_mock):
command = ['command-does-not-exists']
reporter = CommandReporter(command, 'http://testdsn', DEFAULT_STRING_MAX_LENGTH)

reporter.run()
exit_code = reporter.run()

expected_exit_code = 127
expected_stdout = ''
expected_stderr = '[Errno {errno}] {msg}'.format(errno=errno.ENOENT, msg=os.strerror(errno.ENOENT))

client = ClientMock()
assert client.captureMessage.called
client.captureMessage.assert_called_with(
mock.ANY,
time_spent=mock.ANY,
data=mock.ANY,
extra={
'command': command,
'exit_status': expected_exit_code,
"last_lines_stdout": expected_stdout,
"last_lines_stderr": expected_stderr,
})
assert exit_code == expected_exit_code
sys_mock.stdout.write.assert_called_with(expected_stdout)
sys_mock.stderr.write.assert_called_with(expected_stderr)


@mock.patch('cron_sentry.runner.Client')
Expand Down

0 comments on commit aa1f4ea

Please sign in to comment.