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