3 from __future__ import absolute_import, print_function
12 from . import pykka_timeout
14 no_sleep = mock.patch('time.sleep', lambda n: None)
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')
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),
32 'crunch_worker_state': crunch_worker_state,
33 'info': {'ping_secret': 'defaulttestsecret'}}
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'],
42 cloud_object.id = str(name_id)
43 cloud_object.name = cloud_object.id.upper()
46 def cloud_node_mock(node_num=99, **extra):
47 node = mock.NonCallableMagicMock(
48 ['id', 'name', 'state', 'public_ips', 'private_ips', 'driver', 'size',
51 node.id = str(node_num)
54 node.private_ips = [ip_address_mock(node_num)]
58 def ip_address_mock(last_octet):
59 return '10.20.30.{}'.format(last_octet)
61 class MockShutdownTimer(object):
62 def _set_state(self, is_open, next_opening):
63 self.window_open = lambda: is_open
64 self.next_opening = lambda: next_opening
67 class MockSize(object):
68 def __init__(self, factor):
69 self.id = 'z{}.test'.format(factor)
71 self.ram = 128 * factor
72 self.disk = 100 * factor
73 self.bandwidth = 16 * factor
74 self.price = float(factor)
77 def __eq__(self, other):
78 return self.id == other.id
81 class MockTimer(object):
82 def __init__(self, deliver_immediately=True):
83 self.deliver_immediately = deliver_immediately
85 self.lock = threading.Lock()
89 to_deliver = self.messages
91 for callback, args, kwargs in to_deliver:
92 callback(*args, **kwargs)
94 def schedule(self, want_time, callback, *args, **kwargs):
96 self.messages.append((callback, args, kwargs))
97 if self.deliver_immediately:
101 class ActorTestMixin(object):
102 FUTURE_CLASS = pykka.ThreadingFuture
103 TIMEOUT = pykka_timeout
106 pykka.ActorRegistry.stop_all()
108 def stop_proxy(self, proxy):
109 return proxy.actor_ref.stop(timeout=self.TIMEOUT)
111 def wait_for_assignment(self, proxy, attr_name, unassigned=None,
113 deadline = time.time() + timeout
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:
123 class DriverTestMixin(object):
125 self.driver_mock = mock.MagicMock(name='driver_mock')
126 super(DriverTestMixin, self).setUp()
128 def new_driver(self, auth_kwargs={}, list_kwargs={}, create_kwargs={}):
129 create_kwargs.setdefault('ping_host', '100::')
130 return self.TEST_CLASS(
131 auth_kwargs, list_kwargs, create_kwargs,
132 driver_class=self.driver_mock)
134 def driver_method_args(self, method_name):
135 return getattr(self.driver_mock(), method_name).call_args
138 class RemotePollLoopActorTestMixin(ActorTestMixin):
139 def build_monitor(self, *args, **kwargs):
140 self.timer = mock.MagicMock(name='timer_mock')
141 self.client = mock.MagicMock(name='client_mock')
142 self.subscriber = mock.Mock(name='subscriber_mock')
143 self.monitor = self.TEST_CLASS.start(
144 self.client, self.timer, *args, **kwargs).proxy()