10587: Added tests to Python commands to check for the --version argument.
[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 libcloud.common.types as cloud_types
10 import mock
11 import pykka
12
13 from . import pykka_timeout
14
15 no_sleep = mock.patch('time.sleep', lambda n: None)
16
17 def arvados_node_mock(node_num=99, job_uuid=None, age=-1, **kwargs):
18     mod_time = datetime.datetime.utcnow() - datetime.timedelta(seconds=age)
19     mod_time_s = mod_time.strftime('%Y-%m-%dT%H:%M:%S.%fZ')
20     if job_uuid is True:
21         job_uuid = 'zzzzz-jjjjj-jobjobjobjobjob'
22     crunch_worker_state = 'idle' if (job_uuid is None) else 'busy'
23     node = {'uuid': 'zzzzz-yyyyy-{:015x}'.format(node_num),
24             'created_at': '2014-01-01T01:02:03.04050607Z',
25             'modified_at': mod_time_s,
26             'first_ping_at': kwargs.pop('first_ping_at', mod_time_s),
27             'last_ping_at': mod_time_s,
28             'slot_number': node_num,
29             'hostname': 'compute{}'.format(node_num),
30             'domain': 'zzzzz.arvadosapi.com',
31             'ip_address': ip_address_mock(node_num),
32             'job_uuid': job_uuid,
33             'crunch_worker_state': crunch_worker_state,
34             'properties': {},
35             'info': {'ping_secret': 'defaulttestsecret', 'ec2_instance_id': str(node_num)}}
36     node.update(kwargs)
37     return node
38
39 def cloud_object_mock(name_id, **extra):
40     # A very generic mock, useful for stubbing libcloud objects we
41     # only search for and pass around, like locations, subnets, etc.
42     cloud_object = mock.NonCallableMagicMock(['id', 'name'],
43                                              name='cloud_object')
44     cloud_object.name = str(name_id)
45     cloud_object.id = 'id_' + cloud_object.name
46     cloud_object.extra = extra
47     return cloud_object
48
49
50 def cloud_node_fqdn(node):
51     # We intentionally put the FQDN somewhere goofy to make sure tested code is
52     # using this function for lookups.
53     return node.extra.get('testname', 'NoTestName')
54
55 def ip_address_mock(last_octet):
56     return '10.20.30.{}'.format(last_octet)
57
58 class MockShutdownTimer(object):
59     def _set_state(self, is_open, next_opening):
60         self.window_open = lambda: is_open
61         self.next_opening = lambda: next_opening
62
63
64 class MockSize(object):
65     def __init__(self, factor):
66         self.id = 'z{}.test'.format(factor)
67         self.name = self.id
68         self.ram = 128 * factor
69         self.disk = 100 * factor
70         self.bandwidth = 16 * factor
71         self.price = float(factor)
72         self.extra = {}
73
74     def __eq__(self, other):
75         return self.id == other.id
76
77
78 class MockTimer(object):
79     def __init__(self, deliver_immediately=True):
80         self.deliver_immediately = deliver_immediately
81         self.messages = []
82         self.lock = threading.Lock()
83
84     def deliver(self):
85         with self.lock:
86             to_deliver = self.messages
87             self.messages = []
88         for callback, args, kwargs in to_deliver:
89             try:
90                 callback(*args, **kwargs)
91             except pykka.ActorDeadError:
92                 pass
93
94     def schedule(self, want_time, callback, *args, **kwargs):
95         with self.lock:
96             self.messages.append((callback, args, kwargs))
97         if self.deliver_immediately:
98             self.deliver()
99
100
101 class ActorTestMixin(object):
102     FUTURE_CLASS = pykka.ThreadingFuture
103     TIMEOUT = pykka_timeout
104
105     def tearDown(self):
106         pykka.ActorRegistry.stop_all()
107
108     def stop_proxy(self, proxy):
109         return proxy.actor_ref.stop(timeout=self.TIMEOUT)
110
111     def wait_for_assignment(self, proxy, attr_name, unassigned=None,
112                             timeout=TIMEOUT):
113         deadline = time.time() + timeout
114         while True:
115             loop_timeout = deadline - time.time()
116             if loop_timeout <= 0:
117                 self.fail("actor did not assign {} in time".format(attr_name))
118             result = getattr(proxy, attr_name).get(loop_timeout)
119             if result is not unassigned:
120                 return result
121
122     def busywait(self, f):
123         n = 0
124         while not f() and n < 10:
125             time.sleep(.1)
126             n += 1
127         self.assertTrue(f())
128
129
130 class DriverTestMixin(object):
131     def setUp(self):
132         self.driver_mock = mock.MagicMock(name='driver_mock')
133         super(DriverTestMixin, self).setUp()
134
135     def new_driver(self, auth_kwargs={}, list_kwargs={}, create_kwargs={}):
136         create_kwargs.setdefault('ping_host', '100::')
137         return self.TEST_CLASS(
138             auth_kwargs, list_kwargs, create_kwargs,
139             driver_class=self.driver_mock)
140
141     def driver_method_args(self, method_name):
142         return getattr(self.driver_mock(), method_name).call_args
143
144     def test_driver_create_retry(self):
145         with mock.patch('time.sleep'):
146             driver_mock2 = mock.MagicMock(name='driver_mock2')
147             self.driver_mock.side_effect = (Exception("oops"), driver_mock2)
148             kwargs = {'user_id': 'foo'}
149             driver = self.new_driver(auth_kwargs=kwargs)
150             self.assertTrue(self.driver_mock.called)
151             self.assertIs(driver.real, driver_mock2)
152
153     def test_create_can_find_node_after_timeout(self, create_kwargs={}, node_extra={}):
154         driver = self.new_driver(create_kwargs=create_kwargs)
155         arv_node = arvados_node_mock()
156         cloud_node = cloud_node_mock(**node_extra)
157         cloud_node.name = driver.create_cloud_name(arv_node)
158         create_method = self.driver_mock().create_node
159         create_method.side_effect = cloud_types.LibcloudError("fake timeout")
160         list_method = self.driver_mock().list_nodes
161         list_method.return_value = [cloud_node]
162         actual = driver.create_node(MockSize(1), arv_node)
163         self.assertIs(cloud_node, actual)
164
165     def test_create_can_raise_exception_after_timeout(self):
166         driver = self.new_driver()
167         arv_node = arvados_node_mock()
168         create_method = self.driver_mock().create_node
169         create_method.side_effect = cloud_types.LibcloudError("fake timeout")
170         list_method = self.driver_mock().list_nodes
171         list_method.return_value = []
172         with self.assertRaises(cloud_types.LibcloudError) as exc_test:
173             driver.create_node(MockSize(1), arv_node)
174         self.assertIs(create_method.side_effect, exc_test.exception)
175
176     def check_node_found_after_timeout_has_fixed_size(self, size, cloud_node,
177                                                       create_kwargs={}):
178         # This method needs to be called explicitly by driver test suites
179         # that need it.
180         self.driver_mock().list_sizes.return_value = [size]
181         driver = self.new_driver(create_kwargs=create_kwargs)
182         arv_node = arvados_node_mock()
183         cloud_node.name = driver.create_cloud_name(arv_node)
184         create_method = self.driver_mock().create_node
185         create_method.side_effect = cloud_types.LibcloudError("fake timeout")
186         self.driver_mock().list_nodes.return_value = [cloud_node]
187         actual = driver.create_node(size, arv_node)
188         self.assertIs(size, actual.size)
189
190
191 class RemotePollLoopActorTestMixin(ActorTestMixin):
192     def build_monitor(self, *args, **kwargs):
193         self.timer = mock.MagicMock(name='timer_mock')
194         self.client = mock.MagicMock(name='client_mock')
195         self.subscriber = mock.Mock(name='subscriber_mock')
196         self.monitor = self.TEST_CLASS.start(
197             self.client, self.timer, *args, **kwargs).proxy()
198
199 def cloud_node_mock(node_num=99, size=MockSize(1), **extra):
200     node = mock.NonCallableMagicMock(
201         ['id', 'name', 'state', 'public_ips', 'private_ips', 'driver', 'size',
202          'image', 'extra'],
203         name='cloud_node')
204     node.id = str(node_num)
205     node.name = node.id
206     node.size = size
207     node.public_ips = []
208     node.private_ips = [ip_address_mock(node_num)]
209     node.extra = extra
210     return node