3 from __future__ import absolute_import, print_function
9 import libcloud.common.types as cloud_types
13 from . import pykka_timeout
15 no_sleep = mock.patch('time.sleep', lambda n: None)
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')
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),
33 'crunch_worker_state': crunch_worker_state,
35 'info': {'ping_secret': 'defaulttestsecret', 'ec2_instance_id': str(node_num)}}
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'],
44 cloud_object.name = str(name_id)
45 cloud_object.id = 'id_' + cloud_object.name
46 cloud_object.extra = extra
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')
55 def ip_address_mock(last_octet):
56 return '10.20.30.{}'.format(last_octet)
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
64 class MockSize(object):
65 def __init__(self, factor):
66 self.id = 'z{}.test'.format(factor)
68 self.ram = 128 * factor
69 self.disk = 100 * factor
70 self.bandwidth = 16 * factor
71 self.price = float(factor)
74 def __eq__(self, other):
75 return self.id == other.id
78 class MockTimer(object):
79 def __init__(self, deliver_immediately=True):
80 self.deliver_immediately = deliver_immediately
82 self.lock = threading.Lock()
86 to_deliver = self.messages
88 for callback, args, kwargs in to_deliver:
90 callback(*args, **kwargs)
91 except pykka.ActorDeadError:
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:
122 def busywait(self, f):
124 while not f() and n < 10:
130 class DriverTestMixin(object):
132 self.driver_mock = mock.MagicMock(name='driver_mock')
133 super(DriverTestMixin, self).setUp()
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)
141 def driver_method_args(self, method_name):
142 return getattr(self.driver_mock(), method_name).call_args
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)
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)
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)
176 def check_node_found_after_timeout_has_fixed_size(self, size, cloud_node,
178 # This method needs to be called explicitly by driver test suites
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)
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()
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',
204 node.id = str(node_num)
208 node.private_ips = [ip_address_mock(node_num)]