예제 #1
0
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))
예제 #2
0
    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)
예제 #3
0
    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)
예제 #4
0
    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)
예제 #5
0
  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)
예제 #6
0
  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)
예제 #7
0
  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)
예제 #8
0
  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)
예제 #9
0
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()
예제 #10
0
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))
예제 #11
0
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()
예제 #12
0
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()
예제 #13
0
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)])