def test_list_entries_explicit(self): from google.cloud.logging import DESCENDING from google.cloud.logging import Client PROJECT1 = "PROJECT1" PROJECT2 = "PROJECT2" INPUT_FILTER = "resource.type:global" TOKEN = "TOKEN" PAGE_SIZE = 42 client = Client(project=self.PROJECT, credentials=_make_credentials(), _use_grpc=False) client._connection = _Connection({}) logger = self._make_one(self.LOGGER_NAME, client=client) iterator = logger.list_entries( resource_names=[f"projects/{PROJECT1}", f"projects/{PROJECT2}"], filter_=INPUT_FILTER, order_by=DESCENDING, page_size=PAGE_SIZE, page_token=TOKEN, ) entries = list(iterator) token = iterator.next_page_token self.assertEqual(len(entries), 0) self.assertIsNone(token) # self.assertEqual(client._listed, LISTED) # check call payload call_payload_no_filter = deepcopy(client._connection._called_with) call_payload_no_filter["data"]["filter"] = "removed" self.assertEqual( call_payload_no_filter, { "method": "POST", "path": "/entries:list", "data": { "filter": "removed", "orderBy": DESCENDING, "pageSize": PAGE_SIZE, "pageToken": TOKEN, "resourceNames": [f"projects/{PROJECT1}", f"projects/{PROJECT2}"], }, }, ) # verify that default filter is 24 hours LOG_FILTER = "logName=projects/%s/logs/%s" % ( self.PROJECT, self.LOGGER_NAME, ) combined_filter = (INPUT_FILTER + " AND " + LOG_FILTER + " AND " + "timestamp>=" + self.TIME_FORMAT) timestamp = datetime.strptime( client._connection._called_with["data"]["filter"], combined_filter) yesterday = datetime.now(timezone.utc) - timedelta(days=1) self.assertLess(yesterday - timestamp, timedelta(minutes=1))
def test_list_entries_no_paging(self): from google.cloud.logging import Client from google.cloud.logging import TextEntry from google.cloud.logging import Logger NOW, TIMESTAMP = self._make_timestamp() IID = "IID" TEXT = "TEXT" SENT = {"resourceNames": [self.PROJECT_PATH]} TOKEN = "TOKEN" RETURNED = { "entries": [ { "textPayload": TEXT, "insertId": IID, "resource": {"type": "global"}, "timestamp": TIMESTAMP, "logName": f"projects/{self.PROJECT}/logs/{self.LOGGER_NAME}", } ], "nextPageToken": TOKEN, } client = Client( project=self.PROJECT, credentials=_make_credentials(), _use_grpc=False ) client._connection = _Connection(RETURNED) api = self._make_one(client) iterator = api.list_entries([self.PROJECT_PATH]) page = next(iterator.pages) entries = list(page) token = iterator.next_page_token # First check the token. self.assertEqual(token, TOKEN) # Then check the entries returned. self.assertEqual(len(entries), 1) entry = entries[0] self.assertIsInstance(entry, TextEntry) self.assertEqual(entry.payload, TEXT) self.assertIsInstance(entry.logger, Logger) self.assertEqual(entry.logger.name, self.LOGGER_NAME) self.assertEqual(entry.insert_id, IID) self.assertEqual(entry.timestamp, NOW) self.assertIsNone(entry.labels) self.assertIsNone(entry.severity) self.assertIsNone(entry.http_request) called_with = client._connection._called_with expected_path = "/%s" % (self.LIST_ENTRIES_PATH,) self.assertEqual( called_with, {"method": "POST", "path": expected_path, "data": SENT} )
def test_list_entries_explicit_timestamp(self): from google.cloud.logging import DESCENDING from google.cloud.logging import Client PROJECT1 = "PROJECT1" PROJECT2 = "PROJECT2" INPUT_FILTER = 'resource.type:global AND timestamp="2020-10-13T21"' TOKEN = "TOKEN" PAGE_SIZE = 42 client = Client(project=self.PROJECT, credentials=_make_credentials(), _use_grpc=False) client._connection = _Connection({}) logger = self._make_one(self.LOGGER_NAME, client=client) iterator = logger.list_entries( resource_names=[f"projects/{PROJECT1}", f"projects/{PROJECT2}"], filter_=INPUT_FILTER, order_by=DESCENDING, page_size=PAGE_SIZE, page_token=TOKEN, ) entries = list(iterator) token = iterator.next_page_token self.assertEqual(len(entries), 0) self.assertIsNone(token) # self.assertEqual(client._listed, LISTED) # check call payload LOG_FILTER = "logName=projects/%s/logs/%s" % ( self.PROJECT, self.LOGGER_NAME, ) combined_filter = INPUT_FILTER + " AND " + LOG_FILTER self.assertEqual( client._connection._called_with, { "method": "POST", "path": "/entries:list", "data": { "filter": combined_filter, "orderBy": DESCENDING, "pageSize": PAGE_SIZE, "pageToken": TOKEN, "resourceNames": [f"projects/{PROJECT1}", f"projects/{PROJECT2}"], }, }, )
def test_list_entries_defaults(self): from google.cloud.logging import Client TOKEN = "TOKEN" client = Client(project=self.PROJECT, credentials=_make_credentials(), _use_grpc=False) returned = {"nextPageToken": TOKEN} client._connection = _Connection(returned) logger = self._make_one(self.LOGGER_NAME, client=client) iterator = logger.list_entries() page = next(iterator.pages) entries = list(page) token = iterator.next_page_token self.assertEqual(len(entries), 0) self.assertEqual(token, TOKEN) LOG_FILTER = "logName=projects/%s/logs/%s" % (self.PROJECT, self.LOGGER_NAME) # check call payload call_payload_no_filter = deepcopy(client._connection._called_with) call_payload_no_filter["data"]["filter"] = "removed" self.assertEqual( call_payload_no_filter, { "path": "/entries:list", "method": "POST", "data": { "filter": "removed", "resourceNames": [f"projects/{self.PROJECT}"], }, }, ) # verify that default filter is 24 hours timestamp = datetime.strptime( client._connection._called_with["data"]["filter"], LOG_FILTER + " AND timestamp>=" + self.TIME_FORMAT, ) yesterday = datetime.now(timezone.utc) - timedelta(days=1) self.assertLess(yesterday - timestamp, timedelta(minutes=1))
def test_list_entries(self): from google.cloud.logging import DESCENDING from google.cloud.logging import Client from google.cloud.logging import Logger from google.cloud.logging import ProtobufEntry from google.cloud.logging import StructEntry PROJECT1 = "PROJECT1" PROJECT1_PATH = f"projects/{PROJECT1}" PROJECT2 = "PROJECT2" PROJECT2_PATH = f"projects/{PROJECT2}" NOW, TIMESTAMP = self._make_timestamp() IID1 = "IID1" IID2 = "IID2" PAYLOAD = {"message": "MESSAGE", "weather": "partly cloudy"} PROTO_PAYLOAD = PAYLOAD.copy() PROTO_PAYLOAD["@type"] = "type.googleapis.com/testing.example" TOKEN = "TOKEN" PAGE_SIZE = 42 SENT = { "resourceNames": [PROJECT1_PATH, PROJECT2_PATH], "filter": self.FILTER, "orderBy": DESCENDING, "pageSize": PAGE_SIZE, "pageToken": TOKEN, } RETURNED = { "entries": [ { "jsonPayload": PAYLOAD, "insertId": IID1, "resource": {"type": "global"}, "timestamp": TIMESTAMP, "logName": "projects/%s/logs/%s" % (self.PROJECT, self.LOGGER_NAME), }, { "protoPayload": PROTO_PAYLOAD, "insertId": IID2, "resource": {"type": "global"}, "timestamp": TIMESTAMP, "logName": "projects/%s/logs/%s" % (self.PROJECT, self.LOGGER_NAME), }, ] } client = Client( project=self.PROJECT, credentials=_make_credentials(), _use_grpc=False ) client._connection = _Connection(RETURNED) api = self._make_one(client) iterator = api.list_entries( resource_names=[PROJECT1_PATH, PROJECT2_PATH], filter_=self.FILTER, order_by=DESCENDING, page_size=PAGE_SIZE, page_token=TOKEN, ) entries = list(iterator) # Check the entries returned. self.assertEqual(len(entries), 2) entry1 = entries[0] self.assertIsInstance(entry1, StructEntry) self.assertEqual(entry1.payload, PAYLOAD) self.assertIsInstance(entry1.logger, Logger) self.assertEqual(entry1.logger.name, self.LOGGER_NAME) self.assertEqual(entry1.insert_id, IID1) self.assertEqual(entry1.timestamp, NOW) self.assertIsNone(entry1.labels) self.assertIsNone(entry1.severity) self.assertIsNone(entry1.http_request) entry2 = entries[1] self.assertIsInstance(entry2, ProtobufEntry) self.assertEqual(entry2.payload, PROTO_PAYLOAD) self.assertIsInstance(entry2.logger, Logger) self.assertEqual(entry2.logger.name, self.LOGGER_NAME) self.assertEqual(entry2.insert_id, IID2) self.assertEqual(entry2.timestamp, NOW) self.assertIsNone(entry2.labels) self.assertIsNone(entry2.severity) self.assertIsNone(entry2.http_request) called_with = client._connection._called_with expected_path = "/%s" % (self.LIST_ENTRIES_PATH,) self.assertEqual( called_with, {"method": "POST", "path": expected_path, "data": SENT} )
def test_list_entries_with_limits(self): from google.cloud.logging import Client from google.cloud.logging import TextEntry from google.cloud.logging import Logger NOW, TIMESTAMP = self._make_timestamp() IID = "IID" IID1 = "IID1" IID2 = "IID2" TEXT = "TEXT" SENT = {"resourceNames": [self.PROJECT_PATH]} PAYLOAD = {"message": "MESSAGE", "weather": "partly cloudy"} PROTO_PAYLOAD = PAYLOAD.copy() PROTO_PAYLOAD["@type"] = "type.googleapis.com/testing.example" RETURNED = { "entries": [ { "textPayload": TEXT, "insertId": IID, "resource": {"type": "global"}, "timestamp": TIMESTAMP, "logName": f"projects/{self.PROJECT}/logs/{self.LOGGER_NAME}", }, { "jsonPayload": PAYLOAD, "insertId": IID1, "resource": {"type": "global"}, "timestamp": TIMESTAMP, "logName": "projects/%s/logs/%s" % (self.PROJECT, self.LOGGER_NAME), }, { "protoPayload": PROTO_PAYLOAD, "insertId": IID2, "resource": {"type": "global"}, "timestamp": TIMESTAMP, "logName": "projects/%s/logs/%s" % (self.PROJECT, self.LOGGER_NAME), }, ], } client = Client( project=self.PROJECT, credentials=_make_credentials(), _use_grpc=False ) # try with negative max_results with self.assertRaises(ValueError): client._connection = _Connection(RETURNED) api = self._make_one(client) empty = list(api.list_entries([self.PROJECT_PATH], max_results=-1)) # try with max_results of 0 client._connection = _Connection(RETURNED) api = self._make_one(client) empty = list(api.list_entries([self.PROJECT_PATH], max_results=0)) self.assertEqual(empty, []) # try with single result client._connection = _Connection(RETURNED) api = self._make_one(client) iterator = api.list_entries([self.PROJECT_PATH], max_results=1) entries = list(iterator) # check the entries returned. self.assertEqual(len(entries), 1) entry = entries[0] self.assertIsInstance(entry, TextEntry) self.assertEqual(entry.payload, TEXT) self.assertIsInstance(entry.logger, Logger) self.assertEqual(entry.logger.name, self.LOGGER_NAME) self.assertEqual(entry.insert_id, IID) self.assertEqual(entry.timestamp, NOW) self.assertIsNone(entry.labels) self.assertIsNone(entry.severity) self.assertIsNone(entry.http_request) called_with = client._connection._called_with expected_path = "/%s" % (self.LIST_ENTRIES_PATH,) self.assertEqual( called_with, {"method": "POST", "path": expected_path, "data": SENT} )