Example #1
0
def test_log(logger_with_udp_handler):
    expected_msg = (b'<13>1 2000-01-01T17:11:11.111111+06:00 testhostname root 111'
                    b' - - \xef\xbb\xbfThis is an interesting message')
    logger, syslog_socket = logger_with_udp_handler
    adapter = Rfc5424SysLogAdapter(logger, enable_extra_levels=True)
    adapter.log(NOTICE, message)
    syslog_socket.sendto.assert_called_once_with(expected_msg, address)
Example #2
0
def test_extras(logger_with_udp_handler):
    logger, syslog_socket = logger_with_udp_handler
    adapter = Rfc5424SysLogAdapter(logger,
                                   enable_extra_levels=True,
                                   extra={
                                       "a": 1,
                                       "c": 3
                                   })
    expected_return = ("aaaa", {'extra': dict(a=1, b=2, c="c")})
    # Make sure passed extra argument overrides that of the adapter instance
    assert adapter.process("aaaa", kwargs={'extra': {
        "b": 2,
        "c": "c"
    }}) == expected_return
    # Make sure adapter instance variables aren't overwritten
    assert {"a": 1, "c": 3} == adapter.extra
    # Test invalid extra argument
    with pytest.raises(TypeError):
        adapter = Rfc5424SysLogAdapter(logger,
                                       enable_extra_levels=True,
                                       extra="i_am_not_a_dict")
Example #3
0
def test_adapter(logger, handler_kwargs, adapter_kwargs, logger_kwargs, expected):

    sh = Rfc5424SysLogHandler(**handler_kwargs)
    logger.addHandler(sh)
    adapter = Rfc5424SysLogAdapter(logger, **adapter_kwargs)
    with patch.object(sh, 'socket') as syslog_socket:
        adapter.info(message, **logger_kwargs)
        syslog_socket.sendto.assert_called_once_with(expected, address)
        syslog_socket.sendto.reset_mock()

        adapter.log(logging.INFO, message, **logger_kwargs)
        syslog_socket.sendto.assert_called_once_with(expected, address)
        syslog_socket.sendto.reset_mock()
    logger.removeHandler(sh)
Example #4
0
def test_empty_msg(logger_with_udp_handler):
    logger, syslog_socket = logger_with_udp_handler
    adapter = Rfc5424SysLogAdapter(logger, enable_extra_levels=True)

    expected_msg = b'<15>1 2000-01-01T17:11:11.111111+06:00 testhostname root 111 - -'
    adapter.debug()
    syslog_socket.sendto.assert_called_once_with(expected_msg, address)
    syslog_socket.sendto.reset_mock()

    expected_msg = b'<14>1 2000-01-01T17:11:11.111111+06:00 testhostname root 111 - -'
    adapter.info()
    syslog_socket.sendto.assert_called_once_with(expected_msg, address)
    syslog_socket.sendto.reset_mock()

    expected_msg = b'<13>1 2000-01-01T17:11:11.111111+06:00 testhostname root 111 - -'
    adapter.notice()
    syslog_socket.sendto.assert_called_once_with(expected_msg, address)
    syslog_socket.sendto.reset_mock()

    expected_msg = b'<12>1 2000-01-01T17:11:11.111111+06:00 testhostname root 111 - -'
    adapter.warning()
    syslog_socket.sendto.assert_called_once_with(expected_msg, address)
    syslog_socket.sendto.reset_mock()

    expected_msg = b'<11>1 2000-01-01T17:11:11.111111+06:00 testhostname root 111 - -'
    adapter.error()
    syslog_socket.sendto.assert_called_once_with(expected_msg, address)
    syslog_socket.sendto.reset_mock()

    expected_msg = b'<10>1 2000-01-01T17:11:11.111111+06:00 testhostname root 111 - -'
    adapter.critical()
    syslog_socket.sendto.assert_called_once_with(expected_msg, address)
    syslog_socket.sendto.reset_mock()

    expected_msg = b'<9>1 2000-01-01T17:11:11.111111+06:00 testhostname root 111 - -'
    adapter.alert()
    syslog_socket.sendto.assert_called_once_with(expected_msg, address)
    syslog_socket.sendto.reset_mock()

    expected_msg = b'<8>1 2000-01-01T17:11:11.111111+06:00 testhostname root 111 - -'
    adapter.emergency()
    syslog_socket.sendto.assert_called_once_with(expected_msg, address)
Example #5
0
def adapter_with_udp_handler(logger_with_udp_handler):
    logger, syslog_socket = logger_with_udp_handler
    adapter = Rfc5424SysLogAdapter(logger)
    yield adapter, syslog_socket
Example #6
0
def test_extras(logger_with_udp_handler):
    logger, syslog_socket = logger_with_udp_handler
    adapter = Rfc5424SysLogAdapter(logger, enable_extra_levels=True, extra={"a": 1})
    expected_return = ("aaaa", {'extra': dict(a=1, b=2)})
    assert adapter.process("aaaa", kwargs={'extra': {"b": 2}}) == expected_return