def test_eagain_on_buffer_size(self): c = self.test_successful_connection() header = six.b('\x00\x00\x00\x00') + int32_pack(20000) responses = [ header + (six.b('a') * (4096 - len(header))), six.b('a') * 4096, socket_error(errno.EAGAIN), six.b('a') * 100, socket_error(errno.EAGAIN)] def side_effect(*args): response = responses.pop(0) if isinstance(response, socket_error): raise response else: return response self.get_socket(c).recv.side_effect = side_effect c.handle_read(*self.null_handle_function_args) self.assertEqual(c._current_frame.end_pos, 20000 + len(header)) # the EAGAIN prevents it from reading the last 100 bytes c._iobuf.seek(0, os.SEEK_END) pos = c._iobuf.tell() self.assertEqual(pos, 4096 + 4096) # now tell it to read the last 100 bytes c.handle_read(*self.null_handle_function_args) c._iobuf.seek(0, os.SEEK_END) pos = c._iobuf.tell() self.assertEqual(pos, 4096 + 4096 + 100)
def get_listening_udp_socket(ip, port, retry=30, style=None): """Returns a bound socket.socket for accepting UDP datagrams. The socket will be bound to the given ip and tcp port with other optional parameters. :param ip: The ip address to listen on. ``''`` and ``'*'`` are translated to ``'0.0.0.0'`` which will listen on all configured addresses. :param port: The udp port to listen on. :param retry: The number seconds to keep trying to bind the socket, in case there's another process bound but exiting soon. This allows near zero-downtime process handoffs as you start the new one and kill the old. :param style: The libraries you'd like to use in creating the socket. The default will use the standard Python libraries. ``'Eventlet'`` is recognized and will use the Eventlet libraries. Other styles may added in the future. """ if not style: from socket import AF_INET, AF_INET6, AF_UNSPEC, \ error as socket_error, getaddrinfo, socket, SOCK_DGRAM, \ SOL_SOCKET, SO_REUSEADDR from time import sleep elif style.lower() == 'eventlet': from eventlet.green.socket import AF_INET, AF_INET6, AF_UNSPEC, \ error as socket_error, getaddrinfo, socket, SOCK_DGRAM, \ SOL_SOCKET, SO_REUSEADDR from eventlet import sleep else: from socket import error as socket_error raise socket_error('Socket style %r not understood.' % style) if not ip or ip == '*': ip = '0.0.0.0' family = None for a in getaddrinfo(ip, port, AF_UNSPEC, SOCK_DGRAM): if a[0] in (AF_INET, AF_INET6): family = a[0] break if not family: raise socket_error( 'Could not determine address family of %s:%s for binding.' % (ip, port)) good_sock = None retry_until = time() + retry while not good_sock and time() < retry_until: try: sock = socket(family, SOCK_DGRAM) sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) sock.bind((ip, port)) good_sock = sock except socket_error as err: if err.errno != EADDRINUSE: raise sleep(0.1) if not good_sock: raise socket_error( 'Could not bind to %s:%s after trying for %s seconds.' % (ip, port, retry)) return good_sock
def test_egain_on_buffer_size(self, *args): # get a connection that's already fully started c = self.test_successful_connection() header = six.b('\x00\x00\x00\x00') + int32_pack(20000) responses = [ header + (six.b('a') * (4096 - len(header))), six.b('a') * 4096, socket_error(errno.EAGAIN), six.b('a') * 100, socket_error(errno.EAGAIN)] def side_effect(*args): response = responses.pop(0) if isinstance(response, socket_error): raise response else: return response c._socket.recv.side_effect = side_effect c.handle_read(None, 0) self.assertEqual(c._total_reqd_bytes, 20000 + len(header)) # the EAGAIN prevents it from reading the last 100 bytes c._iobuf.seek(0, os.SEEK_END) pos = c._iobuf.tell() self.assertEqual(pos, 4096 + 4096) # now tell it to read the last 100 bytes c.handle_read(None, 0) c._iobuf.seek(0, os.SEEK_END) pos = c._iobuf.tell() self.assertEqual(pos, 4096 + 4096 + 100)
def test_eagain_on_buffer_size(self, *args): # get a connection that's already fully started c = self.test_successful_connection() # Testing with v3, minimum supported version header = six.b('\x03') + uint16_pack(0) + six.b( '\x00\x00') + int32_pack(20000) responses = [ header + (six.b('a') * (4096 - len(header))), six.b('a') * 4096, socket_error(errno.EAGAIN), six.b('a') * 100, socket_error(errno.EAGAIN) ] def side_effect(*args): response = responses.pop(0) if isinstance(response, socket_error): raise response else: return response c._socket.recv.side_effect = side_effect c.handle_read(None, 0) self.assertEqual(c._current_frame.end_pos, 20000 + len(header)) # the EAGAIN prevents it from reading the last 100 bytes c._iobuf.seek(0, os.SEEK_END) pos = c._iobuf.tell() self.assertEqual(pos, 4096 + 4096) # now tell it to read the last 100 bytes c.handle_read(None, 0) c._iobuf.seek(0, os.SEEK_END) pos = c._iobuf.tell() self.assertEqual(pos, 4096 + 4096 + 100)
def test_egain_on_buffer_size(self, *args): # get a connection that's already fully started c = self.test_successful_connection() header = '\x00\x00\x00\x00' + int32_pack(20000) responses = [ header + ('a' * (4096 - len(header))), 'a' * 4096, socket_error(errno.EAGAIN), 'a' * 100, socket_error(errno.EAGAIN) ] def side_effect(*args): response = responses.pop(0) if isinstance(response, socket_error): raise response else: return response c._socket.recv.side_effect = side_effect c.handle_read(None, 0) self.assertEquals(c._total_reqd_bytes, 20000 + len(header)) # the EAGAIN prevents it from reading the last 100 bytes c._iobuf.seek(0, os.SEEK_END) pos = c._iobuf.tell() self.assertEquals(pos, 4096 + 4096) # now tell it to read the last 100 bytes c.handle_read(None, 0) c._iobuf.seek(0, os.SEEK_END) pos = c._iobuf.tell() self.assertEquals(pos, 4096 + 4096 + 100)
def _check_error_recovery_on_buffer_size(self, error_code): c = self.test_successful_connection() header = six.b('\x00\x00\x00\x00') + int32_pack(20000) responses = [ header + (six.b('a') * (4096 - len(header))), six.b('a') * 4096, socket_error(error_code), six.b('a') * 100, socket_error(error_code) ] def side_effect(*args): response = responses.pop(0) log.debug('about to mock return {}'.format(response)) if isinstance(response, socket_error): raise response else: return response self.get_socket(c).recv.side_effect = side_effect c.handle_read(*self.null_handle_function_args) # the EAGAIN prevents it from reading the last 100 bytes c._iobuf.seek(0, os.SEEK_END) pos = c._iobuf.tell() self.assertEqual(pos, 4096 + 4096) # now tell it to read the last 100 bytes c.handle_read(*self.null_handle_function_args) c._iobuf.seek(0, os.SEEK_END) pos = c._iobuf.tell() self.assertEqual(pos, 4096 + 4096 + 100)
def _get_creds(self): from google_auth_oauthlib.flow import InstalledAppFlow from google.auth.transport.requests import Request from google.auth.exceptions import TransportError import pickle self.creds = None # Open pickle file with access and refresh tokens if it exists if os.path.exists(self.auth_token): with open(self.auth_token, "rb") as token: self.creds = pickle.load(token) if not self.creds or not self.creds.valid: try: if self.creds and self.creds.expired and self.creds.refresh_token: # Credentials expired but contain refresh token self.creds.refresh(Request()) else: # No valid credentials so open authorisation URL in browser flow = InstalledAppFlow.from_client_secrets_file( self.client_secret, [self.auth_scope]) self.creds = flow.run_local_server(port=0) # Save the credentials for the next run with open(self.auth_token, "wb") as token: pickle.dump(self.creds, token) except TransportError as e: # Treat the same as a socket_error raise socket_error(e)
def test_create_client_side_connection_hint_taken_during_fn(self): """Tests Sl4aSession._create_client_side_connection(). Tests that the function will call catch an EADDRNOTAVAIL OSError and call itself again, this time with a hinted port of 0 (random). """ session = mock.Mock() session._create_client_side_connection = mock.Mock() error = socket_error() error.errno = errno.EADDRNOTAVAIL with mock.patch('socket.socket') as socket: # Throw an error when trying to bind to the hinted port. socket_instance = mock.Mock() socket_instance.connect = mock.Mock() socket_instance.connect.side_effect = error socket.return_value = socket_instance Sl4aSession._create_client_side_connection( session, sl4a_ports.Sl4aPorts(0, 2, 3)) fn = session._create_client_side_connection self.assertEqual(fn.call_count, 1) # Asserts that the 1st argument (Sl4aPorts) sent to the function # has a client port of 0. self.assertEqual(fn.call_args_list[0][0][0].client_port, 0)
def test_run_fails_when_specified_port_taken(self): e = socket_error() e.errno = 48 self.assert_run_operates(ports=[12345, 12346], send_port_at_start=True, port_errors=[ e, ])
def _do_recv(self, n): ret = b'' while len(ret) < n: buf = self.sock.recv(n - len(ret)) if buf == b'': raise socket_error('Connection closed by remote end.') ret += buf return ret
def test_socket_error_connection_refused(self): # Test e = socket_error(111) code = self.exception_handler.handle_socket_error(e) # Verify self.assertEqual(code, handler.CODE_SOCKET_ERROR) self.assertTrue('refused' in self.recorder.lines[0]) self.assertEqual([TAG_FAILURE], self.prompt.get_write_tags())
def test_socket_error_on_write(self, *args): c = self.make_connection() # make the OptionsMessage write fail c._socket.send.side_effect = socket_error(errno.EIO, "bad stuff!") c.handle_write(None, 0) # make sure it errored correctly self.assertTrue(c.is_defunct) self.assertIsInstance(c.last_error, socket_error) self.assertTrue(c.connected_event.is_set())
def test_socket_error_on_read(self, *args): c = self.make_connection() # let it write the OptionsMessage c.handle_write(None, 0) # read in a SupportedMessage response c._socket.recv.side_effect = socket_error(errno.EIO, "busy socket") c.handle_read(None, 0) # make sure it errored correctly self.assertTrue(c.is_defunct) self.assertIsInstance(c.last_error, socket_error) self.assertTrue(c.connected_event.is_set())
def test_blocking_on_write(self, *args): c = self.make_connection() # make the OptionsMessage write block c._socket.send.side_effect = socket_error(errno.EAGAIN, "socket busy") c.handle_write(None, 0) self.assertFalse(c.is_defunct) # try again with normal behavior c._socket.send.side_effect = lambda x: len(x) c.handle_write(None, 0) self.assertFalse(c.is_defunct) self.assertTrue(c._socket.send.call_args is not None)
def test_mixed_message_and_buffer_sizes(self): """ Validate that all messages are processed with different scenarios: - various message sizes - various socket buffer sizes - random non-fatal errors raised """ c = self.make_connection() c.process_io_buffer = Mock() errors = cycle([ ssl.SSLError(ssl.SSL_ERROR_WANT_READ), ssl.SSLError(ssl.SSL_ERROR_WANT_WRITE), socket_error(errno.EWOULDBLOCK), socket_error(errno.EAGAIN) ]) for buffer_size in [512, 1024, 2048, 4096, 8192]: c.in_buffer_size = buffer_size for i in range(1, 15): c.process_io_buffer.reset_mock() c._iobuf = io.BytesIO() message = io.BytesIO(six.b('a') * (2**i)) def recv_side_effect(*args): if random.randint(1, 10) % 3 == 0: raise next(errors) return message.read(args[0]) self.get_socket(c).recv.side_effect = recv_side_effect c.handle_read(*self.null_handle_function_args) if c._iobuf.tell(): c.process_io_buffer.assert_called_once() else: c.process_io_buffer.assert_not_called()
def ValidateIpV4(value, allow_empty=False, allow_zeros=False, allow_bcast=False): if not value: return ValidateNonEmpty(value) if not allow_empty else "" try: ipv4 = inet_aton(value) if not inet_ntoa(ipv4) == value: raise socket_error("is not in canonical form") if not allow_zeros and ipv4 == '\x00\x00\x00\x00': raise ValueError("0.0.0.0 address cannot be used") if not allow_bcast and ipv4 == '\xff\xff\xff\xff': raise ValueError("255.255.255.255 address cannot be used") return inet_ntoa(ipv4) except socket_error: raise ValueError("%s is not valid IPv4 address" % value)
def test_connect_rx_socket_fails(self, test_rdma): """Test unavailable IP will throw socket error.""" with patch('hexitec.HexitecFem.RdmaUDP') as rdma_mock: rdma_mock.side_effect = socket_error() with pytest.raises(socket_error) as exc_info: self.rdma = RdmaUDP(test_rdma.master_ip, test_rdma.master_port, test_rdma.fake_ip, test_rdma.master_port, test_rdma.target_ip, test_rdma.target_port, test_rdma.target_ip, test_rdma.target_port, UDPMTU=test_rdma.UDPMTU) error_message = "[Errno 99] Cannot assign requested address" e = "Receive socket IP:Port {}:8888 {}".format( test_rdma.fake_ip, error_message) assert exc_info.type is socket_error assert exc_info.value.args[0] == e
def receiveData(socket): # first we get a string that says how long the serialized string is length = socket.recv(BODY_SIZE_STRING_SIZE) if length == '': raise socket_error("") length = int(length) # If we have received the first part of the data, then we need to get all of it # So we will wait for it to all come in timeout = socket.gettimeout() socket.settimeout(None) # We receive and convert a serialized object string to an actual data object data_string = socket.recv(length) # Return the socket to it's previous blocking state socket.settimeout(timeout) return pickle.loads(data_string)
def receiveData(socket): # first we get a string that says how long the serialized string is length = socket.recv(BODY_SIZE_STRING_SIZE).decode() if length == '': raise socket_error() length = int(length) # If we have received the first part of the data, then we need to get all of it # So we will wait for it to all come in timeout = socket.gettimeout() socket.settimeout(None) # We receive and convert a serialized object string to an actual data object data_string = socket.recv(length) # Return the socket to it's previous blocking state socket.settimeout(timeout) return pickle.loads(data_string)
def test_create_client_side_connection_re_raises_uncaught_errors(self): """Tests Sl4aSession._create_client_side_connection(). Tests that the function will re-raise any socket error that does not have errno.EADDRNOTAVAIL. """ session = mock.Mock() session._create_client_side_connection = mock.Mock() error = socket_error() # Some error that isn't EADDRNOTAVAIL error.errno = errno.ESOCKTNOSUPPORT with mock.patch('socket.socket') as socket: # Throw an error when trying to bind to the hinted port. socket_instance = mock.Mock() socket_instance.connect = mock.Mock() socket_instance.connect.side_effect = error socket.return_value = socket_instance with self.assertRaises(socket_error): Sl4aSession._create_client_side_connection( session, sl4a_ports.Sl4aPorts(0, 2, 3))
InvalidPingResponse(invalid_ping_resp_msg)) # force timeout with mock.patch("course.page.code.DOCKER_TIMEOUT", 0.0001): res = request_python_run_with_retries( run_req={}, run_timeout=0.1, retry_count=0) self.assertEqual(res["result"], "uncaught_error") self.assertEqual(res['message'], "Timeout waiting for container.") self.assertEqual(res["exec_host"], fake_host_ip) self.assertIn(InvalidPingResponse.__name__, res["traceback"]) self.assertIn(invalid_ping_resp_msg, res["traceback"]) with self.subTest( case="Docker ping socket error with erron ECONNRESET"): my_socket_error = socket_error() my_socket_error.errno = errno.ECONNRESET mock_ctn_request.side_effect = my_socket_error # force timeout with mock.patch("course.page.code.DOCKER_TIMEOUT", 0.0001): res = request_python_run_with_retries( run_req={}, run_timeout=0.1, retry_count=0) self.assertEqual(res["result"], "uncaught_error") self.assertEqual(res['message'], "Timeout waiting for container.") self.assertEqual(res["exec_host"], fake_host_ip) self.assertIn(type(my_socket_error).__name__, res["traceback"]) with self.subTest( case="Docker ping socket error with erron ECONNREFUSED"):
def test_handle_exception(self): """ Tests the high level call that branches based on exception type for all types. """ # For each exception type, check that the proper code is returned and # that a failure message has been output. For simplicity in those tests, # reset the tags after each run. code = self.exception_handler.handle_exception( exceptions.BadRequestException({})) self.assertEqual(code, handler.CODE_BAD_REQUEST) self.assertEqual(3, len(self.prompt.tags)) self.assertEqual(TAG_FAILURE, self.prompt.get_write_tags()[0]) self.prompt.tags = [] code = self.exception_handler.handle_exception( exceptions.ConflictException({})) self.assertEqual(code, handler.CODE_CONFLICT) self.assertEqual(1, len(self.prompt.tags)) self.assertEqual(TAG_FAILURE, self.prompt.get_write_tags()[0]) self.prompt.tags = [] code = self.exception_handler.handle_exception( exceptions.ConnectionException({})) self.assertEqual(code, handler.CODE_CONNECTION_EXCEPTION) self.assertEqual(1, len(self.prompt.tags)) self.assertEqual(TAG_FAILURE, self.prompt.get_write_tags()[0]) self.prompt.tags = [] code = self.exception_handler.handle_exception( exceptions.NotFoundException({'resources': { 'repo_id': 'foo' }})) self.assertEqual(code, handler.CODE_NOT_FOUND) self.assertEqual(2, len(self.prompt.tags)) self.assertEqual(TAG_FAILURE, self.prompt.get_write_tags()[0]) self.prompt.tags = [] code = self.exception_handler.handle_exception( exceptions.PermissionsException({})) self.assertEqual(code, handler.CODE_PERMISSIONS_EXCEPTION) self.assertEqual(1, len(self.prompt.tags)) self.assertEqual(TAG_FAILURE, self.prompt.get_write_tags()[0]) self.prompt.tags = [] code = self.exception_handler.handle_exception( exceptions.PulpServerException({})) self.assertEqual(code, handler.CODE_PULP_SERVER_EXCEPTION) self.assertEqual(1, len(self.prompt.tags)) self.assertEqual(TAG_FAILURE, self.prompt.get_write_tags()[0]) self.prompt.tags = [] code = self.exception_handler.handle_exception( InvalidConfig('Test Message')) self.assertEqual(code, handler.CODE_INVALID_CONFIG) self.assertEqual(1, len(self.prompt.tags)) self.assertEqual(TAG_FAILURE, self.prompt.get_write_tags()[0]) self.prompt.tags = [] code = self.exception_handler.handle_exception( WrongHost('expected', 'actual')) self.assertEqual(code, handler.CODE_WRONG_HOST) self.assertEqual(1, len(self.prompt.tags)) self.assertEqual(TAG_FAILURE, self.prompt.get_write_tags()[0]) self.prompt.tags = [] code = self.exception_handler.handle_exception( exceptions.ApacheServerException('Test Message')) self.assertEqual(code, handler.CODE_APACHE_SERVER_EXCEPTION) self.assertEqual(1, len(self.prompt.tags)) self.assertEqual(TAG_FAILURE, self.prompt.get_write_tags()[0]) self.prompt.tags = [] code = self.exception_handler.handle_exception(gaierror()) self.assertEqual(code, handler.CODE_UNKNOWN_HOST) self.assertEqual(1, len(self.prompt.tags)) self.assertEqual(TAG_FAILURE, self.prompt.get_write_tags()[0]) self.prompt.tags = [] code = self.exception_handler.handle_exception(socket_error()) self.assertEqual(code, handler.CODE_SOCKET_ERROR) self.assertEqual(1, len(self.prompt.tags)) self.assertEqual(TAG_FAILURE, self.prompt.get_write_tags()[0]) self.prompt.tags = [] code = self.exception_handler.handle_exception( exceptions.ClientCertificateExpiredException(CERT_FILENAME)) self.assertEqual(code, handler.CODE_PERMISSIONS_EXCEPTION) self.assertEqual([TAG_FAILURE, TAG_PARAGRAPH], self.prompt.get_write_tags()) self.prompt.tags = [] code = self.exception_handler.handle_exception(Exception({})) self.assertEqual(code, handler.CODE_UNEXPECTED) self.assertEqual(1, len(self.prompt.tags)) self.assertEqual(TAG_FAILURE, self.prompt.get_write_tags()[0]) self.prompt.tags = []
def test_run_reruns_when_first_port_taken(self): e = socket_error() e.errno = 48 self.assert_run_operates(ports=[12345, 12346], port_errors=[ e, ])
def getpeername(self): if not self.peer_ip: raise socket_error() return (self.peer_ip, None)
def getsockname(self): if not self.local_ip: raise socket_error() return (self.local_ip, None) # pair(ip addr, port num)
def get_listening_tcp_socket(ip, port, backlog=4096, retry=30, certfile=None, keyfile=None, style=None): """ Returns a socket.socket bound to the given ip and tcp port with other optional parameters. :param ip: The ip address to listen on. ``''`` and ``'*'`` are translated to ``'0.0.0.0'`` which will listen on all configured addresses. :param port: The tcp port to listen on. :param backlog: The amount of system queued connections allowed. :param retry: The number seconds to keep trying to bind the socket, in case there's another process bound but exiting soon. This allows near zero-downtime process handoffs as you start the new one and kill the old. :param certfile: The certificate file if you wish the socket to be ssl wrapped (see ssl.wrap_socket). :param keyfile: The key file if you wish the socket to be ssl wrapped (see ssl.wrap_socket). :param style: The libraries you'd like to use in creating the socket. The default will use the standard Python libraries. ``'Eventlet'`` is recognized and will use the Eventlet libraries. Other styles may added in the future. """ if not style: from socket import AF_INET, AF_INET6, AF_UNSPEC, \ error as socket_error, getaddrinfo, IPPROTO_TCP, socket, \ SOCK_STREAM, SO_KEEPALIVE, SOL_SOCKET, SO_REUSEADDR, TCP_KEEPIDLE from ssl import wrap_socket from time import sleep elif style.lower() == 'eventlet': from eventlet.green.socket import AF_INET, AF_INET6, AF_UNSPEC, \ error as socket_error, getaddrinfo, IPPROTO_TCP, socket, \ SOCK_STREAM, SO_KEEPALIVE, SOL_SOCKET, SO_REUSEADDR, TCP_KEEPIDLE from eventlet.green.ssl import wrap_socket from eventlet import sleep else: from socket import error as socket_error raise socket_error('Socket style %r not understood.' % style) if not ip or ip == '*': ip = '0.0.0.0' family = None for a in getaddrinfo(ip, port, AF_UNSPEC, SOCK_STREAM): if a[0] in (AF_INET, AF_INET6): family = a[0] break if not family: raise socket_error('Could not determine address family of %s:%s for ' 'binding.' % (ip, port)) good_sock = None retry_until = time() + retry while not good_sock and time() < retry_until: try: sock = socket(family, SOCK_STREAM) sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) sock.setsockopt(SOL_SOCKET, SO_KEEPALIVE, 1) sock.setsockopt(IPPROTO_TCP, TCP_KEEPIDLE, 600) sock.bind((ip, port)) sock.listen(backlog) if certfile and keyfile: sock = wrap_socket(sock, certfile=certfile, keyfile=keyfile) good_sock = sock except socket_error, err: if err.errno != EADDRINUSE: raise sleep(0.1)
def test_handle_exception(self): """ Tests the high level call that branches based on exception type for all types. """ # For each exception type, check that the proper code is returned and # that a failure message has been output. For simplicity in those tests, # reset the tags after each run. code = self.exception_handler.handle_exception(exceptions.BadRequestException({})) self.assertEqual(code, handler.CODE_BAD_REQUEST) self.assertEqual(3, len(self.prompt.tags)) self.assertEqual(TAG_FAILURE, self.prompt.get_write_tags()[0]) self.prompt.tags = [] code = self.exception_handler.handle_exception(exceptions.ConflictException({})) self.assertEqual(code, handler.CODE_CONFLICT) self.assertEqual(1, len(self.prompt.tags)) self.assertEqual(TAG_FAILURE, self.prompt.get_write_tags()[0]) self.prompt.tags = [] code = self.exception_handler.handle_exception(exceptions.ConnectionException({})) self.assertEqual(code, handler.CODE_CONNECTION_EXCEPTION) self.assertEqual(1, len(self.prompt.tags)) self.assertEqual(TAG_FAILURE, self.prompt.get_write_tags()[0]) self.prompt.tags = [] code = self.exception_handler.handle_exception(exceptions.NotFoundException({'resources' : {'repo_id' : 'foo'}})) self.assertEqual(code, handler.CODE_NOT_FOUND) self.assertEqual(2, len(self.prompt.tags)) self.assertEqual(TAG_FAILURE, self.prompt.get_write_tags()[0]) self.prompt.tags = [] code = self.exception_handler.handle_exception(exceptions.PermissionsException({})) self.assertEqual(code, handler.CODE_PERMISSIONS_EXCEPTION) self.assertEqual(1, len(self.prompt.tags)) self.assertEqual(TAG_FAILURE, self.prompt.get_write_tags()[0]) self.prompt.tags = [] code = self.exception_handler.handle_exception(exceptions.PulpServerException({})) self.assertEqual(code, handler.CODE_PULP_SERVER_EXCEPTION) self.assertEqual(1, len(self.prompt.tags)) self.assertEqual(TAG_FAILURE, self.prompt.get_write_tags()[0]) self.prompt.tags = [] code = self.exception_handler.handle_exception(InvalidConfig('Test Message')) self.assertEqual(code, handler.CODE_INVALID_CONFIG) self.assertEqual(1, len(self.prompt.tags)) self.assertEqual(TAG_FAILURE, self.prompt.get_write_tags()[0]) self.prompt.tags = [] code = self.exception_handler.handle_exception(WrongHost('expected', 'actual')) self.assertEqual(code, handler.CODE_WRONG_HOST) self.assertEqual(1, len(self.prompt.tags)) self.assertEqual(TAG_FAILURE, self.prompt.get_write_tags()[0]) self.prompt.tags = [] code = self.exception_handler.handle_exception(exceptions.ApacheServerException('Test Message')) self.assertEqual(code, handler.CODE_APACHE_SERVER_EXCEPTION) self.assertEqual(1, len(self.prompt.tags)) self.assertEqual(TAG_FAILURE, self.prompt.get_write_tags()[0]) self.prompt.tags = [] code = self.exception_handler.handle_exception(gaierror()) self.assertEqual(code, handler.CODE_UNKNOWN_HOST) self.assertEqual(1, len(self.prompt.tags)) self.assertEqual(TAG_FAILURE, self.prompt.get_write_tags()[0]) self.prompt.tags = [] code = self.exception_handler.handle_exception(socket_error()) self.assertEqual(code, handler.CODE_SOCKET_ERROR) self.assertEqual(1, len(self.prompt.tags)) self.assertEqual(TAG_FAILURE, self.prompt.get_write_tags()[0]) self.prompt.tags = [] code = self.exception_handler.handle_exception(exceptions.ClientCertificateExpiredException( CERT_FILENAME)) self.assertEqual(code, handler.CODE_PERMISSIONS_EXCEPTION) self.assertEqual([TAG_FAILURE, TAG_PARAGRAPH], self.prompt.get_write_tags()) self.prompt.tags = [] code = self.exception_handler.handle_exception(Exception({})) self.assertEqual(code, handler.CODE_UNEXPECTED) self.assertEqual(1, len(self.prompt.tags)) self.assertEqual(TAG_FAILURE, self.prompt.get_write_tags()[0]) self.prompt.tags = []
try: sock = socket(family, SOCK_STREAM) sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) sock.setsockopt(SOL_SOCKET, SO_KEEPALIVE, 1) sock.setsockopt(IPPROTO_TCP, TCP_KEEPIDLE, 600) sock.bind((ip, port)) sock.listen(backlog) if certfile and keyfile: sock = wrap_socket(sock, certfile=certfile, keyfile=keyfile) good_sock = sock except socket_error, err: if err.errno != EADDRINUSE: raise sleep(0.1) if not good_sock: raise socket_error('Could not bind to %s:%s after trying for %s ' 'seconds.' % (ip, port, retry)) return good_sock def get_listening_udp_socket(ip, port, retry=30, style=None): """ Returns a socket.socket bound to the given ip and tcp port with other optional parameters. :param ip: The ip address to listen on. ``''`` and ``'*'`` are translated to ``'0.0.0.0'`` which will listen on all configured addresses. :param port: The udp port to listen on. :param retry: The number seconds to keep trying to bind the socket, in case there's another process bound but exiting soon. This allows near zero-downtime
def test_run_fails_on_unknown_errno_from_socket(self): e = socket_error() e.errno = 49 self.assert_run_operates(ports=[12345, 12346], port_errors=[ e, ])