Merge branch '13338-wb-run-wf-proj' refs #13338
[arvados.git] / services / nodemanager / tests / test_failure.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 errno
9 import logging
10 import time
11 import threading
12 import unittest
13
14 import mock
15 import pykka
16
17 from . import testutil
18
19 import arvnodeman.baseactor
20 import arvnodeman.status as status
21
22 class BogusActor(arvnodeman.baseactor.BaseNodeManagerActor):
23     def __init__(self, e, killfunc=None):
24         super(BogusActor, self).__init__(killfunc=killfunc)
25         self.exp = e
26
27     def doStuff(self):
28         raise self.exp
29
30     def ping(self):
31         # Called by WatchdogActorTest, this delay is longer than the test timeout
32         # of 1 second, which should cause the watchdog ping to fail.
33         time.sleep(2)
34         return True
35
36 class ActorUnhandledExceptionTest(testutil.ActorTestMixin, unittest.TestCase):
37     def test_fatal_error(self):
38         for e in (MemoryError(), threading.ThreadError(), OSError(errno.ENOMEM, "")):
39             kill_mock = mock.Mock('os.kill')
40             bgact = BogusActor.start(e, killfunc=kill_mock)
41             act_thread = bgact.proxy().get_thread().get()
42             act = bgact.tell_proxy()
43             act.doStuff()
44             act.actor_ref.stop(block=True)
45             act_thread.join()
46             self.assertTrue(kill_mock.called)
47
48     def test_nonfatal_error(self):
49         status.tracker.update({'actor_exceptions': 0})
50         kill_mock = mock.Mock('os.kill')
51         act = BogusActor.start(OSError(errno.ENOENT, ""), killfunc=kill_mock).tell_proxy()
52         act.doStuff()
53         act.actor_ref.stop(block=True)
54         self.assertFalse(kill_mock.called)
55         self.assertEqual(1, status.tracker.get('actor_exceptions'))
56
57 class WatchdogActorTest(testutil.ActorTestMixin, unittest.TestCase):
58
59     def test_time_timout(self):
60         kill_mock = mock.Mock('os.kill')
61         act = BogusActor.start(OSError(errno.ENOENT, ""))
62         watch = arvnodeman.baseactor.WatchdogActor.start(1, act, killfunc=kill_mock)
63         time.sleep(1)
64         watch.stop(block=True)
65         act.stop(block=True)
66         self.assertTrue(kill_mock.called)