1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
18 import urllib.parse as urlparse
20 from unittest import mock
21 from . import run_test_server
23 from apiclient import errors as apiclient_errors
24 from apiclient import http as apiclient_http
25 from arvados.api import (
28 api_kwargs_from_config,
29 _googleapiclient_log_lock,
31 from .arvados_testutil import fake_httplib2_response, mock_api_responses, queue_with
34 if not mimetypes.inited:
37 class ArvadosApiTest(run_test_server.TestCaseWithServers):
39 ERROR_HEADERS = {'Content-Type': mimetypes.types_map['.json']}
40 RETRIED_4XX = frozenset([408, 409, 423])
42 def api_error_response(self, code, *errors):
43 return (fake_httplib2_response(code, **self.ERROR_HEADERS),
44 json.dumps({'errors': errors,
45 'error_token': '1234567890+12345678'}).encode())
47 def _config_from_environ(self):
50 for key, value in os.environ.items()
51 if key.startswith('ARVADOS_API_')
54 def _discoveryServiceUrl(
57 path='/discovery/v1/apis/{api}/{apiVersion}/rest',
61 host = os.environ['ARVADOS_API_HOST']
62 return urlparse.urlunsplit((scheme, host, path, None, None))
64 def test_new_api_objects_with_cache(self):
65 clients = [arvados.api('v1', cache=True) for index in [0, 1]]
66 self.assertIsNot(*clients)
68 def test_empty_list(self):
69 answer = arvados.api('v1').collections().list(
70 filters=[['uuid', '=', 'abcdef']]).execute()
71 self.assertEqual(answer['items_available'], len(answer['items']))
73 def test_nonempty_list(self):
74 answer = arvados.api('v1').collections().list().execute()
75 self.assertNotEqual(0, answer['items_available'])
76 self.assertNotEqual(0, len(answer['items']))
78 def test_timestamp_inequality_filter(self):
79 api = arvados.api('v1')
80 new_item = api.collections().create(body={}).execute()
81 for operator, should_include in [
82 ['<', False], ['>', False],
83 ['<=', True], ['>=', True], ['=', True]]:
84 response = api.collections().list(filters=[
85 ['created_at', operator, new_item['created_at']],
86 # Also filter by uuid to ensure (if it matches) it's on page 0
87 ['uuid', '=', new_item['uuid']]]).execute()
88 uuids = [item['uuid'] for item in response['items']]
89 did_include = new_item['uuid'] in uuids
91 did_include, should_include,
92 "'%s %s' filter should%s have matched '%s'" % (
93 operator, new_item['created_at'],
94 ('' if should_include else ' not'),
95 new_item['created_at']))
97 def test_exceptions_include_errors(self):
99 'arvados.collections.get': self.api_error_response(
100 422, "Bad UUID format", "Bad output format"),
102 req_builder = apiclient_http.RequestMockBuilder(mock_responses)
103 api = arvados.api('v1', requestBuilder=req_builder)
104 with self.assertRaises(apiclient_errors.HttpError) as err_ctx:
105 api.collections().get(uuid='xyz-xyz-abcdef').execute()
106 err_s = str(err_ctx.exception)
107 for msg in ["Bad UUID format", "Bad output format"]:
108 self.assertIn(msg, err_s)
110 @mock.patch('time.sleep')
111 def test_exceptions_include_request_id(self, sleep):
112 api = arvados.api('v1')
113 api.request_id='fake-request-id'
114 api._http.orig_http_request = mock.MagicMock()
115 api._http.orig_http_request.side_effect = socket.error('mock error')
118 api.users().current().execute()
119 except Exception as e:
121 self.assertRegex(str(caught), r'fake-request-id')
123 def test_exceptions_without_errors_have_basic_info(self):
125 'arvados.collections.delete': (
126 fake_httplib2_response(500, **self.ERROR_HEADERS),
129 req_builder = apiclient_http.RequestMockBuilder(mock_responses)
130 api = arvados.api('v1', requestBuilder=req_builder)
131 with self.assertRaises(apiclient_errors.HttpError) as err_ctx:
132 api.collections().delete(uuid='xyz-xyz-abcdef').execute()
133 self.assertIn("500", str(err_ctx.exception))
135 def test_request_too_large(self):
136 api = arvados.api('v1')
137 maxsize = api._rootDesc.get('maxRequestSize', 0)
138 with self.assertRaises(apiclient_errors.MediaUploadSizeError):
140 arvados.api('v1').collections().create(body={"manifest_text": text}).execute()
142 def test_default_request_timeout(self):
143 api = arvados.api('v1')
144 self.assertEqual(api._http.timeout, 300,
145 "Default timeout value should be 300")
147 def test_custom_request_timeout(self):
148 api = arvados.api('v1', timeout=1234)
149 self.assertEqual(api._http.timeout, 1234,
150 "Requested timeout value was 1234")
152 def test_4xx_retried(self):
153 client = arvados.api('v1')
154 for code in self.RETRIED_4XX:
155 name = f'retried #{code}'
156 with self.subTest(name), mock.patch('time.sleep'):
157 expected = {'username': name}
158 with mock_api_responses(
160 json.dumps(expected),
165 actual = client.users().current().execute()
166 self.assertEqual(actual, expected)
168 def test_4xx_not_retried(self):
169 client = arvados.api('v1', num_retries=3)
170 # Note that googleapiclient does retry 403 *if* the response JSON
171 # includes flags that say the request was denied by rate limiting.
172 # An empty JSON response like we use here should not be retried.
173 for code in [400, 401, 403, 404, 422]:
174 with self.subTest(f'error {code}'), mock.patch('time.sleep'):
175 with mock_api_responses(
181 ), self.assertRaises(arvados.errors.ApiError) as exc_check:
182 client.users().current().execute()
183 response = exc_check.exception.args[0]
184 self.assertEqual(response.status, code)
185 self.assertEqual(response.get('status'), str(code))
187 def test_4xx_raised_after_retry_exhaustion(self):
188 client = arvados.api('v1', num_retries=1)
189 for code in self.RETRIED_4XX:
190 with self.subTest(f'failed {code}'), mock.patch('time.sleep'):
191 with mock_api_responses(
194 [code, code, code, 200],
197 ), self.assertRaises(arvados.errors.ApiError) as exc_check:
198 client.users().current().execute()
199 response = exc_check.exception.args[0]
200 self.assertEqual(response.status, code)
201 self.assertEqual(response.get('status'), str(code))
203 def test_api_is_threadsafe(self):
205 'host': os.environ['ARVADOS_API_HOST'],
206 'token': os.environ['ARVADOS_API_TOKEN'],
209 config_kwargs = {'apiconfig': os.environ}
210 for api_constructor, kwargs in [
212 (arvados.api, api_kwargs),
213 (arvados.api_from_config, {}),
214 (arvados.api_from_config, config_kwargs),
216 sub_kwargs = "kwargs" if kwargs else "no kwargs"
217 with self.subTest(f"{api_constructor.__name__} with {sub_kwargs}"):
218 api_client = api_constructor('v1', **kwargs)
219 self.assertTrue(hasattr(api_client, 'localapi'),
220 f"client missing localapi method")
221 self.assertTrue(hasattr(api_client, 'keep'),
222 f"client missing keep attribute")
224 def test_api_host_constructor(self):
227 client = arvados.api(
230 os.environ['ARVADOS_API_HOST'],
231 os.environ['ARVADOS_API_TOKEN'],
234 self.assertEqual(client.api_token, os.environ['ARVADOS_API_TOKEN'],
235 "client constructed with incorrect token")
237 def test_api_url_constructor(self):
238 client = arvados.api(
240 discoveryServiceUrl=self._discoveryServiceUrl(),
241 token=os.environ['ARVADOS_API_TOKEN'],
244 self.assertEqual(client.api_token, os.environ['ARVADOS_API_TOKEN'],
245 "client constructed with incorrect token")
247 def test_api_bad_args(self):
249 'host': os.environ['ARVADOS_API_HOST'],
250 'token': os.environ['ARVADOS_API_TOKEN'],
251 'discoveryServiceUrl': self._discoveryServiceUrl(),
254 # Passing only a single key is missing required info
255 *([key] for key in all_kwargs.keys()),
256 # Passing all keys is a conflict
257 list(all_kwargs.keys()),
259 kwargs = {key: all_kwargs[key] for key in use_keys}
260 kwargs_list = ', '.join(use_keys)
261 with self.subTest(f"calling arvados.api with {kwargs_list} fails"), \
262 self.assertRaises(ValueError):
263 arvados.api('v1', insecure=True, **kwargs)
265 def test_api_bad_url(self):
267 {'discoveryServiceUrl': self._discoveryServiceUrl() + '/BadTestURL'},
268 {'version': 'BadTestVersion', 'host': os.environ['ARVADOS_API_HOST']},
270 bad_key = next(iter(bad_kwargs))
271 with self.subTest(f"api fails with bad {bad_key}"), \
272 self.assertRaises(apiclient_errors.UnknownApiNameOrVersion):
273 arvados.api(**bad_kwargs, token='test_api_bad_url', insecure=True)
275 def test_normalize_api_good_args(self):
276 for version, discoveryServiceUrl, host in [
277 ('Test1', None, os.environ['ARVADOS_API_HOST']),
278 (None, self._discoveryServiceUrl(), None)
280 argname = 'discoveryServiceUrl' if host is None else 'host'
281 with self.subTest(f"normalize_api_kwargs with {argname}"):
282 actual = normalize_api_kwargs(
286 os.environ['ARVADOS_API_TOKEN'],
289 self.assertEqual(actual['discoveryServiceUrl'], self._discoveryServiceUrl())
290 self.assertEqual(actual['token'], os.environ['ARVADOS_API_TOKEN'])
291 self.assertEqual(actual['version'], version or 'v1')
292 self.assertTrue(actual['insecure'])
293 self.assertNotIn('host', actual)
295 def test_normalize_api_bad_args(self):
297 self._discoveryServiceUrl(),
298 os.environ['ARVADOS_API_HOST'],
299 os.environ['ARVADOS_API_TOKEN'],
301 for arg_index, arg_value in enumerate(all_args):
302 args = [None] * len(all_args)
303 args[arg_index] = arg_value
304 with self.subTest(f"normalize_api_kwargs with only arg #{arg_index + 1}"), \
305 self.assertRaises(ValueError):
306 normalize_api_kwargs('v1', *args)
307 with self.subTest("normalize_api_kwargs with discoveryServiceUrl and host"), \
308 self.assertRaises(ValueError):
309 normalize_api_kwargs('v1', *all_args)
311 def test_api_from_config_default(self):
312 client = arvados.api_from_config('v1')
313 self.assertEqual(client.api_token, os.environ['ARVADOS_API_TOKEN'],
314 "client constructed with incorrect token")
316 def test_api_from_config_explicit(self):
317 config = self._config_from_environ()
318 client = arvados.api_from_config('v1', config)
319 self.assertEqual(client.api_token, os.environ['ARVADOS_API_TOKEN'],
320 "client constructed with incorrect token")
322 def test_api_from_bad_config(self):
323 base_config = self._config_from_environ()
324 for del_key in ['ARVADOS_API_HOST', 'ARVADOS_API_TOKEN']:
325 with self.subTest(f"api_from_config without {del_key} fails"), \
326 self.assertRaises(ValueError):
327 config = dict(base_config)
329 arvados.api_from_config('v1', config)
331 def test_api_kwargs_from_good_config(self):
332 for config in [None, self._config_from_environ()]:
333 conf_type = 'default' if config is None else 'passed'
334 with self.subTest(f"api_kwargs_from_config with {conf_type} config"):
335 version = 'Test1' if config else None
336 actual = api_kwargs_from_config(version, config)
337 self.assertEqual(actual['discoveryServiceUrl'], self._discoveryServiceUrl())
338 self.assertEqual(actual['token'], os.environ['ARVADOS_API_TOKEN'])
339 self.assertEqual(actual['version'], version or 'v1')
340 self.assertTrue(actual['insecure'])
341 self.assertNotIn('host', actual)
343 def test_api_kwargs_from_bad_config(self):
344 base_config = self._config_from_environ()
345 for del_key in ['ARVADOS_API_HOST', 'ARVADOS_API_TOKEN']:
346 with self.subTest(f"api_kwargs_from_config without {del_key} fails"), \
347 self.assertRaises(ValueError):
348 config = dict(base_config)
350 api_kwargs_from_config('v1', config)
352 def test_api_client_constructor(self):
355 self._discoveryServiceUrl(),
356 os.environ['ARVADOS_API_TOKEN'],
359 self.assertEqual(client.api_token, os.environ['ARVADOS_API_TOKEN'],
360 "client constructed with incorrect token")
362 hasattr(client, 'localapi'),
363 "client has localapi method when it should not be thread-safe",
366 def test_api_client_bad_url(self):
367 all_args = ('v1', self._discoveryServiceUrl(), 'test_api_client_bad_url')
368 for arg_index, arg_value in [
369 (0, 'BadTestVersion'),
370 (1, all_args[1] + '/BadTestURL'),
372 with self.subTest(f"api_client fails with {arg_index}={arg_value!r}"), \
373 self.assertRaises(apiclient_errors.UnknownApiNameOrVersion):
374 args = list(all_args)
375 args[arg_index] = arg_value
376 api_client(*args, insecure=True)
378 def test_initial_retry_logs(self):
380 _googleapiclient_log_lock.release()
382 # Lock was never acquired - that's the state we want anyway
384 real_logger = logging.getLogger('googleapiclient.http')
385 mock_logger = mock.Mock(wraps=real_logger)
386 mock_logger.handlers = logging.getLogger('googleapiclient').handlers
387 mock_logger.level = logging.NOTSET
388 with mock.patch('logging.getLogger', return_value=mock_logger), \
389 mock.patch('time.sleep'), \
390 self.assertLogs(real_logger, 'INFO') as actual_logs:
392 api_client('v1', 'https://test.invalid/', 'NoToken', num_retries=1)
393 except httplib2.error.ServerNotFoundError:
395 mock_logger.addFilter.assert_called()
396 mock_logger.addHandler.assert_called()
397 mock_logger.setLevel.assert_called()
398 mock_logger.removeHandler.assert_called()
399 mock_logger.removeFilter.assert_called()
400 self.assertRegex(actual_logs.output[0], r'^INFO:googleapiclient\.http:Sleeping \d')
402 def test_configured_logger_untouched(self):
403 real_logger = logging.getLogger('googleapiclient.http')
404 mock_logger = mock.Mock(wraps=real_logger)
405 mock_logger.handlers = logging.getLogger().handlers
406 with mock.patch('logging.getLogger', return_value=mock_logger), \
407 mock.patch('time.sleep'):
409 api_client('v1', 'https://test.invalid/', 'NoToken', num_retries=1)
410 except httplib2.error.ServerNotFoundError:
412 mock_logger.addFilter.assert_not_called()
413 mock_logger.addHandler.assert_not_called()
414 mock_logger.setLevel.assert_not_called()
415 mock_logger.removeHandler.assert_not_called()
416 mock_logger.removeFilter.assert_not_called()
419 class ConstructNumRetriesTestCase(unittest.TestCase):
421 def _fake_retry_request(http, num_retries, req_type, sleep, rand, uri, method, *args, **kwargs):
422 return http.request(uri, method, *args, **kwargs)
424 @contextlib.contextmanager
425 def patch_retry(self):
426 # We have this dedicated context manager that goes through `sys.modules`
427 # instead of just using `mock.patch` because of the unfortunate
428 # `arvados.api` name collision.
429 orig_func = sys.modules['arvados.api']._orig_retry_request
430 expect_name = 'googleapiclient.http._retry_request'
432 '{0.__module__}.{0.__name__}'.format(orig_func), expect_name,
433 f"test setup problem: {expect_name} not at arvados.api._orig_retry_request",
435 retry_mock = mock.Mock(wraps=self._fake_retry_request)
436 sys.modules['arvados.api']._orig_retry_request = retry_mock
440 sys.modules['arvados.api']._orig_retry_request = orig_func
442 def _iter_num_retries(self, retry_mock):
443 for call in retry_mock.call_args_list:
447 yield call.kwargs['num_retries']
449 def test_default_num_retries(self):
450 with self.patch_retry() as retry_mock:
451 client = arvados.api('v1')
452 actual = set(self._iter_num_retries(retry_mock))
453 self.assertEqual(len(actual), 1)
454 self.assertTrue(actual.pop() > 6, "num_retries lower than expected")
456 def _test_calls(self, init_arg, call_args, expected):
457 with self.patch_retry() as retry_mock:
458 client = arvados.api('v1', num_retries=init_arg)
459 for num_retries in call_args:
460 client.users().current().execute(num_retries=num_retries)
461 actual = self._iter_num_retries(retry_mock)
462 # The constructor makes two requests with its num_retries argument:
463 # one for the discovery document, and one for the config.
464 self.assertEqual(next(actual, None), init_arg)
465 self.assertEqual(next(actual, None), init_arg)
466 self.assertEqual(list(actual), expected)
468 def test_discovery_num_retries(self):
469 for num_retries in [0, 5, 55]:
470 with self.subTest(f"num_retries={num_retries}"):
471 self._test_calls(num_retries, [], [])
473 def test_num_retries_called_le_init(self):
475 with self.subTest(f"init_arg={n}"):
476 call_args = [n - 4, n - 2, n]
478 self._test_calls(n, call_args, expected)
480 def test_num_retries_called_ge_init(self):
482 with self.subTest(f"init_arg={n}"):
483 call_args = [n, n + 4, n + 8]
484 self._test_calls(n, call_args, call_args)
486 def test_num_retries_called_mixed(self):
487 self._test_calls(5, [2, 6, 4, 8], [5, 6, 5, 8])
490 class PreCloseSocketTestCase(unittest.TestCase):
492 self.api = arvados.api('v1')
493 self.assertTrue(hasattr(self.api._http, 'orig_http_request'),
494 "test doesn't know how to intercept HTTP requests")
495 self.mock_response = {'user': 'person'}
496 self.request_success = (fake_httplib2_response(200),
497 json.dumps(self.mock_response))
498 self.api._http.orig_http_request = mock.MagicMock()
499 # All requests succeed by default. Tests override as needed.
500 self.api._http.orig_http_request.return_value = self.request_success
502 @mock.patch('time.time', side_effect=[i*2**20 for i in range(99)])
503 def test_close_old_connections_non_retryable(self, sleep):
504 self._test_connection_close(expect=1)
506 @mock.patch('time.time', side_effect=itertools.count())
507 def test_no_close_fresh_connections_non_retryable(self, sleep):
508 self._test_connection_close(expect=0)
510 @mock.patch('time.time', side_effect=itertools.count())
511 def test_override_max_idle_time(self, sleep):
512 self.api._http._max_keepalive_idle = 0
513 self._test_connection_close(expect=1)
515 def _test_connection_close(self, expect=0):
516 # Do two POST requests. The second one must close all
517 # connections +expect+ times.
518 self.api.users().create(body={}).execute()
519 mock_conns = {str(i): mock.MagicMock() for i in range(2)}
520 self.api._http.connections = mock_conns.copy()
521 self.api.users().create(body={}).execute()
522 for c in mock_conns.values():
523 self.assertEqual(c.close.call_count, expect)
526 if __name__ == '__main__':