Merge branch '5313-node-manager-node-naming-tag-wip'
[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):
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.id = str(name_id)
43     cloud_object.name = cloud_object.id.upper()
44     return cloud_object
45
46 def cloud_node_mock(node_num=99, **extra):
47     node = mock.NonCallableMagicMock(
48         ['id', 'name', 'state', 'public_ips', 'private_ips', 'driver', 'size',
49          'image', 'extra'],
50         name='cloud_node')
51     node.id = str(node_num)
52     node.name = node.id
53     node.public_ips = []
54     node.private_ips = [ip_address_mock(node_num)]
55     node.extra = extra
56     return node
57
58 def cloud_node_fqdn(node):
59     # We intentionally put the FQDN somewhere goofy to make sure tested code is
60     # using this function for lookups.
61     return node.extra.get('testname', 'NoTestName')
62
63 def ip_address_mock(last_octet):
64     return '10.20.30.{}'.format(last_octet)
65
66 class MockShutdownTimer(object):
67     def _set_state(self, is_open, next_opening):
68         self.window_open = lambda: is_open
69         self.next_opening = lambda: next_opening
70
71
72 class MockSize(object):
73     def __init__(self, factor):
74         self.id = 'z{}.test'.format(factor)
75         self.name = self.id
76         self.ram = 128 * factor
77         self.disk = 100 * factor
78         self.bandwidth = 16 * factor
79         self.price = float(factor)
80         self.extra = {}
81
82     def __eq__(self, other):
83         return self.id == other.id
84
85
86 class MockTimer(object):
87     def __init__(self, deliver_immediately=True):
88         self.deliver_immediately = deliver_immediately
89         self.messages = []
90         self.lock = threading.Lock()
91
92     def deliver(self):
93         with self.lock:
94             to_deliver = self.messages
95             self.messages = []
96         for callback, args, kwargs in to_deliver:
97             callback(*args, **kwargs)
98
99     def schedule(self, want_time, callback, *args, **kwargs):
100         with self.lock:
101             self.messages.append((callback, args, kwargs))
102         if self.deliver_immediately:
103             self.deliver()
104
105
106 class ActorTestMixin(object):
107     FUTURE_CLASS = pykka.ThreadingFuture
108     TIMEOUT = pykka_timeout
109
110     def tearDown(self):
111         pykka.ActorRegistry.stop_all()
112
113     def stop_proxy(self, proxy):
114         return proxy.actor_ref.stop(timeout=self.TIMEOUT)
115
116     def wait_for_assignment(self, proxy, attr_name, unassigned=None,
117                             timeout=TIMEOUT):
118         deadline = time.time() + timeout
119         while True:
120             loop_timeout = deadline - time.time()
121             if loop_timeout <= 0:
122                 self.fail("actor did not assign {} in time".format(attr_name))
123             result = getattr(proxy, attr_name).get(loop_timeout)
124             if result is not unassigned:
125                 return result
126
127
128 class DriverTestMixin(object):
129     def setUp(self):
130         self.driver_mock = mock.MagicMock(name='driver_mock')
131         super(DriverTestMixin, self).setUp()
132
133     def new_driver(self, auth_kwargs={}, list_kwargs={}, create_kwargs={}):
134         create_kwargs.setdefault('ping_host', '100::')
135         return self.TEST_CLASS(
136             auth_kwargs, list_kwargs, create_kwargs,
137             driver_class=self.driver_mock)
138
139     def driver_method_args(self, method_name):
140         return getattr(self.driver_mock(), method_name).call_args
141
142
143 class RemotePollLoopActorTestMixin(ActorTestMixin):
144     def build_monitor(self, *args, **kwargs):
145         self.timer = mock.MagicMock(name='timer_mock')
146         self.client = mock.MagicMock(name='client_mock')
147         self.subscriber = mock.Mock(name='subscriber_mock')
148         self.monitor = self.TEST_CLASS.start(
149             self.client, self.timer, *args, **kwargs).proxy()