5182: Advise adding CORS headers to proxy error responses.
[arvados.git] / services / nodemanager / tests / test_computenode_dispatch_slurm.py
1 #!/usr/bin/env python
2
3 from __future__ import absolute_import, print_function
4
5 import subprocess
6 import unittest
7
8 import mock
9
10 import arvnodeman.computenode.dispatch.slurm as slurm_dispatch
11 from . import testutil
12 from .test_computenode_dispatch import ComputeNodeShutdownActorMixin
13
14 @mock.patch('subprocess.check_output')
15 class SLURMComputeNodeShutdownActorTestCase(ComputeNodeShutdownActorMixin,
16                                             unittest.TestCase):
17     ACTOR_CLASS = slurm_dispatch.ComputeNodeShutdownActor
18
19     def check_slurm_got_args(self, proc_mock, *args):
20         self.assertTrue(proc_mock.called)
21         slurm_cmd = proc_mock.call_args[0][0]
22         for s in args:
23             self.assertIn(s, slurm_cmd)
24
25     def check_success_after_reset(self, proc_mock, end_state='drain\n'):
26         self.make_mocks(arvados_node=testutil.arvados_node_mock(63))
27         self.make_actor()
28         self.check_success_flag(None, 0)
29         self.check_success_flag(None, 0)
30         # Order is critical here: if the mock gets called when no return value
31         # or side effect is set, we may invoke a real subprocess.
32         proc_mock.return_value = end_state
33         proc_mock.side_effect = None
34         self.check_success_flag(True, 3)
35         self.check_slurm_got_args(proc_mock, 'compute63')
36
37     def make_wait_state_test(start_state='drng\n', end_state='drain\n'):
38         def test(self, proc_mock):
39             proc_mock.return_value = start_state
40             self.check_success_after_reset(proc_mock, end_state)
41         return test
42
43     for wait_state in ['alloc\n', 'drng\n', 'idle*\n']:
44         locals()['test_wait_while_' + wait_state.strip()
45                  ] = make_wait_state_test(start_state=wait_state)
46
47     for end_state in ['down\n', 'down*\n', 'drain\n', 'fail\n']:
48         locals()['test_wait_until_' + end_state.strip()
49                  ] = make_wait_state_test(end_state=end_state)
50
51     def test_retry_failed_slurm_calls(self, proc_mock):
52         proc_mock.side_effect = subprocess.CalledProcessError(1, ["mock"])
53         self.check_success_after_reset(proc_mock)
54
55     def test_slurm_bypassed_when_no_arvados_node(self, proc_mock):
56         # Test we correctly handle a node that failed to bootstrap.
57         proc_mock.return_value = 'idle\n'
58         self.make_actor()
59         self.check_success_flag(True)
60         self.assertFalse(proc_mock.called)
61
62     def test_node_undrained_when_shutdown_window_closes(self, proc_mock):
63         proc_mock.return_value = 'alloc\n'
64         self.make_mocks(arvados_node=testutil.arvados_node_mock(job_uuid=True))
65         self.make_actor()
66         self.check_success_flag(False, 2)
67         self.check_slurm_got_args(proc_mock, 'NodeName=compute99',
68                                   'State=RESUME')