Esempio n. 1
0
    def test_missing_code_in_response(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"{\"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_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("Some message", e.error_message)
                else:
                    self.assertEqual("""Some message""", e.error_message)
Esempio n. 2
0
    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_default_user_agent(self):
     client_config = self.init_temp_client_config()
     client = EcsClient(client_config, self.init_credentials_provider())
     with MyServer() as s:
         client.describe_instances()
         user_agent = s.headers.get('User-Agent')
     self.assertEqual(self.joint_default_user_agent(), user_agent)
    def test_no_retry(self):
        client = EcsClient(self.client_config,
                           self.init_credentials_provider())
        tempdir = tempfile.mkdtemp()

        temp_file = os.path.join(tempdir, 'file_logger')

        client.add_rotating_file_log_handler(log_level=logging.DEBUG,
                                             path=temp_file)
        context = client.describe_instances()
        self.assertTrue(os.path.isfile(temp_file))
        with open(temp_file) as logfile:
            s = logfile.read()
        self.assertTrue('alibabacloud-' in s)
        self.assertTrue('DEBUG' in s)
 def test_default_retry_times(self):
     config = self.client_config
     config.endpoint = "somewhere.you.will.never.get"
     client = EcsClient(config, self.init_credentials_provider())
     with patch.object(client.handlers[-1],
                       "handle_request",
                       wraps=client.handlers[-1].handle_request) as monkey:
         try:
             context = client.describe_instances()
             assert False
         except HttpErrorException as e:
             self.assertTrue(
                 e.error_message.startswith(
                     "HTTPConnectionPool(host='somewhere.you.will.never.get', port=80):"
                     " Max retries exceeded with url:"))
     self.assertEqual(4, monkey.call_count)
    def test_no_retry(self):
        config = self.client_config
        config.enable_retry = False
        config.endpoint = 'somewhere.you.never'
        client = EcsClient(config, self.init_credentials_provider())

        with patch.object(client, "_handle_request",
                          wraps=client._handle_request) as monkey:
            try:
                context = client.describe_instances()
                assert False
            except HttpErrorException as e:
                self.assertTrue(e.error_message.startswith(
                    "HTTPConnectionPool(host='somewhere.you.never', port=80): "
                    "Max retries exceeded with url: /"))

        self.assertEqual(1, monkey.call_count)
 def test_doc_help_sample(self):
     self.client_config.endpoint = "ecs-cn-hangzhou.aliyuncs.com"
     ecs_client = EcsClient(self.client_config,
                            self.init_credentials_provider())
     response = ecs_client.describe_instances()
     self.assertTrue(response.get('RequestId'))