7490: Update the previously failing keep_test.go; no new tests added. We can now...
[arvados.git] / services / nodemanager / tests / test_daemon.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 mock
9 import pykka
10
11 import arvnodeman.daemon as nmdaemon
12 from arvnodeman.computenode.dispatch import ComputeNodeMonitorActor
13 from . import testutil
14
15 class NodeManagerDaemonActorTestCase(testutil.ActorTestMixin,
16                                      unittest.TestCase):
17     def new_setup_proxy(self):
18         # Make sure that every time the daemon starts a setup actor,
19         # it gets a new mock object back.
20         self.last_setup = mock.MagicMock(name='setup_proxy_mock')
21         return self.last_setup
22
23     def make_daemon(self, cloud_nodes=[], arvados_nodes=[], want_sizes=[],
24                     min_size=testutil.MockSize(1), min_nodes=0, max_nodes=8):
25         for name in ['cloud_nodes', 'arvados_nodes', 'server_wishlist']:
26             setattr(self, name + '_poller', mock.MagicMock(name=name + '_mock'))
27         self.arv_factory = mock.MagicMock(name='arvados_mock')
28         self.cloud_factory = mock.MagicMock(name='cloud_mock')
29         self.cloud_factory().node_start_time.return_value = time.time()
30         self.cloud_updates = mock.MagicMock(name='updates_mock')
31         self.timer = testutil.MockTimer(deliver_immediately=False)
32         self.node_setup = mock.MagicMock(name='setup_mock')
33         self.node_setup.start().proxy.side_effect = self.new_setup_proxy
34         self.node_setup.reset_mock()
35         self.node_shutdown = mock.MagicMock(name='shutdown_mock')
36         self.daemon = nmdaemon.NodeManagerDaemonActor.start(
37             self.server_wishlist_poller, self.arvados_nodes_poller,
38             self.cloud_nodes_poller, self.cloud_updates, self.timer,
39             self.arv_factory, self.cloud_factory,
40             [54, 5, 1], min_size, min_nodes, max_nodes, 600, 1800, 3600,
41             self.node_setup, self.node_shutdown).proxy()
42         if cloud_nodes is not None:
43             self.daemon.update_cloud_nodes(cloud_nodes).get(self.TIMEOUT)
44         if arvados_nodes is not None:
45             self.daemon.update_arvados_nodes(arvados_nodes).get(self.TIMEOUT)
46         if want_sizes is not None:
47             self.daemon.update_server_wishlist(want_sizes).get(self.TIMEOUT)
48
49     def monitor_list(self):
50         return pykka.ActorRegistry.get_by_class(ComputeNodeMonitorActor)
51
52     def monitored_arvados_nodes(self):
53         pairings = []
54         for future in [actor.proxy().arvados_node
55                        for actor in self.monitor_list()]:
56             try:
57                 pairings.append(future.get(self.TIMEOUT))
58             except pykka.ActorDeadError:
59                 pass
60         return pairings
61
62     def alive_monitor_count(self):
63         return len(self.monitored_arvados_nodes())
64
65     def assertShutdownCancellable(self, expected=True):
66         self.assertTrue(self.node_shutdown.start.called)
67         self.assertIs(expected,
68                       self.node_shutdown.start.call_args[1]['cancellable'],
69                       "ComputeNodeShutdownActor incorrectly cancellable")
70
71     def test_easy_node_creation(self):
72         size = testutil.MockSize(1)
73         self.make_daemon(want_sizes=[size])
74         self.stop_proxy(self.daemon)
75         self.assertTrue(self.node_setup.start.called)
76
77     def check_monitors_arvados_nodes(self, *arv_nodes):
78         self.assertItemsEqual(arv_nodes, self.monitored_arvados_nodes())
79
80     def test_node_pairing(self):
81         cloud_node = testutil.cloud_node_mock(1)
82         arv_node = testutil.arvados_node_mock(1)
83         self.make_daemon([cloud_node], [arv_node])
84         self.stop_proxy(self.daemon)
85         self.check_monitors_arvados_nodes(arv_node)
86
87     def test_node_pairing_after_arvados_update(self):
88         cloud_node = testutil.cloud_node_mock(2)
89         self.make_daemon([cloud_node],
90                          [testutil.arvados_node_mock(2, ip_address=None)])
91         arv_node = testutil.arvados_node_mock(2)
92         self.daemon.update_arvados_nodes([arv_node]).get(self.TIMEOUT)
93         self.stop_proxy(self.daemon)
94         self.check_monitors_arvados_nodes(arv_node)
95
96     def test_arvados_node_un_and_re_paired(self):
97         # We need to create the Arvados node mock after spinning up the daemon
98         # to make sure it's new enough to pair with the cloud node.
99         self.make_daemon([testutil.cloud_node_mock(3)], arvados_nodes=None)
100         arv_node = testutil.arvados_node_mock(3)
101         self.daemon.update_arvados_nodes([arv_node]).get(self.TIMEOUT)
102         self.check_monitors_arvados_nodes(arv_node)
103         self.daemon.update_cloud_nodes([]).get(self.TIMEOUT)
104         self.assertEqual(0, self.alive_monitor_count())
105         self.daemon.update_cloud_nodes([testutil.cloud_node_mock(3)])
106         self.stop_proxy(self.daemon)
107         self.check_monitors_arvados_nodes(arv_node)
108
109     def test_old_arvados_node_not_double_assigned(self):
110         arv_node = testutil.arvados_node_mock(3, age=9000)
111         size = testutil.MockSize(3)
112         self.make_daemon(arvados_nodes=[arv_node])
113         self.daemon.update_server_wishlist([size]).get(self.TIMEOUT)
114         self.daemon.update_server_wishlist([size, size]).get(self.TIMEOUT)
115         self.stop_proxy(self.daemon)
116         used_nodes = [call[1].get('arvados_node')
117                       for call in self.node_setup.start.call_args_list]
118         self.assertEqual(2, len(used_nodes))
119         self.assertIn(arv_node, used_nodes)
120         self.assertIn(None, used_nodes)
121
122     def test_node_count_satisfied(self):
123         self.make_daemon([testutil.cloud_node_mock()],
124                          want_sizes=[testutil.MockSize(1)])
125         self.stop_proxy(self.daemon)
126         self.assertFalse(self.node_setup.start.called)
127
128     def test_dont_count_missing_as_busy(self):
129         size = testutil.MockSize(1)
130         self.make_daemon(cloud_nodes=[testutil.cloud_node_mock(1),
131                                       testutil.cloud_node_mock(2)],
132                          arvados_nodes=[testutil.arvados_node_mock(1),
133                                       testutil.arvados_node_mock(2, last_ping_at='1970-01-01T01:02:03.04050607Z')],
134                          want_sizes=[size, size])
135         self.stop_proxy(self.daemon)
136         self.assertTrue(self.node_setup.start.called)
137
138     def test_missing_counts_towards_max(self):
139         size = testutil.MockSize(1)
140         self.make_daemon(cloud_nodes=[testutil.cloud_node_mock(1),
141                                       testutil.cloud_node_mock(2)],
142                          arvados_nodes=[testutil.arvados_node_mock(1),
143                                         testutil.arvados_node_mock(2, last_ping_at='1970-01-01T01:02:03.04050607Z')],
144                          want_sizes=[size, size],
145                          max_nodes=2)
146         self.stop_proxy(self.daemon)
147         self.assertFalse(self.node_setup.start.called)
148
149     def test_excess_counts_missing(self):
150         size = testutil.MockSize(1)
151         cloud_nodes = [testutil.cloud_node_mock(1), testutil.cloud_node_mock(2)]
152         self.make_daemon(cloud_nodes=cloud_nodes,
153                          arvados_nodes=[testutil.arvados_node_mock(1),
154                                         testutil.arvados_node_mock(2, last_ping_at='1970-01-01T01:02:03.04050607Z')],
155                          want_sizes=[size])
156         self.assertEqual(2, self.alive_monitor_count())
157         for mon_ref in self.monitor_list():
158             self.daemon.node_can_shutdown(mon_ref.proxy()).get(self.TIMEOUT)
159         self.assertEqual(1, self.node_shutdown.start.call_count)
160
161     def test_missing_shutdown_not_excess(self):
162         size = testutil.MockSize(1)
163         cloud_nodes = [testutil.cloud_node_mock(1), testutil.cloud_node_mock(2)]
164         self.make_daemon(cloud_nodes=cloud_nodes,
165                          arvados_nodes=[testutil.arvados_node_mock(1),
166                                         testutil.arvados_node_mock(2, last_ping_at='1970-01-01T01:02:03.04050607Z')],
167                          want_sizes=[size])
168         self.daemon.shutdowns.get()[cloud_nodes[1].id] = True
169         self.assertEqual(2, self.alive_monitor_count())
170         for mon_ref in self.monitor_list():
171             self.daemon.node_can_shutdown(mon_ref.proxy()).get(self.TIMEOUT)
172         self.assertEqual(0, self.node_shutdown.start.call_count)
173
174     def test_booting_nodes_counted(self):
175         cloud_node = testutil.cloud_node_mock(1)
176         arv_node = testutil.arvados_node_mock(1)
177         server_wishlist = [testutil.MockSize(1)] * 2
178         self.make_daemon([cloud_node], [arv_node], server_wishlist)
179         self.daemon.max_nodes.get(self.TIMEOUT)
180         self.assertTrue(self.node_setup.start.called)
181         self.daemon.update_server_wishlist(server_wishlist).get(self.TIMEOUT)
182         self.stop_proxy(self.daemon)
183         self.assertEqual(1, self.node_setup.start.call_count)
184
185     def test_boot_new_node_when_all_nodes_busy(self):
186         arv_node = testutil.arvados_node_mock(2, job_uuid=True)
187         self.make_daemon([testutil.cloud_node_mock(2)], [arv_node],
188                          [testutil.MockSize(2)])
189         self.stop_proxy(self.daemon)
190         self.assertTrue(self.node_setup.start.called)
191
192     def test_boot_new_node_below_min_nodes(self):
193         min_size = testutil.MockSize(1)
194         wish_size = testutil.MockSize(3)
195         self.make_daemon([], [], None, min_size=min_size, min_nodes=2)
196         self.daemon.update_server_wishlist([wish_size]).get(self.TIMEOUT)
197         self.daemon.update_cloud_nodes([]).get(self.TIMEOUT)
198         self.daemon.update_server_wishlist([wish_size]).get(self.TIMEOUT)
199         self.stop_proxy(self.daemon)
200         self.assertEqual([wish_size, min_size],
201                          [call[1].get('cloud_size')
202                           for call in self.node_setup.start.call_args_list])
203
204     def test_no_new_node_when_ge_min_nodes_busy(self):
205         cloud_nodes = [testutil.cloud_node_mock(n) for n in range(1, 4)]
206         arv_nodes = [testutil.arvados_node_mock(n, job_uuid=True)
207                      for n in range(1, 4)]
208         self.make_daemon(cloud_nodes, arv_nodes, [], min_nodes=2)
209         self.stop_proxy(self.daemon)
210         self.assertEqual(0, self.node_setup.start.call_count)
211
212     def test_no_new_node_when_max_nodes_busy(self):
213         self.make_daemon([testutil.cloud_node_mock(3)],
214                          [testutil.arvados_node_mock(3, job_uuid=True)],
215                          [testutil.MockSize(3)],
216                          max_nodes=1)
217         self.stop_proxy(self.daemon)
218         self.assertFalse(self.node_setup.start.called)
219
220     def start_node_boot(self, cloud_node=None, arv_node=None, id_num=1):
221         if cloud_node is None:
222             cloud_node = testutil.cloud_node_mock(id_num)
223         if arv_node is None:
224             arv_node = testutil.arvados_node_mock(id_num)
225         self.make_daemon(want_sizes=[testutil.MockSize(id_num)])
226         self.daemon.max_nodes.get(self.TIMEOUT)
227         self.assertEqual(1, self.node_setup.start.call_count)
228         self.last_setup.cloud_node.get.return_value = cloud_node
229         self.last_setup.arvados_node.get.return_value = arv_node
230         return self.last_setup
231
232     def test_no_new_node_when_booted_node_not_usable(self):
233         cloud_node = testutil.cloud_node_mock(4)
234         arv_node = testutil.arvados_node_mock(4, crunch_worker_state='down')
235         setup = self.start_node_boot(cloud_node, arv_node)
236         self.daemon.node_up(setup).get(self.TIMEOUT)
237         self.assertEqual(1, self.alive_monitor_count())
238         self.daemon.update_cloud_nodes([cloud_node])
239         self.daemon.update_arvados_nodes([arv_node])
240         self.daemon.update_server_wishlist(
241             [testutil.MockSize(1)]).get(self.TIMEOUT)
242         self.stop_proxy(self.daemon)
243         self.assertEqual(1, self.node_setup.start.call_count)
244
245     def test_no_duplication_when_booting_node_listed_fast(self):
246         # Test that we don't start two ComputeNodeMonitorActors when
247         # we learn about a booting node through a listing before we
248         # get the "node up" message from CloudNodeSetupActor.
249         cloud_node = testutil.cloud_node_mock(1)
250         setup = self.start_node_boot(cloud_node)
251         self.daemon.update_cloud_nodes([cloud_node])
252         self.daemon.node_up(setup).get(self.TIMEOUT)
253         self.assertEqual(1, self.alive_monitor_count())
254
255     def test_no_duplication_when_booted_node_listed(self):
256         cloud_node = testutil.cloud_node_mock(2)
257         setup = self.start_node_boot(cloud_node, id_num=2)
258         self.daemon.node_up(setup)
259         self.daemon.update_cloud_nodes([cloud_node]).get(self.TIMEOUT)
260         self.assertEqual(1, self.alive_monitor_count())
261
262     def test_node_counted_after_boot_with_slow_listing(self):
263         # Test that, after we boot a compute node, we assume it exists
264         # even it doesn't appear in the listing (e.g., because of delays
265         # propagating tags).
266         setup = self.start_node_boot()
267         self.daemon.node_up(setup).get(self.TIMEOUT)
268         self.assertEqual(1, self.alive_monitor_count())
269         self.daemon.update_cloud_nodes([]).get(self.TIMEOUT)
270         self.assertEqual(1, self.alive_monitor_count())
271
272     def test_booted_unlisted_node_counted(self):
273         setup = self.start_node_boot(id_num=1)
274         self.daemon.node_up(setup)
275         self.daemon.update_server_wishlist(
276             [testutil.MockSize(1)]).get(self.TIMEOUT)
277         self.stop_proxy(self.daemon)
278         self.assertEqual(1, self.node_setup.start.call_count)
279
280     def test_booted_node_can_shutdown(self):
281         setup = self.start_node_boot()
282         self.daemon.node_up(setup).get(self.TIMEOUT)
283         self.assertEqual(1, self.alive_monitor_count())
284         monitor = self.monitor_list()[0].proxy()
285         self.daemon.update_server_wishlist([])
286         self.daemon.node_can_shutdown(monitor).get(self.TIMEOUT)
287         self.stop_proxy(self.daemon)
288         self.assertTrue(self.node_shutdown.start.called,
289                         "daemon did not shut down booted node on offer")
290
291     def test_booted_node_lifecycle(self):
292         cloud_node = testutil.cloud_node_mock(6)
293         setup = self.start_node_boot(cloud_node, id_num=6)
294         self.daemon.node_up(setup).get(self.TIMEOUT)
295         self.assertEqual(1, self.alive_monitor_count())
296         monitor = self.monitor_list()[0].proxy()
297         self.daemon.update_server_wishlist([])
298         self.daemon.node_can_shutdown(monitor).get(self.TIMEOUT)
299         self.assertShutdownCancellable(True)
300         shutdown = self.node_shutdown.start().proxy()
301         shutdown.cloud_node.get.return_value = cloud_node
302         self.daemon.node_finished_shutdown(shutdown).get(self.TIMEOUT)
303         self.assertTrue(shutdown.stop.called,
304                         "shutdown actor not stopped after finishing")
305         self.assertTrue(monitor.actor_ref.actor_stopped.wait(self.TIMEOUT),
306                         "monitor for booted node not stopped after shutdown")
307         self.daemon.update_server_wishlist(
308             [testutil.MockSize(2)]).get(self.TIMEOUT)
309         self.stop_proxy(self.daemon)
310         self.assertTrue(self.node_setup.start.called,
311                         "second node not started after booted node stopped")
312
313     def test_booted_node_shut_down_when_never_listed(self):
314         setup = self.start_node_boot()
315         self.daemon.node_up(setup).get(self.TIMEOUT)
316         self.assertEqual(1, self.alive_monitor_count())
317         self.assertFalse(self.node_shutdown.start.called)
318         self.timer.deliver()
319         self.stop_proxy(self.daemon)
320         self.assertShutdownCancellable(False)
321
322     def test_booted_node_shut_down_when_never_paired(self):
323         cloud_node = testutil.cloud_node_mock(2)
324         setup = self.start_node_boot(cloud_node)
325         self.daemon.node_up(setup).get(self.TIMEOUT)
326         self.assertEqual(1, self.alive_monitor_count())
327         self.daemon.update_cloud_nodes([cloud_node])
328         self.timer.deliver()
329         self.stop_proxy(self.daemon)
330         self.assertShutdownCancellable(False)
331
332     def test_booted_node_shut_down_when_never_working(self):
333         cloud_node = testutil.cloud_node_mock(4)
334         arv_node = testutil.arvados_node_mock(4, crunch_worker_state='down')
335         setup = self.start_node_boot(cloud_node, arv_node)
336         self.daemon.node_up(setup).get(self.TIMEOUT)
337         self.assertEqual(1, self.alive_monitor_count())
338         self.daemon.update_cloud_nodes([cloud_node])
339         self.daemon.update_arvados_nodes([arv_node]).get(self.TIMEOUT)
340         self.timer.deliver()
341         self.stop_proxy(self.daemon)
342         self.assertShutdownCancellable(False)
343
344     def test_node_that_pairs_not_considered_failed_boot(self):
345         cloud_node = testutil.cloud_node_mock(3)
346         arv_node = testutil.arvados_node_mock(3)
347         setup = self.start_node_boot(cloud_node, arv_node)
348         self.daemon.node_up(setup).get(self.TIMEOUT)
349         self.assertEqual(1, self.alive_monitor_count())
350         self.daemon.update_cloud_nodes([cloud_node])
351         self.daemon.update_arvados_nodes([arv_node]).get(self.TIMEOUT)
352         self.timer.deliver()
353         self.stop_proxy(self.daemon)
354         self.assertFalse(self.node_shutdown.start.called)
355
356     def test_node_that_pairs_busy_not_considered_failed_boot(self):
357         cloud_node = testutil.cloud_node_mock(5)
358         arv_node = testutil.arvados_node_mock(5, job_uuid=True)
359         setup = self.start_node_boot(cloud_node, arv_node)
360         self.daemon.node_up(setup).get(self.TIMEOUT)
361         self.assertEqual(1, self.alive_monitor_count())
362         self.daemon.update_cloud_nodes([cloud_node])
363         self.daemon.update_arvados_nodes([arv_node]).get(self.TIMEOUT)
364         self.timer.deliver()
365         self.stop_proxy(self.daemon)
366         self.assertFalse(self.node_shutdown.start.called)
367
368     def test_booting_nodes_shut_down(self):
369         self.make_daemon(want_sizes=[testutil.MockSize(1)])
370         self.daemon.update_server_wishlist([]).get(self.TIMEOUT)
371         self.stop_proxy(self.daemon)
372         self.assertTrue(self.last_setup.stop_if_no_cloud_node.called)
373
374     def test_all_booting_nodes_tried_to_shut_down(self):
375         size = testutil.MockSize(2)
376         self.make_daemon(want_sizes=[size])
377         self.daemon.max_nodes.get(self.TIMEOUT)
378         setup1 = self.last_setup
379         setup1.stop_if_no_cloud_node().get.return_value = False
380         setup1.stop_if_no_cloud_node.reset_mock()
381         self.daemon.update_server_wishlist([size, size]).get(self.TIMEOUT)
382         self.daemon.max_nodes.get(self.TIMEOUT)
383         self.assertIsNot(setup1, self.last_setup)
384         self.last_setup.stop_if_no_cloud_node().get.return_value = True
385         self.last_setup.stop_if_no_cloud_node.reset_mock()
386         self.daemon.update_server_wishlist([]).get(self.TIMEOUT)
387         self.daemon.max_nodes.get(self.TIMEOUT)
388         self.stop_proxy(self.daemon)
389         self.assertEqual(1, self.last_setup.stop_if_no_cloud_node.call_count)
390         self.assertTrue(setup1.stop_if_no_cloud_node.called)
391
392     def test_shutdown_declined_at_wishlist_capacity(self):
393         cloud_node = testutil.cloud_node_mock(1)
394         size = testutil.MockSize(1)
395         self.make_daemon(cloud_nodes=[cloud_node], want_sizes=[size])
396         self.assertEqual(1, self.alive_monitor_count())
397         monitor = self.monitor_list()[0].proxy()
398         self.daemon.node_can_shutdown(monitor).get(self.TIMEOUT)
399         self.stop_proxy(self.daemon)
400         self.assertFalse(self.node_shutdown.start.called)
401
402     def test_shutdown_declined_below_min_nodes(self):
403         cloud_node = testutil.cloud_node_mock(1)
404         self.make_daemon(cloud_nodes=[cloud_node], min_nodes=1)
405         self.assertEqual(1, self.alive_monitor_count())
406         monitor = self.monitor_list()[0].proxy()
407         self.daemon.node_can_shutdown(monitor).get(self.TIMEOUT)
408         self.stop_proxy(self.daemon)
409         self.assertFalse(self.node_shutdown.start.called)
410
411     def test_shutdown_accepted_below_capacity(self):
412         self.make_daemon(cloud_nodes=[testutil.cloud_node_mock()])
413         self.assertEqual(1, self.alive_monitor_count())
414         monitor = self.monitor_list()[0].proxy()
415         self.daemon.node_can_shutdown(monitor).get(self.TIMEOUT)
416         self.stop_proxy(self.daemon)
417         self.assertTrue(self.node_shutdown.start.called)
418
419     def test_shutdown_declined_when_idle_and_job_queued(self):
420         cloud_nodes = [testutil.cloud_node_mock(n) for n in [3, 4]]
421         arv_nodes = [testutil.arvados_node_mock(3, job_uuid=True),
422                      testutil.arvados_node_mock(4, job_uuid=None)]
423         self.make_daemon(cloud_nodes, arv_nodes, [testutil.MockSize(1)])
424         self.assertEqual(2, self.alive_monitor_count())
425         for mon_ref in self.monitor_list():
426             monitor = mon_ref.proxy()
427             if monitor.cloud_node.get(self.TIMEOUT) is cloud_nodes[-1]:
428                 break
429         else:
430             self.fail("monitor for idle node not found")
431         self.daemon.node_can_shutdown(monitor).get(self.TIMEOUT)
432         self.stop_proxy(self.daemon)
433         self.assertFalse(self.node_shutdown.start.called)
434
435     def test_node_shutdown_after_cancelled_shutdown(self):
436         cloud_node = testutil.cloud_node_mock(5)
437         self.make_daemon([cloud_node], [testutil.arvados_node_mock(5)])
438         self.assertEqual(1, self.alive_monitor_count())
439         monitor = self.monitor_list()[0].proxy()
440         shutdown_proxy = self.node_shutdown.start().proxy
441         shutdown_proxy().cloud_node.get.return_value = cloud_node
442         shutdown_proxy().success.get.return_value = False
443         shutdown_proxy.reset_mock()
444         self.daemon.node_can_shutdown(monitor).get(self.TIMEOUT)
445         self.assertTrue(shutdown_proxy.called)
446         self.daemon.node_finished_shutdown(shutdown_proxy()).get(self.TIMEOUT)
447         shutdown_proxy().success.get.return_value = True
448         shutdown_proxy.reset_mock()
449         self.daemon.node_can_shutdown(monitor).get(self.TIMEOUT)
450         self.assertTrue(shutdown_proxy.called)
451
452     def test_broken_node_blackholed_after_cancelled_shutdown(self):
453         cloud_node = testutil.cloud_node_mock(8)
454         wishlist = [testutil.MockSize(8)]
455         self.make_daemon([cloud_node], [testutil.arvados_node_mock(8)],
456                          wishlist)
457         self.assertEqual(1, self.alive_monitor_count())
458         self.assertFalse(self.node_setup.start.called)
459         monitor = self.monitor_list()[0].proxy()
460         shutdown_proxy = self.node_shutdown.start().proxy
461         shutdown_proxy().cloud_node.get.return_value = cloud_node
462         shutdown_proxy().success.get.return_value = False
463         shutdown_proxy().cancel_reason.get.return_value = self.node_shutdown.NODE_BROKEN
464         self.daemon.update_server_wishlist([]).get(self.TIMEOUT)
465         self.daemon.node_can_shutdown(monitor).get(self.TIMEOUT)
466         self.daemon.node_finished_shutdown(shutdown_proxy()).get(self.TIMEOUT)
467         self.daemon.update_cloud_nodes([cloud_node]).get(self.TIMEOUT)
468         self.daemon.update_server_wishlist(wishlist).get(self.TIMEOUT)
469         self.stop_proxy(self.daemon)
470         self.assertEqual(1, self.node_setup.start.call_count)
471
472     def test_nodes_shutting_down_replaced_below_max_nodes(self):
473         cloud_node = testutil.cloud_node_mock(6)
474         self.make_daemon([cloud_node], [testutil.arvados_node_mock(6)])
475         self.assertEqual(1, self.alive_monitor_count())
476         monitor = self.monitor_list()[0].proxy()
477         self.daemon.node_can_shutdown(monitor).get(self.TIMEOUT)
478         self.assertTrue(self.node_shutdown.start.called)
479         self.daemon.update_server_wishlist(
480             [testutil.MockSize(6)]).get(self.TIMEOUT)
481         self.stop_proxy(self.daemon)
482         self.assertTrue(self.node_setup.start.called)
483
484     def test_nodes_shutting_down_not_replaced_at_max_nodes(self):
485         cloud_node = testutil.cloud_node_mock(7)
486         self.make_daemon([cloud_node], [testutil.arvados_node_mock(7)],
487                          max_nodes=1)
488         self.assertEqual(1, self.alive_monitor_count())
489         monitor = self.monitor_list()[0].proxy()
490         self.daemon.node_can_shutdown(monitor).get(self.TIMEOUT)
491         self.assertTrue(self.node_shutdown.start.called)
492         self.daemon.update_server_wishlist(
493             [testutil.MockSize(7)]).get(self.TIMEOUT)
494         self.stop_proxy(self.daemon)
495         self.assertFalse(self.node_setup.start.called)
496
497     def test_nodes_shutting_down_count_against_excess(self):
498         cloud_nodes = [testutil.cloud_node_mock(n) for n in [8, 9]]
499         arv_nodes = [testutil.arvados_node_mock(n) for n in [8, 9]]
500         self.make_daemon(cloud_nodes, arv_nodes, [testutil.MockSize(8)])
501         self.assertEqual(2, self.alive_monitor_count())
502         for mon_ref in self.monitor_list():
503             self.daemon.node_can_shutdown(mon_ref.proxy()).get(self.TIMEOUT)
504         self.assertEqual(1, self.node_shutdown.start.call_count)
505
506     def test_clean_shutdown_waits_for_node_setup_finish(self):
507         new_node = self.start_node_boot()
508         new_node.stop_if_no_cloud_node().get.return_value = False
509         new_node.stop_if_no_cloud_node.reset_mock()
510         self.daemon.shutdown().get(self.TIMEOUT)
511         self.assertTrue(new_node.stop_if_no_cloud_node.called)
512         self.daemon.node_up(new_node).get(self.TIMEOUT)
513         self.assertTrue(new_node.stop.called)
514         self.timer.deliver()
515         self.assertTrue(
516             self.daemon.actor_ref.actor_stopped.wait(self.TIMEOUT))
517
518     def test_wishlist_ignored_after_shutdown(self):
519         new_node = self.start_node_boot()
520         new_node.stop_if_no_cloud_node().get.return_value = False
521         new_node.stop_if_no_cloud_node.reset_mock()
522         self.daemon.shutdown().get(self.TIMEOUT)
523         size = testutil.MockSize(2)
524         self.daemon.update_server_wishlist([size] * 2).get(self.TIMEOUT)
525         self.timer.deliver()
526         self.stop_proxy(self.daemon)
527         self.assertEqual(1, self.node_setup.start.call_count)
528
529     def test_shutdown_actor_stopped_when_cloud_node_delisted(self):
530         self.make_daemon(cloud_nodes=[testutil.cloud_node_mock()])
531         self.assertEqual(1, self.alive_monitor_count())
532         monitor = self.monitor_list()[0].proxy()
533         self.daemon.node_can_shutdown(monitor).get(self.TIMEOUT)
534         self.daemon.update_cloud_nodes([]).get(self.TIMEOUT)
535         self.stop_proxy(self.daemon)
536         self.assertEqual(
537             1, self.node_shutdown.start().proxy().stop().get.call_count)
538
539     def test_shutdown_actor_cleanup_copes_with_dead_actors(self):
540         self.make_daemon(cloud_nodes=[testutil.cloud_node_mock()])
541         self.assertEqual(1, self.alive_monitor_count())
542         monitor = self.monitor_list()[0].proxy()
543         self.daemon.node_can_shutdown(monitor).get(self.TIMEOUT)
544         # We're mainly testing that update_cloud_nodes catches and handles
545         # the ActorDeadError.
546         stop_method = self.node_shutdown.start().proxy().stop().get
547         stop_method.side_effect = pykka.ActorDeadError
548         self.daemon.update_cloud_nodes([]).get(self.TIMEOUT)
549         self.stop_proxy(self.daemon)
550         self.assertEqual(1, stop_method.call_count)