Merge branch 'master' into 4126-preserve-parameter-hash
[arvados.git] / services / nodemanager / tests / test_jobqueue.py
1 #!/usr/bin/env python
2
3 from __future__ import absolute_import, print_function
4
5 import unittest
6
7 import arvnodeman.jobqueue as jobqueue
8 from . import testutil
9
10 class ServerCalculatorTestCase(unittest.TestCase):
11     def make_calculator(self, factors, **kwargs):
12         return jobqueue.ServerCalculator(
13             [(testutil.MockSize(n), {'cores': n}) for n in factors], **kwargs)
14
15     def calculate(self, servcalc, *constraints):
16         return servcalc.servers_for_queue(
17             [{'runtime_constraints': cdict} for cdict in constraints])
18
19     def test_empty_queue_needs_no_servers(self):
20         servcalc = self.make_calculator([1])
21         self.assertEqual([], servcalc.servers_for_queue([]))
22
23     def test_easy_server_count(self):
24         servcalc = self.make_calculator([1])
25         servlist = self.calculate(servcalc, {'min_nodes': 3})
26         self.assertEqual(3, len(servlist))
27
28     def test_implicit_server_count(self):
29         servcalc = self.make_calculator([1])
30         servlist = self.calculate(servcalc, {}, {'min_nodes': 3})
31         self.assertEqual(4, len(servlist))
32
33     def test_bad_min_nodes_override(self):
34         servcalc = self.make_calculator([1])
35         servlist = self.calculate(servcalc,
36                                   {'min_nodes': -2}, {'min_nodes': 'foo'})
37         self.assertEqual(2, len(servlist))
38
39     def test_ignore_unsatisfiable_jobs(self):
40         servcalc = self.make_calculator([1], max_nodes=9)
41         servlist = self.calculate(servcalc,
42                                   {'min_cores_per_node': 2},
43                                   {'min_ram_mb_per_node': 256},
44                                   {'min_nodes': 6},
45                                   {'min_nodes': 12},
46                                   {'min_scratch_mb_per_node': 200})
47         self.assertEqual(6, len(servlist))
48
49
50 class JobQueueMonitorActorTestCase(testutil.RemotePollLoopActorTestMixin,
51                                    unittest.TestCase):
52     TEST_CLASS = jobqueue.JobQueueMonitorActor
53
54     class MockCalculator(object):
55         @staticmethod
56         def servers_for_queue(queue):
57             return [testutil.MockSize(n) for n in queue]
58
59
60     def build_monitor(self, side_effect, *args, **kwargs):
61         super(JobQueueMonitorActorTestCase, self).build_monitor(*args, **kwargs)
62         self.client.jobs().queue().execute.side_effect = side_effect
63
64     def test_subscribers_get_server_lists(self):
65         self.build_monitor([{'items': [1, 2]}], self.MockCalculator())
66         self.monitor.subscribe(self.subscriber)
67         self.wait_for_call(self.subscriber)
68         self.subscriber.assert_called_with([testutil.MockSize(1),
69                                             testutil.MockSize(2)])
70
71
72 if __name__ == '__main__':
73     unittest.main()
74