def test_allocate_too_much_raises(self): resource = Resources(10000, 1) self.assertTrue(resource.exceeds(self.executor.available_resources)) self.assertEqual(self.executor.available_resources, self.executor.total_resources) with self.assertRaisesRegex(InconsistentResourcesException, 'Available resources are negative'): self.executor.allocate(resource, self.logger)
def test_raise_if_oversized_does_nothing(self): rsc = Resources(100, 1) self.assertFalse(rsc.exceeds(self.executor.total_resources)) job = make_mock_job(rsc) self.executor.raise_if_oversized(job)
def test_raise_if_oversized_raises_with_oversized(self): rsc = Resources(100, 4) self.assertTrue(rsc.exceeds(self.executor.total_resources)) job = make_mock_job(rsc) with self.assertRaisesRegex(OversizedJobException, 'exceed total'): self.executor.raise_if_oversized(job)
class ResourcesTestCase(TestCase): def setUp(self): self.resource11 = Resources(1, 1) self.resource22 = Resources(2, 2) self.resource33 = Resources(3, 3) self.resource21 = Resources(2, 1) self.resource12 = Resources(1, 2) self.resource_neg = Resources(-1, 0) def test_init(self): self.assertEqual(self.resource11.cores, 1) self.assertEqual(self.resource11.ram, 1) self.assertEqual(self.resource22.cores, 2) self.assertEqual(self.resource22.ram, 2) self.assertEqual(self.resource33.cores, 3) self.assertEqual(self.resource33.ram, 3) self.assertEqual(self.resource21.cores, 1) self.assertEqual(self.resource21.ram, 2) def test_subtraction(self): result = self.resource33 - self.resource21 self.assertEqual(result.ram, 1) self.assertEqual(result.cores, 2) def test_addition(self): result = self.resource11 + self.resource22 self.assertEqual(result.ram, 3) self.assertEqual(result.cores, 3) def test_neg(self): result = -self.resource11 self.assertEqual(result.ram, -1) self.assertEqual(result.cores, -1) def test_lt(self): self.assertTrue(self.resource11 < self.resource22) self.assertTrue(self.resource21 < self.resource33) self.assertFalse(self.resource11 < self.resource21) def test_gt(self): self.assertTrue(self.resource22 > self.resource11) self.assertTrue(self.resource33 > self.resource21) self.assertFalse(self.resource21 > self.resource11) def test_ge(self): self.assertTrue(self.resource21 >= self.resource11) self.assertFalse(self.resource22 >= self.resource33) def test_le(self): self.assertTrue(self.resource11 <= self.resource21) self.assertFalse(self.resource33 <= self.resource22) def test_eq(self): other = Resources(1, 1) self.assertEqual(self.resource11, other) def test_from_job(self): mock_job = make_mock_job(Resources(4, 2)) result = Resources.from_job(mock_job) self.assertEqual(result.ram, 4) self.assertEqual(result.cores, 2) def test_from_job_defaults_empty_without_builder(self): mock_job = make_mock_job(Resources(4, 2)) del mock_job.builder result = Resources.from_job(mock_job) self.assertEqual(result.ram, 0) self.assertEqual(result.cores, 0) def test_from_dict(self): result = Resources.from_dict({'cores': 3, 'ram': 400}) self.assertEqual(result.cores, 3) self.assertEqual(result.ram, 400) def test_min(self): result = Resources.min(self.resource21, self.resource12) self.assertEqual(result, self.resource11) def test_is_negative(self): self.assertFalse(self.resource11.is_negative()) self.assertTrue(self.resource_neg.is_negative()) def test_exceeds(self): self.assertTrue(self.resource21.exceeds(self.resource11)) self.assertFalse(self.resource11.exceeds(self.resource21)) self.assertFalse(self.resource21.exceeds(self.resource21)) def test_empty(self): self.assertEqual(Resources.EMPTY.ram, 0) self.assertEqual(Resources.EMPTY.cores, 0)