Merge branch '8567-docker-migrator' refs #8567
[arvados.git] / services / nodemanager / tests / test_failure.py
1 #!/usr/bin/env python
2
3 from __future__ import absolute_import, print_function
4
5 import errno
6 import logging
7 import time
8 import threading
9 import unittest
10
11 import mock
12 import pykka
13
14 from . import testutil
15
16 import arvnodeman.baseactor
17
18 class BogusActor(arvnodeman.baseactor.BaseNodeManagerActor):
19     def __init__(self, e):
20         super(BogusActor, self).__init__()
21         self.exp = e
22
23     def doStuff(self):
24         raise self.exp
25
26     def ping(self):
27         # Called by WatchdogActorTest, this delay is longer than the test timeout
28         # of 1 second, which should cause the watchdog ping to fail.
29         time.sleep(2)
30         return True
31
32 class ActorUnhandledExceptionTest(testutil.ActorTestMixin, unittest.TestCase):
33     def test_fatal_error(self):
34         for e in (MemoryError(), threading.ThreadError(), OSError(errno.ENOMEM, "")):
35             with mock.patch('os.kill') as kill_mock:
36                 act = BogusActor.start(e).tell_proxy()
37                 act.doStuff()
38                 act.actor_ref.stop(block=True)
39                 self.assertTrue(kill_mock.called)
40
41     @mock.patch('os.kill')
42     def test_nonfatal_error(self, kill_mock):
43         act = BogusActor.start(OSError(errno.ENOENT, "")).tell_proxy()
44         act.doStuff()
45         act.actor_ref.stop(block=True)
46         self.assertFalse(kill_mock.called)
47
48 class WatchdogActorTest(testutil.ActorTestMixin, unittest.TestCase):
49     @mock.patch('os.kill')
50     def test_time_timout(self, kill_mock):
51         act = BogusActor.start(OSError(errno.ENOENT, ""))
52         watch = arvnodeman.baseactor.WatchdogActor.start(1, act)
53         watch.stop(block=True)
54         act.stop(block=True)
55         self.assertTrue(kill_mock.called)