def _mock_api_call(self, mock_method, code, body): mock_method = mock_method().execute if code == 200: mock_method.return_value = body else: mock_method.side_effect = arvados.errors.ApiError( tutil.fake_httplib2_response(code), "{}")
def setUpClass(cls): # The apiclient library has support for mocking requests for # testing, but it doesn't extend to the discovery document # itself. For now, bring up an API server that will serve # a discovery document. # FIXME: Figure out a better way to stub this out. run_test_server.run() mock_responses = { 'arvados.humans.delete': (fake_httplib2_response(500, **cls.ERROR_HEADERS), ""), 'arvados.humans.get': cls.api_error_response(422, "Bad UUID format", "Bad output format"), 'arvados.humans.list': (None, json.dumps({ 'items_available': 0, 'items': [] })), } req_builder = apiclient_http.RequestMockBuilder(mock_responses) cls.api = arvados.api('v1', host=os.environ['ARVADOS_API_HOST'], token='discovery-doc-only-no-token-needed', insecure=True, requestBuilder=req_builder)
def test_try_next_server_after_timeout(self): side_effects = [ socket.timeout("timed out"), (tutil.fake_httplib2_response(200), self.DEFAULT_EXPECT)] with mock.patch('httplib2.Http.request', side_effect=iter(side_effects)): self.check_success(locator=self.HINTED_LOCATOR)
def test_exceptions_without_errors_have_basic_info(self): mock_responses = {"arvados.humans.delete": (fake_httplib2_response(500, **self.ERROR_HEADERS), "")} req_builder = apiclient_http.RequestMockBuilder(mock_responses) api = arvados.api("v1", requestBuilder=req_builder) with self.assertRaises(apiclient_errors.HttpError) as err_ctx: api.humans().delete(uuid="xyz-xyz-abcdef").execute() self.assertIn("500", str(err_ctx.exception))
def test_try_next_server_after_timeout(self): side_effects = [ socket.timeout("timed out"), (tutil.fake_httplib2_response(200), self.DEFAULT_EXPECT) ] with mock.patch('httplib2.Http.request', side_effect=iter(side_effects)): self.check_success(locator=self.HINTED_LOCATOR)
def test_exceptions_without_errors_have_basic_info(self): mock_responses = { 'arvados.humans.delete': (fake_httplib2_response(500, **self.ERROR_HEADERS), "") } req_builder = apiclient_http.RequestMockBuilder(mock_responses) api = arvados.api('v1', requestBuilder=req_builder) with self.assertRaises(apiclient_errors.HttpError) as err_ctx: api.humans().delete(uuid='xyz-xyz-abcdef').execute() self.assertIn("500", str(err_ctx.exception))
def setUp(self): self.api = arvados.api("v1") self.assertTrue( hasattr(self.api._http, "orig_http_request"), "test doesn't know how to intercept HTTP requests" ) self.mock_response = {"user": "******"} self.request_success = (fake_httplib2_response(200), json.dumps(self.mock_response)) self.api._http.orig_http_request = mock.MagicMock() # All requests succeed by default. Tests override as needed. self.api._http.orig_http_request.return_value = self.request_success
def setUp(self): self.api = arvados.api('v1') self.assertTrue(hasattr(self.api._http, 'orig_http_request'), "test doesn't know how to intercept HTTP requests") self.mock_response = {'user': '******'} self.request_success = (fake_httplib2_response(200), json.dumps(self.mock_response)) self.api._http.orig_http_request = mock.MagicMock() # All requests succeed by default. Tests override as needed. self.api._http.orig_http_request.return_value = self.request_success
def test_api_error_handling(self): collections_mock = mock.Mock(name='arv.collections()') coll_create_mock = collections_mock().create().execute coll_create_mock.side_effect = arvados.errors.ApiError( fake_httplib2_response(403), '{}') arv_put.api_client = arvados.api('v1') arv_put.api_client.collections = collections_mock with self.assertRaises(SystemExit) as exc_test: self.call_main_with_args(['/dev/null']) self.assertLess(0, exc_test.exception.args[0]) self.assertLess(0, coll_create_mock.call_count) self.assertEqual("", self.main_stdout.getvalue())
def test_api_error_handling(self): coll_save_mock = mock.Mock( name='arv.collection.Collection().save_new()') coll_save_mock.side_effect = arvados.errors.ApiError( fake_httplib2_response(403), '{}') with mock.patch('arvados.collection.Collection.save_new', new=coll_save_mock): with self.assertRaises(SystemExit) as exc_test: self.call_main_with_args(['/dev/null']) self.assertLess(0, exc_test.exception.args[0]) self.assertLess(0, coll_save_mock.call_count) self.assertEqual("", self.main_stdout.getvalue())
def test_socket_errors_retried(self): api = arvados.api('v1') self.assertTrue(hasattr(api._http, 'orig_http_request'), "test doesn't know how to intercept HTTP requests") api._http.orig_http_request = mock.MagicMock() mock_response = {'user': '******'} api._http.orig_http_request.side_effect = [ socket.error("mock error"), (fake_httplib2_response(200), json.dumps(mock_response)) ] actual_response = api.users().current().execute() self.assertEqual(mock_response, actual_response) self.assertGreater(api._http.orig_http_request.call_count, 1, "client got the right response without retrying")
def setUpClass(cls): # The apiclient library has support for mocking requests for # testing, but it doesn't extend to the discovery document # itself. For now, bring up an API server that will serve # a discovery document. # FIXME: Figure out a better way to stub this out. run_test_server.run() mock_responses = { 'arvados.humans.delete': ( fake_httplib2_response(500, **cls.ERROR_HEADERS), ""), 'arvados.humans.get': cls.api_error_response( 422, "Bad UUID format", "Bad output format"), 'arvados.humans.list': (None, json.dumps( {'items_available': 0, 'items': []})), } req_builder = RequestMockBuilder(mock_responses) cls.api = arvados.api('v1', cache=False, host=os.environ['ARVADOS_API_HOST'], token='discovery-doc-only-no-token-needed', insecure=True, requestBuilder=req_builder)
def api_error_response(self, code, *errors): return ( fake_httplib2_response(code, **self.ERROR_HEADERS), json.dumps({"errors": errors, "error_token": "1234567890+12345678"}), )
def test_retry_data_with_wrong_checksum(self): side_effects = ((tutil.fake_httplib2_response(200), s) for s in ['baddata', self.TEST_DATA]) with mock.patch('httplib2.Http.request', side_effect=side_effects): self.check_success(locator=self.HINTED_LOCATOR)
def results_map(self, *codes): for code in codes: response = (fake_httplib2_response(code), None) yield code, arv_retry.check_http_response_success(response)
def api_error_response(cls, code, *errors): return (fake_httplib2_response(code, **cls.ERROR_HEADERS), json.dumps({ 'errors': errors, 'error_token': '1234567890+12345678' }))
def api_error_response(self, code, *errors): return (fake_httplib2_response(code, **self.ERROR_HEADERS), json.dumps({'errors': errors, 'error_token': '1234567890+12345678'}))