1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
5 from __future__ import absolute_import
6 from builtins import str
7 from builtins import range
21 import urllib.parse as urlparse
24 from . import run_test_server
26 from apiclient import errors as apiclient_errors
27 from apiclient import http as apiclient_http
28 from arvados.api import (
31 api_kwargs_from_config,
33 _googleapiclient_log_lock,
35 from .arvados_testutil import fake_httplib2_response, mock_api_responses, queue_with
38 if not mimetypes.inited:
41 class ArvadosApiTest(run_test_server.TestCaseWithServers):
43 ERROR_HEADERS = {'Content-Type': mimetypes.types_map['.json']}
44 RETRIED_4XX = frozenset([408, 409, 423])
46 def api_error_response(self, code, *errors):
47 return (fake_httplib2_response(code, **self.ERROR_HEADERS),
48 json.dumps({'errors': errors,
49 'error_token': '1234567890+12345678'}).encode())
51 def _config_from_environ(self):
54 for key, value in os.environ.items()
55 if key.startswith('ARVADOS_API_')
58 def _discoveryServiceUrl(
61 path='/discovery/v1/apis/{api}/{apiVersion}/rest',
65 host = os.environ['ARVADOS_API_HOST']
66 return urlparse.urlunsplit((scheme, host, path, None, None))
68 def test_new_api_objects_with_cache(self):
69 clients = [arvados.api('v1', cache=True) for index in [0, 1]]
70 self.assertIsNot(*clients)
72 def test_empty_list(self):
73 answer = arvados.api('v1').humans().list(
74 filters=[['uuid', '=', None]]).execute()
75 self.assertEqual(answer['items_available'], len(answer['items']))
77 def test_nonempty_list(self):
78 answer = arvados.api('v1').collections().list().execute()
79 self.assertNotEqual(0, answer['items_available'])
80 self.assertNotEqual(0, len(answer['items']))
82 def test_timestamp_inequality_filter(self):
83 api = arvados.api('v1')
84 new_item = api.specimens().create(body={}).execute()
85 for operator, should_include in [
86 ['<', False], ['>', False],
87 ['<=', True], ['>=', True], ['=', True]]:
88 response = api.specimens().list(filters=[
89 ['created_at', operator, new_item['created_at']],
90 # Also filter by uuid to ensure (if it matches) it's on page 0
91 ['uuid', '=', new_item['uuid']]]).execute()
92 uuids = [item['uuid'] for item in response['items']]
93 did_include = new_item['uuid'] in uuids
95 did_include, should_include,
96 "'%s %s' filter should%s have matched '%s'" % (
97 operator, new_item['created_at'],
98 ('' if should_include else ' not'),
99 new_item['created_at']))
101 def test_exceptions_include_errors(self):
103 'arvados.humans.get': self.api_error_response(
104 422, "Bad UUID format", "Bad output format"),
106 req_builder = apiclient_http.RequestMockBuilder(mock_responses)
107 api = arvados.api('v1', requestBuilder=req_builder)
108 with self.assertRaises(apiclient_errors.HttpError) as err_ctx:
109 api.humans().get(uuid='xyz-xyz-abcdef').execute()
110 err_s = str(err_ctx.exception)
111 for msg in ["Bad UUID format", "Bad output format"]:
112 self.assertIn(msg, err_s)
114 @mock.patch('time.sleep')
115 def test_exceptions_include_request_id(self, sleep):
116 api = arvados.api('v1')
117 api.request_id='fake-request-id'
118 api._http.orig_http_request = mock.MagicMock()
119 api._http.orig_http_request.side_effect = socket.error('mock error')
122 api.users().current().execute()
123 except Exception as e:
125 self.assertRegex(str(caught), r'fake-request-id')
127 def test_exceptions_without_errors_have_basic_info(self):
129 'arvados.humans.delete': (
130 fake_httplib2_response(500, **self.ERROR_HEADERS),
133 req_builder = apiclient_http.RequestMockBuilder(mock_responses)
134 api = arvados.api('v1', requestBuilder=req_builder)
135 with self.assertRaises(apiclient_errors.HttpError) as err_ctx:
136 api.humans().delete(uuid='xyz-xyz-abcdef').execute()
137 self.assertIn("500", str(err_ctx.exception))
139 def test_request_too_large(self):
140 api = arvados.api('v1')
141 maxsize = api._rootDesc.get('maxRequestSize', 0)
142 with self.assertRaises(apiclient_errors.MediaUploadSizeError):
144 arvados.api('v1').collections().create(body={"manifest_text": text}).execute()
146 def test_default_request_timeout(self):
147 api = arvados.api('v1')
148 self.assertEqual(api._http.timeout, 300,
149 "Default timeout value should be 300")
151 def test_custom_request_timeout(self):
152 api = arvados.api('v1', timeout=1234)
153 self.assertEqual(api._http.timeout, 1234,
154 "Requested timeout value was 1234")
156 def test_4xx_retried(self):
157 client = arvados.api('v1')
158 for code in self.RETRIED_4XX:
159 name = f'retried #{code}'
160 with self.subTest(name), mock.patch('time.sleep'):
161 expected = {'username': name}
162 with mock_api_responses(
164 json.dumps(expected),
169 actual = client.users().current().execute()
170 self.assertEqual(actual, expected)
172 def test_4xx_not_retried(self):
173 client = arvados.api('v1', num_retries=3)
174 # Note that googleapiclient does retry 403 *if* the response JSON
175 # includes flags that say the request was denied by rate limiting.
176 # An empty JSON response like we use here should not be retried.
177 for code in [400, 401, 403, 404, 422]:
178 with self.subTest(f'error {code}'), mock.patch('time.sleep'):
179 with mock_api_responses(
185 ), self.assertRaises(arvados.errors.ApiError) as exc_check:
186 client.users().current().execute()
187 response = exc_check.exception.args[0]
188 self.assertEqual(response.status, code)
189 self.assertEqual(response.get('status'), str(code))
191 def test_4xx_raised_after_retry_exhaustion(self):
192 client = arvados.api('v1', num_retries=1)
193 for code in self.RETRIED_4XX:
194 with self.subTest(f'failed {code}'), mock.patch('time.sleep'):
195 with mock_api_responses(
198 [code, code, code, 200],
201 ), self.assertRaises(arvados.errors.ApiError) as exc_check:
202 client.users().current().execute()
203 response = exc_check.exception.args[0]
204 self.assertEqual(response.status, code)
205 self.assertEqual(response.get('status'), str(code))
207 def test_ordered_json_model(self):
209 'arvados.humans.get': (
211 json.dumps(collections.OrderedDict(
212 (c, int(c, 16)) for c in string.hexdigits
216 req_builder = apiclient_http.RequestMockBuilder(mock_responses)
217 api = arvados.api('v1',
218 requestBuilder=req_builder, model=OrderedJsonModel())
219 result = api.humans().get(uuid='test').execute()
220 self.assertEqual(string.hexdigits, ''.join(list(result.keys())))
222 def test_api_is_threadsafe(self):
224 'host': os.environ['ARVADOS_API_HOST'],
225 'token': os.environ['ARVADOS_API_TOKEN'],
228 config_kwargs = {'apiconfig': os.environ}
229 for api_constructor, kwargs in [
231 (arvados.api, api_kwargs),
232 (arvados.api_from_config, {}),
233 (arvados.api_from_config, config_kwargs),
235 sub_kwargs = "kwargs" if kwargs else "no kwargs"
236 with self.subTest(f"{api_constructor.__name__} with {sub_kwargs}"):
237 api_client = api_constructor('v1', **kwargs)
238 self.assertTrue(hasattr(api_client, 'localapi'),
239 f"client missing localapi method")
240 self.assertTrue(hasattr(api_client, 'keep'),
241 f"client missing keep attribute")
243 def test_api_host_constructor(self):
246 client = arvados.api(
249 os.environ['ARVADOS_API_HOST'],
250 os.environ['ARVADOS_API_TOKEN'],
253 self.assertEqual(client.api_token, os.environ['ARVADOS_API_TOKEN'],
254 "client constructed with incorrect token")
256 def test_api_url_constructor(self):
257 client = arvados.api(
259 discoveryServiceUrl=self._discoveryServiceUrl(),
260 token=os.environ['ARVADOS_API_TOKEN'],
263 self.assertEqual(client.api_token, os.environ['ARVADOS_API_TOKEN'],
264 "client constructed with incorrect token")
266 def test_api_bad_args(self):
268 'host': os.environ['ARVADOS_API_HOST'],
269 'token': os.environ['ARVADOS_API_TOKEN'],
270 'discoveryServiceUrl': self._discoveryServiceUrl(),
273 # Passing only a single key is missing required info
274 *([key] for key in all_kwargs.keys()),
275 # Passing all keys is a conflict
276 list(all_kwargs.keys()),
278 kwargs = {key: all_kwargs[key] for key in use_keys}
279 kwargs_list = ', '.join(use_keys)
280 with self.subTest(f"calling arvados.api with {kwargs_list} fails"), \
281 self.assertRaises(ValueError):
282 arvados.api('v1', insecure=True, **kwargs)
284 def test_api_bad_url(self):
286 {'discoveryServiceUrl': self._discoveryServiceUrl() + '/BadTestURL'},
287 {'version': 'BadTestVersion', 'host': os.environ['ARVADOS_API_HOST']},
289 bad_key = next(iter(bad_kwargs))
290 with self.subTest(f"api fails with bad {bad_key}"), \
291 self.assertRaises(apiclient_errors.UnknownApiNameOrVersion):
292 arvados.api(**bad_kwargs, token='test_api_bad_url', insecure=True)
294 def test_normalize_api_good_args(self):
295 for version, discoveryServiceUrl, host in [
296 ('Test1', None, os.environ['ARVADOS_API_HOST']),
297 (None, self._discoveryServiceUrl(), None)
299 argname = 'discoveryServiceUrl' if host is None else 'host'
300 with self.subTest(f"normalize_api_kwargs with {argname}"):
301 actual = normalize_api_kwargs(
305 os.environ['ARVADOS_API_TOKEN'],
308 self.assertEqual(actual['discoveryServiceUrl'], self._discoveryServiceUrl())
309 self.assertEqual(actual['token'], os.environ['ARVADOS_API_TOKEN'])
310 self.assertEqual(actual['version'], version or 'v1')
311 self.assertTrue(actual['insecure'])
312 self.assertNotIn('host', actual)
314 def test_normalize_api_bad_args(self):
316 self._discoveryServiceUrl(),
317 os.environ['ARVADOS_API_HOST'],
318 os.environ['ARVADOS_API_TOKEN'],
320 for arg_index, arg_value in enumerate(all_args):
321 args = [None] * len(all_args)
322 args[arg_index] = arg_value
323 with self.subTest(f"normalize_api_kwargs with only arg #{arg_index + 1}"), \
324 self.assertRaises(ValueError):
325 normalize_api_kwargs('v1', *args)
326 with self.subTest("normalize_api_kwargs with discoveryServiceUrl and host"), \
327 self.assertRaises(ValueError):
328 normalize_api_kwargs('v1', *all_args)
330 def test_api_from_config_default(self):
331 client = arvados.api_from_config('v1')
332 self.assertEqual(client.api_token, os.environ['ARVADOS_API_TOKEN'],
333 "client constructed with incorrect token")
335 def test_api_from_config_explicit(self):
336 config = self._config_from_environ()
337 client = arvados.api_from_config('v1', config)
338 self.assertEqual(client.api_token, os.environ['ARVADOS_API_TOKEN'],
339 "client constructed with incorrect token")
341 def test_api_from_bad_config(self):
342 base_config = self._config_from_environ()
343 for del_key in ['ARVADOS_API_HOST', 'ARVADOS_API_TOKEN']:
344 with self.subTest(f"api_from_config without {del_key} fails"), \
345 self.assertRaises(ValueError):
346 config = dict(base_config)
348 arvados.api_from_config('v1', config)
350 def test_api_kwargs_from_good_config(self):
351 for config in [None, self._config_from_environ()]:
352 conf_type = 'default' if config is None else 'passed'
353 with self.subTest(f"api_kwargs_from_config with {conf_type} config"):
354 version = 'Test1' if config else None
355 actual = api_kwargs_from_config(version, config)
356 self.assertEqual(actual['discoveryServiceUrl'], self._discoveryServiceUrl())
357 self.assertEqual(actual['token'], os.environ['ARVADOS_API_TOKEN'])
358 self.assertEqual(actual['version'], version or 'v1')
359 self.assertTrue(actual['insecure'])
360 self.assertNotIn('host', actual)
362 def test_api_kwargs_from_bad_config(self):
363 base_config = self._config_from_environ()
364 for del_key in ['ARVADOS_API_HOST', 'ARVADOS_API_TOKEN']:
365 with self.subTest(f"api_kwargs_from_config without {del_key} fails"), \
366 self.assertRaises(ValueError):
367 config = dict(base_config)
369 api_kwargs_from_config('v1', config)
371 def test_api_client_constructor(self):
374 self._discoveryServiceUrl(),
375 os.environ['ARVADOS_API_TOKEN'],
378 self.assertEqual(client.api_token, os.environ['ARVADOS_API_TOKEN'],
379 "client constructed with incorrect token")
381 hasattr(client, 'localapi'),
382 "client has localapi method when it should not be thread-safe",
385 def test_api_client_bad_url(self):
386 all_args = ('v1', self._discoveryServiceUrl(), 'test_api_client_bad_url')
387 for arg_index, arg_value in [
388 (0, 'BadTestVersion'),
389 (1, all_args[1] + '/BadTestURL'),
391 with self.subTest(f"api_client fails with {arg_index}={arg_value!r}"), \
392 self.assertRaises(apiclient_errors.UnknownApiNameOrVersion):
393 args = list(all_args)
394 args[arg_index] = arg_value
395 api_client(*args, insecure=True)
397 def test_initial_retry_logs(self):
399 _googleapiclient_log_lock.release()
401 # Lock was never acquired - that's the state we want anyway
403 real_logger = logging.getLogger('googleapiclient.http')
404 mock_logger = mock.Mock(wraps=real_logger)
405 mock_logger.handlers = logging.getLogger('googleapiclient').handlers
406 mock_logger.level = logging.NOTSET
407 with mock.patch('logging.getLogger', return_value=mock_logger), \
408 mock.patch('time.sleep'), \
409 self.assertLogs(real_logger, 'INFO') as actual_logs:
411 api_client('v1', 'https://test.invalid/', 'NoToken', num_retries=1)
412 except httplib2.error.ServerNotFoundError:
414 mock_logger.addFilter.assert_called()
415 mock_logger.addHandler.assert_called()
416 mock_logger.setLevel.assert_called()
417 mock_logger.removeHandler.assert_called()
418 mock_logger.removeFilter.assert_called()
419 self.assertRegex(actual_logs.output[0], r'^INFO:googleapiclient\.http:Sleeping \d')
421 def test_configured_logger_untouched(self):
422 real_logger = logging.getLogger('googleapiclient.http')
423 mock_logger = mock.Mock(wraps=real_logger)
424 mock_logger.handlers = logging.getLogger().handlers
425 with mock.patch('logging.getLogger', return_value=mock_logger), \
426 mock.patch('time.sleep'):
428 api_client('v1', 'https://test.invalid/', 'NoToken', num_retries=1)
429 except httplib2.error.ServerNotFoundError:
431 mock_logger.addFilter.assert_not_called()
432 mock_logger.addHandler.assert_not_called()
433 mock_logger.setLevel.assert_not_called()
434 mock_logger.removeHandler.assert_not_called()
435 mock_logger.removeFilter.assert_not_called()
438 class ConstructNumRetriesTestCase(unittest.TestCase):
440 def _fake_retry_request(http, num_retries, req_type, sleep, rand, uri, method, *args, **kwargs):
441 return http.request(uri, method, *args, **kwargs)
443 @contextlib.contextmanager
444 def patch_retry(self):
445 # We have this dedicated context manager that goes through `sys.modules`
446 # instead of just using `mock.patch` because of the unfortunate
447 # `arvados.api` name collision.
448 orig_func = sys.modules['arvados.api']._orig_retry_request
449 expect_name = 'googleapiclient.http._retry_request'
451 '{0.__module__}.{0.__name__}'.format(orig_func), expect_name,
452 f"test setup problem: {expect_name} not at arvados.api._orig_retry_request",
454 retry_mock = mock.Mock(wraps=self._fake_retry_request)
455 sys.modules['arvados.api']._orig_retry_request = retry_mock
459 sys.modules['arvados.api']._orig_retry_request = orig_func
461 def _iter_num_retries(self, retry_mock):
462 for call in retry_mock.call_args_list:
466 yield call.kwargs['num_retries']
468 def test_default_num_retries(self):
469 with self.patch_retry() as retry_mock:
470 client = arvados.api('v1')
471 actual = set(self._iter_num_retries(retry_mock))
472 self.assertEqual(len(actual), 1)
473 self.assertTrue(actual.pop() > 6, "num_retries lower than expected")
475 def _test_calls(self, init_arg, call_args, expected):
476 with self.patch_retry() as retry_mock:
477 client = arvados.api('v1', num_retries=init_arg)
478 for num_retries in call_args:
479 client.users().current().execute(num_retries=num_retries)
480 actual = self._iter_num_retries(retry_mock)
481 # The constructor makes two requests with its num_retries argument:
482 # one for the discovery document, and one for the config.
483 self.assertEqual(next(actual, None), init_arg)
484 self.assertEqual(next(actual, None), init_arg)
485 self.assertEqual(list(actual), expected)
487 def test_discovery_num_retries(self):
488 for num_retries in [0, 5, 55]:
489 with self.subTest(f"num_retries={num_retries}"):
490 self._test_calls(num_retries, [], [])
492 def test_num_retries_called_le_init(self):
494 with self.subTest(f"init_arg={n}"):
495 call_args = [n - 4, n - 2, n]
497 self._test_calls(n, call_args, expected)
499 def test_num_retries_called_ge_init(self):
501 with self.subTest(f"init_arg={n}"):
502 call_args = [n, n + 4, n + 8]
503 self._test_calls(n, call_args, call_args)
505 def test_num_retries_called_mixed(self):
506 self._test_calls(5, [2, 6, 4, 8], [5, 6, 5, 8])
509 class PreCloseSocketTestCase(unittest.TestCase):
511 self.api = arvados.api('v1')
512 self.assertTrue(hasattr(self.api._http, 'orig_http_request'),
513 "test doesn't know how to intercept HTTP requests")
514 self.mock_response = {'user': 'person'}
515 self.request_success = (fake_httplib2_response(200),
516 json.dumps(self.mock_response))
517 self.api._http.orig_http_request = mock.MagicMock()
518 # All requests succeed by default. Tests override as needed.
519 self.api._http.orig_http_request.return_value = self.request_success
521 @mock.patch('time.time', side_effect=[i*2**20 for i in range(99)])
522 def test_close_old_connections_non_retryable(self, sleep):
523 self._test_connection_close(expect=1)
525 @mock.patch('time.time', side_effect=itertools.count())
526 def test_no_close_fresh_connections_non_retryable(self, sleep):
527 self._test_connection_close(expect=0)
529 @mock.patch('time.time', side_effect=itertools.count())
530 def test_override_max_idle_time(self, sleep):
531 self.api._http._max_keepalive_idle = 0
532 self._test_connection_close(expect=1)
534 def _test_connection_close(self, expect=0):
535 # Do two POST requests. The second one must close all
536 # connections +expect+ times.
537 self.api.users().create(body={}).execute()
538 mock_conns = {str(i): mock.MagicMock() for i in range(2)}
539 self.api._http.connections = mock_conns.copy()
540 self.api.users().create(body={}).execute()
541 for c in mock_conns.values():
542 self.assertEqual(c.close.call_count, expect)
545 if __name__ == '__main__':