7658: update connect error test to use stream handler to read the log file, instead...
[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_ignore_too_expensive_jobs(self):
52         servcalc = self.make_calculator([1, 2], max_nodes=12, max_price=6)
53         servlist = self.calculate(servcalc,
54                                   {'min_cores_per_node': 1, 'min_nodes': 6})
55         self.assertEqual(6, len(servlist))
56
57         servlist = self.calculate(servcalc,
58                                   {'min_cores_per_node': 2, 'min_nodes': 6})
59         self.assertEqual(0, len(servlist))
60
61     def test_job_requesting_max_nodes_accepted(self):
62         servcalc = self.make_calculator([1], max_nodes=4)
63         servlist = self.calculate(servcalc, {'min_nodes': 4})
64         self.assertEqual(4, len(servlist))
65
66     def test_cheapest_size(self):
67         servcalc = self.make_calculator([2, 4, 1, 3])
68         self.assertEqual(testutil.MockSize(1), servcalc.cheapest_size())
69
70     def test_next_biggest(self):
71         servcalc = self.make_calculator([1, 2, 4, 8])
72         servlist = self.calculate(servcalc,
73                                   {'min_cores_per_node': 3},
74                                   {'min_cores_per_node': 6})
75         self.assertEqual([servcalc.cloud_sizes[2].id,
76                           servcalc.cloud_sizes[3].id],
77                          [s.id for s in servlist])
78
79     def test_multiple_sizes(self):
80         servcalc = self.make_calculator([1, 2])
81         servlist = self.calculate(servcalc,
82                                   {'min_cores_per_node': 2},
83                                   {'min_cores_per_node': 1},
84                                   {'min_cores_per_node': 1})
85         self.assertEqual([servcalc.cloud_sizes[1].id,
86                           servcalc.cloud_sizes[0].id,
87                           servcalc.cloud_sizes[0].id],
88                          [s.id for s in servlist])
89
90         servlist = self.calculate(servcalc,
91                                   {'min_cores_per_node': 1},
92                                   {'min_cores_per_node': 2},
93                                   {'min_cores_per_node': 1})
94         self.assertEqual([servcalc.cloud_sizes[0].id,
95                           servcalc.cloud_sizes[1].id,
96                           servcalc.cloud_sizes[0].id],
97                          [s.id for s in servlist])
98
99         servlist = self.calculate(servcalc,
100                                   {'min_cores_per_node': 1},
101                                   {'min_cores_per_node': 1},
102                                   {'min_cores_per_node': 2})
103         self.assertEqual([servcalc.cloud_sizes[0].id,
104                           servcalc.cloud_sizes[0].id,
105                           servcalc.cloud_sizes[1].id],
106                          [s.id for s in servlist])
107
108
109
110 class JobQueueMonitorActorTestCase(testutil.RemotePollLoopActorTestMixin,
111                                    unittest.TestCase):
112     TEST_CLASS = jobqueue.JobQueueMonitorActor
113
114     class MockCalculator(object):
115         @staticmethod
116         def servers_for_queue(queue):
117             return [testutil.MockSize(n) for n in queue]
118
119
120     def build_monitor(self, side_effect, *args, **kwargs):
121         super(JobQueueMonitorActorTestCase, self).build_monitor(*args, **kwargs)
122         self.client.jobs().queue().execute.side_effect = side_effect
123
124     def test_subscribers_get_server_lists(self):
125         self.build_monitor([{'items': [1, 2]}], self.MockCalculator())
126         self.monitor.subscribe(self.subscriber).get(self.TIMEOUT)
127         self.stop_proxy(self.monitor)
128         self.subscriber.assert_called_with([testutil.MockSize(1),
129                                             testutil.MockSize(2)])
130
131
132 if __name__ == '__main__':
133     unittest.main()