Merge branch '4058-workbench-collection-redirect' closes #4058
[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
52 class JobQueueMonitorActorTestCase(testutil.RemotePollLoopActorTestMixin,
53                                    unittest.TestCase):
54     TEST_CLASS = jobqueue.JobQueueMonitorActor
55
56     class MockCalculator(object):
57         @staticmethod
58         def servers_for_queue(queue):
59             return [testutil.MockSize(n) for n in queue]
60
61
62     def build_monitor(self, side_effect, *args, **kwargs):
63         super(JobQueueMonitorActorTestCase, self).build_monitor(*args, **kwargs)
64         self.client.jobs().queue().execute.side_effect = side_effect
65
66     def test_subscribers_get_server_lists(self):
67         self.build_monitor([{'items': [1, 2]}], self.MockCalculator())
68         self.monitor.subscribe(self.subscriber).get(self.TIMEOUT)
69         self.stop_proxy(self.monitor)
70         self.subscriber.assert_called_with([testutil.MockSize(1),
71                                             testutil.MockSize(2)])
72
73
74 if __name__ == '__main__':
75     unittest.main()
76