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() def connect_side_effect(*_, **__): m = second_mock_send_patch.start() m.side_effect = [True, True] mock_sends['second_mock_send'] = m try: with mock.patch.object(socket.socket, 'close') as mock_close, \ mock.patch.object(socket.socket, 'connect') as mock_reconnect: m = first_mock_send_patch.start() m.side_effect = [True, OSError()] mock_sends['first_mock_send'] = m mock_close.side_effect = close_side_effect mock_reconnect.side_effect = connect_side_effect handler._send([ 'this is the first part', 'here is another part', 'one more part' ]) finally: mock.patch.stopall() m = mock_sends['first_mock_send'] assert m is not None m.assert_has_calls([ mock.call('this is the first part'), mock.call('here is another part'), ]) m = mock_sends['second_mock_send'] assert m is not None m.assert_has_calls([ mock.call('here is another part'), mock.call('one more part'), ]) mock_reconnect.assert_called_once_with('/path/to/a/different.socket') mock_close.assert_called_once_with()
def test_send_udp(self): handler = SyslogHandler(address=('127.0.0.1', logging.handlers.SYSLOG_UDP_PORT)) with mock.patch.object(socket.socket, 'sendto') as mock_send_to: handler._send(['this is the first part', 'here is another part', 'one more part']) mock_send_to.assert_has_calls([ mock.call('this is the first part', ('127.0.0.1', logging.handlers.SYSLOG_UDP_PORT)), mock.call('here is another part', ('127.0.0.1', logging.handlers.SYSLOG_UDP_PORT)), mock.call('one more part', ('127.0.0.1', logging.handlers.SYSLOG_UDP_PORT)), ])
def test_send_unix(self): with mock.patch.object(socket.socket, 'connect') as mock_connect: handler = SyslogHandler(address='/path/to/unix.socket') mock_connect.assert_called_once_with('/path/to/unix.socket') with mock.patch.object(socket.socket, 'send') as mock_send: handler._send(['this is the first part', 'here is another part', 'one more part']) mock_send.assert_has_calls([ mock.call('this is the first part'), mock.call('here is another part'), mock.call('one more part'), ])
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'), ])