Beispiel #1
0
def test_close_stdio(mock_close):
    mock_options = unittest.mock.Mock()
    mock_options_values = unittest.mock.Mock()
    mock_options.for_global_scope.return_value = mock_options_values
    mock_options_values.pants_subprocessdir = "non_existent_dir"
    mock_server = unittest.mock.Mock()

    def create_services(bootstrap_options, legacy_graph_scheduler):
        return PantsServices()

    pantsd = PantsDaemon(
        native=Native(),
        work_dir="test_work_dir",
        log_level=logging.INFO,
        server=mock_server,
        core=PantsDaemonCore(create_services),
        metadata_base_dir="/tmp/pants_test_metadata_dir",
        bootstrap_options=mock_options,
    )

    with stdio_as(-1, -1, -1):
        handles = (sys.stdin, sys.stdout, sys.stderr)
        fds = [h.fileno() for h in handles]
        pantsd._close_stdio()
        mock_close.assert_has_calls(unittest.mock.call(x) for x in fds)
        for handle in handles:
            assert handle.closed is True
Beispiel #2
0
class PantsDaemonTest(TestBase):
    def setUp(self):
        super().setUp()
        mock_options = unittest.mock.Mock()
        mock_options_values = unittest.mock.Mock()
        mock_options.for_global_scope.return_value = mock_options_values
        mock_options_values.pants_subprocessdir = "non_existent_dir"
        mock_server = unittest.mock.Mock()

        def create_services(bootstrap_options, legacy_graph_scheduler):
            return PantsServices()

        self.pantsd = PantsDaemon(
            native=Native(),
            work_dir="test_work_dir",
            log_level=logging.INFO,
            server=mock_server,
            core=PantsDaemonCore(create_services),
            metadata_base_dir="/tmp/pants_test_metadata_dir",
            bootstrap_options=mock_options,
        )

    @unittest.mock.patch("os.close", **PATCH_OPTS)
    def test_close_stdio(self, mock_close):
        with stdio_as(-1, -1, -1):
            handles = (sys.stdin, sys.stdout, sys.stderr)
            fds = [h.fileno() for h in handles]
            self.pantsd._close_stdio()
            mock_close.assert_has_calls(unittest.mock.call(x) for x in fds)
            for handle in handles:
                self.assertTrue(handle.closed, f"{handle} was not closed")
Beispiel #3
0
class PantsDaemonTest(TestBase):
    def setUp(self):
        super(PantsDaemonTest, self).setUp()
        mock_options = mock.Mock()
        mock_options.pants_subprocessdir = 'non_existent_dir'
        self.pantsd = PantsDaemon(None, 'test_buildroot', 'test_work_dir',
                                  logging.INFO, PantsServices(),
                                  '/tmp/pants_test_metadata_dir', mock_options)
        self.mock_killswitch = mock.Mock()
        self.pantsd._kill_switch = self.mock_killswitch
        self.mock_service = mock.create_autospec(PantsService, spec_set=True)

    @mock.patch('os.close', **PATCH_OPTS)
    def test_close_stdio(self, mock_close):
        with stdio_as(-1, -1, -1):
            handles = (sys.stdin, sys.stdout, sys.stderr)
            fds = [h.fileno() for h in handles]
            self.pantsd._close_stdio()
            mock_close.assert_has_calls(mock.call(x) for x in fds)
            for handle in handles:
                self.assertTrue(handle.closed,
                                '{} was not closed'.format(handle))

    def test_shutdown(self):
        mock_thread = mock.Mock()
        mock_service_thread_map = {self.mock_service: mock_thread}

        self.pantsd.shutdown(mock_service_thread_map)

        self.mock_service.terminate.assert_called_once_with()
        self.assertTrue(self.pantsd.is_killed)
        mock_thread.join.assert_called_once_with(
            PantsDaemon.JOIN_TIMEOUT_SECONDS)

    def test_run_services_no_services(self):
        self.pantsd._run_services(PantsServices())

    @mock.patch('threading.Thread', **PATCH_OPTS)
    @mock.patch.object(PantsDaemon, 'shutdown', spec_set=True)
    def test_run_services_startupfailure(self, mock_shutdown, mock_thread):
        mock_thread.return_value.start.side_effect = RuntimeError('oops!')

        with self.assertRaises(PantsDaemon.StartupFailure):
            self.pantsd._run_services(
                PantsServices(services=(self.mock_service, )))

        self.assertGreater(mock_shutdown.call_count, 0)

    @mock.patch('threading.Thread', **PATCH_OPTS)
    @mock.patch.object(PantsDaemon, 'shutdown', spec_set=True)
    @mock.patch.object(PantsDaemon,
                       'options_fingerprint',
                       spec_set=True,
                       new_callable=mock.PropertyMock)
    def test_run_services_runtimefailure(self, mock_fp, mock_shutdown,
                                         mock_thread):
        self.mock_killswitch.is_set.side_effect = [False, False, True]
        mock_thread.return_value.is_alive.side_effect = [True, False]
        mock_fp.return_value = 'some_sha'

        with self.assertRaises(PantsDaemon.RuntimeFailure):
            self.pantsd._run_services(
                PantsServices(services=(self.mock_service, )))

        self.assertGreater(mock_shutdown.call_count, 0)
Beispiel #4
0
class PantsDaemonTest(TestBase):
    def setUp(self):
        super().setUp()
        mock_options = unittest.mock.Mock()
        mock_options_values = unittest.mock.Mock()
        mock_options.for_global_scope.return_value = mock_options_values
        mock_options_values.pants_subprocessdir = "non_existent_dir"

        self.pantsd = PantsDaemon(
            None,
            "test_buildroot",
            "test_work_dir",
            logging.INFO,
            PantsServices(),
            "/tmp/pants_test_metadata_dir",
            mock_options,
        )
        self.mock_killswitch = unittest.mock.Mock()
        self.pantsd._kill_switch = self.mock_killswitch
        self.mock_service = unittest.mock.create_autospec(PantsService,
                                                          spec_set=True)

    @unittest.mock.patch("os.close", **PATCH_OPTS)
    def test_close_stdio(self, mock_close):
        with stdio_as(-1, -1, -1):
            handles = (sys.stdin, sys.stdout, sys.stderr)
            fds = [h.fileno() for h in handles]
            self.pantsd._close_stdio()
            mock_close.assert_has_calls(unittest.mock.call(x) for x in fds)
            for handle in handles:
                self.assertTrue(handle.closed, f"{handle} was not closed")

    def test_shutdown(self):
        mock_thread = unittest.mock.Mock()
        mock_service_thread_map = {self.mock_service: mock_thread}

        self.pantsd.shutdown(mock_service_thread_map)

        self.mock_service.terminate.assert_called_once_with()
        self.assertTrue(self.pantsd.is_killed)
        mock_thread.join.assert_called_once_with(
            PantsDaemon.JOIN_TIMEOUT_SECONDS)

    def test_run_services_no_services(self):
        self.pantsd._run_services(PantsServices())

    @unittest.mock.patch("threading.Thread", **PATCH_OPTS)
    @unittest.mock.patch.object(PantsDaemon, "shutdown", spec_set=True)
    def test_run_services_startupfailure(self, mock_shutdown, mock_thread):
        mock_thread.return_value.start.side_effect = RuntimeError("oops!")

        with self.assertRaises(PantsDaemon.StartupFailure):
            self.pantsd._run_services(
                PantsServices(services=(self.mock_service, )))

        self.assertGreater(mock_shutdown.call_count, 0)

    @unittest.mock.patch("threading.Thread", **PATCH_OPTS)
    @unittest.mock.patch.object(PantsDaemon, "shutdown", spec_set=True)
    @unittest.mock.patch.object(
        PantsDaemonProcessManager,
        "options_fingerprint",
        spec_set=True,
        new_callable=unittest.mock.PropertyMock,
    )
    def test_run_services_runtimefailure(self, mock_fp, mock_shutdown,
                                         mock_thread):
        self.mock_killswitch.is_set.side_effect = [False, False, True]
        mock_thread.return_value.is_alive.side_effect = [True, False]
        mock_fp.return_value = "some_sha"

        with self.assertRaises(PantsDaemon.RuntimeFailure):
            self.pantsd._run_services(
                PantsServices(services=(self.mock_service, )))

        self.assertGreater(mock_shutdown.call_count, 0)
Beispiel #5
0
class PantsDaemonTest(TestBase):
  def setUp(self):
    super(PantsDaemonTest, self).setUp()
    mock_options = mock.Mock()
    mock_options.pants_subprocessdir = 'non_existent_dir'
    self.pantsd = PantsDaemon(None,
                              'test_buildroot',
                              'test_work_dir',
                              logging.INFO,
                              [],
                              {},
                              '/tmp/pants_test_metadata_dir',
                              mock_options)
    self.mock_killswitch = mock.Mock()
    self.pantsd._kill_switch = self.mock_killswitch
    self.mock_service = mock.create_autospec(PantsService, spec_set=True)

  @mock.patch('os.close', **PATCH_OPTS)
  def test_close_stdio(self, mock_close):
    with stdio_as(-1, -1, -1):
      handles = (sys.stdin, sys.stdout, sys.stderr)
      fds = [h.fileno() for h in handles]
      self.pantsd._close_stdio()
      mock_close.assert_has_calls(mock.call(x) for x in fds)
      for handle in handles:
        self.assertTrue(handle.closed, '{} was not closed'.format(handle))

  def test_shutdown(self):
    mock_thread = mock.Mock()
    mock_service_thread_map = {self.mock_service: mock_thread}

    self.pantsd.shutdown(mock_service_thread_map)

    self.mock_service.terminate.assert_called_once_with()
    self.assertTrue(self.pantsd.is_killed)
    mock_thread.join.assert_called_once_with(PantsDaemon.JOIN_TIMEOUT_SECONDS)

  def test_run_services_no_services(self):
    self.pantsd._run_services([])

  @mock.patch('threading.Thread', **PATCH_OPTS)
  @mock.patch.object(PantsDaemon, 'shutdown', spec_set=True)
  def test_run_services_startupfailure(self, mock_shutdown, mock_thread):
    mock_thread.return_value.start.side_effect = RuntimeError('oops!')

    with self.assertRaises(PantsDaemon.StartupFailure):
      self.pantsd._run_services([self.mock_service])

    self.assertGreater(mock_shutdown.call_count, 0)

  @mock.patch('threading.Thread', **PATCH_OPTS)
  @mock.patch.object(PantsDaemon, 'shutdown', spec_set=True)
  @mock.patch.object(PantsDaemon, 'options_fingerprint', spec_set=True,
                     new_callable=mock.PropertyMock)
  def test_run_services_runtimefailure(self, mock_fp, mock_shutdown, mock_thread):
    self.mock_killswitch.is_set.side_effect = [False, False, True]
    mock_thread.return_value.is_alive.side_effect = [True, False]
    mock_fp.return_value = 'some_sha'

    with self.assertRaises(PantsDaemon.RuntimeFailure):
      self.pantsd._run_services([self.mock_service])

    self.assertGreater(mock_shutdown.call_count, 0)