3 from __future__ import absolute_import, print_function
11 import arvnodeman.computenode.dispatch.slurm as slurm_dispatch
12 from . import testutil
13 from .test_computenode_dispatch import ComputeNodeShutdownActorMixin, ComputeNodeUpdateActorTestCase
15 @mock.patch('subprocess.check_output')
16 class SLURMComputeNodeShutdownActorTestCase(ComputeNodeShutdownActorMixin,
18 ACTOR_CLASS = slurm_dispatch.ComputeNodeShutdownActor
20 def check_slurm_got_args(self, proc_mock, *args):
21 self.assertTrue(proc_mock.called)
22 slurm_cmd = proc_mock.call_args[0][0]
24 self.assertIn(s, slurm_cmd)
26 def check_success_after_reset(self, proc_mock, end_state='drain\n'):
27 self.make_mocks(arvados_node=testutil.arvados_node_mock(63))
29 self.check_success_flag(None, 0)
30 self.check_success_flag(None, 0)
31 # Order is critical here: if the mock gets called when no return value
32 # or side effect is set, we may invoke a real subprocess.
33 proc_mock.return_value = end_state
34 proc_mock.side_effect = None
35 self.check_success_flag(True, 3)
36 self.check_slurm_got_args(proc_mock, 'NodeName=compute63')
38 def make_wait_state_test(start_state='drng\n', end_state='drain\n'):
39 def test(self, proc_mock):
40 proc_mock.return_value = start_state
41 self.check_success_after_reset(proc_mock, end_state)
44 for wait_state in ['alloc\n', 'drng\n']:
45 locals()['test_wait_while_' + wait_state.strip()
46 ] = make_wait_state_test(start_state=wait_state)
48 for end_state in ['idle*\n', 'down\n', 'down*\n', 'drain\n', 'fail\n']:
49 locals()['test_wait_until_' + end_state.strip()
50 ] = make_wait_state_test(end_state=end_state)
52 def test_retry_failed_slurm_calls(self, proc_mock):
53 proc_mock.side_effect = subprocess.CalledProcessError(1, ["mock"])
54 self.check_success_after_reset(proc_mock)
56 def test_slurm_bypassed_when_no_arvados_node(self, proc_mock):
57 # Test we correctly handle a node that failed to bootstrap.
58 proc_mock.return_value = 'down\n'
59 self.make_actor(start_time=0)
60 self.check_success_flag(True)
61 self.assertFalse(proc_mock.called)
63 def test_node_undrained_when_shutdown_cancelled(self, proc_mock):
65 proc_mock.side_effect = iter(['', 'drng\n', 'drng\n', ''])
66 self.make_mocks(arvados_node=testutil.arvados_node_mock(job_uuid=True))
67 self.timer = testutil.MockTimer(False)
69 self.busywait(lambda: proc_mock.call_args is not None)
70 self.shutdown_actor.cancel_shutdown("test").get(self.TIMEOUT)
71 self.check_success_flag(False, 2)
72 self.assertEqual(proc_mock.call_args_list,
73 [mock.call(['scontrol', 'update', 'NodeName=compute99', 'State=DRAIN', 'Reason=Node Manager shutdown']),
74 mock.call(['sinfo', '--noheader', '-o', '%t', '-n', 'compute99']),
75 mock.call(['sinfo', '--noheader', '-o', '%t', '-n', 'compute99']),
76 mock.call(['scontrol', 'update', 'NodeName=compute99', 'State=RESUME'])])
78 self.shutdown_actor.actor_ref.stop()
80 def test_cancel_shutdown_retry(self, proc_mock):
81 proc_mock.side_effect = iter([OSError, 'drain\n', OSError, 'idle\n', 'idle\n'])
82 self.make_mocks(arvados_node=testutil.arvados_node_mock(job_uuid=True))
84 self.check_success_flag(False, 2)
86 def test_issue_slurm_drain_retry(self, proc_mock):
87 proc_mock.side_effect = iter([OSError, '', OSError, 'drng\n'])
88 self.check_success_after_reset(proc_mock)
90 def test_arvados_node_cleaned_after_shutdown(self, proc_mock):
91 proc_mock.return_value = 'drain\n'
92 super(SLURMComputeNodeShutdownActorTestCase,
93 self).test_arvados_node_cleaned_after_shutdown()
95 def test_cancellable_shutdown(self, proc_mock):
96 proc_mock.return_value = 'other\n'
97 super(SLURMComputeNodeShutdownActorTestCase,
98 self).test_cancellable_shutdown()
100 def test_uncancellable_shutdown(self, proc_mock):
101 proc_mock.return_value = 'other\n'
102 super(SLURMComputeNodeShutdownActorTestCase,
103 self).test_uncancellable_shutdown()
105 @mock.patch('subprocess.check_output')
106 class SLURMComputeNodeUpdateActorTestCase(ComputeNodeUpdateActorTestCase):
107 ACTOR_CLASS = slurm_dispatch.ComputeNodeUpdateActor
109 def test_update_node_weight(self, check_output):
111 cloud_node = testutil.cloud_node_mock()
112 arv_node = testutil.arvados_node_mock()
113 self.updater.sync_node(cloud_node, arv_node).get(self.TIMEOUT)
114 check_output.assert_called_with(['scontrol', 'update', 'NodeName=compute99', 'Weight=99000'])