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