13937: Export stats as prometheus metrics. (WIP)
[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         bgact = BogusActor.start(OSError(errno.ENOENT, ""), killfunc=kill_mock)
52         act_thread = bgact.proxy().get_thread().get()
53         act = bgact.tell_proxy()
54         act.doStuff()
55         act.actor_ref.stop(block=True)
56         act_thread.join()
57         self.assertFalse(kill_mock.called)
58         self.assertEqual(1, status.tracker.get('actor_exceptions'))
59
60 class WatchdogActorTest(testutil.ActorTestMixin, unittest.TestCase):
61
62     def test_time_timout(self):
63         kill_mock = mock.Mock('os.kill')
64         act = BogusActor.start(OSError(errno.ENOENT, ""))
65         watch = arvnodeman.baseactor.WatchdogActor.start(1, act, killfunc=kill_mock)
66         time.sleep(1)
67         watch.stop(block=True)
68         act.stop(block=True)
69         self.assertTrue(kill_mock.called)