3 from __future__ import absolute_import, print_function
8 import arvados.errors as arverror
13 import arvnodeman.computenode.dispatch as dispatch
14 from . import testutil
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)
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()
35 def test_creation_without_arvados_node(self):
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))
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))
51 def test_failed_arvados_calls_retried(self):
53 arverror.ApiError(httplib2.Response({'status': '500'}), ""),
54 testutil.arvados_node_mock(),
57 self.wait_for_assignment(self.setup_actor, 'arvados_node')
59 def test_failed_cloud_calls_retried(self):
61 self.cloud_client.create_node.side_effect = [
62 Exception("test cloud creation error"),
63 self.cloud_client.create_node.return_value,
66 self.wait_for_assignment(self.setup_actor, 'cloud_node')
68 def test_failed_post_create_retried(self):
70 self.cloud_client.post_create_node.side_effect = [
71 Exception("test cloud post-create error"), None]
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)
78 def test_stop_when_no_cloud_node(self):
80 arverror.ApiError(httplib2.Response({'status': '500'}), ""))
83 self.setup_actor.stop_if_no_cloud_node().get(self.TIMEOUT))
85 self.setup_actor.actor_ref.actor_stopped.wait(self.TIMEOUT))
87 def test_no_stop_when_cloud_node(self):
89 self.wait_for_assignment(self.setup_actor, 'cloud_node')
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")
95 def test_subscribe(self):
97 arverror.ApiError(httplib2.Response({'status': '500'}), ""))
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)
107 def test_late_subscribe(self):
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)
117 class ComputeNodeShutdownActorMixin(testutil.ActorTestMixin):
118 def make_mocks(self, cloud_node=None, arvados_node=None,
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
130 def make_actor(self, cancellable=True):
131 if not hasattr(self, 'timer'):
133 monitor_actor = dispatch.ComputeNodeMonitorActor.start(
134 self.cloud_node, time.time(), self.shutdowns,
135 testutil.cloud_node_fqdn, self.timer, self.updates,
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()
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:
149 self.fail("success flag {} is not {}".format(last_flag, expected))
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)
161 class ComputeNodeShutdownActorTestCase(ComputeNodeShutdownActorMixin,
163 ACTOR_CLASS = dispatch.ComputeNodeShutdownActor
165 def test_easy_shutdown(self):
167 self.check_success_flag(True)
168 self.assertTrue(self.cloud_client.destroy_node.called)
170 def test_shutdown_cancelled_when_window_closes(self):
171 self.make_mocks(shutdown_open=False)
173 self.check_success_flag(False, 2)
174 self.assertFalse(self.cloud_client.destroy_node.called)
176 def test_shutdown_retries_when_cloud_fails(self):
178 self.cloud_client.destroy_node.return_value = False
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)
184 def test_late_subscribe(self):
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)
194 class ComputeNodeUpdateActorTestCase(testutil.ActorTestMixin,
196 def make_actor(self):
197 self.driver = mock.MagicMock(name='driver_mock')
198 self.updater = dispatch.ComputeNodeUpdateActor.start(self.driver).proxy()
200 def test_node_sync(self):
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)
208 class ComputeNodeMonitorActorTestCase(testutil.ActorTestMixin,
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')
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,
227 self.node_actor.subscribe(self.subscriber).get(self.TIMEOUT)
229 def node_state(self, *states):
230 return self.node_actor.in_state(*states).get(self.TIMEOUT)
232 def test_in_state_when_unpaired(self):
234 self.assertIsNone(self.node_state('idle', 'busy'))
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'))
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'))
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'))
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'))
258 def test_init_shutdown_scheduling(self):
260 self.assertTrue(self.timer.schedule.called)
261 self.assertEqual(300, self.timer.schedule.call_args[0][0])
263 def test_shutdown_window_close_scheduling(self):
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)
273 def test_shutdown_subscription(self):
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)
281 def test_shutdown_without_arvados_node(self):
283 self.shutdowns._set_state(True, 600)
284 self.assertTrue(self.node_actor.shutdown_eligible().get(self.TIMEOUT))
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))
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))
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))
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))
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))
311 def test_arvados_node_match(self):
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)
320 def test_arvados_node_mismatch(self):
322 arv_node = testutil.arvados_node_mock(1)
324 self.node_actor.offer_arvados_pair(arv_node).get(self.TIMEOUT))
326 def test_arvados_node_mismatch_first_ping_too_early(self):
328 arv_node = testutil.arvados_node_mock(
329 4, first_ping_at='1971-03-02T14:15:16.1717282Z')
331 self.node_actor.offer_arvados_pair(arv_node).get(self.TIMEOUT))
333 def test_update_cloud_node(self):
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)
342 def test_missing_cloud_node_update(self):
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)
349 def test_update_arvados_node(self):
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'])
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'])
364 def test_update_arvados_node_syncs_when_fqdn_mismatch(self):
366 self.cloud_mock.extra['testname'] = 'cloudfqdn.zzzzz.arvadosapi.com'
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)
372 def test_update_arvados_node_skips_sync_when_fqdn_match(self):
374 arv_node = testutil.arvados_node_mock(6)
375 self.cloud_mock.extra['testname'] ='{n[hostname]}.{n[domain]}'.format(
378 self.node_actor.update_arvados_node(arv_node).get(self.TIMEOUT)
379 self.assertEqual(0, self.updates.sync_node.call_count)