Merge branch 'master' of git.curoverse.com:arvados into 3408-production-datamanager
[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-12345abcde67890',
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 MockSize(object):
45     def __init__(self, factor):
46         self.id = 'z{}.test'.format(factor)
47         self.name = self.id
48         self.ram = 128 * factor
49         self.disk = 100 * factor
50         self.bandwidth = 16 * factor
51         self.price = float(factor)
52         self.extra = {}
53
54     def __eq__(self, other):
55         return self.id == other.id
56
57
58 class MockTimer(object):
59     def schedule(self, want_time, callback, *args, **kwargs):
60         return callback(*args, **kwargs)
61
62
63 class ActorTestMixin(object):
64     FUTURE_CLASS = pykka.ThreadingFuture
65     TIMEOUT = pykka_timeout
66
67     def tearDown(self):
68         pykka.ActorRegistry.stop_all()
69
70     def stop_proxy(self, proxy):
71         return proxy.actor_ref.stop(timeout=self.TIMEOUT)
72
73     def wait_for_assignment(self, proxy, attr_name, unassigned=None,
74                             timeout=TIMEOUT):
75         deadline = time.time() + timeout
76         while True:
77             loop_timeout = deadline - time.time()
78             if loop_timeout <= 0:
79                 self.fail("actor did not assign {} in time".format(attr_name))
80             result = getattr(proxy, attr_name).get(loop_timeout)
81             if result is not unassigned:
82                 return result
83
84
85 class RemotePollLoopActorTestMixin(ActorTestMixin):
86     def build_monitor(self, *args, **kwargs):
87         self.timer = mock.MagicMock(name='timer_mock')
88         self.client = mock.MagicMock(name='client_mock')
89         self.subscriber = mock.Mock(name='subscriber_mock')
90         self.monitor = self.TEST_CLASS.start(
91             self.client, self.timer, *args, **kwargs).proxy()