Add 'apps/arv-web/' from commit 'f9732ad8460d013c2f28363655d0d1b91894dca5'
[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     slurm_state = 'idle' if (job_uuid is None) else 'alloc'
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             'hostname': 'compute{}'.format(node_num),
24             'domain': 'zzzzz.arvadosapi.com',
25             'ip_address': ip_address_mock(node_num),
26             'job_uuid': job_uuid,
27             'info': {'slurm_state': slurm_state}}
28     node.update(kwargs)
29     return node
30
31 def cloud_node_mock(node_num=99):
32     node = mock.NonCallableMagicMock(
33         ['id', 'name', 'state', 'public_ips', 'private_ips', 'driver', 'size',
34          'image', 'extra'],
35         name='cloud_node')
36     node.id = str(node_num)
37     node.name = node.id
38     node.public_ips = []
39     node.private_ips = [ip_address_mock(node_num)]
40     return node
41
42 def ip_address_mock(last_octet):
43     return '10.20.30.{}'.format(last_octet)
44
45 class MockShutdownTimer(object):
46     def _set_state(self, is_open, next_opening):
47         self.window_open = lambda: is_open
48         self.next_opening = lambda: next_opening
49
50
51 class MockSize(object):
52     def __init__(self, factor):
53         self.id = 'z{}.test'.format(factor)
54         self.name = self.id
55         self.ram = 128 * factor
56         self.disk = 100 * factor
57         self.bandwidth = 16 * factor
58         self.price = float(factor)
59         self.extra = {}
60
61     def __eq__(self, other):
62         return self.id == other.id
63
64
65 class MockTimer(object):
66     def __init__(self, deliver_immediately=True):
67         self.deliver_immediately = deliver_immediately
68         self.messages = []
69         self.lock = threading.Lock()
70
71     def deliver(self):
72         with self.lock:
73             to_deliver = self.messages
74             self.messages = []
75         for callback, args, kwargs in to_deliver:
76             callback(*args, **kwargs)
77
78     def schedule(self, want_time, callback, *args, **kwargs):
79         with self.lock:
80             self.messages.append((callback, args, kwargs))
81         if self.deliver_immediately:
82             self.deliver()
83
84
85 class ActorTestMixin(object):
86     FUTURE_CLASS = pykka.ThreadingFuture
87     TIMEOUT = pykka_timeout
88
89     def tearDown(self):
90         pykka.ActorRegistry.stop_all()
91
92     def stop_proxy(self, proxy):
93         return proxy.actor_ref.stop(timeout=self.TIMEOUT)
94
95     def wait_for_assignment(self, proxy, attr_name, unassigned=None,
96                             timeout=TIMEOUT):
97         deadline = time.time() + timeout
98         while True:
99             loop_timeout = deadline - time.time()
100             if loop_timeout <= 0:
101                 self.fail("actor did not assign {} in time".format(attr_name))
102             result = getattr(proxy, attr_name).get(loop_timeout)
103             if result is not unassigned:
104                 return result
105
106
107 class RemotePollLoopActorTestMixin(ActorTestMixin):
108     def build_monitor(self, *args, **kwargs):
109         self.timer = mock.MagicMock(name='timer_mock')
110         self.client = mock.MagicMock(name='client_mock')
111         self.subscriber = mock.Mock(name='subscriber_mock')
112         self.monitor = self.TEST_CLASS.start(
113             self.client, self.timer, *args, **kwargs).proxy()