4062: update python testing to handle the multiple fixture object creation, similar...
[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
10 import arvnodeman.daemon as nmdaemon
11 from . import testutil
12
13 class NodeManagerDaemonActorTestCase(testutil.ActorTestMixin,
14                                      unittest.TestCase):
15     def make_daemon(self, cloud_nodes=[], arvados_nodes=[], want_sizes=[]):
16         for name in ['cloud_nodes', 'arvados_nodes', 'server_wishlist']:
17             setattr(self, name + '_poller', mock.MagicMock(name=name + '_mock'))
18         self.arv_factory = mock.MagicMock(name='arvados_mock')
19         self.cloud_factory = mock.MagicMock(name='cloud_mock')
20         self.cloud_factory().node_start_time.return_value = time.time()
21         self.cloud_updates = mock.MagicMock(name='updates_mock')
22         self.timer = testutil.MockTimer()
23         self.node_factory = mock.MagicMock(name='factory_mock')
24         self.node_setup = mock.MagicMock(name='setup_mock')
25         self.node_shutdown = mock.MagicMock(name='shutdown_mock')
26         self.daemon = nmdaemon.NodeManagerDaemonActor.start(
27             self.server_wishlist_poller, self.arvados_nodes_poller,
28             self.cloud_nodes_poller, self.cloud_updates, self.timer,
29             self.arv_factory, self.cloud_factory,
30             [54, 5, 1], 8, 600, 3600,
31             self.node_setup, self.node_shutdown, self.node_factory).proxy()
32         if cloud_nodes is not None:
33             self.daemon.update_cloud_nodes(cloud_nodes).get(self.TIMEOUT)
34         if arvados_nodes is not None:
35             self.daemon.update_arvados_nodes(arvados_nodes).get(self.TIMEOUT)
36         if want_sizes is not None:
37             self.daemon.update_server_wishlist(want_sizes).get(self.TIMEOUT)
38
39     def test_easy_node_creation(self):
40         size = testutil.MockSize(1)
41         self.make_daemon(want_sizes=[size])
42         self.stop_proxy(self.daemon)
43         self.assertTrue(self.node_setup.start.called)
44
45     def test_node_pairing(self):
46         cloud_node = testutil.cloud_node_mock(1)
47         arv_node = testutil.arvados_node_mock(1)
48         self.make_daemon([cloud_node], [arv_node])
49         self.stop_proxy(self.daemon)
50         self.node_factory.start().proxy().offer_arvados_pair.assert_called_with(
51             arv_node)
52
53     def test_node_pairing_after_arvados_update(self):
54         cloud_node = testutil.cloud_node_mock(2)
55         arv_node = testutil.arvados_node_mock(2, ip_address=None)
56         self.make_daemon([cloud_node], None)
57         pair_func = self.node_factory.start().proxy().offer_arvados_pair
58         pair_func().get.return_value = None
59         self.daemon.update_arvados_nodes([arv_node]).get(self.TIMEOUT)
60         pair_func.assert_called_with(arv_node)
61
62         pair_func().get.return_value = cloud_node.id
63         pair_func.reset_mock()
64         arv_node = testutil.arvados_node_mock(2)
65         self.daemon.update_arvados_nodes([arv_node]).get(self.TIMEOUT)
66         pair_func.assert_called_with(arv_node)
67
68     def test_old_arvados_node_not_double_assigned(self):
69         arv_node = testutil.arvados_node_mock(3, age=9000)
70         size = testutil.MockSize(3)
71         self.make_daemon(arvados_nodes=[arv_node])
72         setup_ref = self.node_setup.start().proxy().actor_ref
73         setup_ref.actor_urn = 0
74         self.node_setup.start.reset_mock()
75         self.daemon.update_server_wishlist([size]).get(self.TIMEOUT)
76         self.daemon.max_nodes.get(self.TIMEOUT)
77         setup_ref.actor_urn += 1
78         self.daemon.update_server_wishlist([size, size]).get(self.TIMEOUT)
79         self.stop_proxy(self.daemon)
80         used_nodes = [call[1].get('arvados_node')
81                       for call in self.node_setup.start.call_args_list]
82         self.assertEqual(2, len(used_nodes))
83         self.assertIn(arv_node, used_nodes)
84         self.assertIn(None, used_nodes)
85
86     def test_node_count_satisfied(self):
87         self.make_daemon([testutil.cloud_node_mock()],
88                          want_sizes=[testutil.MockSize(1)])
89         self.stop_proxy(self.daemon)
90         self.assertFalse(self.node_setup.called)
91
92     def test_booting_nodes_counted(self):
93         cloud_node = testutil.cloud_node_mock(1)
94         arv_node = testutil.arvados_node_mock(1)
95         server_wishlist = [testutil.MockSize(1)] * 2
96         self.make_daemon([cloud_node], [arv_node], server_wishlist)
97         self.daemon.max_nodes.get(self.TIMEOUT)
98         self.assertTrue(self.node_setup.start.called)
99         self.daemon.update_server_wishlist(server_wishlist).get(self.TIMEOUT)
100         self.stop_proxy(self.daemon)
101         self.assertEqual(1, self.node_setup.start.call_count)
102
103     def test_no_duplication_when_booting_node_listed_fast(self):
104         # Test that we don't start two ComputeNodeMonitorActors when
105         # we learn about a booting node through a listing before we
106         # get the "node up" message from CloudNodeSetupActor.
107         cloud_node = testutil.cloud_node_mock(1)
108         self.make_daemon(want_sizes=[testutil.MockSize(1)])
109         self.daemon.max_nodes.get(self.TIMEOUT)
110         self.assertEqual(1, self.node_setup.start.call_count)
111         setup = mock.MagicMock(name='setup_node_mock')
112         setup.actor_ref = self.node_setup.start().proxy().actor_ref
113         setup.cloud_node.get.return_value = cloud_node
114         setup.arvados_node.get.return_value = testutil.arvados_node_mock(1)
115         self.daemon.update_cloud_nodes([cloud_node]).get(self.TIMEOUT)
116         self.assertTrue(self.node_factory.start.called)
117         self.daemon.node_up(setup).get(self.TIMEOUT)
118         self.assertEqual(1, self.node_factory.start.call_count)
119
120     def test_booting_nodes_shut_down(self):
121         self.make_daemon(want_sizes=[testutil.MockSize(1)])
122         self.daemon.update_server_wishlist([]).get(self.TIMEOUT)
123         self.stop_proxy(self.daemon)
124         self.assertTrue(
125             self.node_setup.start().proxy().stop_if_no_cloud_node.called)
126
127     def test_shutdown_declined_at_wishlist_capacity(self):
128         cloud_node = testutil.cloud_node_mock(1)
129         size = testutil.MockSize(1)
130         self.make_daemon(cloud_nodes=[cloud_node], want_sizes=[size])
131         node_actor = self.node_factory().proxy()
132         self.daemon.node_can_shutdown(node_actor).get(self.TIMEOUT)
133         self.stop_proxy(self.daemon)
134         self.assertFalse(node_actor.shutdown.called)
135
136     def test_shutdown_accepted_below_capacity(self):
137         self.make_daemon(cloud_nodes=[testutil.cloud_node_mock()])
138         node_actor = self.node_factory().proxy()
139         self.daemon.node_can_shutdown(node_actor).get(self.TIMEOUT)
140         self.stop_proxy(self.daemon)
141         self.assertTrue(self.node_shutdown.start.called)
142
143     def test_clean_shutdown_waits_for_node_setup_finish(self):
144         self.make_daemon(want_sizes=[testutil.MockSize(1)])
145         self.daemon.max_nodes.get(self.TIMEOUT)
146         self.assertTrue(self.node_setup.start.called)
147         new_node = self.node_setup.start().proxy()
148         self.daemon.shutdown().get(self.TIMEOUT)
149         self.assertTrue(new_node.stop_if_no_cloud_node.called)
150         self.daemon.node_up(new_node).get(self.TIMEOUT)
151         self.assertTrue(new_node.stop.called)
152         self.assertTrue(
153             self.daemon.actor_ref.actor_stopped.wait(self.TIMEOUT))
154
155     def test_wishlist_ignored_after_shutdown(self):
156         size = testutil.MockSize(2)
157         self.make_daemon(want_sizes=[size])
158         self.daemon.shutdown().get(self.TIMEOUT)
159         self.daemon.update_server_wishlist([size] * 2).get(self.TIMEOUT)
160         self.stop_proxy(self.daemon)
161         self.assertEqual(1, self.node_setup.start.call_count)