4380: Node Manager shutdown actor is more robust.
[arvados.git] / services / nodemanager / tests / testutil.py
1 #!/usr/bin/env python
2
3 from __future__ import absolute_import, print_function
4
5 import time
6
7 import mock
8 import pykka
9
10 from . import pykka_timeout
11
12 no_sleep = mock.patch('time.sleep', lambda n: None)
13
14 def arvados_node_mock(node_num=99, job_uuid=None, age=0, **kwargs):
15     if job_uuid is True:
16         job_uuid = 'zzzzz-jjjjj-jobjobjobjobjob'
17     slurm_state = 'idle' if (job_uuid is None) else 'alloc'
18     node = {'uuid': 'zzzzz-yyyyy-{:015x}'.format(node_num),
19             'created_at': '2014-01-01T01:02:03Z',
20             'modified_at': time.strftime('%Y-%m-%dT%H:%M:%SZ',
21                                          time.gmtime(time.time() - age)),
22             'hostname': 'compute{}'.format(node_num),
23             'domain': 'zzzzz.arvadosapi.com',
24             'ip_address': ip_address_mock(node_num),
25             'job_uuid': job_uuid,
26             'info': {'slurm_state': slurm_state}}
27     node.update(kwargs)
28     return node
29
30 def cloud_node_mock(node_num=99):
31     node = mock.NonCallableMagicMock(
32         ['id', 'name', 'state', 'public_ips', 'private_ips', 'driver', 'size',
33          'image', 'extra'],
34         name='cloud_node')
35     node.id = str(node_num)
36     node.name = node.id
37     node.public_ips = []
38     node.private_ips = [ip_address_mock(node_num)]
39     return node
40
41 def ip_address_mock(last_octet):
42     return '10.20.30.{}'.format(last_octet)
43
44 class MockShutdownTimer(object):
45     def _set_state(self, is_open, next_opening):
46         self.window_open = lambda: is_open
47         self.next_opening = lambda: next_opening
48
49
50 class MockSize(object):
51     def __init__(self, factor):
52         self.id = 'z{}.test'.format(factor)
53         self.name = self.id
54         self.ram = 128 * factor
55         self.disk = 100 * factor
56         self.bandwidth = 16 * factor
57         self.price = float(factor)
58         self.extra = {}
59
60     def __eq__(self, other):
61         return self.id == other.id
62
63
64 class MockTimer(object):
65     def schedule(self, want_time, callback, *args, **kwargs):
66         return callback(*args, **kwargs)
67
68
69 class ActorTestMixin(object):
70     FUTURE_CLASS = pykka.ThreadingFuture
71     TIMEOUT = pykka_timeout
72
73     def tearDown(self):
74         pykka.ActorRegistry.stop_all()
75
76     def stop_proxy(self, proxy):
77         return proxy.actor_ref.stop(timeout=self.TIMEOUT)
78
79     def wait_for_assignment(self, proxy, attr_name, unassigned=None,
80                             timeout=TIMEOUT):
81         deadline = time.time() + timeout
82         while True:
83             loop_timeout = deadline - time.time()
84             if loop_timeout <= 0:
85                 self.fail("actor did not assign {} in time".format(attr_name))
86             result = getattr(proxy, attr_name).get(loop_timeout)
87             if result is not unassigned:
88                 return result
89
90
91 class RemotePollLoopActorTestMixin(ActorTestMixin):
92     def build_monitor(self, *args, **kwargs):
93         self.timer = mock.MagicMock(name='timer_mock')
94         self.client = mock.MagicMock(name='client_mock')
95         self.subscriber = mock.Mock(name='subscriber_mock')
96         self.monitor = self.TEST_CLASS.start(
97             self.client, self.timer, *args, **kwargs).proxy()