Merge branch '3198-inode-cache' refs #3198
[arvados.git] / services / nodemanager / tests / test_computenode_dispatch.py
1 #!/usr/bin/env python
2
3 from __future__ import absolute_import, print_function
4
5 import time
6 import unittest
7
8 import arvados.errors as arverror
9 import httplib2
10 import mock
11 import pykka
12
13 import arvnodeman.computenode.dispatch as dispatch
14 from . import testutil
15
16 class ComputeNodeSetupActorTestCase(testutil.ActorTestMixin, unittest.TestCase):
17     def make_mocks(self, arvados_effect=None):
18         if arvados_effect is None:
19             arvados_effect = [testutil.arvados_node_mock()]
20         self.arvados_effect = arvados_effect
21         self.timer = testutil.MockTimer()
22         self.api_client = mock.MagicMock(name='api_client')
23         self.api_client.nodes().create().execute.side_effect = arvados_effect
24         self.api_client.nodes().update().execute.side_effect = arvados_effect
25         self.cloud_client = mock.MagicMock(name='cloud_client')
26         self.cloud_client.create_node.return_value = testutil.cloud_node_mock(1)
27
28     def make_actor(self, arv_node=None):
29         if not hasattr(self, 'timer'):
30             self.make_mocks(arvados_effect=[arv_node])
31         self.setup_actor = dispatch.ComputeNodeSetupActor.start(
32             self.timer, self.api_client, self.cloud_client,
33             testutil.MockSize(1), arv_node).proxy()
34
35     def test_creation_without_arvados_node(self):
36         self.make_actor()
37         self.assertEqual(self.arvados_effect[-1],
38                          self.setup_actor.arvados_node.get(self.TIMEOUT))
39         self.assertTrue(self.api_client.nodes().create().execute.called)
40         self.assertEqual(self.cloud_client.create_node(),
41                          self.setup_actor.cloud_node.get(self.TIMEOUT))
42
43     def test_creation_with_arvados_node(self):
44         self.make_actor(testutil.arvados_node_mock())
45         self.assertEqual(self.arvados_effect[-1],
46                          self.setup_actor.arvados_node.get(self.TIMEOUT))
47         self.assertTrue(self.api_client.nodes().update().execute.called)
48         self.assertEqual(self.cloud_client.create_node(),
49                          self.setup_actor.cloud_node.get(self.TIMEOUT))
50
51     def test_failed_arvados_calls_retried(self):
52         self.make_mocks([
53                 arverror.ApiError(httplib2.Response({'status': '500'}), ""),
54                 testutil.arvados_node_mock(),
55                 ])
56         self.make_actor()
57         self.wait_for_assignment(self.setup_actor, 'arvados_node')
58
59     def test_failed_cloud_calls_retried(self):
60         self.make_mocks()
61         self.cloud_client.create_node.side_effect = [
62             Exception("test cloud creation error"),
63             self.cloud_client.create_node.return_value,
64             ]
65         self.make_actor()
66         self.wait_for_assignment(self.setup_actor, 'cloud_node')
67
68     def test_failed_post_create_retried(self):
69         self.make_mocks()
70         self.cloud_client.post_create_node.side_effect = [
71             Exception("test cloud post-create error"), None]
72         self.make_actor()
73         done = self.FUTURE_CLASS()
74         self.setup_actor.subscribe(done.set)
75         done.get(self.TIMEOUT)
76         self.assertEqual(2, self.cloud_client.post_create_node.call_count)
77
78     def test_stop_when_no_cloud_node(self):
79         self.make_mocks(
80             arverror.ApiError(httplib2.Response({'status': '500'}), ""))
81         self.make_actor()
82         self.assertTrue(
83             self.setup_actor.stop_if_no_cloud_node().get(self.TIMEOUT))
84         self.assertTrue(
85             self.setup_actor.actor_ref.actor_stopped.wait(self.TIMEOUT))
86
87     def test_no_stop_when_cloud_node(self):
88         self.make_actor()
89         self.wait_for_assignment(self.setup_actor, 'cloud_node')
90         self.assertFalse(
91             self.setup_actor.stop_if_no_cloud_node().get(self.TIMEOUT))
92         self.assertTrue(self.stop_proxy(self.setup_actor),
93                         "actor was stopped by stop_if_no_cloud_node")
94
95     def test_subscribe(self):
96         self.make_mocks(
97             arverror.ApiError(httplib2.Response({'status': '500'}), ""))
98         self.make_actor()
99         subscriber = mock.Mock(name='subscriber_mock')
100         self.setup_actor.subscribe(subscriber)
101         self.api_client.nodes().create().execute.side_effect = [
102             testutil.arvados_node_mock()]
103         self.wait_for_assignment(self.setup_actor, 'cloud_node')
104         self.assertEqual(self.setup_actor.actor_ref.actor_urn,
105                          subscriber.call_args[0][0].actor_ref.actor_urn)
106
107     def test_late_subscribe(self):
108         self.make_actor()
109         subscriber = mock.Mock(name='subscriber_mock')
110         self.wait_for_assignment(self.setup_actor, 'cloud_node')
111         self.setup_actor.subscribe(subscriber).get(self.TIMEOUT)
112         self.stop_proxy(self.setup_actor)
113         self.assertEqual(self.setup_actor.actor_ref.actor_urn,
114                          subscriber.call_args[0][0].actor_ref.actor_urn)
115
116
117 class ComputeNodeShutdownActorMixin(testutil.ActorTestMixin):
118     def make_mocks(self, cloud_node=None, arvados_node=None,
119                    shutdown_open=True):
120         self.timer = testutil.MockTimer()
121         self.shutdowns = testutil.MockShutdownTimer()
122         self.shutdowns._set_state(shutdown_open, 300)
123         self.cloud_client = mock.MagicMock(name='cloud_client')
124         self.updates = mock.MagicMock(name='update_mock')
125         if cloud_node is None:
126             cloud_node = testutil.cloud_node_mock()
127         self.cloud_node = cloud_node
128         self.arvados_node = arvados_node
129
130     def make_actor(self, cancellable=True):
131         if not hasattr(self, 'timer'):
132             self.make_mocks()
133         monitor_actor = dispatch.ComputeNodeMonitorActor.start(
134             self.cloud_node, time.time(), self.shutdowns,
135             testutil.cloud_node_fqdn, self.timer, self.updates,
136             self.arvados_node)
137         self.shutdown_actor = self.ACTOR_CLASS.start(
138             self.timer, self.cloud_client, monitor_actor, cancellable).proxy()
139         self.monitor_actor = monitor_actor.proxy()
140
141     def check_success_flag(self, expected, allow_msg_count=1):
142         # allow_msg_count is the number of internal messages that may
143         # need to be handled for shutdown to finish.
144         for try_num in range(1 + allow_msg_count):
145             last_flag = self.shutdown_actor.success.get(self.TIMEOUT)
146             if last_flag is expected:
147                 break
148         else:
149             self.fail("success flag {} is not {}".format(last_flag, expected))
150
151     def test_uncancellable_shutdown(self, *mocks):
152         self.make_mocks(shutdown_open=False)
153         self.cloud_client.destroy_node.return_value = False
154         self.make_actor(cancellable=False)
155         self.check_success_flag(None, 0)
156         self.shutdowns._set_state(True, 600)
157         self.cloud_client.destroy_node.return_value = True
158         self.check_success_flag(True)
159
160
161 class ComputeNodeShutdownActorTestCase(ComputeNodeShutdownActorMixin,
162                                        unittest.TestCase):
163     ACTOR_CLASS = dispatch.ComputeNodeShutdownActor
164
165     def test_easy_shutdown(self):
166         self.make_actor()
167         self.check_success_flag(True)
168         self.assertTrue(self.cloud_client.destroy_node.called)
169
170     def test_shutdown_cancelled_when_window_closes(self):
171         self.make_mocks(shutdown_open=False)
172         self.make_actor()
173         self.check_success_flag(False, 2)
174         self.assertFalse(self.cloud_client.destroy_node.called)
175
176     def test_shutdown_retries_when_cloud_fails(self):
177         self.make_mocks()
178         self.cloud_client.destroy_node.return_value = False
179         self.make_actor()
180         self.assertIsNone(self.shutdown_actor.success.get(self.TIMEOUT))
181         self.cloud_client.destroy_node.return_value = True
182         self.check_success_flag(True)
183
184     def test_late_subscribe(self):
185         self.make_actor()
186         subscriber = mock.Mock(name='subscriber_mock')
187         self.shutdown_actor.subscribe(subscriber).get(self.TIMEOUT)
188         self.stop_proxy(self.shutdown_actor)
189         self.assertTrue(subscriber.called)
190         self.assertEqual(self.shutdown_actor.actor_ref.actor_urn,
191                          subscriber.call_args[0][0].actor_ref.actor_urn)
192
193
194 class ComputeNodeUpdateActorTestCase(testutil.ActorTestMixin,
195                                      unittest.TestCase):
196     def make_actor(self):
197         self.driver = mock.MagicMock(name='driver_mock')
198         self.updater = dispatch.ComputeNodeUpdateActor.start(self.driver).proxy()
199
200     def test_node_sync(self):
201         self.make_actor()
202         cloud_node = testutil.cloud_node_mock()
203         arv_node = testutil.arvados_node_mock()
204         self.updater.sync_node(cloud_node, arv_node).get(self.TIMEOUT)
205         self.driver().sync_node.assert_called_with(cloud_node, arv_node)
206
207
208 class ComputeNodeMonitorActorTestCase(testutil.ActorTestMixin,
209                                       unittest.TestCase):
210     def make_mocks(self, node_num):
211         self.shutdowns = testutil.MockShutdownTimer()
212         self.shutdowns._set_state(False, 300)
213         self.timer = mock.MagicMock(name='timer_mock')
214         self.updates = mock.MagicMock(name='update_mock')
215         self.cloud_mock = testutil.cloud_node_mock(node_num)
216         self.subscriber = mock.Mock(name='subscriber_mock')
217
218     def make_actor(self, node_num=1, arv_node=None, start_time=None):
219         if not hasattr(self, 'cloud_mock'):
220             self.make_mocks(node_num)
221         if start_time is None:
222             start_time = time.time()
223         self.node_actor = dispatch.ComputeNodeMonitorActor.start(
224             self.cloud_mock, start_time, self.shutdowns,
225             testutil.cloud_node_fqdn, self.timer, self.updates,
226             arv_node).proxy()
227         self.node_actor.subscribe(self.subscriber).get(self.TIMEOUT)
228
229     def node_state(self, *states):
230         return self.node_actor.in_state(*states).get(self.TIMEOUT)
231
232     def test_in_state_when_unpaired(self):
233         self.make_actor()
234         self.assertIsNone(self.node_state('idle', 'busy'))
235
236     def test_in_state_when_pairing_stale(self):
237         self.make_actor(arv_node=testutil.arvados_node_mock(
238                 job_uuid=None, age=90000))
239         self.assertIsNone(self.node_state('idle', 'busy'))
240
241     def test_in_state_when_no_state_available(self):
242         self.make_actor(arv_node=testutil.arvados_node_mock(
243                 crunch_worker_state=None))
244         self.assertIsNone(self.node_state('idle', 'busy'))
245
246     def test_in_idle_state(self):
247         self.make_actor(2, arv_node=testutil.arvados_node_mock(job_uuid=None))
248         self.assertTrue(self.node_state('idle'))
249         self.assertFalse(self.node_state('busy'))
250         self.assertTrue(self.node_state('idle', 'busy'))
251
252     def test_in_busy_state(self):
253         self.make_actor(3, arv_node=testutil.arvados_node_mock(job_uuid=True))
254         self.assertFalse(self.node_state('idle'))
255         self.assertTrue(self.node_state('busy'))
256         self.assertTrue(self.node_state('idle', 'busy'))
257
258     def test_init_shutdown_scheduling(self):
259         self.make_actor()
260         self.assertTrue(self.timer.schedule.called)
261         self.assertEqual(300, self.timer.schedule.call_args[0][0])
262
263     def test_shutdown_window_close_scheduling(self):
264         self.make_actor()
265         self.shutdowns._set_state(False, 600)
266         self.timer.schedule.reset_mock()
267         self.node_actor.consider_shutdown().get(self.TIMEOUT)
268         self.stop_proxy(self.node_actor)
269         self.assertTrue(self.timer.schedule.called)
270         self.assertEqual(600, self.timer.schedule.call_args[0][0])
271         self.assertFalse(self.subscriber.called)
272
273     def test_shutdown_subscription(self):
274         self.make_actor()
275         self.shutdowns._set_state(True, 600)
276         self.node_actor.consider_shutdown().get(self.TIMEOUT)
277         self.assertTrue(self.subscriber.called)
278         self.assertEqual(self.node_actor.actor_ref.actor_urn,
279                          self.subscriber.call_args[0][0].actor_ref.actor_urn)
280
281     def test_shutdown_without_arvados_node(self):
282         self.make_actor()
283         self.shutdowns._set_state(True, 600)
284         self.assertTrue(self.node_actor.shutdown_eligible().get(self.TIMEOUT))
285
286     def test_no_shutdown_without_arvados_node_and_old_cloud_node(self):
287         self.make_actor(start_time=0)
288         self.shutdowns._set_state(True, 600)
289         self.assertFalse(self.node_actor.shutdown_eligible().get(self.TIMEOUT))
290
291     def test_no_shutdown_when_window_closed(self):
292         self.make_actor(3, testutil.arvados_node_mock(3, job_uuid=None))
293         self.assertFalse(self.node_actor.shutdown_eligible().get(self.TIMEOUT))
294
295     def test_no_shutdown_when_node_running_job(self):
296         self.make_actor(4, testutil.arvados_node_mock(4, job_uuid=True))
297         self.shutdowns._set_state(True, 600)
298         self.assertFalse(self.node_actor.shutdown_eligible().get(self.TIMEOUT))
299
300     def test_no_shutdown_when_node_state_unknown(self):
301         self.make_actor(5, testutil.arvados_node_mock(
302             5, crunch_worker_state=None))
303         self.shutdowns._set_state(True, 600)
304         self.assertFalse(self.node_actor.shutdown_eligible().get(self.TIMEOUT))
305
306     def test_no_shutdown_when_node_state_stale(self):
307         self.make_actor(6, testutil.arvados_node_mock(6, age=90000))
308         self.shutdowns._set_state(True, 600)
309         self.assertFalse(self.node_actor.shutdown_eligible().get(self.TIMEOUT))
310
311     def test_arvados_node_match(self):
312         self.make_actor(2)
313         arv_node = testutil.arvados_node_mock(
314             2, hostname='compute-two.zzzzz.arvadosapi.com')
315         pair_id = self.node_actor.offer_arvados_pair(arv_node).get(self.TIMEOUT)
316         self.assertEqual(self.cloud_mock.id, pair_id)
317         self.stop_proxy(self.node_actor)
318         self.updates.sync_node.assert_called_with(self.cloud_mock, arv_node)
319
320     def test_arvados_node_mismatch(self):
321         self.make_actor(3)
322         arv_node = testutil.arvados_node_mock(1)
323         self.assertIsNone(
324             self.node_actor.offer_arvados_pair(arv_node).get(self.TIMEOUT))
325
326     def test_arvados_node_mismatch_first_ping_too_early(self):
327         self.make_actor(4)
328         arv_node = testutil.arvados_node_mock(
329             4, first_ping_at='1971-03-02T14:15:16.1717282Z')
330         self.assertIsNone(
331             self.node_actor.offer_arvados_pair(arv_node).get(self.TIMEOUT))
332
333     def test_update_cloud_node(self):
334         self.make_actor(1)
335         self.make_mocks(2)
336         self.cloud_mock.id = '1'
337         self.node_actor.update_cloud_node(self.cloud_mock)
338         current_cloud = self.node_actor.cloud_node.get(self.TIMEOUT)
339         self.assertEqual([testutil.ip_address_mock(2)],
340                          current_cloud.private_ips)
341
342     def test_missing_cloud_node_update(self):
343         self.make_actor(1)
344         self.node_actor.update_cloud_node(None)
345         current_cloud = self.node_actor.cloud_node.get(self.TIMEOUT)
346         self.assertEqual([testutil.ip_address_mock(1)],
347                          current_cloud.private_ips)
348
349     def test_update_arvados_node(self):
350         self.make_actor(3)
351         job_uuid = 'zzzzz-jjjjj-updatejobnode00'
352         new_arvados = testutil.arvados_node_mock(3, job_uuid)
353         self.node_actor.update_arvados_node(new_arvados)
354         current_arvados = self.node_actor.arvados_node.get(self.TIMEOUT)
355         self.assertEqual(job_uuid, current_arvados['job_uuid'])
356
357     def test_missing_arvados_node_update(self):
358         self.make_actor(4, testutil.arvados_node_mock(4))
359         self.node_actor.update_arvados_node(None)
360         current_arvados = self.node_actor.arvados_node.get(self.TIMEOUT)
361         self.assertEqual(testutil.ip_address_mock(4),
362                          current_arvados['ip_address'])
363
364     def test_update_arvados_node_syncs_when_fqdn_mismatch(self):
365         self.make_mocks(5)
366         self.cloud_mock.extra['testname'] = 'cloudfqdn.zzzzz.arvadosapi.com'
367         self.make_actor()
368         arv_node = testutil.arvados_node_mock(5)
369         self.node_actor.update_arvados_node(arv_node).get(self.TIMEOUT)
370         self.assertEqual(1, self.updates.sync_node.call_count)
371
372     def test_update_arvados_node_skips_sync_when_fqdn_match(self):
373         self.make_mocks(6)
374         arv_node = testutil.arvados_node_mock(6)
375         self.cloud_mock.extra['testname'] ='{n[hostname]}.{n[domain]}'.format(
376             n=arv_node)
377         self.make_actor()
378         self.node_actor.update_arvados_node(arv_node).get(self.TIMEOUT)
379         self.assertEqual(0, self.updates.sync_node.call_count)