def test_server_error_with_a_bad_json(self): def _handle_request(context): context.retry_flag = False context.http_response = Response() context.http_response.status_code = 400 context.http_response.headers = {} context.http_response._content = b"bad-json" ecs_client = EcsClient(self.client_config, self.init_credentials_provider()) ecs_client.handlers = DEFAULT_HANDLERS with patch.object(http_handler, "handle_request", wraps=_handle_request): try: ecs_client.delete_instance() assert False except ServerException as e: self.assertEqual(400, e.http_status) self.assertEqual("Ecs", e.service_name) self.assertEqual("ecs-cn-hangzhou.aliyuncs.com", e.endpoint) self.assertEqual("SDK.UnknownServerError", e.error_code) if six.PY2: self.assertEqual('ServerResponseBody: bad-json', e.error_message) else: self.assertEqual("ServerResponseBody: b'bad-json'", e.error_message)
def test_missing_message_in_response2(self): # test missing Code in response def _handle_request(context): context.retry_flag = False context.http_response = Response() context.http_response.status_code = 400 context.http_response.headers = {} context.http_response._content = b"{\"Code\": \"YouMessedSomethingUp\"}" ecs_client = EcsClient(self.client_config, self.init_credentials_provider()) ecs_client.handlers = DEFAULT_HANDLERS with patch.object(http_handler, "handle_request", wraps=_handle_request): try: ecs_client.describe_regions() assert False except ServerException as e: self.assertEqual(400, e.http_status) self.assertEqual("Ecs", e.service_name) self.assertEqual("ecs-cn-hangzhou.aliyuncs.com", e.endpoint) self.assertEqual("YouMessedSomethingUp", e.error_code) if six.PY2: self.assertEqual('ServerResponseBody: {"Code": "YouMessedSomethingUp"}', e.error_message) else: self.assertEqual("""ServerResponseBody: b'{"Code": "YouMessedSomethingUp"}'""", e.error_message)
def test_server_error_with_valid_json_no_code_or_message(self): # test valid json format but no Code or Message def _handle_request(context): context.retry_flag = False context.http_response = Response() context.http_response.status_code = 400 context.http_response.headers = {} context.http_response._content = b"""{"key" : "this is a valid json string"}""" ecs_client = EcsClient(self.client_config, self.init_credentials_provider()) ecs_client.handlers = DEFAULT_HANDLERS with patch.object(http_handler, "handle_request", wraps=_handle_request): try: ecs_client.describe_instances() assert False except ServerException as e: self.assertEqual(400, e.http_status) self.assertEqual("Ecs", e.service_name) self.assertEqual("ecs-cn-hangzhou.aliyuncs.com", e.endpoint) self.assertEqual("SDK.UnknownServerError", e.error_code) if six.PY2: self.assertEqual( 'ServerResponseBody: {"key" : "this is a valid json string"}', e.error_message) else: self.assertEqual( """ServerResponseBody: b'{"key" : "this is a valid json string"}'""", e.error_message)
def test_throttled_backoff(self): def _handle_response(context): context.exception = ServerException("Throttling", "some error") config = self.client_config config.max_retry_times = 10 config.endpoint = "somewhere.you.will.never.get" client = EcsClient(config, self.init_credentials_provider()) api_request = APIRequest('DescribeInstances', 'GET', 'http', 'RPC') globals()["_test_compute_delay"] = [] def record_sleep(delay): global _test_compute_delay _test_compute_delay.append(delay) from alibabacloud.handlers.api_protocol_handler import APIProtocolHandler from alibabacloud.handlers.credentials_handler import CredentialsHandler from alibabacloud.handlers.signer_handler import SignerHandler from alibabacloud.handlers.timeout_config_reader import TimeoutConfigReader from alibabacloud.handlers.endpoint_handler import EndpointHandler from alibabacloud.handlers.retry_handler import RetryHandler from alibabacloud.handlers.server_error_handler import ServerErrorHandler from alibabacloud.handlers.http_handler import HttpHandler DEFAULT_HANDLERS = [ RetryHandler(), APIProtocolHandler(), CredentialsHandler(), SignerHandler(), TimeoutConfigReader(), EndpointHandler(), ServerErrorHandler(), HttpHandler(), ] client.handlers = DEFAULT_HANDLERS client.config = config with patch.object(time, "sleep", wraps=record_sleep) as monkey: with patch.object(ServerErrorHandler, "handle_response", wraps=_handle_response): try: client._handle_request(api_request) assert False except ServerException as e: self.assertEqual("Throttling", e.error_code) self.assertEqual(10, monkey.call_count) self.assertEqual(10, len(_test_compute_delay))
def test_retry_with_client_token(self): config = self.client_config client = EcsClient(config, self.init_credentials_provider()) client.max_retry_times = 3 client.handlers = DEFAULT_HANDLERS api_request = APIRequest('CreateInstance', 'GET', 'http', 'RPC') api_request._params = { 'ImageId': "coreos_1745_7_0_64_30G_alibase_20180705.vhd", 'InstanceType': "ecs.n2.small", } globals()['_test_client_token'] = None globals()['_test_retry_times'] = 0 def _handle_request(context): global _test_client_token global _test_retry_times context.retry_flag = False request = context.api_request if _test_retry_times > 0: assert _test_client_token == request._params.get("ClientToken") _test_retry_times += 1 _test_client_token = request._params.get("ClientToken") context.exception = HttpErrorException(http_error="some error") from alibabacloud.request import HTTPResponse context.http_response = HTTPResponse('', None, {}, None) def no_sleep(delay): pass with patch.object(time, "sleep", no_sleep): with patch.object(client.handlers[6], "handle_request", wraps=_handle_request) as monkey: try: ret = client._handle_request(api_request) assert False except HttpErrorException as e: self.assertEqual("some error", e.error_message) self.assertEqual(4, monkey.call_count)
def test_missing_message_in_response(self): # test missing message in response def _handle_request(context): context.retry_flag = False context.http_response = Response() context.http_response.status_code = 400 context.http_response.headers = {} context.http_response._content = b"{\"Message\": \"Some message\"}" ecs_client = EcsClient(self.client_config, self.init_credentials_provider()) ecs_client.handlers = DEFAULT_HANDLERS with patch.object(http_handler, "handle_request", wraps=_handle_request): try: ecs_client.describe_regions() assert False except ServerException as e: self.assertEqual(400, e.http_status) self.assertEqual("Ecs", e.service_name) self.assertEqual("ecs-cn-hangzhou.aliyuncs.com", e.endpoint) self.assertEqual("SDK.UnknownServerError", e.error_code) self.assertEqual("""Some message""", e.error_message)