5353: Parameterize the following methods on node size: _nodes_up, _nodes_busy,
[arvados.git] / services / nodemanager / tests / testutil.py
1 #!/usr/bin/env python
2
3 from __future__ import absolute_import, print_function
4
5 import datetime
6 import threading
7 import time
8
9 import mock
10 import pykka
11
12 from . import pykka_timeout
13
14 no_sleep = mock.patch('time.sleep', lambda n: None)
15
16 def arvados_node_mock(node_num=99, job_uuid=None, age=-1, **kwargs):
17     mod_time = datetime.datetime.utcnow() - datetime.timedelta(seconds=age)
18     mod_time_s = mod_time.strftime('%Y-%m-%dT%H:%M:%S.%fZ')
19     if job_uuid is True:
20         job_uuid = 'zzzzz-jjjjj-jobjobjobjobjob'
21     crunch_worker_state = 'idle' if (job_uuid is None) else 'busy'
22     node = {'uuid': 'zzzzz-yyyyy-{:015x}'.format(node_num),
23             'created_at': '2014-01-01T01:02:03.04050607Z',
24             'modified_at': mod_time_s,
25             'first_ping_at': kwargs.pop('first_ping_at', mod_time_s),
26             'last_ping_at': mod_time_s,
27             'slot_number': node_num,
28             'hostname': 'compute{}'.format(node_num),
29             'domain': 'zzzzz.arvadosapi.com',
30             'ip_address': ip_address_mock(node_num),
31             'job_uuid': job_uuid,
32             'crunch_worker_state': crunch_worker_state,
33             'info': {'ping_secret': 'defaulttestsecret'}}
34     node.update(kwargs)
35     return node
36
37 def cloud_object_mock(name_id, **extra):
38     # A very generic mock, useful for stubbing libcloud objects we
39     # only search for and pass around, like locations, subnets, etc.
40     cloud_object = mock.NonCallableMagicMock(['id', 'name'],
41                                              name='cloud_object')
42     cloud_object.name = str(name_id)
43     cloud_object.id = 'id_' + cloud_object.name
44     cloud_object.extra = extra
45     return cloud_object
46
47
48 def cloud_node_fqdn(node):
49     # We intentionally put the FQDN somewhere goofy to make sure tested code is
50     # using this function for lookups.
51     return node.extra.get('testname', 'NoTestName')
52
53 def ip_address_mock(last_octet):
54     return '10.20.30.{}'.format(last_octet)
55
56 class MockShutdownTimer(object):
57     def _set_state(self, is_open, next_opening):
58         self.window_open = lambda: is_open
59         self.next_opening = lambda: next_opening
60
61
62 class MockSize(object):
63     def __init__(self, factor):
64         self.id = 'z{}.test'.format(factor)
65         self.name = self.id
66         self.ram = 128 * factor
67         self.disk = 100 * factor
68         self.bandwidth = 16 * factor
69         self.price = float(factor)
70         self.extra = {}
71
72     def __eq__(self, other):
73         return self.id == other.id
74
75
76 class MockTimer(object):
77     def __init__(self, deliver_immediately=True):
78         self.deliver_immediately = deliver_immediately
79         self.messages = []
80         self.lock = threading.Lock()
81
82     def deliver(self):
83         with self.lock:
84             to_deliver = self.messages
85             self.messages = []
86         for callback, args, kwargs in to_deliver:
87             callback(*args, **kwargs)
88
89     def schedule(self, want_time, callback, *args, **kwargs):
90         with self.lock:
91             self.messages.append((callback, args, kwargs))
92         if self.deliver_immediately:
93             self.deliver()
94
95
96 class ActorTestMixin(object):
97     FUTURE_CLASS = pykka.ThreadingFuture
98     TIMEOUT = pykka_timeout
99
100     def tearDown(self):
101         pykka.ActorRegistry.stop_all()
102
103     def stop_proxy(self, proxy):
104         return proxy.actor_ref.stop(timeout=self.TIMEOUT)
105
106     def wait_for_assignment(self, proxy, attr_name, unassigned=None,
107                             timeout=TIMEOUT):
108         deadline = time.time() + timeout
109         while True:
110             loop_timeout = deadline - time.time()
111             if loop_timeout <= 0:
112                 self.fail("actor did not assign {} in time".format(attr_name))
113             result = getattr(proxy, attr_name).get(loop_timeout)
114             if result is not unassigned:
115                 return result
116
117
118 class DriverTestMixin(object):
119     def setUp(self):
120         self.driver_mock = mock.MagicMock(name='driver_mock')
121         super(DriverTestMixin, self).setUp()
122
123     def new_driver(self, auth_kwargs={}, list_kwargs={}, create_kwargs={}):
124         create_kwargs.setdefault('ping_host', '100::')
125         return self.TEST_CLASS(
126             auth_kwargs, list_kwargs, create_kwargs,
127             driver_class=self.driver_mock)
128
129     def driver_method_args(self, method_name):
130         return getattr(self.driver_mock(), method_name).call_args
131
132
133 class RemotePollLoopActorTestMixin(ActorTestMixin):
134     def build_monitor(self, *args, **kwargs):
135         self.timer = mock.MagicMock(name='timer_mock')
136         self.client = mock.MagicMock(name='client_mock')
137         self.subscriber = mock.Mock(name='subscriber_mock')
138         self.monitor = self.TEST_CLASS.start(
139             self.client, self.timer, *args, **kwargs).proxy()
140
141 def cloud_node_mock(node_num=99, size=MockSize(1), **extra):
142     node = mock.NonCallableMagicMock(
143         ['id', 'name', 'state', 'public_ips', 'private_ips', 'driver', 'size',
144          'image', 'extra'],
145         name='cloud_node')
146     node.id = str(node_num)
147     node.name = node.id
148     node.size = size
149     node.public_ips = []
150     node.private_ips = [ip_address_mock(node_num)]
151     node.extra = extra
152     return node