Esempio n. 1
0
class TestProcessMetadataManager(BaseTest):
  NAME = '_test_'
  TEST_KEY = 'TEST'
  TEST_VALUE = '300'
  TEST_VALUE_INT = 300
  BUILDROOT = '/mock_buildroot/'

  def setUp(self):
    super(TestProcessMetadataManager, self).setUp()
    self.pmm = ProcessMetadataManager(metadata_base_dir=self.subprocess_dir)

  def test_maybe_cast(self):
    self.assertIsNone(self.pmm._maybe_cast(None, int))
    self.assertEqual(self.pmm._maybe_cast('3333', int), 3333)
    self.assertEqual(self.pmm._maybe_cast('ssss', int), 'ssss')

  def test_get_metadata_dir_by_name(self):
    self.pmm = ProcessMetadataManager(metadata_base_dir=self.BUILDROOT)
    self.assertEqual(self.pmm._get_metadata_dir_by_name(self.NAME),
                     os.path.join(self.BUILDROOT, self.NAME))

  def test_maybe_init_metadata_dir_by_name(self):
    with mock.patch('pants.pantsd.process_manager.safe_mkdir', **PATCH_OPTS) as mock_mkdir:
      self.pmm._maybe_init_metadata_dir_by_name(self.NAME)
      mock_mkdir.assert_called_once_with(
        self.pmm._get_metadata_dir_by_name(self.NAME))

  def test_readwrite_metadata_by_name(self):
    with temporary_dir() as tmpdir, \
         mock.patch('pants.pantsd.process_manager.get_buildroot', return_value=tmpdir):
      self.pmm.write_metadata_by_name(self.NAME, self.TEST_KEY, self.TEST_VALUE)
      self.assertEqual(
        self.pmm.read_metadata_by_name(self.NAME, self.TEST_KEY),
        self.TEST_VALUE
      )
      self.assertEqual(
        self.pmm.read_metadata_by_name(self.NAME, self.TEST_KEY, int),
        self.TEST_VALUE_INT
      )

  def test_deadline_until(self):
    with self.assertRaises(self.pmm.Timeout):
      self.pmm._deadline_until(lambda: False, timeout=.1)

  def test_wait_for_file(self):
    with temporary_dir() as td:
      test_filename = os.path.join(td, 'test.out')
      safe_file_dump(test_filename, 'test')
      self.pmm._wait_for_file(test_filename, timeout=.1)

  def test_wait_for_file_timeout(self):
    with temporary_dir() as td:
      with self.assertRaises(self.pmm.Timeout):
        self.pmm._wait_for_file(os.path.join(td, 'non_existent_file'), timeout=.1)

  def test_await_metadata_by_name(self):
    with temporary_dir() as tmpdir, \
         mock.patch('pants.pantsd.process_manager.get_buildroot', return_value=tmpdir):
      self.pmm.write_metadata_by_name(self.NAME, self.TEST_KEY, self.TEST_VALUE)

      self.assertEquals(
        self.pmm.await_metadata_by_name(self.NAME, self.TEST_KEY, .1),
        self.TEST_VALUE
      )

  def test_purge_metadata(self):
    with mock.patch('pants.pantsd.process_manager.rm_rf') as mock_rm:
      self.pmm.purge_metadata_by_name(self.NAME)
    self.assertGreater(mock_rm.call_count, 0)

  def test_purge_metadata_error(self):
    with mock.patch('pants.pantsd.process_manager.rm_rf') as mock_rm:
      mock_rm.side_effect = OSError(errno.EACCES, os.strerror(errno.EACCES))
      with self.assertRaises(ProcessManager.MetadataError):
        self.pmm.purge_metadata_by_name(self.NAME)
    self.assertGreater(mock_rm.call_count, 0)
Esempio n. 2
0
class TestProcessMetadataManager(TestBase):
    NAME = "_test_"
    TEST_KEY = "TEST"
    TEST_VALUE = "300"
    TEST_VALUE_INT = 300
    BUILDROOT = "/mock_buildroot/"

    def setUp(self):
        super().setUp()
        self.pmm = ProcessMetadataManager(
            metadata_base_dir=self.subprocess_dir)

    def test_maybe_cast(self):
        self.assertIsNone(self.pmm._maybe_cast(None, int))
        self.assertEqual(self.pmm._maybe_cast("3333", int), 3333)
        self.assertEqual(self.pmm._maybe_cast("ssss", int), "ssss")

    def test_get_metadata_dir_by_name(self):
        self.pmm = ProcessMetadataManager(metadata_base_dir=self.BUILDROOT)
        self.assertEqual(
            self.pmm._get_metadata_dir_by_name(self.NAME, self.BUILDROOT),
            os.path.join(self.BUILDROOT, self.NAME),
        )

    def test_maybe_init_metadata_dir_by_name(self):
        with unittest.mock.patch("pants.pantsd.process_manager.safe_mkdir",
                                 **PATCH_OPTS) as mock_mkdir:
            self.pmm._maybe_init_metadata_dir_by_name(self.NAME)
            mock_mkdir.assert_called_once_with(
                self.pmm._get_metadata_dir_by_name(self.NAME,
                                                   self.subprocess_dir))

    def test_readwrite_metadata_by_name(self):
        with temporary_dir() as tmpdir, unittest.mock.patch(
                "pants.pantsd.process_manager.get_buildroot",
                return_value=tmpdir):
            self.pmm.write_metadata_by_name(self.NAME, self.TEST_KEY,
                                            self.TEST_VALUE)
            self.assertEqual(
                self.pmm.read_metadata_by_name(self.NAME, self.TEST_KEY),
                self.TEST_VALUE)
            self.assertEqual(
                self.pmm.read_metadata_by_name(self.NAME, self.TEST_KEY, int),
                self.TEST_VALUE_INT)

    @pytest.mark.skip(
        reason="flaky: https://github.com/pantsbuild/pants/issues/6836")
    def test_deadline_until(self):
        with self.assertRaises(ProcessMetadataManager.Timeout):
            with self.captured_logging(logging.INFO) as captured:
                self.pmm._deadline_until(lambda: False,
                                         "the impossible",
                                         timeout=0.5,
                                         info_interval=0.1)
        self.assertTrue(
            4 <= len(captured.infos()) <= 6,
            f"Expected between 4 and 6 infos, got: {captured.infos()}",
        )

    def test_wait_for_file(self):
        with temporary_dir() as td:
            test_filename = os.path.join(td, "test.out")
            safe_file_dump(test_filename, "test")
            self.pmm._wait_for_file(test_filename,
                                    "file to be created",
                                    "file was created",
                                    timeout=0.1)

    def test_wait_for_file_timeout(self):
        with temporary_dir() as td:
            with self.assertRaises(ProcessMetadataManager.Timeout):
                self.pmm._wait_for_file(
                    os.path.join(td, "non_existent_file"),
                    "file to be created",
                    "file was created",
                    timeout=0.1,
                )

    def test_await_metadata_by_name(self):
        with temporary_dir() as tmpdir, unittest.mock.patch(
                "pants.pantsd.process_manager.get_buildroot",
                return_value=tmpdir):
            self.pmm.write_metadata_by_name(self.NAME, self.TEST_KEY,
                                            self.TEST_VALUE)

            self.assertEqual(
                self.pmm.await_metadata_by_name(self.NAME, self.TEST_KEY,
                                                "metadata to be created",
                                                "metadata was created", 0.1),
                self.TEST_VALUE,
            )

    def test_purge_metadata(self):
        with unittest.mock.patch(
                "pants.pantsd.process_manager.rm_rf") as mock_rm:
            self.pmm.purge_metadata_by_name(self.NAME)
        self.assertGreater(mock_rm.call_count, 0)

    def test_purge_metadata_error(self):
        with unittest.mock.patch(
                "pants.pantsd.process_manager.rm_rf") as mock_rm:
            mock_rm.side_effect = OSError(errno.EACCES,
                                          os.strerror(errno.EACCES))
            with self.assertRaises(ProcessMetadataManager.MetadataError):
                self.pmm.purge_metadata_by_name(self.NAME)
        self.assertGreater(mock_rm.call_count, 0)
Esempio n. 3
0
class TestProcessMetadataManager(TestBase):
  NAME = '_test_'
  TEST_KEY = 'TEST'
  TEST_VALUE = '300'
  TEST_VALUE_INT = 300
  BUILDROOT = '/mock_buildroot/'

  def setUp(self):
    super(TestProcessMetadataManager, self).setUp()
    self.pmm = ProcessMetadataManager(metadata_base_dir=self.subprocess_dir)

  def test_maybe_cast(self):
    self.assertIsNone(self.pmm._maybe_cast(None, int))
    self.assertEqual(self.pmm._maybe_cast('3333', int), 3333)
    self.assertEqual(self.pmm._maybe_cast('ssss', int), 'ssss')

  def test_get_metadata_dir_by_name(self):
    self.pmm = ProcessMetadataManager(metadata_base_dir=self.BUILDROOT)
    self.assertEqual(self.pmm._get_metadata_dir_by_name(self.NAME, self.BUILDROOT),
                     os.path.join(self.BUILDROOT, self.NAME))

  def test_maybe_init_metadata_dir_by_name(self):
    with mock.patch('pants.pantsd.process_manager.safe_mkdir', **PATCH_OPTS) as mock_mkdir:
      self.pmm._maybe_init_metadata_dir_by_name(self.NAME)
      mock_mkdir.assert_called_once_with(
        self.pmm._get_metadata_dir_by_name(self.NAME, self.subprocess_dir))

  def test_readwrite_metadata_by_name(self):
    with temporary_dir() as tmpdir, \
         mock.patch('pants.pantsd.process_manager.get_buildroot', return_value=tmpdir):
      self.pmm.write_metadata_by_name(self.NAME, self.TEST_KEY, self.TEST_VALUE)
      self.assertEqual(
        self.pmm.read_metadata_by_name(self.NAME, self.TEST_KEY),
        self.TEST_VALUE
      )
      self.assertEqual(
        self.pmm.read_metadata_by_name(self.NAME, self.TEST_KEY, int),
        self.TEST_VALUE_INT
      )

  def test_deadline_until(self):
    with self.assertRaises(self.pmm.Timeout):
      with self.captured_logging(logging.INFO) as captured:
        self.pmm._deadline_until(lambda: False, 'the impossible', timeout=.5, info_interval=.1)
    self.assertTrue(4 <= len(captured.infos()) <= 6,
                    'Expected between 4 and 6 infos, got: {}'.format(captured.infos()))

  def test_wait_for_file(self):
    with temporary_dir() as td:
      test_filename = os.path.join(td, 'test.out')
      safe_file_dump(test_filename, 'test')
      self.pmm._wait_for_file(test_filename, timeout=.1)

  def test_wait_for_file_timeout(self):
    with temporary_dir() as td:
      with self.assertRaises(self.pmm.Timeout):
        self.pmm._wait_for_file(os.path.join(td, 'non_existent_file'), timeout=.1)

  def test_await_metadata_by_name(self):
    with temporary_dir() as tmpdir, \
         mock.patch('pants.pantsd.process_manager.get_buildroot', return_value=tmpdir):
      self.pmm.write_metadata_by_name(self.NAME, self.TEST_KEY, self.TEST_VALUE)

      self.assertEquals(
        self.pmm.await_metadata_by_name(self.NAME, self.TEST_KEY, .1),
        self.TEST_VALUE
      )

  def test_purge_metadata(self):
    with mock.patch('pants.pantsd.process_manager.rm_rf') as mock_rm:
      self.pmm.purge_metadata_by_name(self.NAME)
    self.assertGreater(mock_rm.call_count, 0)

  def test_purge_metadata_error(self):
    with mock.patch('pants.pantsd.process_manager.rm_rf') as mock_rm:
      mock_rm.side_effect = OSError(errno.EACCES, os.strerror(errno.EACCES))
      with self.assertRaises(ProcessManager.MetadataError):
        self.pmm.purge_metadata_by_name(self.NAME)
    self.assertGreater(mock_rm.call_count, 0)
Esempio n. 4
0
 def test_maybe_cast(self):
   self.assertIsNone(ProcessMetadataManager._maybe_cast(None, int))
   self.assertEqual(ProcessMetadataManager._maybe_cast('3333', int), 3333)
   self.assertEqual(ProcessMetadataManager._maybe_cast('ssss', int), 'ssss')
Esempio n. 5
0
 def test_maybe_cast(self):
     self.assertIsNone(ProcessMetadataManager._maybe_cast(None, int))
     self.assertEqual(ProcessMetadataManager._maybe_cast("3333", int), 3333)
     self.assertEqual(ProcessMetadataManager._maybe_cast("ssss", int), "ssss")
Esempio n. 6
0
 def test_maybe_cast(self):
     self.assertIsNone(ProcessMetadataManager._maybe_cast(None, int))
     self.assertEqual(ProcessMetadataManager._maybe_cast('3333', int), 3333)
     self.assertEqual(ProcessMetadataManager._maybe_cast('ssss', int),
                      'ssss')
class TestProcessMetadataManager(BaseTest):
    NAME = "_test_"
    TEST_KEY = "TEST"
    TEST_VALUE = "300"
    TEST_VALUE_INT = 300
    BUILDROOT = "/mock_buildroot/"

    def setUp(self):
        super(TestProcessMetadataManager, self).setUp()
        self.pmm = ProcessMetadataManager(metadata_base_dir=self.subprocess_dir)

    def test_maybe_cast(self):
        self.assertIsNone(self.pmm._maybe_cast(None, int))
        self.assertEqual(self.pmm._maybe_cast("3333", int), 3333)
        self.assertEqual(self.pmm._maybe_cast("ssss", int), "ssss")

    def test_get_metadata_dir_by_name(self):
        self.pmm = ProcessMetadataManager(metadata_base_dir=self.BUILDROOT)
        self.assertEqual(self.pmm._get_metadata_dir_by_name(self.NAME), os.path.join(self.BUILDROOT, self.NAME))

    def test_maybe_init_metadata_dir_by_name(self):
        with mock.patch("pants.pantsd.process_manager.safe_mkdir", **PATCH_OPTS) as mock_mkdir:
            self.pmm._maybe_init_metadata_dir_by_name(self.NAME)
            mock_mkdir.assert_called_once_with(self.pmm._get_metadata_dir_by_name(self.NAME))

    def test_readwrite_metadata_by_name(self):
        with temporary_dir() as tmpdir, mock.patch("pants.pantsd.process_manager.get_buildroot", return_value=tmpdir):
            self.pmm.write_metadata_by_name(self.NAME, self.TEST_KEY, self.TEST_VALUE)
            self.assertEqual(self.pmm.read_metadata_by_name(self.NAME, self.TEST_KEY), self.TEST_VALUE)
            self.assertEqual(self.pmm.read_metadata_by_name(self.NAME, self.TEST_KEY, int), self.TEST_VALUE_INT)

    def test_deadline_until(self):
        with self.assertRaises(self.pmm.Timeout):
            self.pmm._deadline_until(lambda: False, timeout=0.1)

    def test_wait_for_file(self):
        with temporary_dir() as td:
            test_filename = os.path.join(td, "test.out")
            safe_file_dump(test_filename, "test")
            self.pmm._wait_for_file(test_filename, timeout=0.1)

    def test_wait_for_file_timeout(self):
        with temporary_dir() as td:
            with self.assertRaises(self.pmm.Timeout):
                self.pmm._wait_for_file(os.path.join(td, "non_existent_file"), timeout=0.1)

    def test_await_metadata_by_name(self):
        with temporary_dir() as tmpdir, mock.patch("pants.pantsd.process_manager.get_buildroot", return_value=tmpdir):
            self.pmm.write_metadata_by_name(self.NAME, self.TEST_KEY, self.TEST_VALUE)

            self.assertEquals(self.pmm.await_metadata_by_name(self.NAME, self.TEST_KEY, 0.1), self.TEST_VALUE)

    def test_purge_metadata(self):
        with mock.patch("pants.pantsd.process_manager.rm_rf") as mock_rm:
            self.pmm.purge_metadata_by_name(self.NAME)
        self.assertGreater(mock_rm.call_count, 0)

    def test_purge_metadata_error(self):
        with mock.patch("pants.pantsd.process_manager.rm_rf") as mock_rm:
            mock_rm.side_effect = OSError(errno.EACCES, os.strerror(errno.EACCES))
            with self.assertRaises(ProcessManager.MetadataError):
                self.pmm.purge_metadata_by_name(self.NAME)
        self.assertGreater(mock_rm.call_count, 0)
Esempio n. 8
0
class TestProcessMetadataManager(TestBase):
  NAME = '_test_'
  TEST_KEY = 'TEST'
  TEST_VALUE = '300'
  TEST_VALUE_INT = 300
  BUILDROOT = '/mock_buildroot/'

  def setUp(self):
    super().setUp()
    self.pmm = ProcessMetadataManager(metadata_base_dir=self.subprocess_dir)

  def test_maybe_cast(self):
    self.assertIsNone(self.pmm._maybe_cast(None, int))
    self.assertEqual(self.pmm._maybe_cast('3333', int), 3333)
    self.assertEqual(self.pmm._maybe_cast('ssss', int), 'ssss')

  def test_get_metadata_dir_by_name(self):
    self.pmm = ProcessMetadataManager(metadata_base_dir=self.BUILDROOT)
    self.assertEqual(self.pmm._get_metadata_dir_by_name(self.NAME, self.BUILDROOT),
                     os.path.join(self.BUILDROOT, self.NAME))

  def test_maybe_init_metadata_dir_by_name(self):
    with unittest.mock.patch('pants.pantsd.process_manager.safe_mkdir', **PATCH_OPTS) as mock_mkdir:
      self.pmm._maybe_init_metadata_dir_by_name(self.NAME)
      mock_mkdir.assert_called_once_with(
        self.pmm._get_metadata_dir_by_name(self.NAME, self.subprocess_dir))

  def test_readwrite_metadata_by_name(self):
    with temporary_dir() as tmpdir, \
         unittest.mock.patch('pants.pantsd.process_manager.get_buildroot', return_value=tmpdir):
      self.pmm.write_metadata_by_name(self.NAME, self.TEST_KEY, self.TEST_VALUE)
      self.assertEqual(
        self.pmm.read_metadata_by_name(self.NAME, self.TEST_KEY),
        self.TEST_VALUE
      )
      self.assertEqual(
        self.pmm.read_metadata_by_name(self.NAME, self.TEST_KEY, int),
        self.TEST_VALUE_INT
      )

  @pytest.mark.flaky(retries=1)  # https://github.com/pantsbuild/pants/issues/6836
  def test_deadline_until(self):
    with self.assertRaises(ProcessMetadataManager.Timeout):
      with self.captured_logging(logging.INFO) as captured:
        self.pmm._deadline_until(lambda: False, 'the impossible', timeout=.5, info_interval=.1)
    self.assertTrue(4 <= len(captured.infos()) <= 6,
                    f'Expected between 4 and 6 infos, got: {captured.infos()}')

  def test_wait_for_file(self):
    with temporary_dir() as td:
      test_filename = os.path.join(td, 'test.out')
      safe_file_dump(test_filename, 'test')
      self.pmm._wait_for_file(test_filename, timeout=.1)

  def test_wait_for_file_timeout(self):
    with temporary_dir() as td:
      with self.assertRaises(ProcessMetadataManager.Timeout):
        self.pmm._wait_for_file(os.path.join(td, 'non_existent_file'), timeout=.1)

  def test_await_metadata_by_name(self):
    with temporary_dir() as tmpdir, \
         unittest.mock.patch('pants.pantsd.process_manager.get_buildroot', return_value=tmpdir):
      self.pmm.write_metadata_by_name(self.NAME, self.TEST_KEY, self.TEST_VALUE)

      self.assertEqual(
        self.pmm.await_metadata_by_name(self.NAME, self.TEST_KEY, .1),
        self.TEST_VALUE
      )

  def test_purge_metadata(self):
    with unittest.mock.patch('pants.pantsd.process_manager.rm_rf') as mock_rm:
      self.pmm.purge_metadata_by_name(self.NAME)
    self.assertGreater(mock_rm.call_count, 0)

  def test_purge_metadata_error(self):
    with unittest.mock.patch('pants.pantsd.process_manager.rm_rf') as mock_rm:
      mock_rm.side_effect = OSError(errno.EACCES, os.strerror(errno.EACCES))
      with self.assertRaises(ProcessMetadataManager.MetadataError):
        self.pmm.purge_metadata_by_name(self.NAME)
    self.assertGreater(mock_rm.call_count, 0)