def test_command_reporter_accepts_parameters(ClientMock): reporter = CommandReporter(['date', '--invalid-option'], 'http://testdsn', DEFAULT_STRING_MAX_LENGTH) reporter.run() client = ClientMock() assert client.captureMessage.called
def test_command_reporter_keeps_stdout_and_stderr(ClientMock, sys_mock): command = [ sys.executable, '-c', """ import sys sys.stdout.write("test-out") sys.stderr.write("test-err") sys.exit(2) """ ] reporter = CommandReporter(command, 'http://testdsn', DEFAULT_STRING_MAX_LENGTH) client = ClientMock() reporter.run() sys_mock.stdout.write.assert_called_with('test-out') sys_mock.stderr.write.assert_called_with('test-err') client.captureMessage.assert_called_with(mock.ANY, time_spent=mock.ANY, data=mock.ANY, extra={ 'command': command, 'exit_status': 2, "last_lines_stdout": "test-out", "last_lines_stderr": "test-err", })
def test_reports_correctly_to_with_long_messages_but_trims_stdout_and_stderr(ClientMock, sys_mock): command = [sys.executable, '-c', """ import sys sys.stdout.write("a" * 20000) sys.stderr.write("b" * 20000) sys.exit(2) """] reporter = CommandReporter(command, 'http://testdsn', DEFAULT_STRING_MAX_LENGTH) client = ClientMock() reporter.run() expected_stdout = '...{0}'.format('a' * (DEFAULT_STRING_MAX_LENGTH - 3)) expected_stderr = '...{0}'.format('b' * (DEFAULT_STRING_MAX_LENGTH - 3)) sys_mock.stdout.write.assert_called_with(expected_stdout) sys_mock.stderr.write.assert_called_with(expected_stderr) client.captureMessage.assert_called_with( mock.ANY, time_spent=mock.ANY, data=mock.ANY, extra={ 'command': command, 'exit_status': 2, "last_lines_stdout": expected_stdout, "last_lines_stderr": expected_stderr, })
def test_command_reporter_works_with_no_params_commands(ClientMock): reporter = CommandReporter(['date'], 'http://testdsn', DEFAULT_STRING_MAX_LENGTH) reporter.run() client = ClientMock() assert not client.captureMessage.called
def test_command_reporter_catches_invalid_commands(ClientMock, sys_mock): command = ['command-does-not-exists'] reporter = CommandReporter(command, 'http://testdsn', DEFAULT_STRING_MAX_LENGTH) 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() 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": mock.ANY, }) assert exit_code == expected_exit_code sys_mock.stdout.write.assert_called_with(expected_stdout) # python 3 exception `FileNotFoundError` has additional content compared to python 2's `OSError` assert client.captureMessage.call_args[1]['extra'][ 'last_lines_stderr'].startswith(expected_stderr) assert sys_mock.stderr.write.call_args[0][0].startswith(expected_stderr)
def test_reports_correctly_to_with_long_messages_but_trims_stdout_and_stderr( ClientMock, sys_mock): command = [ sys.executable, '-c', """ import sys sys.stdout.write("a" * 20000) sys.stderr.write("b" * 20000) sys.exit(2) """ ] reporter = CommandReporter(command, 'http://testdsn', DEFAULT_STRING_MAX_LENGTH) client = ClientMock() reporter.run() expected_stdout = '...{0}'.format('a' * (DEFAULT_STRING_MAX_LENGTH - 3)) expected_stderr = '...{0}'.format('b' * (DEFAULT_STRING_MAX_LENGTH - 3)) sys_mock.stdout.write.assert_called_with(expected_stdout) sys_mock.stderr.write.assert_called_with(expected_stderr) client.captureMessage.assert_called_with(mock.ANY, time_spent=mock.ANY, data=mock.ANY, extra={ 'command': command, 'exit_status': 2, "last_lines_stdout": expected_stdout, "last_lines_stderr": expected_stderr, })
def test_handles_broken_command(ClientMock, sys_mock): command = ['/tmp/fake_exe_fkjsfisdjfidsjf.sh'] reporter = CommandReporter(command, 'http://testdsn', DEFAULT_STRING_MAX_LENGTH) client = ClientMock() reporter.run() sys_mock.stdout.write.assert_called_with('') sys_mock.stderr.write.assert_called_with('') client.captureMessage.assert_called_with(mock.ANY, time_spent=mock.ANY, data=mock.ANY, extra={ 'command': command, 'exit_status': 1, "last_lines_stdout": mock.ANY, "last_lines_stderr": mock.ANY, })
def test_command_reporter_keeps_stdout_and_stderr(ClientMock, sys_mock): command = [sys.executable, '-c', """ import sys sys.stdout.write("test-out") sys.stderr.write("test-err") sys.exit(2) """] reporter = CommandReporter(command, 'http://testdsn', DEFAULT_STRING_MAX_LENGTH) client = ClientMock() reporter.run() sys_mock.stdout.write.assert_called_with('test-out') sys_mock.stderr.write.assert_called_with('test-err') client.captureMessage.assert_called_with( mock.ANY, time_spent=mock.ANY, data=mock.ANY, extra={ 'command': command, 'exit_status': 2, "last_lines_stdout": "test-out", "last_lines_stderr": "test-err", })