Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_emit_longer_than_limit_fragment(self):
handler = SyslogHandler()
handler.maximum_length = 100
handler.overflow = SyslogHandler.OVERFLOW_BEHAVIOR_FRAGMENT
handler.formatter = Formatter('foo_file: %(name)s %(levelname)s %(message)s')
record = LogRecord(
name='bar_service',
level=WARNING,
pathname='/path/to/file.py',
lineno=122,
msg='This is a much longer message that is going to exceed the maximum byte count and will need truncating',
args=(),
exc_info=None,
)
with mock.patch.object(handler, '_send') as mock_send:
handler.emit(record)
def test_emit_shorter_than_limit(self):
handler = SyslogHandler()
handler.maximum_length = 500
handler.overflow = SyslogHandler.OVERFLOW_BEHAVIOR_FRAGMENT
handler.formatter = Formatter('foo_file: %(name)s %(levelname)s %(message)s')
record = LogRecord(
name='bar_service',
level=WARNING,
pathname='/path/to/file.py',
lineno=122,
msg='This is a fairly short message',
args=(),
exc_info=None,
)
with mock.patch.object(handler, '_send') as mock_send:
handler.emit(record)
def test_send_unix_with_failure_part_way_through(self):
with mock.patch.object(socket.socket, 'connect') as mock_connect:
handler = SyslogHandler(address='/path/to/a/different.socket')
mock_connect.assert_called_once_with('/path/to/a/different.socket')
# This is weird. Creating a new socket actually dynamically creates the `send` method, which breaks mocking.
# So we have to mock the send, connect, and close methods, and then when the send returns an error on the
# second call, the close method has to de-mock send so that a new socket can be created, and then the
# connection method has to re-mock send so that we can capture the send retries. Yuck.
first_mock_send_patch = mock.patch.object(socket.socket, 'send')
second_mock_send_patch = mock.patch.object(socket.socket, 'send')
mock_sends = {
'first_mock_send': None,
'second_mock_send': None,
} # type: Dict[six.text_type, Optional[mock.MagicMock]]
def close_side_effect(*_, **__):
first_mock_send_patch.stop()
else:
assert handler.unixsocket is False # type: ignore
assert handler.overflow == SyslogHandler.OVERFLOW_BEHAVIOR_TRUNCATE
assert handler.maximum_length >= 1252 # (1280 - 28)
with mock.patch.object(socket.socket, 'connect'):
handler = SyslogHandler(socket_type=socket.SOCK_STREAM)
assert handler.socktype == socket.SOCK_STREAM # type: ignore
if six.PY2:
assert not handler.unixsocket # type: ignore
else:
assert handler.unixsocket is False # type: ignore
assert handler.overflow == SyslogHandler.OVERFLOW_BEHAVIOR_TRUNCATE
assert handler.maximum_length == 1024 * 1024
handler = SyslogHandler(address='/path/to/unix.socket')
assert handler.socktype == socket.SOCK_DGRAM # type: ignore
assert handler.unixsocket is True or handler.unixsocket == 1 # type: ignore # Python 2 compatibility
assert handler.overflow == SyslogHandler.OVERFLOW_BEHAVIOR_TRUNCATE
assert handler.maximum_length == 1024 * 1024
handler = SyslogHandler(address='/path/to/unix.socket', socket_type=socket.SOCK_STREAM)
assert handler.socktype == socket.SOCK_STREAM # type: ignore
assert handler.unixsocket is True or handler.unixsocket == 1 # type: ignore # Python 2 compatibility
assert handler.overflow == SyslogHandler.OVERFLOW_BEHAVIOR_TRUNCATE
assert handler.maximum_length == 1024 * 1024
handler = SyslogHandler(socket_type=socket.SOCK_STREAM)
assert handler.socktype == socket.SOCK_STREAM # type: ignore
if six.PY2:
assert not handler.unixsocket # type: ignore
else:
assert handler.unixsocket is False # type: ignore
assert handler.overflow == SyslogHandler.OVERFLOW_BEHAVIOR_TRUNCATE
assert handler.maximum_length == 1024 * 1024
handler = SyslogHandler(address='/path/to/unix.socket')
assert handler.socktype == socket.SOCK_DGRAM # type: ignore
assert handler.unixsocket is True or handler.unixsocket == 1 # type: ignore # Python 2 compatibility
assert handler.overflow == SyslogHandler.OVERFLOW_BEHAVIOR_TRUNCATE
assert handler.maximum_length == 1024 * 1024
handler = SyslogHandler(address='/path/to/unix.socket', socket_type=socket.SOCK_STREAM)
assert handler.socktype == socket.SOCK_STREAM # type: ignore
assert handler.unixsocket is True or handler.unixsocket == 1 # type: ignore # Python 2 compatibility
assert handler.overflow == SyslogHandler.OVERFLOW_BEHAVIOR_TRUNCATE
assert handler.maximum_length == 1024 * 1024
def test_send_tcp(self):
with mock.patch.object(socket.socket, 'connect') as mock_connect:
handler = SyslogHandler(
address=('127.0.0.1', logging.handlers.SYSLOG_UDP_PORT),
socket_type=socket.SOCK_STREAM,
)
mock_connect.assert_called_once_with(('127.0.0.1', logging.handlers.SYSLOG_UDP_PORT))
with mock.patch.object(socket.socket, 'sendall') as mock_send_all:
handler._send(['this is the first part', 'here is another part', 'one more part'])
mock_send_all.assert_has_calls([
mock.call('this is the first part'),
mock.call('here is another part'),
mock.call('one more part'),
])
def test_constructor(self):
handler = SyslogHandler()
assert handler.socktype == socket.SOCK_DGRAM # type: ignore
if six.PY2:
assert not handler.unixsocket # type: ignore
else:
assert handler.unixsocket is False # type: ignore
assert handler.overflow == SyslogHandler.OVERFLOW_BEHAVIOR_FRAGMENT
assert handler.maximum_length >= 1252 # (1280 - 28)
handler = SyslogHandler(overflow=SyslogHandler.OVERFLOW_BEHAVIOR_TRUNCATE)
assert handler.socktype == socket.SOCK_DGRAM # type: ignore
if six.PY2:
assert not handler.unixsocket # type: ignore
else:
assert handler.unixsocket is False # type: ignore
assert handler.overflow == SyslogHandler.OVERFLOW_BEHAVIOR_TRUNCATE
assert handler.maximum_length >= 1252 # (1280 - 28)
def test_emit_longer_than_limit_truncate_unicode_within(self):
# b'\xf0\x9f\x98\xb1' = u'\U0001f631' = shocked face with hands to cheeks
handler = SyslogHandler()
handler.maximum_length = 100
handler.overflow = SyslogHandler.OVERFLOW_BEHAVIOR_TRUNCATE
handler.formatter = Formatter('foo_file: %(name)s %(levelname)s %(message)s')
handler.ident = '5678' # type: ignore
record = LogRecord(
name='bar_service',
level=WARNING,
pathname='/path/to/file.py',
lineno=122,
msg='This is a much longer message \U0001f631 that is going to exceed the maximum byte count and will '
'need truncating',
args=(),
exc_info=None,
)
else:
assert handler.unixsocket is False # type: ignore
assert handler.overflow == SyslogHandler.OVERFLOW_BEHAVIOR_FRAGMENT
assert handler.maximum_length >= 1252 # (1280 - 28)
handler = SyslogHandler(overflow=SyslogHandler.OVERFLOW_BEHAVIOR_TRUNCATE)
assert handler.socktype == socket.SOCK_DGRAM # type: ignore
if six.PY2:
assert not handler.unixsocket # type: ignore
else:
assert handler.unixsocket is False # type: ignore
assert handler.overflow == SyslogHandler.OVERFLOW_BEHAVIOR_TRUNCATE
assert handler.maximum_length >= 1252 # (1280 - 28)
with mock.patch.object(socket.socket, 'connect'):
handler = SyslogHandler(socket_type=socket.SOCK_STREAM)
assert handler.socktype == socket.SOCK_STREAM # type: ignore
if six.PY2:
assert not handler.unixsocket # type: ignore
else:
assert handler.unixsocket is False # type: ignore
assert handler.overflow == SyslogHandler.OVERFLOW_BEHAVIOR_TRUNCATE
assert handler.maximum_length == 1024 * 1024
handler = SyslogHandler(address='/path/to/unix.socket')
assert handler.socktype == socket.SOCK_DGRAM # type: ignore
assert handler.unixsocket is True or handler.unixsocket == 1 # type: ignore # Python 2 compatibility
assert handler.overflow == SyslogHandler.OVERFLOW_BEHAVIOR_TRUNCATE
assert handler.maximum_length == 1024 * 1024
handler = SyslogHandler(address='/path/to/unix.socket', socket_type=socket.SOCK_STREAM)
assert handler.socktype == socket.SOCK_STREAM # type: ignore
'filters': {
'pysoa_logging_context_filter': {
'()': 'pysoa.common.logging.PySOALogContextFilter',
},
},
'handlers': {
'console': {
'level': 'INFO',
'class': 'logging.StreamHandler',
'formatter': 'console',
'filters': ['pysoa_logging_context_filter'],
},
'syslog': {
'level': 'INFO',
'class': 'pysoa.common.logging.SyslogHandler',
'facility': SyslogHandler.LOG_LOCAL7,
'address': ('localhost', 514),
'formatter': 'syslog',
'filters': ['pysoa_logging_context_filter'],
},
},
'loggers': {},
'root': {
'handlers': ['console'],
'level': 'INFO',
},
'disable_existing_loggers': False,
},
'harakiri': {
'timeout': 300,
'shutdown_grace': 30,
},