3 from __future__ import absolute_import, print_function
11 from . import pykka_timeout
13 no_sleep = mock.patch('time.sleep', lambda n: None)
15 def arvados_node_mock(node_num=99, job_uuid=None, age=0, **kwargs):
17 job_uuid = 'zzzzz-jjjjj-jobjobjobjobjob'
18 slurm_state = 'idle' if (job_uuid is None) else 'alloc'
19 node = {'uuid': 'zzzzz-yyyyy-{:015x}'.format(node_num),
20 'created_at': '2014-01-01T01:02:03Z',
21 'modified_at': time.strftime('%Y-%m-%dT%H:%M:%SZ',
22 time.gmtime(time.time() - age)),
23 'hostname': 'compute{}'.format(node_num),
24 'domain': 'zzzzz.arvadosapi.com',
25 'ip_address': ip_address_mock(node_num),
27 'info': {'slurm_state': slurm_state}}
31 def cloud_node_mock(node_num=99):
32 node = mock.NonCallableMagicMock(
33 ['id', 'name', 'state', 'public_ips', 'private_ips', 'driver', 'size',
36 node.id = str(node_num)
39 node.private_ips = [ip_address_mock(node_num)]
42 def ip_address_mock(last_octet):
43 return '10.20.30.{}'.format(last_octet)
45 class MockShutdownTimer(object):
46 def _set_state(self, is_open, next_opening):
47 self.window_open = lambda: is_open
48 self.next_opening = lambda: next_opening
51 class MockSize(object):
52 def __init__(self, factor):
53 self.id = 'z{}.test'.format(factor)
55 self.ram = 128 * factor
56 self.disk = 100 * factor
57 self.bandwidth = 16 * factor
58 self.price = float(factor)
61 def __eq__(self, other):
62 return self.id == other.id
65 class MockTimer(object):
66 def __init__(self, deliver_immediately=True):
67 self.deliver_immediately = deliver_immediately
69 self.lock = threading.Lock()
73 to_deliver = self.messages
75 for callback, args, kwargs in to_deliver:
76 callback(*args, **kwargs)
78 def schedule(self, want_time, callback, *args, **kwargs):
80 self.messages.append((callback, args, kwargs))
81 if self.deliver_immediately:
85 class ActorTestMixin(object):
86 FUTURE_CLASS = pykka.ThreadingFuture
87 TIMEOUT = pykka_timeout
90 pykka.ActorRegistry.stop_all()
92 def stop_proxy(self, proxy):
93 return proxy.actor_ref.stop(timeout=self.TIMEOUT)
95 def wait_for_assignment(self, proxy, attr_name, unassigned=None,
97 deadline = time.time() + timeout
99 loop_timeout = deadline - time.time()
100 if loop_timeout <= 0:
101 self.fail("actor did not assign {} in time".format(attr_name))
102 result = getattr(proxy, attr_name).get(loop_timeout)
103 if result is not unassigned:
107 class RemotePollLoopActorTestMixin(ActorTestMixin):
108 def build_monitor(self, *args, **kwargs):
109 self.timer = mock.MagicMock(name='timer_mock')
110 self.client = mock.MagicMock(name='client_mock')
111 self.subscriber = mock.Mock(name='subscriber_mock')
112 self.monitor = self.TEST_CLASS.start(
113 self.client, self.timer, *args, **kwargs).proxy()