3 from __future__ import absolute_import, print_function
10 from . import pykka_timeout
12 no_sleep = mock.patch('time.sleep', lambda n: None)
14 def arvados_node_mock(node_num=99, job_uuid=None, age=0, **kwargs):
16 job_uuid = 'zzzzz-jjjjj-jobjobjobjobjob'
17 slurm_state = 'idle' if (job_uuid is None) else 'alloc'
18 node = {'uuid': 'zzzzz-yyyyy-{:015x}'.format(node_num),
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),
26 'info': {'slurm_state': slurm_state}}
30 def cloud_node_mock(node_num=99):
31 node = mock.NonCallableMagicMock(
32 ['id', 'name', 'state', 'public_ips', 'private_ips', 'driver', 'size',
35 node.id = str(node_num)
38 node.private_ips = [ip_address_mock(node_num)]
41 def ip_address_mock(last_octet):
42 return '10.20.30.{}'.format(last_octet)
44 class MockSize(object):
45 def __init__(self, factor):
46 self.id = 'z{}.test'.format(factor)
48 self.ram = 128 * factor
49 self.disk = 100 * factor
50 self.bandwidth = 16 * factor
51 self.price = float(factor)
54 def __eq__(self, other):
55 return self.id == other.id
58 class MockTimer(object):
59 def schedule(self, want_time, callback, *args, **kwargs):
60 return callback(*args, **kwargs)
63 class ActorTestMixin(object):
64 FUTURE_CLASS = pykka.ThreadingFuture
65 TIMEOUT = pykka_timeout
68 pykka.ActorRegistry.stop_all()
70 def stop_proxy(self, proxy):
71 return proxy.actor_ref.stop(timeout=self.TIMEOUT)
73 def wait_for_assignment(self, proxy, attr_name, unassigned=None,
75 deadline = time.time() + timeout
77 loop_timeout = deadline - time.time()
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:
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()