Merge branch 'master' into 4844-stricter-min-nodes-wip
[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             [{'uuid': 'zzzzz-jjjjj-{:015x}'.format(index),
18               'runtime_constraints': cdict}
19              for index, cdict in enumerate(constraints)])
20
21     def test_empty_queue_needs_no_servers(self):
22         servcalc = self.make_calculator([1])
23         self.assertEqual([], servcalc.servers_for_queue([]))
24
25     def test_easy_server_count(self):
26         servcalc = self.make_calculator([1])
27         servlist = self.calculate(servcalc, {'min_nodes': 3})
28         self.assertEqual(3, len(servlist))
29
30     def test_implicit_server_count(self):
31         servcalc = self.make_calculator([1])
32         servlist = self.calculate(servcalc, {}, {'min_nodes': 3})
33         self.assertEqual(4, len(servlist))
34
35     def test_bad_min_nodes_override(self):
36         servcalc = self.make_calculator([1])
37         servlist = self.calculate(servcalc,
38                                   {'min_nodes': -2}, {'min_nodes': 'foo'})
39         self.assertEqual(2, len(servlist))
40
41     def test_ignore_unsatisfiable_jobs(self):
42         servcalc = self.make_calculator([1], max_nodes=9)
43         servlist = self.calculate(servcalc,
44                                   {'min_cores_per_node': 2},
45                                   {'min_ram_mb_per_node': 256},
46                                   {'min_nodes': 6},
47                                   {'min_nodes': 12},
48                                   {'min_scratch_mb_per_node': 200})
49         self.assertEqual(6, len(servlist))
50
51     def test_job_requesting_max_nodes_accepted(self):
52         servcalc = self.make_calculator([1], max_nodes=4)
53         servlist = self.calculate(servcalc, {'min_nodes': 4})
54         self.assertEqual(4, len(servlist))
55
56     def test_cheapest_size(self):
57         servcalc = self.make_calculator([2, 4, 1, 3])
58         self.assertEqual(testutil.MockSize(1), servcalc.cheapest_size())
59
60
61 class JobQueueMonitorActorTestCase(testutil.RemotePollLoopActorTestMixin,
62                                    unittest.TestCase):
63     TEST_CLASS = jobqueue.JobQueueMonitorActor
64
65     class MockCalculator(object):
66         @staticmethod
67         def servers_for_queue(queue):
68             return [testutil.MockSize(n) for n in queue]
69
70
71     def build_monitor(self, side_effect, *args, **kwargs):
72         super(JobQueueMonitorActorTestCase, self).build_monitor(*args, **kwargs)
73         self.client.jobs().queue().execute.side_effect = side_effect
74
75     def test_subscribers_get_server_lists(self):
76         self.build_monitor([{'items': [1, 2]}], self.MockCalculator())
77         self.monitor.subscribe(self.subscriber).get(self.TIMEOUT)
78         self.stop_proxy(self.monitor)
79         self.subscriber.assert_called_with([testutil.MockSize(1),
80                                             testutil.MockSize(2)])
81
82
83 if __name__ == '__main__':
84     unittest.main()
85