d00425e57e55109fa0b6d0e8536f442d228d39ac
[arvados.git] / services / nodemanager / tests / test_computenode_dispatch_slurm.py
1 #!/usr/bin/env python
2 # Copyright (C) The Arvados Authors. All rights reserved.
3 #
4 # SPDX-License-Identifier: AGPL-3.0
5
6 from __future__ import absolute_import, print_function
7
8 import subprocess
9 import time
10 import unittest
11
12 import mock
13
14 import arvnodeman.computenode.dispatch.slurm as slurm_dispatch
15 from . import testutil
16 from .test_computenode_dispatch import ComputeNodeShutdownActorMixin, ComputeNodeUpdateActorTestCase
17
18 @mock.patch('subprocess.check_output')
19 class SLURMComputeNodeShutdownActorTestCase(ComputeNodeShutdownActorMixin,
20                                             unittest.TestCase):
21     ACTOR_CLASS = slurm_dispatch.ComputeNodeShutdownActor
22
23     def check_slurm_got_args(self, proc_mock, *args):
24         self.assertTrue(proc_mock.called)
25         slurm_cmd = proc_mock.call_args[0][0]
26         for s in args:
27             self.assertIn(s, slurm_cmd)
28
29     def check_success_after_reset(self, proc_mock, end_state='drain\n', timer=False):
30         self.make_mocks(arvados_node=testutil.arvados_node_mock(63))
31         if not timer:
32             self.timer = testutil.MockTimer(False)
33         self.make_actor()
34         self.check_success_flag(None, 0)
35         # At this point, 1st try should have happened.
36
37         self.timer.deliver()
38         self.check_success_flag(None, 0)
39         # At this point, 2nd try should have happened.
40
41         # Order is critical here: if the mock gets called when no return value
42         # or side effect is set, we may invoke a real subprocess.
43         proc_mock.return_value = end_state
44         proc_mock.side_effect = None
45
46         # 3rd try
47         self.timer.deliver()
48
49         self.check_success_flag(True, 3)
50         self.check_slurm_got_args(proc_mock, 'NodeName=compute63')
51
52     def make_wait_state_test(start_state='drng\n', end_state='drain\n'):
53         def test(self, proc_mock):
54             proc_mock.return_value = start_state
55             self.check_success_after_reset(proc_mock, end_state)
56         return test
57
58     for wait_state in ['alloc\n', 'drng\n']:
59         locals()['test_wait_while_' + wait_state.strip()
60                  ] = make_wait_state_test(start_state=wait_state)
61
62     for end_state in ['idle*\n', 'down\n', 'down*\n', 'drain\n', 'fail\n']:
63         locals()['test_wait_until_' + end_state.strip()
64                  ] = make_wait_state_test(end_state=end_state)
65
66     def test_retry_failed_slurm_calls(self, proc_mock):
67         proc_mock.side_effect = subprocess.CalledProcessError(1, ["mock"])
68         self.check_success_after_reset(proc_mock)
69
70     def test_slurm_bypassed_when_no_arvados_node(self, proc_mock):
71         # Test we correctly handle a node that failed to bootstrap.
72         proc_mock.return_value = 'down\n'
73         self.make_actor(start_time=0)
74         self.check_success_flag(True)
75         self.assertFalse(proc_mock.called)
76
77     def test_node_undrained_when_shutdown_cancelled(self, proc_mock):
78         try:
79             proc_mock.side_effect = iter(['', 'drng\n', 'drng\n', ''])
80             self.make_mocks(arvados_node=testutil.arvados_node_mock(job_uuid=True))
81             self.timer = testutil.MockTimer(False)
82             self.make_actor()
83             self.shutdown_actor.ping()
84             self.busywait(lambda: proc_mock.call_args is not None)
85             self.shutdown_actor.cancel_shutdown("test")
86             self.check_success_flag(False, 2)
87             self.assertEqual(proc_mock.call_args_list,
88                              [mock.call(['scontrol', 'update', 'NodeName=compute99', 'State=DRAIN', 'Reason=Node Manager shutdown']),
89                               mock.call(['sinfo', '--noheader', '-o', '%t', '-n', 'compute99']),
90                               mock.call(['sinfo', '--noheader', '-o', '%t', '-n', 'compute99']),
91                               mock.call(['scontrol', 'update', 'NodeName=compute99', 'State=RESUME'])])
92         finally:
93             self.shutdown_actor.actor_ref.stop()
94
95     def test_cancel_shutdown_retry(self, proc_mock):
96         proc_mock.side_effect = iter([OSError, 'drain\n', OSError, 'idle\n', 'idle\n'])
97         self.make_mocks(arvados_node=testutil.arvados_node_mock(job_uuid=True))
98         self.make_actor()
99         self.check_success_flag(False, 5)
100
101     def test_issue_slurm_drain_retry(self, proc_mock):
102         proc_mock.side_effect = iter([OSError, OSError, 'drng\n', 'drain\n'])
103         self.check_success_after_reset(proc_mock, timer=False)
104
105     def test_arvados_node_cleaned_after_shutdown(self, proc_mock):
106         proc_mock.return_value = 'drain\n'
107         super(SLURMComputeNodeShutdownActorTestCase,
108               self).test_arvados_node_cleaned_after_shutdown()
109
110     def test_cancellable_shutdown(self, proc_mock):
111         proc_mock.return_value = 'other\n'
112         super(SLURMComputeNodeShutdownActorTestCase,
113               self).test_cancellable_shutdown()
114
115     def test_uncancellable_shutdown(self, proc_mock):
116         proc_mock.return_value = 'other\n'
117         super(SLURMComputeNodeShutdownActorTestCase,
118               self).test_uncancellable_shutdown()
119
120 @mock.patch('subprocess.check_output')
121 class SLURMComputeNodeUpdateActorTestCase(ComputeNodeUpdateActorTestCase):
122     ACTOR_CLASS = slurm_dispatch.ComputeNodeUpdateActor
123
124     def test_update_node_weight(self, check_output):
125         self.make_actor()
126         cloud_node = testutil.cloud_node_mock()
127         arv_node = testutil.arvados_node_mock()
128         self.updater.sync_node(cloud_node, arv_node).get(self.TIMEOUT)
129         check_output.assert_called_with(['scontrol', 'update', 'NodeName=compute99', 'Weight=99000'])