@@ -39,15 +39,18 @@ def _makeOne(self, *args, **kw):
3939@unittest .skipUnless (_HAVE_GAX , 'No gax-python' )
4040class Test_LoggingAPI (_Base , unittest .TestCase ):
4141 LOG_NAME = 'log_name'
42+ LOG_PATH = 'projects/%s/logs/%s' % (_Base .PROJECT , LOG_NAME )
4243
4344 def _getTargetClass (self ):
4445 from google .cloud .logging ._gax import _LoggingAPI
4546 return _LoggingAPI
4647
4748 def test_ctor (self ):
4849 gax_api = _GAXLoggingAPI ()
49- api = self ._makeOne (gax_api )
50+ client = object ()
51+ api = self ._makeOne (gax_api , client )
5052 self .assertIs (api ._gax_api , gax_api )
53+ self .assertIs (api ._client , client )
5154
5255 def test_list_entries_no_paging (self ):
5356 import datetime
@@ -57,32 +60,47 @@ def test_list_entries_no_paging(self):
5760 from google .logging .v2 .log_entry_pb2 import LogEntry
5861
5962 from google .cloud ._helpers import _datetime_to_pb_timestamp
63+ from google .cloud ._helpers import UTC
6064 from google .cloud ._testing import _GAXPageIterator
6165 from google .cloud .logging import DESCENDING
66+ from google .cloud .logging .client import Client
67+ from google .cloud .logging .entries import TextEntry
68+ from google .cloud .logging .logger import Logger
6269
6370 TOKEN = 'TOKEN'
6471 TEXT = 'TEXT'
6572 resource_pb = MonitoredResource (type = 'global' )
66- timestamp_pb = _datetime_to_pb_timestamp (
67- datetime . datetime . utcnow () )
68- entry_pb = LogEntry (log_name = self .LOG_NAME ,
73+ timestamp = datetime . datetime . utcnow (). replace ( tzinfo = UTC )
74+ timestamp_pb = _datetime_to_pb_timestamp ( timestamp )
75+ entry_pb = LogEntry (log_name = self .LOG_PATH ,
6976 resource = resource_pb ,
7077 timestamp = timestamp_pb ,
7178 text_payload = TEXT )
7279 response = _GAXPageIterator ([entry_pb ], page_token = TOKEN )
7380 gax_api = _GAXLoggingAPI (_list_log_entries_response = response )
74- api = self ._makeOne (gax_api )
81+ client = Client (project = self .PROJECT , credentials = object (),
82+ use_gax = True )
83+ api = self ._makeOne (gax_api , client )
7584
76- entries , next_token = api .list_entries (
85+ iterator = api .list_entries (
7786 [self .PROJECT ], self .FILTER , DESCENDING )
87+ entries = list (iterator )
88+ next_token = iterator .next_page_token
7889
90+ # First check the token.
91+ self .assertEqual (next_token , TOKEN )
92+ # Then check the entries returned.
7993 self .assertEqual (len (entries ), 1 )
8094 entry = entries [0 ]
81- self .assertIsInstance (entry , dict )
82- self .assertEqual (entry ['logName' ], self .LOG_NAME )
83- self .assertEqual (entry ['resource' ], {'type' : 'global' })
84- self .assertEqual (entry ['textPayload' ], TEXT )
85- self .assertEqual (next_token , TOKEN )
95+ self .assertIsInstance (entry , TextEntry )
96+ self .assertEqual (entry .payload , TEXT )
97+ self .assertIsInstance (entry .logger , Logger )
98+ self .assertEqual (entry .logger .name , self .LOG_NAME )
99+ self .assertIsNone (entry .insert_id )
100+ self .assertEqual (entry .timestamp , timestamp )
101+ self .assertIsNone (entry .labels )
102+ self .assertIsNone (entry .severity )
103+ self .assertIsNone (entry .http_request )
86104
87105 projects , filter_ , order_by , page_size , options = (
88106 gax_api ._list_log_entries_called_with )
@@ -97,33 +115,47 @@ def _list_entries_with_paging_helper(self, payload, struct_pb):
97115
98116 from google .api .monitored_resource_pb2 import MonitoredResource
99117 from google .logging .v2 .log_entry_pb2 import LogEntry
100- from google .cloud ._testing import _GAXPageIterator
101118 from google .cloud ._helpers import _datetime_to_pb_timestamp
119+ from google .cloud ._helpers import UTC
120+ from google .cloud ._testing import _GAXPageIterator
121+ from google .cloud .logging .client import Client
122+ from google .cloud .logging .entries import StructEntry
123+ from google .cloud .logging .logger import Logger
102124
103125 SIZE = 23
104126 TOKEN = 'TOKEN'
105127 NEW_TOKEN = 'NEW_TOKEN'
106128 resource_pb = MonitoredResource (type = 'global' )
107- timestamp_pb = _datetime_to_pb_timestamp (
108- datetime . datetime . utcnow () )
109- entry_pb = LogEntry (log_name = self .LOG_NAME ,
129+ timestamp = datetime . datetime . utcnow (). replace ( tzinfo = UTC )
130+ timestamp_pb = _datetime_to_pb_timestamp ( timestamp )
131+ entry_pb = LogEntry (log_name = self .LOG_PATH ,
110132 resource = resource_pb ,
111133 timestamp = timestamp_pb ,
112134 json_payload = struct_pb )
113135 response = _GAXPageIterator ([entry_pb ], page_token = NEW_TOKEN )
114136 gax_api = _GAXLoggingAPI (_list_log_entries_response = response )
115- api = self ._makeOne (gax_api )
137+ client = Client (project = self .PROJECT , credentials = object (),
138+ use_gax = True )
139+ api = self ._makeOne (gax_api , client )
116140
117- entries , next_token = api .list_entries (
141+ iterator = api .list_entries (
118142 [self .PROJECT ], page_size = SIZE , page_token = TOKEN )
143+ entries = list (iterator )
144+ next_token = iterator .next_page_token
119145
146+ # First check the token.
147+ self .assertEqual (next_token , NEW_TOKEN )
120148 self .assertEqual (len (entries ), 1 )
121149 entry = entries [0 ]
122- self .assertIsInstance (entry , dict )
123- self .assertEqual (entry ['logName' ], self .LOG_NAME )
124- self .assertEqual (entry ['resource' ], {'type' : 'global' })
125- self .assertEqual (entry ['jsonPayload' ], payload )
126- self .assertEqual (next_token , NEW_TOKEN )
150+ self .assertIsInstance (entry , StructEntry )
151+ self .assertEqual (entry .payload , payload )
152+ self .assertIsInstance (entry .logger , Logger )
153+ self .assertEqual (entry .logger .name , self .LOG_NAME )
154+ self .assertIsNone (entry .insert_id )
155+ self .assertEqual (entry .timestamp , timestamp )
156+ self .assertIsNone (entry .labels )
157+ self .assertIsNone (entry .severity )
158+ self .assertIsNone (entry .http_request )
127159
128160 projects , filter_ , order_by , page_size , options = (
129161 gax_api ._list_log_entries_called_with )
@@ -201,7 +233,7 @@ def _make_log_entry_with_extras(self, labels, iid, type_url, now):
201233 last = True ,
202234 id = 'OPID' ,
203235 )
204- entry_pb = LogEntry (log_name = self .LOG_NAME ,
236+ entry_pb = LogEntry (log_name = self .LOG_PATH ,
205237 resource = resource_pb ,
206238 proto_payload = proto_payload ,
207239 timestamp = timestamp_pb ,
@@ -213,18 +245,20 @@ def _make_log_entry_with_extras(self, labels, iid, type_url, now):
213245 return entry_pb
214246
215247 def test_list_entries_with_extra_properties (self ):
216- from datetime import datetime
248+ import datetime
217249
218250 # Import the wrappers to register the type URL for BoolValue
219251 # pylint: disable=unused-variable
220252 from google .protobuf import wrappers_pb2
221253 # pylint: enable=unused-variable
222254
223255 from google .cloud ._helpers import UTC
224- from google .cloud ._helpers import _datetime_to_rfc3339
225256 from google .cloud ._testing import _GAXPageIterator
257+ from google .cloud .logging .client import Client
258+ from google .cloud .logging .entries import ProtobufEntry
259+ from google .cloud .logging .logger import Logger
226260
227- NOW = datetime .utcnow ().replace (tzinfo = UTC )
261+ NOW = datetime .datetime . utcnow ().replace (tzinfo = UTC )
228262 SIZE = 23
229263 TOKEN = 'TOKEN'
230264 NEW_TOKEN = 'NEW_TOKEN'
@@ -239,47 +273,42 @@ def test_list_entries_with_extra_properties(self):
239273
240274 response = _GAXPageIterator ([entry_pb ], page_token = NEW_TOKEN )
241275 gax_api = _GAXLoggingAPI (_list_log_entries_response = response )
242- api = self ._makeOne (gax_api )
276+ client = Client (project = self .PROJECT , credentials = object (),
277+ use_gax = True )
278+ api = self ._makeOne (gax_api , client )
243279
244- entries , next_token = api .list_entries (
280+ iterator = api .list_entries (
245281 [self .PROJECT ], page_size = SIZE , page_token = TOKEN )
282+ entries = list (iterator )
283+ next_token = iterator .next_page_token
246284
285+ # First check the token.
286+ self .assertEqual (next_token , NEW_TOKEN )
287+ # Then check the entries returned.
247288 self .assertEqual (len (entries ), 1 )
248289 entry = entries [0 ]
249- self .assertIsInstance (entry , dict )
250- self .assertEqual (entry ['logName' ], self .LOG_NAME )
251- self .assertEqual (entry ['resource' ],
252- {'type' : 'global' , 'labels' : {'foo' : 'bar' }})
253- self .assertEqual (entry ['protoPayload' ], {
290+ self .assertIsInstance (entry , ProtobufEntry )
291+ self .assertEqual (entry .payload , {
254292 '@type' : bool_type_url ,
255293 'value' : False ,
256294 })
257- self .assertEqual (entry ['severity' ], SEVERITY )
258- self .assertEqual (entry ['labels' ], LABELS )
259- self .assertEqual (entry ['insertId' ], IID )
260- self .assertEqual (entry ['timestamp' ], _datetime_to_rfc3339 (NOW ))
261- request = entry_pb .http_request
262- EXPECTED_REQUEST = {
263- 'requestMethod' : request .request_method ,
264- 'requestUrl' : request .request_url ,
265- 'status' : request .status ,
266- 'requestSize' : str (request .request_size ),
267- 'responseSize' : str (request .response_size ),
268- 'referer' : request .referer ,
269- 'userAgent' : request .user_agent ,
270- 'remoteIp' : request .remote_ip ,
271- 'cacheHit' : request .cache_hit ,
272- }
273- self .assertEqual (entry ['httpRequest' ], EXPECTED_REQUEST )
274- operation = entry_pb .operation
275- EXPECTED_OPERATION = {
276- 'producer' : operation .producer ,
277- 'id' : operation .id ,
278- 'first' : operation .first ,
279- 'last' : operation .last ,
280- }
281- self .assertEqual (entry ['operation' ], EXPECTED_OPERATION )
282- self .assertEqual (next_token , NEW_TOKEN )
295+ self .assertIsInstance (entry .logger , Logger )
296+ self .assertEqual (entry .logger .name , self .LOG_NAME )
297+ self .assertEqual (entry .insert_id , IID )
298+ self .assertEqual (entry .timestamp , NOW )
299+ self .assertEqual (entry .labels , {'foo' : 'bar' })
300+ self .assertEqual (entry .severity , SEVERITY )
301+ self .assertEqual (entry .http_request , {
302+ 'requestMethod' : entry_pb .http_request .request_method ,
303+ 'requestUrl' : entry_pb .http_request .request_url ,
304+ 'status' : entry_pb .http_request .status ,
305+ 'requestSize' : str (entry_pb .http_request .request_size ),
306+ 'responseSize' : str (entry_pb .http_request .response_size ),
307+ 'referer' : entry_pb .http_request .referer ,
308+ 'userAgent' : entry_pb .http_request .user_agent ,
309+ 'remoteIp' : entry_pb .http_request .remote_ip ,
310+ 'cacheHit' : entry_pb .http_request .cache_hit ,
311+ })
283312
284313 projects , filter_ , order_by , page_size , options = (
285314 gax_api ._list_log_entries_called_with )
@@ -292,14 +321,13 @@ def test_list_entries_with_extra_properties(self):
292321 def test_write_entries_single (self ):
293322 from google .logging .v2 .log_entry_pb2 import LogEntry
294323 TEXT = 'TEXT'
295- LOG_PATH = 'projects/%s/logs/%s' % (self .PROJECT , self .LOG_NAME )
296324 ENTRY = {
297- 'logName' : LOG_PATH ,
325+ 'logName' : self . LOG_PATH ,
298326 'resource' : {'type' : 'global' },
299327 'textPayload' : TEXT ,
300328 }
301329 gax_api = _GAXLoggingAPI ()
302- api = self ._makeOne (gax_api )
330+ api = self ._makeOne (gax_api , None )
303331
304332 api .write_entries ([ENTRY ])
305333
@@ -309,7 +337,7 @@ def test_write_entries_single(self):
309337
310338 entry = entries [0 ]
311339 self .assertIsInstance (entry , LogEntry )
312- self .assertEqual (entry .log_name , LOG_PATH )
340+ self .assertEqual (entry .log_name , self . LOG_PATH )
313341 self .assertEqual (entry .resource .type , 'global' )
314342 self .assertEqual (entry .labels , {})
315343 self .assertEqual (entry .text_payload , TEXT )
@@ -328,7 +356,6 @@ def test_write_entries_w_extra_properties(self):
328356 from google .cloud ._helpers import UTC , _pb_timestamp_to_datetime
329357 NOW = datetime .utcnow ().replace (tzinfo = UTC )
330358 TEXT = 'TEXT'
331- LOG_PATH = 'projects/%s/logs/%s' % (self .PROJECT , self .LOG_NAME )
332359 SEVERITY = 'WARNING'
333360 LABELS = {
334361 'foo' : 'bar' ,
@@ -362,7 +389,7 @@ def test_write_entries_w_extra_properties(self):
362389 'last' : True ,
363390 }
364391 ENTRY = {
365- 'logName' : LOG_PATH ,
392+ 'logName' : self . LOG_PATH ,
366393 'resource' : {'type' : 'global' },
367394 'textPayload' : TEXT ,
368395 'severity' : SEVERITY ,
@@ -373,7 +400,7 @@ def test_write_entries_w_extra_properties(self):
373400 'operation' : OPERATION ,
374401 }
375402 gax_api = _GAXLoggingAPI ()
376- api = self ._makeOne (gax_api )
403+ api = self ._makeOne (gax_api , None )
377404
378405 api .write_entries ([ENTRY ])
379406
@@ -383,7 +410,7 @@ def test_write_entries_w_extra_properties(self):
383410
384411 entry = entries [0 ]
385412 self .assertIsInstance (entry , LogEntry )
386- self .assertEqual (entry .log_name , LOG_PATH )
413+ self .assertEqual (entry .log_name , self . LOG_PATH )
387414 self .assertEqual (entry .resource .type , 'global' )
388415 self .assertEqual (entry .text_payload , TEXT )
389416 self .assertEqual (entry .severity , WARNING )
@@ -441,17 +468,16 @@ def _write_entries_multiple_helper(self, json_payload, json_struct_pb):
441468 {'protoPayload' : PROTO ,
442469 'httpRequest' : {'requestUrl' : URL }},
443470 ]
444- LOG_PATH = 'projects/%s/logs/%s' % (self .PROJECT , self .LOG_NAME )
445471 RESOURCE = {
446472 'type' : 'global' ,
447473 }
448474 LABELS = {
449475 'foo' : 'bar' ,
450476 }
451477 gax_api = _GAXLoggingAPI ()
452- api = self ._makeOne (gax_api )
478+ api = self ._makeOne (gax_api , None )
453479
454- api .write_entries (ENTRIES , LOG_PATH , RESOURCE , LABELS )
480+ api .write_entries (ENTRIES , self . LOG_PATH , RESOURCE , LABELS )
455481
456482 entries , log_name , resource , labels , partial_success , options = (
457483 gax_api ._write_log_entries_called_with )
@@ -486,7 +512,7 @@ def _write_entries_multiple_helper(self, json_payload, json_struct_pb):
486512 request = entry .http_request
487513 self .assertEqual (request .request_url , URL )
488514
489- self .assertEqual (log_name , LOG_PATH )
515+ self .assertEqual (log_name , self . LOG_PATH )
490516 self .assertEqual (resource , RESOURCE )
491517 self .assertEqual (labels , LABELS )
492518 self .assertEqual (partial_success , False )
@@ -532,40 +558,39 @@ def test_write_entries_multiple_nested_payload(self):
532558 self ._write_entries_multiple_helper (json_payload , json_struct_pb )
533559
534560 def test_logger_delete (self ):
535- LOG_PATH = 'projects/%s/logs/%s' % (self .PROJECT , self .LOG_NAME )
536561 gax_api = _GAXLoggingAPI ()
537- api = self ._makeOne (gax_api )
562+ api = self ._makeOne (gax_api , None )
538563
539564 api .logger_delete (self .PROJECT , self .LOG_NAME )
540565
541566 log_name , options = gax_api ._delete_log_called_with
542- self .assertEqual (log_name , LOG_PATH )
567+ self .assertEqual (log_name , self . LOG_PATH )
543568 self .assertIsNone (options )
544569
545570 def test_logger_delete_not_found (self ):
546571 from google .cloud .exceptions import NotFound
547- LOG_PATH = 'projects/%s/logs/%s' % ( self . PROJECT , self . LOG_NAME )
572+
548573 gax_api = _GAXLoggingAPI (_delete_not_found = True )
549- api = self ._makeOne (gax_api )
574+ api = self ._makeOne (gax_api , None )
550575
551576 with self .assertRaises (NotFound ):
552577 api .logger_delete (self .PROJECT , self .LOG_NAME )
553578
554579 log_name , options = gax_api ._delete_log_called_with
555- self .assertEqual (log_name , LOG_PATH )
580+ self .assertEqual (log_name , self . LOG_PATH )
556581 self .assertIsNone (options )
557582
558583 def test_logger_delete_error (self ):
559584 from google .gax .errors import GaxError
560- LOG_PATH = 'projects/%s/logs/%s' % ( self . PROJECT , self . LOG_NAME )
585+
561586 gax_api = _GAXLoggingAPI (_random_gax_error = True )
562- api = self ._makeOne (gax_api )
587+ api = self ._makeOne (gax_api , None )
563588
564589 with self .assertRaises (GaxError ):
565590 api .logger_delete (self .PROJECT , self .LOG_NAME )
566591
567592 log_name , options = gax_api ._delete_log_called_with
568- self .assertEqual (log_name , LOG_PATH )
593+ self .assertEqual (log_name , self . LOG_PATH )
569594 self .assertIsNone (options )
570595
571596
0 commit comments