def _connect_and_execute(self, pantsd_handle): port = pantsd_handle.port # Merge the nailgun TTY capability environment variables with the passed environment dict. ng_env = NailgunProtocol.isatty_to_env(self._stdin, self._stdout, self._stderr) modified_env = combined_dict(self._env, ng_env) modified_env['PANTSD_RUNTRACKER_CLIENT_START_TIME'] = str( self._start_time) modified_env['PANTSD_REQUEST_TIMEOUT_LIMIT'] = str( self._bootstrap_options.for_global_scope( ).pantsd_timeout_when_multiple_invocations) assert isinstance(port, int), \ 'port {} is not an integer! It has type {}.'.format(port, type(port)) # Instantiate a NailgunClient. client = NailgunClient( port=port, ins=self._stdin, out=self._stdout, err=self._stderr, exit_on_broken_pipe=True, metadata_base_dir=pantsd_handle.metadata_base_dir) with self._trapped_signals(client), STTYSettings.preserved(): # Execute the command on the pailgun. result = client.execute(self.PANTS_COMMAND, *self._args, **modified_env) # Exit. self._exiter.exit(result)
def _connect_and_execute(self, pantsd_handle: PantsDaemon.Handle) -> ExitCode: port = pantsd_handle.port pid = pantsd_handle.pid # Merge the nailgun TTY capability environment variables with the passed environment dict. ng_env = NailgunProtocol.ttynames_to_env(self._stdin, self._stdout, self._stderr) modified_env = { **self._env, **ng_env, "PANTSD_RUNTRACKER_CLIENT_START_TIME": str(self._start_time), "PANTSD_REQUEST_TIMEOUT_LIMIT": str( self._bootstrap_options.for_global_scope().pantsd_timeout_when_multiple_invocations ), } assert isinstance(port, int), "port {} is not an integer! It has type {}.".format( port, type(port) ) # Instantiate a NailgunClient. client = NailgunClient( port=port, remote_pid=pid, ins=self._stdin, out=self._stdout, err=self._stderr, exit_on_broken_pipe=True, metadata_base_dir=pantsd_handle.metadata_base_dir, ) with self._trapped_signals(client, pantsd_handle.pid), STTYSettings.preserved(): # Execute the command on the pailgun. return client.execute(self._args[0], self._args[1:], modified_env)
def _connect_and_execute(self, port): # Merge the nailgun TTY capability environment variables with the passed environment dict. ng_env = NailgunProtocol.isatty_to_env(self._stdin, self._stdout, self._stderr) modified_env = combined_dict(self._env, ng_env) modified_env['PANTSD_RUNTRACKER_CLIENT_START_TIME'] = str( self._start_time) assert isinstance(port, int), 'port {} is not an integer!'.format(port) # Instantiate a NailgunClient. client = NailgunClient(port=port, ins=self._stdin, out=self._stdout, err=self._stderr, exit_on_broken_pipe=True, expects_pid=True) with self._trapped_signals(client), STTYSettings.preserved(): # Execute the command on the pailgun. result = client.execute(self.PANTS_COMMAND, *self._args, **modified_env) # Exit. self._exiter.exit(result)
def run(self, args=None): # Merge the nailgun TTY capability environment variables with the passed environment dict. ng_env = NailgunProtocol.isatty_to_env(self._stdin, self._stdout, self._stderr) modified_env = self._combine_dicts(self._env, ng_env) # Instantiate a NailgunClient. client = NailgunClient(port=self._port, ins=self._stdin, out=self._stdout, err=self._stderr) with self._trapped_control_c(client): # Execute the command on the pailgun. result = client.execute(self.PANTS_COMMAND, *self._args, **modified_env) # Exit. self._exiter.exit(result)
def _await_nailgun_server(self, stdout, stderr, debug_desc): # TODO(Eric Ayers) Make these cmdline/config parameters once we have a global way to fetch # the global options scope. nailgun_timeout_seconds = 10 max_socket_connect_attempts = 5 nailgun = None port_parse_start = time.time() with safe_open(self._ng_out, 'r') as ng_out: while not nailgun: started = ng_out.readline() if started.find( 'Listening for transport dt_socket at address:') >= 0: nailgun_timeout_seconds = 60 logger.warn( 'Timeout extended to {timeout} seconds for debugger to attach to ng server.' .format(timeout=nailgun_timeout_seconds)) started = ng_out.readline() if started: port = self._parse_nailgun_port(started) nailgun = self._create_ngclient(port, stdout, stderr) logger.debug('Detected ng server up on port {port}'.format( port=port)) elif time.time() - port_parse_start > nailgun_timeout_seconds: raise NailgunClient.NailgunError( 'Failed to read ng output after {sec} seconds.\n {desc}' .format(sec=nailgun_timeout_seconds, desc=debug_desc)) attempt = 0 while nailgun: sock = nailgun.try_connect() if sock: sock.close() endpoint = self._get_nailgun_endpoint() if endpoint: logger.debug( 'Connected to ng server launched with {endpoint}'. format(endpoint=repr(endpoint))) else: raise NailgunClient.NailgunError( 'Failed to connect to ng server.') return nailgun elif attempt > max_socket_connect_attempts: raise nailgun.NailgunError( 'Failed to connect to ng output after {count} connect attempts' .format(count=max_socket_connect_attempts)) attempt += 1 logger.debug( 'Failed to connect on attempt {count}'.format(count=attempt)) time.sleep(0.1)
def test_execute_propagates_connection_error_on_connect( self, mock_session, mock_try_connect): mock_try_connect.side_effect = NailgunClient.NailgunConnectionError( 'oops') with self.assertRaises(NailgunClient.NailgunConnectionError): self.nailgun_client.execute('test')
def _parse_nailgun_port(self, line): match = self._PARSE_NG_PORT.match(line) if not match: raise NailgunClient.NailgunError( 'Failed to determine spawned ng port from response' ' line: {line}'.format(line=line)) return int(match.group(1))
def test_execute_propagates_connection_error_on_connect(self, mock_session, mock_try_connect): mock_try_connect.side_effect = NailgunClient.NailgunConnectionError( "127.0.0.1:31337", 31337, -31336, Exception("oops"), ) with self.assertRaises(NailgunClient.NailgunConnectionError): self.nailgun_client.execute("test")
def _await_socket(self, timeout): """Blocks for the nailgun subprocess to bind and emit a listening port in the nailgun stdout.""" with safe_open(self._ng_stdout, 'r') as ng_stdout: start_time = time.time() accumulated_stdout = '' while 1: readable, _, _ = select.select([ng_stdout], [], [], self._SELECT_WAIT) if readable: line = ng_stdout.readline() # TODO: address deadlock risk here. try: return self._NG_PORT_REGEX.match(line).group(1) except AttributeError: pass accumulated_stdout += line if (time.time() - start_time) > timeout: stderr = read_file(self._ng_stderr) raise NailgunClient.NailgunError( 'Failed to read nailgun output after {sec} seconds!\n' 'Stdout:\n{stdout}\nStderr:\n{stderr}'.format( sec=timeout, stdout=accumulated_stdout, stderr=stderr, ) )
def _await_nailgun_server(self, stdout, stderr): nailgun_timeout_seconds = 5 max_socket_connect_attempts = 10 nailgun = None port_parse_start = time.time() with safe_open(self._ng_out, 'r') as ng_out: while not nailgun: started = ng_out.readline() if started.find( 'Listening for transport dt_socket at address:') >= 0: nailgun_timeout_seconds = 60 log.warn( 'Timeout extended to {timeout} seconds for debugger to attach to ng server.' .format(timeout=nailgun_timeout_seconds)) started = ng_out.readline() if started: port = self._parse_nailgun_port(started) nailgun = self._create_ngclient(port, stdout, stderr) log.debug('Detected ng server up on port %d' % port) elif time.time() - port_parse_start > nailgun_timeout_seconds: raise NailgunClient.NailgunError( 'Failed to read ng output after' ' %s seconds' % nailgun_timeout_seconds) attempt = 0 while nailgun: sock = nailgun.try_connect() if sock: sock.close() endpoint = self._get_nailgun_endpoint() if endpoint: log.debug( 'Connected to ng server launched with %s fingerprint %s pid: %d @ port: %d' % endpoint) else: raise NailgunClient.NailgunError( 'Failed to connect to ng server.') return nailgun elif attempt > max_socket_connect_attempts: raise nailgun.NailgunError( 'Failed to connect to ng output after %d connect attempts' % max_socket_connect_attempts) attempt += 1 log.debug('Failed to connect on attempt %d' % attempt) time.sleep(0.1)
def run(self, args=None): # Merge the nailgun TTY capability environment variables with the passed environment dict. ng_env = NailgunProtocol.isatty_to_env(self._stdin, self._stdout, self._stderr) modified_env = self._combine_dicts(self._env, ng_env) # Instantiate a NailgunClient. client = NailgunClient(port=self._port, ins=self._stdin, out=self._stdout, err=self._stderr, exit_on_broken_pipe=True) with self._trapped_control_c(client): # Execute the command on the pailgun. result = client.execute(self.PANTS_COMMAND, *self._args, **modified_env) # Exit. self._exiter.exit(result)
def _connect_and_execute(self, port): # Merge the nailgun TTY capability environment variables with the passed environment dict. ng_env = NailgunProtocol.isatty_to_env(self._stdin, self._stdout, self._stderr) modified_env = combined_dict(self._env, ng_env) assert isinstance(port, int), 'port {} is not an integer!'.format(port) # Instantiate a NailgunClient. client = NailgunClient(port=port, ins=self._stdin, out=self._stdout, err=self._stderr, exit_on_broken_pipe=True) with self._trapped_signals(client), STTYSettings.preserved(): # Execute the command on the pailgun. result = client.execute(self.PANTS_COMMAND, *self._args, **modified_env) # Exit. self._exiter.exit(result)
def _connect_and_execute(self, port): # Merge the nailgun TTY capability environment variables with the passed environment dict. ng_env = NailgunProtocol.isatty_to_env(self._stdin, self._stdout, self._stderr) modified_env = combined_dict(self._env, ng_env) assert isinstance(port, int), 'port {} is not an integer!'.format(port) # Instantiate a NailgunClient. client = NailgunClient(port=port, ins=self._stdin, out=self._stdout, err=self._stderr, exit_on_broken_pipe=True) with self._trapped_control_c(client): # Execute the command on the pailgun. result = client.execute(self.PANTS_COMMAND, *self._args, **modified_env) # Exit. self._exiter.exit(result)
def ensure_connectable(self, nailgun): """Ensures that a nailgun client is connectable or raises NailgunError.""" attempt_count = 0 while 1: if attempt_count > self._connect_attempts: logger.debug('Failed to connect to ng after {count} attempts' .format(count=self._connect_attempts)) raise NailgunClient.NailgunError('Failed to connect to ng server.') try: sock = nailgun.try_connect() if sock: logger.debug('Connected to ng server {server!r}'.format(server=self)) return finally: sock.close() attempt_count += 1 time.sleep(self.WAIT_INTERVAL)
class TestNailgunClient(unittest.TestCase): def setUp(self): self.nailgun_client = NailgunClient() @mock.patch('pants.java.nailgun_client.RecvBufferedSocket', **PATCH_OPTS) def test_try_connect(self, mock_socket_cls): mock_socket = mock.Mock() mock_socket_cls.return_value = mock_socket self.assertEqual(self.nailgun_client.try_connect(), mock_socket) self.assertEqual(mock_socket_cls.call_count, 1) mock_socket.connect.assert_called_once_with( (NailgunClient.DEFAULT_NG_HOST, NailgunClient.DEFAULT_NG_PORT) ) @mock.patch('pants.java.nailgun_client.RecvBufferedSocket', **PATCH_OPTS) def test_try_connect_socket_error(self, mock_socket_cls): mock_socket = mock.Mock() mock_socket.connect.side_effect = socket.error() mock_socket_cls.return_value = mock_socket with self.assertRaises(NailgunClient.NailgunConnectionError): self.nailgun_client.try_connect() @mock.patch.object(NailgunClient, 'try_connect', **PATCH_OPTS) @mock.patch('pants.java.nailgun_client.NailgunClientSession', **PATCH_OPTS) def test_execute(self, mock_session, mock_try_connect): self.nailgun_client.execute('test') self.assertEqual(mock_try_connect.call_count, 1) self.assertEqual(mock_session.call_count, 1) @mock.patch.object(NailgunClient, 'try_connect', **PATCH_OPTS) @mock.patch('pants.java.nailgun_client.NailgunClientSession', **PATCH_OPTS) def test_execute_propagates_connection_error_on_connect(self, mock_session, mock_try_connect): mock_try_connect.side_effect = NailgunClient.NailgunConnectionError( '127.0.0.1:31337', 31337, -31336, Exception('oops'), ) with self.assertRaises(NailgunClient.NailgunConnectionError): self.nailgun_client.execute('test') @mock.patch.object(NailgunClient, 'try_connect', **PATCH_OPTS) @mock.patch('pants.java.nailgun_client.NailgunClientSession', **PATCH_OPTS) def test_execute_socketerror_on_execute(self, mock_session, mock_try_connect): mock_session.return_value.execute.side_effect = socket.error('oops') with self.assertRaises(NailgunClient.NailgunError): self.nailgun_client.execute('test') @mock.patch.object(NailgunClient, 'try_connect', **PATCH_OPTS) @mock.patch('pants.java.nailgun_client.NailgunClientSession', **PATCH_OPTS) def test_execute_protocolerror_on_execute(self, mock_session, mock_try_connect): mock_session.return_value.ProtocolError = NailgunProtocol.ProtocolError mock_session.return_value.execute.side_effect = NailgunProtocol.ProtocolError('oops') with self.assertRaises(NailgunClient.NailgunError): self.nailgun_client.execute('test') def test_repr(self): self.assertIsNotNone(repr(self.nailgun_client)) @mock.patch('os.kill', **PATCH_OPTS) def test_send_control_c(self, mock_kill): self.nailgun_client._maybe_last_pid = lambda: 31337 self.nailgun_client._maybe_last_pgrp = lambda: -31336 self.nailgun_client.maybe_send_signal(signal.SIGINT) mock_kill.assert_has_calls([ # The pid is killed first, then the pgrp, if include_pgrp=True. mock.call(31337, signal.SIGINT), mock.call(-31336, signal.SIGINT), ]) @mock.patch('os.kill', **PATCH_OPTS) def test_send_signal_no_pgrp(self, mock_kill): self.nailgun_client._maybe_last_pid = lambda: 111111 self.nailgun_client.maybe_send_signal(signal.SIGINT, include_pgrp=False) mock_kill.assert_called_once_with(111111, signal.SIGINT) @mock.patch('os.kill', **PATCH_OPTS) def test_send_control_c_noop_none(self, mock_kill): self.nailgun_client._session = None self.nailgun_client.maybe_send_signal(signal.SIGINT) mock_kill.assert_not_called() @mock.patch('os.kill', **PATCH_OPTS) def test_send_control_c_noop_nopid(self, mock_kill): self.nailgun_client._session = mock.Mock(remote_pid=None, remote_pgrp=None) self.nailgun_client.maybe_send_signal(signal.SIGINT) mock_kill.assert_not_called()
def setUp(self): self.nailgun_client = NailgunClient()
class TestNailgunClient(unittest.TestCase): def setUp(self): self.nailgun_client = NailgunClient() @unittest.mock.patch("pants.java.nailgun_client.RecvBufferedSocket", **PATCH_OPTS) def test_try_connect(self, mock_socket_cls): mock_socket = unittest.mock.Mock() mock_socket_cls.return_value = mock_socket self.assertEqual(self.nailgun_client.try_connect(), mock_socket) self.assertEqual(mock_socket_cls.call_count, 1) mock_socket.connect.assert_called_once_with( (NailgunClient.DEFAULT_NG_HOST, NailgunClient.DEFAULT_NG_PORT) ) @unittest.mock.patch("pants.java.nailgun_client.RecvBufferedSocket", **PATCH_OPTS) def test_try_connect_socket_error(self, mock_socket_cls): mock_socket = unittest.mock.Mock() mock_socket.connect.side_effect = socket.error() mock_socket_cls.return_value = mock_socket with self.assertRaises(NailgunClient.NailgunConnectionError): self.nailgun_client.try_connect() @unittest.mock.patch.object(NailgunClient, "try_connect", **PATCH_OPTS) @unittest.mock.patch("pants.java.nailgun_client.NailgunClientSession", **PATCH_OPTS) def test_execute(self, mock_session, mock_try_connect): self.nailgun_client.execute("test") self.assertEqual(mock_try_connect.call_count, 1) self.assertEqual(mock_session.call_count, 1) @unittest.mock.patch.object(NailgunClient, "try_connect", **PATCH_OPTS) @unittest.mock.patch("pants.java.nailgun_client.NailgunClientSession", **PATCH_OPTS) def test_execute_propagates_connection_error_on_connect(self, mock_session, mock_try_connect): mock_try_connect.side_effect = NailgunClient.NailgunConnectionError( "127.0.0.1:31337", 31337, -31336, Exception("oops"), ) with self.assertRaises(NailgunClient.NailgunConnectionError): self.nailgun_client.execute("test") @unittest.mock.patch.object(NailgunClient, "try_connect", **PATCH_OPTS) @unittest.mock.patch("pants.java.nailgun_client.NailgunClientSession", **PATCH_OPTS) def test_execute_socketerror_on_execute(self, mock_session, mock_try_connect): mock_session.return_value.execute.side_effect = socket.error("oops") with self.assertRaises(NailgunClient.NailgunError): self.nailgun_client.execute("test") @unittest.mock.patch.object(NailgunClient, "try_connect", **PATCH_OPTS) @unittest.mock.patch("pants.java.nailgun_client.NailgunClientSession", **PATCH_OPTS) def test_execute_protocolerror_on_execute(self, mock_session, mock_try_connect): mock_session.return_value.ProtocolError = NailgunProtocol.ProtocolError mock_session.return_value.execute.side_effect = NailgunProtocol.ProtocolError("oops") with self.assertRaises(NailgunClient.NailgunError): self.nailgun_client.execute("test") def test_repr(self): self.assertIsNotNone(repr(self.nailgun_client)) @unittest.mock.patch("os.kill", **PATCH_OPTS) def test_send_control_c(self, mock_kill): self.nailgun_client._maybe_last_pid = lambda: 31337 self.nailgun_client._maybe_last_pgrp = lambda: -31336 self.nailgun_client.maybe_send_signal(signal.SIGINT) mock_kill.assert_has_calls( [ # The pid is killed first, then the pgrp, if include_pgrp=True. unittest.mock.call(31337, signal.SIGINT), unittest.mock.call(-31336, signal.SIGINT), ] ) @unittest.mock.patch("os.kill", **PATCH_OPTS) def test_send_signal_no_pgrp(self, mock_kill): self.nailgun_client._maybe_last_pid = lambda: 111111 self.nailgun_client.maybe_send_signal(signal.SIGINT, include_pgrp=False) mock_kill.assert_called_once_with(111111, signal.SIGINT) @unittest.mock.patch("os.kill", **PATCH_OPTS) def test_send_control_c_noop_none(self, mock_kill): self.nailgun_client._session = None self.nailgun_client.maybe_send_signal(signal.SIGINT) mock_kill.assert_not_called() @unittest.mock.patch("os.kill", **PATCH_OPTS) def test_send_control_c_noop_nopid(self, mock_kill): self.nailgun_client._session = unittest.mock.Mock(remote_pid=None, remote_pgrp=None) self.nailgun_client.maybe_send_signal(signal.SIGINT) mock_kill.assert_not_called()
class TestNailgunClient(unittest.TestCase): def setUp(self): self.nailgun_client = NailgunClient() @unittest.mock.patch("pants.java.nailgun_client.RecvBufferedSocket", **PATCH_OPTS) def test_try_connect(self, mock_socket_cls): mock_socket = unittest.mock.Mock() mock_socket_cls.return_value = mock_socket self.assertEqual(self.nailgun_client.try_connect(), mock_socket) self.assertEqual(mock_socket_cls.call_count, 1) mock_socket.connect.assert_called_once_with( (NailgunClient.DEFAULT_NG_HOST, NailgunClient.DEFAULT_NG_PORT)) @unittest.mock.patch("pants.java.nailgun_client.RecvBufferedSocket", **PATCH_OPTS) def test_try_connect_socket_error(self, mock_socket_cls): mock_socket = unittest.mock.Mock() mock_socket.connect.side_effect = socket.error() mock_socket_cls.return_value = mock_socket with self.assertRaises(NailgunClient.NailgunConnectionError): self.nailgun_client.try_connect() @unittest.mock.patch.object(NailgunClient, "try_connect", **PATCH_OPTS) @unittest.mock.patch("pants.java.nailgun_client.NailgunClientSession", **PATCH_OPTS) def test_execute(self, mock_session, mock_try_connect): self.nailgun_client.execute("test", []) self.assertEqual(mock_try_connect.call_count, 1) self.assertEqual(mock_session.call_count, 1) @unittest.mock.patch.object(NailgunClient, "try_connect", **PATCH_OPTS) @unittest.mock.patch("pants.java.nailgun_client.NailgunClientSession", **PATCH_OPTS) def test_execute_propagates_connection_error_on_connect( self, mock_session, mock_try_connect): mock_try_connect.side_effect = NailgunClient.NailgunConnectionError( "127.0.0.1:31337", Exception("oops"), ) with self.assertRaises(NailgunClient.NailgunConnectionError): self.nailgun_client.execute("test", []) @unittest.mock.patch.object(NailgunClient, "try_connect", **PATCH_OPTS) @unittest.mock.patch("pants.java.nailgun_client.NailgunClientSession", **PATCH_OPTS) def test_execute_socketerror_on_execute(self, mock_session, mock_try_connect): mock_session.return_value.execute.side_effect = socket.error("oops") with self.assertRaises(NailgunClient.NailgunError): self.nailgun_client.execute("test", []) @unittest.mock.patch.object(NailgunClient, "try_connect", **PATCH_OPTS) @unittest.mock.patch("pants.java.nailgun_client.NailgunClientSession", **PATCH_OPTS) def test_execute_protocolerror_on_execute(self, mock_session, mock_try_connect): mock_session.return_value.ProtocolError = NailgunProtocol.ProtocolError mock_session.return_value.execute.side_effect = NailgunProtocol.ProtocolError( "oops") with self.assertRaises(NailgunClient.NailgunError): self.nailgun_client.execute("test", []) def test_repr(self): self.assertIsNotNone(repr(self.nailgun_client)) @unittest.mock.patch("os.kill", **PATCH_OPTS) def test_send_control_c(self, mock_kill): self.nailgun_client.remote_pid = 31337 self.nailgun_client.maybe_send_signal(signal.SIGINT) mock_kill.assert_has_calls([unittest.mock.call(31337, signal.SIGINT)])
class TestNailgunClient(unittest.TestCase): def setUp(self): self.nailgun_client = NailgunClient() @mock.patch('pants.java.nailgun_client.RecvBufferedSocket', **PATCH_OPTS) def test_try_connect(self, mock_socket_cls): mock_socket = mock.Mock() mock_socket_cls.return_value = mock_socket self.assertEquals(self.nailgun_client.try_connect(), mock_socket) self.assertEquals(mock_socket_cls.call_count, 1) mock_socket.connect.assert_called_once_with( (NailgunClient.DEFAULT_NG_HOST, NailgunClient.DEFAULT_NG_PORT)) @mock.patch('pants.java.nailgun_client.RecvBufferedSocket', **PATCH_OPTS) def test_try_connect_socket_error(self, mock_socket_cls): mock_socket = mock.Mock() mock_socket.connect.side_effect = socket.error() mock_socket_cls.return_value = mock_socket with self.assertRaises(NailgunClient.NailgunConnectionError): self.nailgun_client.try_connect() @mock.patch.object(NailgunClient, 'try_connect', **PATCH_OPTS) @mock.patch('pants.java.nailgun_client.NailgunClientSession', **PATCH_OPTS) def test_execute(self, mock_session, mock_try_connect): self.nailgun_client.execute('test') self.assertEquals(mock_try_connect.call_count, 1) self.assertEquals(mock_session.call_count, 1) @mock.patch.object(NailgunClient, 'try_connect', **PATCH_OPTS) @mock.patch('pants.java.nailgun_client.NailgunClientSession', **PATCH_OPTS) def test_execute_propagates_connection_error_on_connect( self, mock_session, mock_try_connect): mock_try_connect.side_effect = NailgunClient.NailgunConnectionError( 'oops') with self.assertRaises(NailgunClient.NailgunConnectionError): self.nailgun_client.execute('test') @mock.patch.object(NailgunClient, 'try_connect', **PATCH_OPTS) @mock.patch('pants.java.nailgun_client.NailgunClientSession', **PATCH_OPTS) def test_execute_socketerror_on_execute(self, mock_session, mock_try_connect): mock_session.return_value.execute.side_effect = socket.error('oops') with self.assertRaises(NailgunClient.NailgunError): self.nailgun_client.execute('test') @mock.patch.object(NailgunClient, 'try_connect', **PATCH_OPTS) @mock.patch('pants.java.nailgun_client.NailgunClientSession', **PATCH_OPTS) def test_execute_protocolerror_on_execute(self, mock_session, mock_try_connect): mock_session.return_value.ProtocolError = NailgunProtocol.ProtocolError mock_session.return_value.execute.side_effect = NailgunProtocol.ProtocolError( 'oops') with self.assertRaises(NailgunClient.NailgunError): self.nailgun_client.execute('test') def test_repr(self): self.assertIsNotNone(repr(self.nailgun_client))
def _create_ngclient(self, port, stdout, stderr, stdin): return NailgunClient(port=port, ins=stdin, out=stdout, err=stderr)
class TestNailgunClient(unittest.TestCase): def setUp(self): self.nailgun_client = NailgunClient() @mock.patch('pants.java.nailgun_client.RecvBufferedSocket', **PATCH_OPTS) def test_try_connect(self, mock_socket_cls): mock_socket = mock.Mock() mock_socket_cls.return_value = mock_socket self.assertEquals(self.nailgun_client.try_connect(), mock_socket) self.assertEquals(mock_socket_cls.call_count, 1) mock_socket.connect.assert_called_once_with( (NailgunClient.DEFAULT_NG_HOST, NailgunClient.DEFAULT_NG_PORT)) @mock.patch('pants.java.nailgun_client.RecvBufferedSocket', **PATCH_OPTS) def test_try_connect_socket_error(self, mock_socket_cls): mock_socket = mock.Mock() mock_socket.connect.side_effect = socket.error() mock_socket_cls.return_value = mock_socket with self.assertRaises(NailgunClient.NailgunConnectionError): self.nailgun_client.try_connect() @mock.patch.object(NailgunClient, 'try_connect', **PATCH_OPTS) @mock.patch('pants.java.nailgun_client.NailgunClientSession', **PATCH_OPTS) def test_execute(self, mock_session, mock_try_connect): self.nailgun_client.execute('test') self.assertEquals(mock_try_connect.call_count, 1) self.assertEquals(mock_session.call_count, 1) @mock.patch.object(NailgunClient, 'try_connect', **PATCH_OPTS) @mock.patch('pants.java.nailgun_client.NailgunClientSession', **PATCH_OPTS) def test_execute_propagates_connection_error_on_connect( self, mock_session, mock_try_connect): mock_try_connect.side_effect = NailgunClient.NailgunConnectionError( '127.0.0.1:31337', 31337, Exception('oops'), None) with self.assertRaises(NailgunClient.NailgunConnectionError): self.nailgun_client.execute('test') @mock.patch.object(NailgunClient, 'try_connect', **PATCH_OPTS) @mock.patch('pants.java.nailgun_client.NailgunClientSession', **PATCH_OPTS) def test_execute_socketerror_on_execute(self, mock_session, mock_try_connect): mock_session.return_value.execute.side_effect = socket.error('oops') with self.assertRaises(NailgunClient.NailgunError): self.nailgun_client.execute('test') @mock.patch.object(NailgunClient, 'try_connect', **PATCH_OPTS) @mock.patch('pants.java.nailgun_client.NailgunClientSession', **PATCH_OPTS) def test_execute_protocolerror_on_execute(self, mock_session, mock_try_connect): mock_session.return_value.ProtocolError = NailgunProtocol.ProtocolError mock_session.return_value.execute.side_effect = NailgunProtocol.ProtocolError( 'oops') with self.assertRaises(NailgunClient.NailgunError): self.nailgun_client.execute('test') def test_repr(self): self.assertIsNotNone(repr(self.nailgun_client)) @mock.patch('os.kill', **PATCH_OPTS) def test_send_control_c(self, mock_kill): self.nailgun_client._session = mock.Mock(remote_pid=31337) self.nailgun_client.send_control_c() mock_kill.assert_called_once_with(31337, signal.SIGINT) @mock.patch('os.kill', **PATCH_OPTS) def test_send_control_c_noop_none(self, mock_kill): self.nailgun_client._session = None self.nailgun_client.send_control_c() mock_kill.assert_not_called() @mock.patch('os.kill', **PATCH_OPTS) def test_send_control_c_noop_nopid(self, mock_kill): self.nailgun_client._session = mock.Mock(remote_pid=None) self.nailgun_client.send_control_c() mock_kill.assert_not_called()
class TestNailgunClient(unittest.TestCase): def setUp(self): self.nailgun_client = NailgunClient() @mock.patch('pants.java.nailgun_client.RecvBufferedSocket', **PATCH_OPTS) def test_try_connect(self, mock_socket_cls): mock_socket = mock.Mock() mock_socket_cls.return_value = mock_socket self.assertEquals(self.nailgun_client.try_connect(), mock_socket) self.assertEquals(mock_socket_cls.call_count, 1) mock_socket.connect.assert_called_once_with( (NailgunClient.DEFAULT_NG_HOST, NailgunClient.DEFAULT_NG_PORT) ) @mock.patch('pants.java.nailgun_client.RecvBufferedSocket', **PATCH_OPTS) def test_try_connect_socket_error(self, mock_socket_cls): mock_socket = mock.Mock() mock_socket.connect.side_effect = socket.error() mock_socket_cls.return_value = mock_socket with self.assertRaises(NailgunClient.NailgunConnectionError): self.nailgun_client.try_connect() @mock.patch.object(NailgunClient, 'try_connect', **PATCH_OPTS) @mock.patch('pants.java.nailgun_client.NailgunClientSession', **PATCH_OPTS) def test_execute(self, mock_session, mock_try_connect): self.nailgun_client.execute('test') self.assertEquals(mock_try_connect.call_count, 1) self.assertEquals(mock_session.call_count, 1) @mock.patch.object(NailgunClient, 'try_connect', **PATCH_OPTS) @mock.patch('pants.java.nailgun_client.NailgunClientSession', **PATCH_OPTS) def test_execute_propagates_connection_error_on_connect(self, mock_session, mock_try_connect): mock_try_connect.side_effect = NailgunClient.NailgunConnectionError('oops') with self.assertRaises(NailgunClient.NailgunConnectionError): self.nailgun_client.execute('test') @mock.patch.object(NailgunClient, 'try_connect', **PATCH_OPTS) @mock.patch('pants.java.nailgun_client.NailgunClientSession', **PATCH_OPTS) def test_execute_socketerror_on_execute(self, mock_session, mock_try_connect): mock_session.return_value.execute.side_effect = socket.error('oops') with self.assertRaises(NailgunClient.NailgunError): self.nailgun_client.execute('test') @mock.patch.object(NailgunClient, 'try_connect', **PATCH_OPTS) @mock.patch('pants.java.nailgun_client.NailgunClientSession', **PATCH_OPTS) def test_execute_protocolerror_on_execute(self, mock_session, mock_try_connect): mock_session.return_value.ProtocolError = NailgunProtocol.ProtocolError mock_session.return_value.execute.side_effect = NailgunProtocol.ProtocolError('oops') with self.assertRaises(NailgunClient.NailgunError): self.nailgun_client.execute('test') def test_repr(self): self.assertIsNotNone(repr(self.nailgun_client)) @mock.patch('os.kill', **PATCH_OPTS) def test_send_control_c(self, mock_kill): self.nailgun_client._session = mock.Mock(remote_pid=31337) self.nailgun_client.send_control_c() mock_kill.assert_called_once_with(31337, signal.SIGINT) @mock.patch('os.kill', **PATCH_OPTS) def test_send_control_c_noop_none(self, mock_kill): self.nailgun_client._session = None self.nailgun_client.send_control_c() mock_kill.assert_not_called() @mock.patch('os.kill', **PATCH_OPTS) def test_send_control_c_noop_nopid(self, mock_kill): self.nailgun_client._session = mock.Mock(remote_pid=None) self.nailgun_client.send_control_c() mock_kill.assert_not_called()
def _create_ngclient(self, port, stdout, stderr): return NailgunClient(port=port, ins=self._ins, out=stdout, err=stderr, workdir=get_buildroot())