def test_serialize_api_call_event_with_attempts(self):
     event = APICallEvent(service=self.service,
                          operation=self.operation,
                          timestamp=1000)
     event.new_api_call_attempt(2000)
     serialized_event_dict = self.get_serialized_event_dict(event)
     self.assertEqual(serialized_event_dict['AttemptCount'], 1)
 def test_serialize_api_call_event_http_status_code(self):
     event = APICallEvent(service=self.service,
                          operation=self.operation,
                          timestamp=1000)
     attempt = event.new_api_call_attempt(2000)
     attempt.http_status_code = 200
     serialized_event_dict = self.get_serialized_event_dict(event)
     self.assertEqual(serialized_event_dict['FinalHttpStatusCode'], 200)
 def test_serialize_api_call_event_user_agent(self):
     event = APICallEvent(service=self.service,
                          operation=self.operation,
                          timestamp=1000)
     attempt = event.new_api_call_attempt(2000)
     attempt.request_headers = {'User-Agent': self.user_agent}
     serialized_event_dict = self.get_serialized_event_dict(event)
     self.assertEqual(serialized_event_dict['UserAgent'], self.user_agent)
 def test_serialize_api_call_event_wire_exception(self):
     event = APICallEvent(service=self.service,
                          operation=self.operation,
                          timestamp=1000)
     attempt = event.new_api_call_attempt(2000)
     attempt.wire_exception = Exception('Error on the wire')
     serialized_event_dict = self.get_serialized_event_dict(event)
     self.assertEqual(serialized_event_dict['FinalSdkException'],
                      'Exception')
     self.assertEqual(serialized_event_dict['FinalSdkExceptionMessage'],
                      'Error on the wire')
 def test_new_api_call_attempt_event(self):
     event = APICallEvent(service='MyService',
                          operation='MyOperation',
                          timestamp=1000,
                          latency=2000,
                          attempts=[])
     attempt_event = event.new_api_call_attempt(timestamp=2000)
     self.assertEqual(
         attempt_event,
         APICallAttemptEvent(service='MyService',
                             operation='MyOperation',
                             timestamp=2000))
     self.assertEqual(event.attempts, [attempt_event])
 def test_serialize_api_call_event_region(self):
     event = APICallEvent(service=self.service,
                          operation=self.operation,
                          timestamp=1000)
     attempt = event.new_api_call_attempt(2000)
     auth_value = (
         'AWS4-HMAC-SHA256 '
         'Credential=myaccesskey/20180523/my-region-1/ec2/aws4_request,'
         'SignedHeaders=content-type;host;x-amz-date, '
         'Signature=somesignature')
     self.request_headers['Authorization'] = auth_value
     attempt.request_headers = self.request_headers
     serialized_event_dict = self.get_serialized_event_dict(event)
     self.assertEqual(serialized_event_dict['Region'], 'my-region-1')
 def test_serialize_api_call_event_parsed_error(self):
     event = APICallEvent(service=self.service,
                          operation=self.operation,
                          timestamp=1000)
     attempt = event.new_api_call_attempt(2000)
     attempt.parsed_error = {
         'Code': 'MyErrorCode',
         'Message': 'My error message'
     }
     serialized_event_dict = self.get_serialized_event_dict(event)
     self.assertEqual(serialized_event_dict['FinalAwsException'],
                      'MyErrorCode')
     self.assertEqual(serialized_event_dict['FinalAwsExceptionMessage'],
                      'My error message')
 def test_serialize_api_call_event_with_latency(self):
     event = APICallEvent(service=self.service,
                          operation=self.operation,
                          timestamp=1000,
                          latency=2000)
     serialized_event_dict = self.get_serialized_event_dict(event)
     self.assertEqual(serialized_event_dict['Latency'], self.latency)
    def test_feed_with_wire_exception_retries_exceeded(self):
        self.feed_before_parameter_build_event(current_time=1)
        self.feed_request_created_event(current_time=2)

        self.mock_time.return_value = 3
        # Connection errors are retryable
        wire_exception = ConnectionError(error='connection issue')
        attempt_event = self.adapter.feed(
            'response-received', {
                'parsed_response': None,
                'context': self.context,
                'exception': wire_exception
            })
        self.mock_time.return_value = 4
        call_event = self.adapter.feed('after-call-error', {
            'exception': wire_exception,
            'context': self.context
        })
        self.assertEqual(
            call_event,
            APICallEvent(service=self.service_id,
                         operation=self.wire_name,
                         timestamp=1000,
                         latency=3000,
                         attempts=[attempt_event],
                         retries_exceeded=True))
 def test_serialize_api_call_event_with_retries_exceeded(self):
     event = APICallEvent(service=self.service,
                          operation=self.operation,
                          timestamp=1000,
                          retries_exceeded=True)
     serialized_event_dict = self.get_serialized_event_dict(event)
     self.assertEqual(serialized_event_dict['MaxRetriesExceeded'], 1)
 def test_init(self):
     event = APICallEvent(service='MyService',
                          operation='MyOperation',
                          timestamp=1000,
                          latency=2000,
                          attempts=[])
     self.assertEqual(event.service, 'MyService')
     self.assertEqual(event.operation, 'MyOperation')
     self.assertEqual(event.timestamp, 1000)
     self.assertEqual(event.latency, 2000)
     self.assertEqual(event.attempts, [])
    def test_feed_with_retries_exceeded(self):
        self.feed_before_parameter_build_event(current_time=1)
        self.feed_request_created_event(current_time=2)

        self.mock_time.return_value = 3
        first_attempt_event = self.adapter.feed(
            'response-received', {
                'parsed_response': {
                    'ResponseMetadata': {
                        'HTTPStatusCode': 500,
                        'HTTPHeaders': self.response_headers
                    }
                },
                'context': self.context,
                'exception': None
            })
        self.feed_request_created_event(current_time=5)
        self.mock_time.return_value = 6
        second_attempt_event = self.adapter.feed(
            'response-received', {
                'parsed_response': {
                    'ResponseMetadata': {
                        'HTTPStatusCode': 200,
                        'HTTPHeaders': self.response_headers,
                        'MaxAttemptsReached': True
                    }
                },
                'context': self.context,
                'exception': None
            })
        self.mock_time.return_value = 7
        call_event = self.adapter.feed(
            'after-call', {
                'parsed': {
                    'ResponseMetadata': {
                        'HTTPStatusCode': 200,
                        'HTTPHeaders': self.response_headers,
                        'MaxAttemptsReached': True
                    }
                },
                'context': self.context
            })
        self.assertEqual(
            call_event,
            APICallEvent(service=self.service_id,
                         operation=self.wire_name,
                         timestamp=1000,
                         latency=6000,
                         attempts=[first_attempt_event, second_attempt_event],
                         retries_exceeded=True))
 def test_serialize_api_call_event(self):
     event = APICallEvent(service=self.service,
                          operation=self.operation,
                          timestamp=1000)
     serialized_event_dict = self.get_serialized_event_dict(event)
     self.assertEqual(
         serialized_event_dict, {
             'Version': 1,
             'Type': 'ApiCall',
             'Service': self.service,
             'Api': self.operation,
             'ClientId': self.csm_client_id,
             'MaxRetriesExceeded': 0,
             'Timestamp': 1000,
             'AttemptCount': 0,
         })
    def test_feed_with_successful_response(self):
        self.feed_before_parameter_build_event(current_time=1)
        self.feed_request_created_event(current_time=2)

        self.mock_time.return_value = 3
        attempt_event = self.adapter.feed(
            'response-received', {
                'parsed_response': {
                    'ResponseMetadata': {
                        'HTTPStatusCode': self.http_status_code,
                        'HTTPHeaders': self.response_headers
                    }
                },
                'context': self.context,
                'exception': None
            })
        self.assertEqual(
            attempt_event,
            APICallAttemptEvent(
                service=self.service_id,
                operation=self.wire_name,
                timestamp=2000,
                latency=1000,
                url=self.url,
                request_headers=self.request_headers,
                http_status_code=self.http_status_code,
                response_headers=self.response_headers,
            ))

        self.mock_time.return_value = 4
        call_event = self.adapter.feed(
            'after-call', {
                'parsed': {
                    'ResponseMetadata': {
                        'HTTPStatusCode': self.http_status_code,
                        'HTTPHeaders': self.response_headers
                    }
                },
                'context': self.context
            })
        self.assertEqual(
            call_event,
            APICallEvent(service=self.service_id,
                         operation=self.wire_name,
                         timestamp=1000,
                         latency=3000,
                         attempts=[attempt_event]))
    def test_feed_with_wire_exception(self):
        self.feed_before_parameter_build_event(current_time=1)
        self.feed_request_created_event(current_time=2)

        self.mock_time.return_value = 3
        wire_exception = Exception('Some wire exception')
        attempt_event = self.adapter.feed(
            'response-received', {
                'parsed_response': None,
                'context': self.context,
                'exception': wire_exception
            })
        self.assertEqual(
            attempt_event,
            APICallAttemptEvent(
                service=self.service_id,
                operation=self.wire_name,
                timestamp=2000,
                latency=1000,
                url=self.url,
                request_headers=self.request_headers,
                wire_exception=wire_exception,
            ))

        self.mock_time.return_value = 4
        call_event = self.adapter.feed('after-call-error', {
            'exception': wire_exception,
            'context': self.context
        })
        self.assertEqual(
            call_event,
            APICallEvent(service=self.service_id,
                         operation=self.wire_name,
                         timestamp=1000,
                         latency=3000,
                         attempts=[attempt_event]))
 def test_serialize_produces_bytes(self):
     event = APICallEvent(service=self.service,
                          operation=self.operation,
                          timestamp=1000)
     serialized_event = self.serializer.serialize(event)
     self.assertIsInstance(serialized_event, six.binary_type)
 def test_serialize_does_not_add_whitespace(self):
     event = APICallEvent(service=self.service,
                          operation=self.operation,
                          timestamp=1000)
     serialized_event = self.serializer.serialize(event)
     self.assertIsNone(re.match(r'\s', serialized_event.decode('utf-8')))