示例#1
0
文件: test_db.py 项目: aws/aws-cli
 def test_emit_does_write_cli_rc_record(self):
     writer = mock.Mock(DatabaseRecordWriter)
     record_builder = RecordBuilder()
     handler = DatabaseHistoryHandler(writer, record_builder)
     handler.emit('CLI_RC', 0, 'CLI')
     call = writer.write_record.call_args[0][0]
     self.assertEqual(
         call, {
             'command_id': mock.ANY,
             'event_type': 'CLI_RC',
             'payload': 0,
             'source': 'CLI',
             'timestamp': mock.ANY
         })
     self.assertTrue(self.UUID_PATTERN.match(call['command_id']))
     self.assertIsInstance(call['timestamp'], numbers.Number)
示例#2
0
def attach_history_handler(session, parsed_args, **kwargs):
    if _should_enable_cli_history(session, parsed_args):
        LOG.debug('Enabling CLI history')

        history_filename = os.environ.get(HISTORY_FILENAME_ENV_VAR,
                                          DEFAULT_HISTORY_FILENAME)
        if not os.path.isdir(os.path.dirname(history_filename)):
            os.makedirs(os.path.dirname(history_filename))

        connection = DatabaseConnection(history_filename)
        writer = DatabaseRecordWriter(connection)
        record_builder = RecordBuilder()
        db_handler = DatabaseHistoryHandler(writer, record_builder)

        HISTORY_RECORDER.add_handler(db_handler)
        HISTORY_RECORDER.enable()
示例#3
0
文件: test_db.py 项目: aws/aws-cli
 def test_emit_does_write_api_call_record(self):
     writer = mock.Mock(DatabaseRecordWriter)
     record_builder = RecordBuilder()
     handler = DatabaseHistoryHandler(writer, record_builder)
     payload = {'foo': 'bar'}
     handler.emit('API_CALL', payload, 'BOTOCORE')
     call = writer.write_record.call_args[0][0]
     self.assertEqual(
         call, {
             'command_id': mock.ANY,
             'request_id': mock.ANY,
             'event_type': 'API_CALL',
             'payload': payload,
             'source': 'BOTOCORE',
             'timestamp': mock.ANY
         })
     self.assertTrue(self.UUID_PATTERN.match(call['command_id']))
     self.assertTrue(self.UUID_PATTERN.match(call['request_id']))
示例#4
0
文件: test_db.py 项目: aws/aws-cli
class TestIdentifierLifecycles(unittest.TestCase):
    def setUp(self):
        self.builder = RecordBuilder()

    def _get_multiple_request_ids(self, events):
        fake_payload = {'body': b''}
        request_ids = [
            self.builder.build_record(event, fake_payload.copy(),
                                      '')['request_id'] for event in events
        ]
        return request_ids

    def test_multiple_http_lifecycle_writes_have_same_request_id(self):
        request_ids = self._get_multiple_request_ids(
            ['API_CALL', 'HTTP_REQUEST', 'HTTP_RESPONSE', 'PARSED_RESPONSE'])
        # All request_ids should match since this is one request lifecycle
        unique_request_ids = set(request_ids)
        self.assertEqual(len(unique_request_ids), 1)

    def test_request_id_reset_on_api_call(self):
        request_ids = self._get_multiple_request_ids([
            'API_CALL', 'HTTP_REQUEST', 'HTTP_RESPONSE', 'PARSED_RESPONSE',
            'API_CALL', 'HTTP_REQUEST', 'HTTP_RESPONSE', 'PARSED_RESPONSE',
            'API_CALL', 'HTTP_REQUEST', 'HTTP_RESPONSE', 'PARSED_RESPONSE'
        ])

        # There should be three distinct requet_ids since there are three
        # distinct calls that end with a parsed response.
        unique_request_ids = set(request_ids)
        self.assertEqual(len(unique_request_ids), 3)

        # Check that the request ids match the correct events.
        first_request_ids = request_ids[:4]
        unique_first_request_ids = set(first_request_ids)
        self.assertEqual(len(unique_first_request_ids), 1)

        second_request_ids = request_ids[4:8]
        unique_second_request_ids = set(second_request_ids)
        self.assertEqual(len(unique_second_request_ids), 1)

        third_request_ids = request_ids[8:]
        unique_third_request_ids = set(third_request_ids)
        self.assertEqual(len(unique_third_request_ids), 1)
示例#5
0
文件: test_db.py 项目: aws/aws-cli
 def test_emit_does_write_parsed_response_record(self):
     writer = mock.Mock(DatabaseRecordWriter)
     record_builder = RecordBuilder()
     handler = DatabaseHistoryHandler(writer, record_builder)
     payload = {'metadata': {'data': 'foobar'}}
     # In order for an http_response to have a request_id it must have been
     # preceeded by an api_call record.
     handler.emit('API_CALL', '', 'BOTOCORE')
     handler.emit('PARSED_RESPONSE', payload, 'BOTOCORE')
     call = writer.write_record.call_args[0][0]
     self.assertEqual(
         call, {
             'command_id': mock.ANY,
             'request_id': mock.ANY,
             'event_type': 'PARSED_RESPONSE',
             'payload': payload,
             'source': 'BOTOCORE',
             'timestamp': mock.ANY
         })
     self.assertTrue(self.UUID_PATTERN.match(call['command_id']))
     self.assertTrue(self.UUID_PATTERN.match(call['request_id']))
示例#6
0
 def setUp(self):
     self.db = DatabaseConnection(':memory:')
     self.writer = DatabaseRecordWriter(connection=self.db)
     self.record_builder = RecordBuilder()
     self.handler = DatabaseHistoryHandler(
         writer=self.writer, record_builder=self.record_builder)
示例#7
0
文件: test_db.py 项目: aws/aws-cli
 def setUp(self):
     self.builder = RecordBuilder()
示例#8
0
文件: test_db.py 项目: aws/aws-cli
class TestRecordBuilder(unittest.TestCase):
    UUID_PATTERN = re.compile('^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$',
                              re.I)

    def setUp(self):
        self.builder = RecordBuilder()

    def _get_request_id_for_event_type(self, event_type):
        record = self.builder.build_record(event_type, {'body': b''}, '')
        return record.get('request_id')

    def test_does_inject_timestamp(self):
        record = self.builder.build_record('TEST', '', '')
        self.assertTrue('timestamp' in record)
        self.assertTrue(isinstance(record['timestamp'], numbers.Number))

    def test_does_inject_command_id(self):
        record = self.builder.build_record('TEST', '', '')
        self.assertTrue('timestamp' in record)
        self.assertTrue(isinstance(record['timestamp'], numbers.Number))
        self.assertTrue('command_id' in record)
        self.assertTrue(self.UUID_PATTERN.match(record['command_id']))

    def test_does_create_record_with_correct_fields(self):
        record = self.builder.build_record('type', 'payload', 'source')
        self.assertEqual(record['event_type'], 'type')
        self.assertEqual(record['payload'], 'payload')
        self.assertEqual(record['source'], 'source')
        self.assertTrue('command_id' in record)
        self.assertTrue('timestamp' in record)

    def test_can_process_http_request_with_none_body(self):
        try:
            self.builder.build_record('HTTP_REQUEST', {'body': None}, '')
        except ValueError:
            self.fail("Should not raise value error")

    def test_can_process_http_response_with_nono_body(self):
        try:
            self.builder.build_record('HTTP_RESPONSE', {'body': None}, '')
        except ValueError:
            self.fail("Should not raise value error")

    def test_can_get_request_id_from_api_call(self):
        identifier = self._get_request_id_for_event_type('API_CALL')
        self.assertTrue(self.UUID_PATTERN.match(identifier))

    def test_does_get_id_for_http_request_with_api_call(self):
        call_identifier = self._get_request_id_for_event_type('API_CALL')
        request_identifier = self._get_request_id_for_event_type(
            'HTTP_REQUEST')

        self.assertEqual(call_identifier, request_identifier)
        self.assertTrue(self.UUID_PATTERN.match(call_identifier))

    def test_does_get_id_for_http_response_with_api_call(self):
        call_identifier = self._get_request_id_for_event_type('API_CALL')
        response_identifier = self._get_request_id_for_event_type(
            'HTTP_RESPONSE')

        self.assertEqual(call_identifier, response_identifier)
        self.assertTrue(self.UUID_PATTERN.match(call_identifier))

    def test_does_get_id_for_parsed_response_with_api_call(self):
        call_identifier = self._get_request_id_for_event_type('API_CALL')
        response_identifier = self._get_request_id_for_event_type(
            'PARSED_RESPONSE')

        self.assertEqual(call_identifier, response_identifier)
        self.assertTrue(self.UUID_PATTERN.match(call_identifier))

    def test_does_not_get_id_for_http_request_without_api_call(self):
        identifier = self._get_request_id_for_event_type('HTTP_REQUEST')
        self.assertIsNone(identifier)

    def test_does_not_get_id_for_http_response_without_api_call(self):
        identifier = self._get_request_id_for_event_type('HTTP_RESPONSE')
        self.assertIsNone(identifier)

    def test_does_not_get_id_for_parsed_response_without_api_call(self):
        identifier = self._get_request_id_for_event_type('PARSED_RESPONSE')
        self.assertIsNone(identifier)
示例#9
0
文件: test_db.py 项目: aws/aws-cli
 def setUp(self):
     self.builder = RecordBuilder()
     self.threads = []