2 # Copyright (C) The Arvados Authors. All rights reserved.
4 # SPDX-License-Identifier: AGPL-3.0
6 from __future__ import absolute_import, print_function
11 import arvados.errors as arverror
17 from libcloud.common.exceptions import BaseHTTPError
19 import arvnodeman.computenode.dispatch as dispatch
20 from arvnodeman.computenode.driver import BaseComputeNodeDriver
21 from . import testutil
23 class ComputeNodeSetupActorTestCase(testutil.ActorTestMixin, unittest.TestCase):
24 def make_mocks(self, arvados_effect=None):
25 if arvados_effect is None:
26 arvados_effect = [testutil.arvados_node_mock()]
27 self.arvados_effect = arvados_effect
28 self.timer = testutil.MockTimer()
29 self.api_client = mock.MagicMock(name='api_client')
30 self.api_client.nodes().create().execute.side_effect = arvados_effect
31 self.api_client.nodes().update().execute.side_effect = arvados_effect
32 self.cloud_client = mock.MagicMock(name='cloud_client')
33 self.cloud_client.create_node.return_value = testutil.cloud_node_mock(1)
35 def make_actor(self, arv_node=None):
36 if not hasattr(self, 'timer'):
37 self.make_mocks(arvados_effect=[arv_node] if arv_node else None)
38 self.setup_actor = dispatch.ComputeNodeSetupActor.start(
39 self.timer, self.api_client, self.cloud_client,
40 testutil.MockSize(1), arv_node).proxy()
42 def assert_node_properties_updated(self, uuid=None,
43 size=testutil.MockSize(1)):
44 self.api_client.nodes().update.assert_any_call(
45 uuid=(uuid or self.arvados_effect[-1]['uuid']),
50 'price': size.price}}})
52 def test_creation_without_arvados_node(self):
54 finished = threading.Event()
55 self.setup_actor.subscribe(lambda _: finished.set())
56 self.assertEqual(self.arvados_effect[-1],
57 self.setup_actor.arvados_node.get(self.TIMEOUT))
58 assert(finished.wait(self.TIMEOUT))
59 self.assertEqual(1, self.api_client.nodes().create().execute.call_count)
60 self.assertEqual(1, self.api_client.nodes().update().execute.call_count)
61 self.assert_node_properties_updated()
62 self.assertEqual(self.cloud_client.create_node(),
63 self.setup_actor.cloud_node.get(self.TIMEOUT))
65 def test_creation_with_arvados_node(self):
66 self.make_mocks(arvados_effect=[testutil.arvados_node_mock()]*2)
67 self.make_actor(testutil.arvados_node_mock())
68 finished = threading.Event()
69 self.setup_actor.subscribe(lambda _: finished.set())
70 self.assertEqual(self.arvados_effect[-1],
71 self.setup_actor.arvados_node.get(self.TIMEOUT))
72 assert(finished.wait(self.TIMEOUT))
73 self.assert_node_properties_updated()
74 self.assertEqual(2, self.api_client.nodes().update().execute.call_count)
75 self.assertEqual(self.cloud_client.create_node(),
76 self.setup_actor.cloud_node.get(self.TIMEOUT))
78 def test_failed_arvados_calls_retried(self):
80 arverror.ApiError(httplib2.Response({'status': '500'}), ""),
81 testutil.arvados_node_mock(),
84 self.wait_for_assignment(self.setup_actor, 'arvados_node')
86 def test_failed_cloud_calls_retried(self):
88 self.cloud_client.create_node.side_effect = [
89 Exception("test cloud creation error"),
90 self.cloud_client.create_node.return_value,
93 self.wait_for_assignment(self.setup_actor, 'cloud_node')
95 def test_basehttperror_retried(self):
97 self.cloud_client.create_node.side_effect = [
98 BaseHTTPError(500, "Try again"),
99 self.cloud_client.create_node.return_value,
102 self.wait_for_assignment(self.setup_actor, 'cloud_node')
103 self.setup_actor.ping().get(self.TIMEOUT)
104 self.assertEqual(1, self.cloud_client.post_create_node.call_count)
106 def test_instance_exceeded_not_retried(self):
108 self.cloud_client.create_node.side_effect = [
109 BaseHTTPError(400, "InstanceLimitExceeded"),
110 self.cloud_client.create_node.return_value,
113 done = self.FUTURE_CLASS()
114 self.setup_actor.subscribe(done.set)
115 done.get(self.TIMEOUT)
116 self.assertEqual(0, self.cloud_client.post_create_node.call_count)
118 def test_failed_post_create_retried(self):
120 self.cloud_client.post_create_node.side_effect = [
121 Exception("test cloud post-create error"), None]
123 done = self.FUTURE_CLASS()
124 self.setup_actor.subscribe(done.set)
125 done.get(self.TIMEOUT)
126 self.assertEqual(2, self.cloud_client.post_create_node.call_count)
128 def test_stop_when_no_cloud_node(self):
130 arverror.ApiError(httplib2.Response({'status': '500'}), ""))
133 self.setup_actor.stop_if_no_cloud_node().get(self.TIMEOUT))
135 self.setup_actor.actor_ref.actor_stopped.wait(self.TIMEOUT))
137 def test_no_stop_when_cloud_node(self):
139 self.wait_for_assignment(self.setup_actor, 'cloud_node')
141 self.setup_actor.stop_if_no_cloud_node().get(self.TIMEOUT))
142 self.assertTrue(self.stop_proxy(self.setup_actor),
143 "actor was stopped by stop_if_no_cloud_node")
145 def test_subscribe(self):
147 arverror.ApiError(httplib2.Response({'status': '500'}), ""))
149 subscriber = mock.Mock(name='subscriber_mock')
150 self.setup_actor.subscribe(subscriber)
151 retry_resp = [testutil.arvados_node_mock()]
152 self.api_client.nodes().create().execute.side_effect = retry_resp
153 self.api_client.nodes().update().execute.side_effect = retry_resp
154 self.wait_for_assignment(self.setup_actor, 'cloud_node')
155 self.setup_actor.ping().get(self.TIMEOUT)
156 self.assertEqual(self.setup_actor.actor_ref.actor_urn,
157 subscriber.call_args[0][0].actor_ref.actor_urn)
159 def test_late_subscribe(self):
161 subscriber = mock.Mock(name='subscriber_mock')
162 self.wait_for_assignment(self.setup_actor, 'cloud_node')
163 self.setup_actor.subscribe(subscriber).get(self.TIMEOUT)
164 self.stop_proxy(self.setup_actor)
165 self.assertEqual(self.setup_actor.actor_ref.actor_urn,
166 subscriber.call_args[0][0].actor_ref.actor_urn)
169 class ComputeNodeShutdownActorMixin(testutil.ActorTestMixin):
170 def make_mocks(self, cloud_node=None, arvados_node=None,
171 shutdown_open=True, node_broken=False):
172 self.timer = testutil.MockTimer()
173 self.shutdowns = testutil.MockShutdownTimer()
174 self.shutdowns._set_state(shutdown_open, 300)
175 self.cloud_client = mock.MagicMock(name='cloud_client')
176 self.cloud_client.broken.return_value = node_broken
177 self.arvados_client = mock.MagicMock(name='arvados_client')
178 self.updates = mock.MagicMock(name='update_mock')
179 if cloud_node is None:
180 cloud_node = testutil.cloud_node_mock()
181 self.cloud_node = cloud_node
182 self.arvados_node = arvados_node
184 def make_actor(self, cancellable=True, start_time=None):
185 if not hasattr(self, 'timer'):
187 if start_time is None:
188 start_time = time.time()
189 monitor_actor = dispatch.ComputeNodeMonitorActor.start(
190 self.cloud_node, start_time, self.shutdowns,
191 testutil.cloud_node_fqdn, self.timer, self.updates, self.cloud_client,
193 self.shutdown_actor = self.ACTOR_CLASS.start(
194 self.timer, self.cloud_client, self.arvados_client, monitor_actor,
196 self.monitor_actor = monitor_actor.proxy()
198 def check_success_flag(self, expected, allow_msg_count=1):
199 # allow_msg_count is the number of internal messages that may
200 # need to be handled for shutdown to finish.
201 for try_num in range(1 + allow_msg_count):
202 last_flag = self.shutdown_actor.success.get(self.TIMEOUT)
203 if last_flag is expected:
206 self.fail("success flag {} is not {}".format(last_flag, expected))
208 def test_cancellable_shutdown(self, *mocks):
209 self.make_mocks(shutdown_open=True, arvados_node=testutil.arvados_node_mock(crunch_worker_state="busy"))
210 self.cloud_client.destroy_node.return_value = True
211 self.make_actor(cancellable=True)
212 self.check_success_flag(False, 2)
213 self.assertFalse(self.cloud_client.destroy_node.called)
215 def test_uncancellable_shutdown(self, *mocks):
216 self.make_mocks(shutdown_open=True, arvados_node=testutil.arvados_node_mock(crunch_worker_state="busy"))
217 self.cloud_client.destroy_node.return_value = True
218 self.make_actor(cancellable=False)
219 self.check_success_flag(True, 4)
220 self.assertTrue(self.cloud_client.destroy_node.called)
222 def test_arvados_node_cleaned_after_shutdown(self, *mocks):
224 mocks[0].return_value = "drain\n"
225 cloud_node = testutil.cloud_node_mock(62)
226 arv_node = testutil.arvados_node_mock(62)
227 self.make_mocks(cloud_node, arv_node)
229 self.check_success_flag(True, 3)
230 update_mock = self.arvados_client.nodes().update
231 self.assertTrue(update_mock.called)
232 update_kwargs = update_mock.call_args_list[0][1]
233 self.assertEqual(arv_node['uuid'], update_kwargs.get('uuid'))
234 self.assertIn('body', update_kwargs)
235 for clear_key in ['slot_number', 'hostname', 'ip_address',
236 'first_ping_at', 'last_ping_at']:
237 self.assertIn(clear_key, update_kwargs['body'])
238 self.assertIsNone(update_kwargs['body'][clear_key])
239 self.assertTrue(update_mock().execute.called)
241 def test_arvados_node_not_cleaned_after_shutdown_cancelled(self, *mocks):
243 mocks[0].return_value = "idle\n"
244 cloud_node = testutil.cloud_node_mock(61)
245 arv_node = testutil.arvados_node_mock(61)
246 self.make_mocks(cloud_node, arv_node, shutdown_open=False)
247 self.cloud_client.destroy_node.return_value = False
248 self.make_actor(cancellable=True)
249 self.shutdown_actor.cancel_shutdown("test")
250 self.shutdown_actor.ping().get(self.TIMEOUT)
251 self.check_success_flag(False, 2)
252 self.assertFalse(self.arvados_client.nodes().update.called)
255 class ComputeNodeShutdownActorTestCase(ComputeNodeShutdownActorMixin,
257 ACTOR_CLASS = dispatch.ComputeNodeShutdownActor
259 def test_easy_shutdown(self):
260 self.make_actor(start_time=0)
261 self.check_success_flag(True)
262 self.assertTrue(self.cloud_client.destroy_node.called)
264 def test_shutdown_cancelled_when_destroy_node_fails(self):
265 self.make_mocks(node_broken=True)
266 self.cloud_client.destroy_node.return_value = False
267 self.make_actor(start_time=0)
268 self.check_success_flag(False, 2)
269 self.assertEqual(1, self.cloud_client.destroy_node.call_count)
270 self.assertEqual(self.ACTOR_CLASS.DESTROY_FAILED,
271 self.shutdown_actor.cancel_reason.get(self.TIMEOUT))
273 def test_late_subscribe(self):
275 subscriber = mock.Mock(name='subscriber_mock')
276 self.shutdown_actor.subscribe(subscriber).get(self.TIMEOUT)
277 self.stop_proxy(self.shutdown_actor)
278 self.assertTrue(subscriber.called)
279 self.assertEqual(self.shutdown_actor.actor_ref.actor_urn,
280 subscriber.call_args[0][0].actor_ref.actor_urn)
283 class ComputeNodeUpdateActorTestCase(testutil.ActorTestMixin,
285 ACTOR_CLASS = dispatch.ComputeNodeUpdateActor
287 def make_actor(self):
288 self.driver = mock.MagicMock(name='driver_mock')
289 self.timer = mock.MagicMock(name='timer_mock')
290 self.updater = self.ACTOR_CLASS.start(self.driver, self.timer).proxy()
292 def test_node_sync(self, *args):
294 cloud_node = testutil.cloud_node_mock()
295 arv_node = testutil.arvados_node_mock()
296 self.updater.sync_node(cloud_node, arv_node).get(self.TIMEOUT)
297 self.driver().sync_node.assert_called_with(cloud_node, arv_node)
300 def test_node_sync_error(self, *args):
302 cloud_node = testutil.cloud_node_mock()
303 arv_node = testutil.arvados_node_mock()
304 self.driver().sync_node.side_effect = (IOError, Exception, True)
305 self.updater.sync_node(cloud_node, arv_node).get(self.TIMEOUT)
306 self.updater.sync_node(cloud_node, arv_node).get(self.TIMEOUT)
307 self.updater.sync_node(cloud_node, arv_node).get(self.TIMEOUT)
308 self.driver().sync_node.assert_called_with(cloud_node, arv_node)
310 class ComputeNodeMonitorActorTestCase(testutil.ActorTestMixin,
312 def make_mocks(self, node_num):
313 self.shutdowns = testutil.MockShutdownTimer()
314 self.shutdowns._set_state(False, 300)
315 self.timer = mock.MagicMock(name='timer_mock')
316 self.updates = mock.MagicMock(name='update_mock')
317 self.cloud_mock = testutil.cloud_node_mock(node_num)
318 self.subscriber = mock.Mock(name='subscriber_mock')
319 self.cloud_client = mock.MagicMock(name='cloud_client')
320 self.cloud_client.broken.return_value = False
322 def make_actor(self, node_num=1, arv_node=None, start_time=None):
323 if not hasattr(self, 'cloud_mock'):
324 self.make_mocks(node_num)
325 if start_time is None:
326 start_time = time.time()
327 self.node_actor = dispatch.ComputeNodeMonitorActor.start(
328 self.cloud_mock, start_time, self.shutdowns,
329 testutil.cloud_node_fqdn, self.timer, self.updates, self.cloud_client,
330 arv_node, boot_fail_after=300).proxy()
331 self.node_actor.subscribe(self.subscriber).get(self.TIMEOUT)
333 def node_state(self, *states):
334 return self.node_actor.in_state(*states).get(self.TIMEOUT)
336 def test_in_state_when_unpaired(self):
338 self.assertTrue(self.node_state('unpaired'))
340 def test_in_state_when_pairing_stale(self):
341 self.make_actor(arv_node=testutil.arvados_node_mock(
342 job_uuid=None, age=90000))
343 self.assertTrue(self.node_state('down'))
345 def test_in_state_when_no_state_available(self):
346 self.make_actor(arv_node=testutil.arvados_node_mock(
347 crunch_worker_state=None))
348 self.assertTrue(self.node_state('idle'))
350 def test_in_state_when_no_state_available_old(self):
351 self.make_actor(arv_node=testutil.arvados_node_mock(
352 crunch_worker_state=None, age=90000))
353 self.assertTrue(self.node_state('down'))
355 def test_in_idle_state(self):
356 self.make_actor(2, arv_node=testutil.arvados_node_mock(job_uuid=None))
357 self.assertTrue(self.node_state('idle'))
358 self.assertFalse(self.node_state('busy'))
359 self.assertTrue(self.node_state('idle', 'busy'))
361 def test_in_busy_state(self):
362 self.make_actor(3, arv_node=testutil.arvados_node_mock(job_uuid=True))
363 self.assertFalse(self.node_state('idle'))
364 self.assertTrue(self.node_state('busy'))
365 self.assertTrue(self.node_state('idle', 'busy'))
367 def test_init_shutdown_scheduling(self):
369 self.assertTrue(self.timer.schedule.called)
370 self.assertEqual(300, self.timer.schedule.call_args[0][0])
372 def test_shutdown_window_close_scheduling(self):
374 self.shutdowns._set_state(False, 600)
375 self.timer.schedule.reset_mock()
376 self.node_actor.consider_shutdown().get(self.TIMEOUT)
377 self.stop_proxy(self.node_actor)
378 self.assertTrue(self.timer.schedule.called)
379 self.assertEqual(600, self.timer.schedule.call_args[0][0])
380 self.assertFalse(self.subscriber.called)
382 def test_shutdown_subscription(self):
383 self.make_actor(start_time=0)
384 self.shutdowns._set_state(True, 600)
385 self.node_actor.consider_shutdown().get(self.TIMEOUT)
386 self.assertTrue(self.subscriber.called)
387 self.assertEqual(self.node_actor.actor_ref.actor_urn,
388 self.subscriber.call_args[0][0].actor_ref.actor_urn)
390 def test_no_shutdown_booting(self):
392 self.shutdowns._set_state(True, 600)
393 self.assertEquals(self.node_actor.shutdown_eligible().get(self.TIMEOUT),
394 (False, "node state is ('unpaired', 'open', 'boot wait', 'idle exceeded')"))
396 def test_shutdown_without_arvados_node(self):
397 self.make_actor(start_time=0)
398 self.shutdowns._set_state(True, 600)
399 self.assertEquals((True, "node state is ('unpaired', 'open', 'boot exceeded', 'idle exceeded')"),
400 self.node_actor.shutdown_eligible().get(self.TIMEOUT))
402 def test_shutdown_missing(self):
403 arv_node = testutil.arvados_node_mock(10, job_uuid=None,
404 crunch_worker_state="down",
405 last_ping_at='1970-01-01T01:02:03.04050607Z')
406 self.make_actor(10, arv_node)
407 self.shutdowns._set_state(True, 600)
408 self.assertEquals((True, "node state is ('down', 'open', 'boot wait', 'idle exceeded')"),
409 self.node_actor.shutdown_eligible().get(self.TIMEOUT))
411 def test_shutdown_running_broken(self):
412 arv_node = testutil.arvados_node_mock(12, job_uuid=None,
413 crunch_worker_state="down")
414 self.make_actor(12, arv_node)
415 self.shutdowns._set_state(True, 600)
416 self.cloud_client.broken.return_value = True
417 self.assertEquals((True, "node state is ('down', 'open', 'boot wait', 'idle exceeded')"),
418 self.node_actor.shutdown_eligible().get(self.TIMEOUT))
420 def test_shutdown_missing_broken(self):
421 arv_node = testutil.arvados_node_mock(11, job_uuid=None,
422 crunch_worker_state="down",
423 last_ping_at='1970-01-01T01:02:03.04050607Z')
424 self.make_actor(11, arv_node)
425 self.shutdowns._set_state(True, 600)
426 self.cloud_client.broken.return_value = True
427 self.assertEquals(self.node_actor.shutdown_eligible().get(self.TIMEOUT), (True, "node state is ('down', 'open', 'boot wait', 'idle exceeded')"))
429 def test_no_shutdown_when_window_closed(self):
430 self.make_actor(3, testutil.arvados_node_mock(3, job_uuid=None))
431 self.assertEquals((False, "node state is ('idle', 'closed', 'boot wait', 'idle exceeded')"),
432 self.node_actor.shutdown_eligible().get(self.TIMEOUT))
434 def test_no_shutdown_when_node_running_job(self):
435 self.make_actor(4, testutil.arvados_node_mock(4, job_uuid=True))
436 self.shutdowns._set_state(True, 600)
437 self.assertEquals((False, "node state is ('busy', 'open', 'boot wait', 'idle exceeded')"),
438 self.node_actor.shutdown_eligible().get(self.TIMEOUT))
440 def test_shutdown_when_node_state_unknown(self):
441 self.make_actor(5, testutil.arvados_node_mock(
442 5, crunch_worker_state=None))
443 self.shutdowns._set_state(True, 600)
444 self.assertEquals((True, "node state is ('idle', 'open', 'boot wait', 'idle exceeded')"),
445 self.node_actor.shutdown_eligible().get(self.TIMEOUT))
447 def test_no_shutdown_when_node_state_stale(self):
448 self.make_actor(6, testutil.arvados_node_mock(6, age=90000))
449 self.shutdowns._set_state(True, 600)
450 self.assertEquals((False, "node state is stale"),
451 self.node_actor.shutdown_eligible().get(self.TIMEOUT))
453 def test_arvados_node_match(self):
455 arv_node = testutil.arvados_node_mock(
456 2, hostname='compute-two.zzzzz.arvadosapi.com')
457 self.cloud_client.node_id.return_value = '2'
458 pair_id = self.node_actor.offer_arvados_pair(arv_node).get(self.TIMEOUT)
459 self.assertEqual(self.cloud_mock.id, pair_id)
460 self.stop_proxy(self.node_actor)
461 self.updates.sync_node.assert_called_with(self.cloud_mock, arv_node)
463 def test_arvados_node_mismatch(self):
465 arv_node = testutil.arvados_node_mock(1)
467 self.node_actor.offer_arvados_pair(arv_node).get(self.TIMEOUT))
469 def test_arvados_node_mismatch_first_ping_too_early(self):
471 arv_node = testutil.arvados_node_mock(
472 4, first_ping_at='1971-03-02T14:15:16.1717282Z')
474 self.node_actor.offer_arvados_pair(arv_node).get(self.TIMEOUT))
476 def test_update_cloud_node(self):
479 self.cloud_mock.id = '1'
480 self.node_actor.update_cloud_node(self.cloud_mock)
481 current_cloud = self.node_actor.cloud_node.get(self.TIMEOUT)
482 self.assertEqual([testutil.ip_address_mock(2)],
483 current_cloud.private_ips)
485 def test_missing_cloud_node_update(self):
487 self.node_actor.update_cloud_node(None)
488 current_cloud = self.node_actor.cloud_node.get(self.TIMEOUT)
489 self.assertEqual([testutil.ip_address_mock(1)],
490 current_cloud.private_ips)
492 def test_update_arvados_node(self):
494 job_uuid = 'zzzzz-jjjjj-updatejobnode00'
495 new_arvados = testutil.arvados_node_mock(3, job_uuid)
496 self.node_actor.update_arvados_node(new_arvados)
497 current_arvados = self.node_actor.arvados_node.get(self.TIMEOUT)
498 self.assertEqual(job_uuid, current_arvados['job_uuid'])
500 def test_missing_arvados_node_update(self):
501 self.make_actor(4, testutil.arvados_node_mock(4))
502 self.node_actor.update_arvados_node(None)
503 current_arvados = self.node_actor.arvados_node.get(self.TIMEOUT)
504 self.assertEqual(testutil.ip_address_mock(4),
505 current_arvados['ip_address'])
507 def test_update_arvados_node_syncs_when_fqdn_mismatch(self):
509 self.cloud_mock.extra['testname'] = 'cloudfqdn.zzzzz.arvadosapi.com'
511 arv_node = testutil.arvados_node_mock(5)
512 self.node_actor.update_arvados_node(arv_node).get(self.TIMEOUT)
513 self.assertEqual(1, self.updates.sync_node.call_count)
515 def test_update_arvados_node_skips_sync_when_fqdn_match(self):
517 arv_node = testutil.arvados_node_mock(6)
518 self.cloud_mock.extra['testname'] ='{n[hostname]}.{n[domain]}'.format(
521 self.node_actor.update_arvados_node(arv_node).get(self.TIMEOUT)
522 self.assertEqual(0, self.updates.sync_node.call_count)