def test_should_trim_stdout_and_stderr_based_on_command_line(ClientMock, sys_mock):
    command = [
    '--dsn', 'http://testdsn',
    '--max-message-length', '100',
    sys.executable, '-c', """
import sys
sys.stdout.write("a" * 20000 + "end")
sys.stderr.write("b" * 20000 + "end")
sys.exit(2)
"""]

    run(command)

    # -3 refers to "..." and "end"
    expected_stdout = '...{0}end'.format('a' * (100 - 3 - 3))
    expected_stderr = '...{0}end'.format('b' * (100 - 3 - 3))

    sys_mock.stdout.write.assert_called_with(expected_stdout)
    sys_mock.stderr.write.assert_called_with(expected_stderr)
    client = ClientMock()
    client.captureMessage.assert_called_with(
        mock.ANY,
        time_spent=mock.ANY,
        data=mock.ANY,
        extra={
            'command': mock.ANY,
            'exit_status': mock.ANY,
            "last_lines_stdout": expected_stdout,
            "last_lines_stderr": expected_stderr,
    })
def test_should_suppress_stdout_and_stderr_based_on_command_line(ClientMock, sys_mock):
    command = [
    '--dsn', 'http://testdsn',
    '--quiet',
    sys.executable, '-c', """
import sys
sys.stdout.write("a" * 100 + "end")
sys.stderr.write("b" * 100 + "end")
sys.exit(2)
"""]

    run(command)

    expected_stdout = "a" * 100 + "end"
    expected_stderr = "b" * 100 + "end"

    assert not sys_mock.stdout.write.called
    assert not sys_mock.stderr.write.called

    client = ClientMock()
    client.captureMessage.assert_called_with(
        mock.ANY,
        time_spent=mock.ANY,
        data=mock.ANY,
        extra={
            'command': mock.ANY,
            'exit_status': mock.ANY,
            "last_lines_stdout": expected_stdout,
            "last_lines_stderr": expected_stderr,
    })
Esempio n. 3
0
def test_should_suppress_stdout_and_stderr_based_on_command_line(
        ClientMock, sys_mock):
    command = [
        '--dsn', 'http://testdsn', '--quiet', sys.executable, '-c', """
import sys
sys.stdout.write("a" * 100 + "end")
sys.stderr.write("b" * 100 + "end")
sys.exit(2)
"""
    ]

    run(command)

    expected_stdout = "a" * 100 + "end"
    expected_stderr = "b" * 100 + "end"

    assert not sys_mock.stdout.write.called
    assert not sys_mock.stderr.write.called

    client = ClientMock()
    client.captureMessage.assert_called_with(mock.ANY,
                                             time_spent=mock.ANY,
                                             data=mock.ANY,
                                             extra={
                                                 'command':
                                                 mock.ANY,
                                                 'exit_status':
                                                 mock.ANY,
                                                 "last_lines_stdout":
                                                 expected_stdout,
                                                 "last_lines_stderr":
                                                 expected_stderr,
                                             })
Esempio n. 4
0
def test_extra_data_via_env_vars_should_go_to_sentry(ClientMock, sys_mock):
    command = ['--dsn', 'http://testdsn', 'false']

    os.environ['CRON_SENTRY_EXTRA_secret1'] = 'hello'
    os.environ['CRON_SENTRY_EXTRA_secret2'] = 'world'
    try:
        run(command)
    finally:
        del os.environ['CRON_SENTRY_EXTRA_secret1']
        del os.environ['CRON_SENTRY_EXTRA_secret2']

    expected_stdout = "a" * 100 + "end"
    expected_stderr = "b" * 100 + "end"

    client = ClientMock()
    client.captureMessage.assert_called_with(mock.ANY,
                                             time_spent=mock.ANY,
                                             data=mock.ANY,
                                             extra={
                                                 'command': mock.ANY,
                                                 'exit_status': mock.ANY,
                                                 'last_lines_stdout': mock.ANY,
                                                 'last_lines_stderr': mock.ANY,
                                                 'secret1': 'hello',
                                                 'secret2': 'world',
                                             })
Esempio n. 5
0
def test_should_trim_stdout_and_stderr_based_on_command_line(
        ClientMock, sys_mock):
    command = [
        '--dsn', 'http://testdsn', '--max-message-length', '100',
        sys.executable, '-c', """
import sys
sys.stdout.write("a" * 20000 + "end")
sys.stderr.write("b" * 20000 + "end")
sys.exit(2)
"""
    ]

    run(command)

    # -3 refers to "..." and "end"
    expected_stdout = '...{0}end'.format('a' * (100 - 3 - 3))
    expected_stderr = '...{0}end'.format('b' * (100 - 3 - 3))

    sys_mock.stdout.write.assert_called_with(expected_stdout)
    sys_mock.stderr.write.assert_called_with(expected_stderr)
    client = ClientMock()
    client.captureMessage.assert_called_with(mock.ANY,
                                             time_spent=mock.ANY,
                                             data=mock.ANY,
                                             extra={
                                                 'command':
                                                 mock.ANY,
                                                 'exit_status':
                                                 mock.ANY,
                                                 "last_lines_stdout":
                                                 expected_stdout,
                                                 "last_lines_stderr":
                                                 expected_stderr,
                                             })
def test_should_display_help_text_and_exit_with_1_if_no_command_is_specified(CommandReporterMock, argparse_sys, cron_sentry_sys):
    command = []
    run(command)

    cron_sentry_sys.stderr.write.assert_called_with("ERROR: Missing command parameter!\n")
    argparse_sys.stdout.write.assert_called_with(parser.format_usage())
    cron_sentry_sys.exit.assert_called_with(1)
    assert not CommandReporterMock.called
def test_command_line_should_support_command_with_double_dashes(CommandReporterMock):
    command = ['--dsn', 'http://testdsn', '--', 'command', '--arg1', 'value1', '--arg2', 'value2']

    run(command)

    CommandReporterMock.assert_called_with(
        cmd=command[3:],
        dsn='http://testdsn')
Esempio n. 8
0
def test_should_display_help_text_and_exit_with_1_if_no_command_is_specified(
        CommandReporterMock, argparse_sys, cron_sentry_sys):
    command = []
    run(command)

    cron_sentry_sys.stderr.write.assert_called_with(
        "ERROR: Missing command parameter!\n")
    argparse_sys.stdout.write.assert_called_with(parser.format_usage())
    cron_sentry_sys.exit.assert_called_with(1)
    assert not CommandReporterMock.called
def test_command_line_should_support_command_args_without_double_dashes(CommandReporterMock, sys_mock):
    command = ['--dsn', 'http://testdsn', 'command', '--arg1', 'value1', '--arg2', 'value2']

    run(command)

    CommandReporterMock.assert_called_with(
        cmd=command[2:],
        dsn='http://testdsn',
        string_max_length=DEFAULT_STRING_MAX_LENGTH,
        quiet=False
    )
def test_command_line_should_support_command_with_double_dashes(
        CommandReporterMock, sys_mock):
    command = [
        '--dsn', 'http://testdsn', '--', 'command', '--arg1', 'value1',
        '--arg2', 'value2'
    ]

    run(command)

    CommandReporterMock.assert_called_with(
        cmd=command[3:],
        dsn='http://testdsn',
        string_max_length=DEFAULT_STRING_MAX_LENGTH,
    )
def test_exit_status_code_should_be_preserved(ClientMock, sys_mock):
    command = [sys.executable, '-c', 'import sys; sys.exit(123)']

    run(command)

    sys_mock.exit.assert_called_with(123)
Esempio n. 12
0
def test_exit_status_code_should_be_preserved(ClientMock, sys_mock):
    command = [sys.executable, '-c', 'import sys; sys.exit(123)']

    run(command)

    sys_mock.exit.assert_called_with(123)