def test_list_monitored_resource_descriptors(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = logging_service_v2_client.LoggingServiceV2Client() # Mock response next_page_token = '' resource_descriptors_element = monitored_resource_pb2.MonitoredResourceDescriptor( ) resource_descriptors = [resource_descriptors_element] expected_response = logging_pb2.ListMonitoredResourceDescriptorsResponse( next_page_token=next_page_token, resource_descriptors=resource_descriptors) grpc_stub.ListMonitoredResourceDescriptors.return_value = expected_response paged_list_response = client.list_monitored_resource_descriptors() resources = list(paged_list_response) self.assertEqual(1, len(resources)) self.assertEqual(expected_response.resource_descriptors[0], resources[0]) grpc_stub.ListMonitoredResourceDescriptors.assert_called_once() args, kwargs = grpc_stub.ListMonitoredResourceDescriptors.call_args self.assertEqual(len(args), 2) self.assertEqual(len(kwargs), 1) self.assertIn('metadata', kwargs) actual_request = args[0] expected_request = logging_pb2.ListMonitoredResourceDescriptorsRequest( ) self.assertEqual(expected_request, actual_request)
def test_write_log_entries(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = logging_service_v2_client.LoggingServiceV2Client() # Mock request entries = [] # Mock response expected_response = logging_pb2.WriteLogEntriesResponse() grpc_stub.WriteLogEntries.return_value = expected_response response = client.write_log_entries(entries) self.assertEqual(expected_response, response) grpc_stub.WriteLogEntries.assert_called_once() args, kwargs = grpc_stub.WriteLogEntries.call_args self.assertEqual(len(args), 2) self.assertEqual(len(kwargs), 1) self.assertIn('metadata', kwargs) actual_request = args[0] expected_request = logging_pb2.WriteLogEntriesRequest(entries=entries) self.assertEqual(expected_request, actual_request)
def test_list_logs(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = logging_service_v2_client.LoggingServiceV2Client() # Mock request parent = client.project_path('[PROJECT]') # Mock response next_page_token = '' log_names_element = 'logNamesElement-1079688374' log_names = [log_names_element] expected_response = logging_pb2.ListLogsResponse( next_page_token=next_page_token, log_names=log_names) grpc_stub.ListLogs.return_value = expected_response paged_list_response = client.list_logs(parent) resources = list(paged_list_response) self.assertEqual(1, len(resources)) self.assertEqual(expected_response.log_names[0], resources[0]) grpc_stub.ListLogs.assert_called_once() args, kwargs = grpc_stub.ListLogs.call_args self.assertEqual(len(args), 2) self.assertEqual(len(kwargs), 1) self.assertIn('metadata', kwargs) actual_request = args[0] expected_request = logging_pb2.ListLogsRequest(parent=parent) self.assertEqual(expected_request, actual_request)
def test_list_log_entries(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = logging_service_v2_client.LoggingServiceV2Client() # Mock request resource_names = [] # Mock response next_page_token = '' entries_element = log_entry_pb2.LogEntry() entries = [entries_element] expected_response = logging_pb2.ListLogEntriesResponse( next_page_token=next_page_token, entries=entries) grpc_stub.ListLogEntries.return_value = expected_response paged_list_response = client.list_log_entries(resource_names) resources = list(paged_list_response) self.assertEqual(1, len(resources)) self.assertEqual(expected_response.entries[0], resources[0]) grpc_stub.ListLogEntries.assert_called_once() args, kwargs = grpc_stub.ListLogEntries.call_args self.assertEqual(len(args), 2) self.assertEqual(len(kwargs), 1) self.assertIn('metadata', kwargs) actual_request = args[0] expected_request = logging_pb2.ListLogEntriesRequest( resource_names=resource_names) self.assertEqual(expected_request, actual_request)
def test_delete_log_exception(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = logging_service_v2_client.LoggingServiceV2Client() # Mock request log_name = client.log_path('[PROJECT]', '[LOG]') # Mock exception response grpc_stub.DeleteLog.side_effect = CustomException() self.assertRaises(errors.GaxError, client.delete_log, log_name)
def test_list_monitored_resource_descriptors_exception( self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = logging_service_v2_client.LoggingServiceV2Client() # Mock exception response grpc_stub.ListMonitoredResourceDescriptors.side_effect = CustomException( ) paged_list_response = client.list_monitored_resource_descriptors() self.assertRaises(errors.GaxError, list, paged_list_response)
def test_write_log_entries_exception(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = logging_service_v2_client.LoggingServiceV2Client() # Mock request entries = [] # Mock exception response grpc_stub.WriteLogEntries.side_effect = CustomException() self.assertRaises(errors.GaxError, client.write_log_entries, entries)
def test_list_logs_exception(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = logging_service_v2_client.LoggingServiceV2Client() # Mock request parent = client.project_path('[PROJECT]') # Mock exception response grpc_stub.ListLogs.side_effect = CustomException() paged_list_response = client.list_logs(parent) self.assertRaises(errors.GaxError, list, paged_list_response)
def test_list_log_entries_exception(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = logging_service_v2_client.LoggingServiceV2Client() # Mock request resource_names = [] # Mock exception response grpc_stub.ListLogEntries.side_effect = CustomException() paged_list_response = client.list_log_entries(resource_names) self.assertRaises(errors.GaxError, list, paged_list_response)
def __init__(self): self.logger = None self.init_logger('LogMine') self.args = self.arguments() self.results = list() self.new_data = dict() self.stopfile_data = {} self.filterfile_data = [] self.min_len = 16 self.max_len = 64 self.max_substring_length = 128 # truncate log entry payloads to 1st N characters if FILTER2 self.logger_client = logging_service_v2_client.LoggingServiceV2Client() self.credentials = GoogleCredentials.get_application_default() self.dataflow_service = discovery.build('dataflow', 'v1b3', credentials=self.credentials) if self.args.send_file: self.send_requested_file() else: self.logger.info('do query') entries = self.do_query() self.logger.debug('list entries') for entry in entries: self.logger.debug(entry) self.logger.info('output raw results to file') self.write_raw_results('{}/{}'.format(self.args.output_dir, self.args.output_file), entries) # look for new input (based on what is not in the stopfile) and report it, then # update the stopfile with the new input # use optional output_file arg in case of pre-existing file (no query and no parse) self.logger.info('do process') self.do_process(entries) # send notification emails if sender, recipients and new_data if self.args.sender and self.args.recipients and self.new_data: if self.new_data: self.logger.info('send notification(s)') for recipient in self.args.recipients: self.do_sendmail(self.args.sender, recipient, self.new_data) # write out the new filter_file, including existing and new entries found by FilterFinder self.logger.info('write filter file') self.write_filter_file()
def test_delete_log(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = logging_service_v2_client.LoggingServiceV2Client() # Mock request log_name = client.log_path('[PROJECT]', '[LOG]') client.delete_log(log_name) grpc_stub.DeleteLog.assert_called_once() args, kwargs = grpc_stub.DeleteLog.call_args self.assertEqual(len(args), 2) self.assertEqual(len(kwargs), 1) self.assertIn('metadata', kwargs) actual_request = args[0] expected_request = logging_pb2.DeleteLogRequest(log_name=log_name) self.assertEqual(expected_request, actual_request)