12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
- from copy import deepcopy
16
- from datetime import datetime
17
- from datetime import timedelta
18
- from datetime import timezone
19
15
import sys
20
-
21
16
import unittest
22
- import pytest
17
+ from copy import deepcopy
18
+ from datetime import datetime , timedelta , timezone
23
19
24
20
import mock
21
+ import pytest
25
22
26
23
27
24
def _make_credentials ():
@@ -131,6 +128,7 @@ def test_log_empty_defaults_w_default_labels(self):
131
128
132
129
def test_log_empty_w_explicit (self ):
133
130
import datetime
131
+
134
132
from google .cloud .logging import Resource
135
133
136
134
ALT_LOG_NAME = "projects/foo/logs/alt.log.name"
@@ -237,6 +235,7 @@ def test_log_text_w_unicode_and_default_labels(self):
237
235
238
236
def test_log_text_explicit (self ):
239
237
import datetime
238
+
240
239
from google .cloud .logging import Resource
241
240
242
241
ALT_LOG_NAME = "projects/foo/logs/alt.log.name"
@@ -370,6 +369,7 @@ def test_log_struct_w_default_labels(self):
370
369
371
370
def test_log_struct_w_explicit (self ):
372
371
import datetime
372
+
373
373
from google .cloud .logging import Resource
374
374
375
375
ALT_LOG_NAME = "projects/foo/logs/alt.log.name"
@@ -533,10 +533,11 @@ def test_log_lowercase_severity(self):
533
533
)
534
534
535
535
def test_log_proto_defaults (self ):
536
+ import json
537
+
536
538
from google .cloud .logging_v2 .handlers ._monitored_resources import (
537
539
detect_resource ,
538
540
)
539
- import json
540
541
from google .protobuf .json_format import MessageToJson
541
542
from google .protobuf .struct_pb2 import Struct , Value
542
543
@@ -559,10 +560,11 @@ def test_log_proto_defaults(self):
559
560
)
560
561
561
562
def test_log_proto_w_default_labels (self ):
563
+ import json
564
+
562
565
from google .cloud .logging_v2 .handlers ._monitored_resources import (
563
566
detect_resource ,
564
567
)
565
- import json
566
568
from google .protobuf .json_format import MessageToJson
567
569
from google .protobuf .struct_pb2 import Struct , Value
568
570
@@ -587,12 +589,12 @@ def test_log_proto_w_default_labels(self):
587
589
)
588
590
589
591
def test_log_proto_w_explicit (self ):
590
- import json
591
592
import datetime
592
- from google .protobuf .json_format import MessageToJson
593
- from google .protobuf .struct_pb2 import Struct
594
- from google .protobuf .struct_pb2 import Value
593
+ import json
594
+
595
595
from google .cloud .logging import Resource
596
+ from google .protobuf .json_format import MessageToJson
597
+ from google .protobuf .struct_pb2 import Struct , Value
596
598
597
599
message = Struct (fields = {"foo" : Value (bool_value = True )})
598
600
ALT_LOG_NAME = "projects/foo/logs/alt.log.name"
@@ -720,11 +722,12 @@ def test_log_inference_struct(self):
720
722
721
723
def test_log_inference_proto (self ):
722
724
import json
723
- from google .protobuf .json_format import MessageToJson
724
- from google .protobuf .struct_pb2 import Struct , Value
725
+
725
726
from google .cloud .logging_v2 .handlers ._monitored_resources import (
726
727
detect_resource ,
727
728
)
729
+ from google .protobuf .json_format import MessageToJson
730
+ from google .protobuf .struct_pb2 import Struct , Value
728
731
729
732
message = Struct (fields = {"foo" : Value (bool_value = True )})
730
733
ENTRIES = [
@@ -809,8 +812,7 @@ def test_list_entries_defaults(self):
809
812
self .assertLess (yesterday - timestamp , timedelta (minutes = 1 ))
810
813
811
814
def test_list_entries_explicit (self ):
812
- from google .cloud .logging import DESCENDING
813
- from google .cloud .logging import Client
815
+ from google .cloud .logging import DESCENDING , Client
814
816
815
817
PROJECT1 = "PROJECT1"
816
818
PROJECT2 = "PROJECT2"
@@ -870,8 +872,7 @@ def test_list_entries_explicit(self):
870
872
self .assertLess (yesterday - timestamp , timedelta (minutes = 1 ))
871
873
872
874
def test_list_entries_explicit_timestamp (self ):
873
- from google .cloud .logging import DESCENDING
874
- from google .cloud .logging import Client
875
+ from google .cloud .logging import DESCENDING , Client
875
876
876
877
PROJECT1 = "PROJECT1"
877
878
PROJECT2 = "PROJECT2"
@@ -916,11 +917,13 @@ def test_list_entries_explicit_timestamp(self):
916
917
)
917
918
918
919
def test_list_entries_limit (self ):
919
- from google .cloud .logging import DESCENDING
920
- from google .cloud .logging import ProtobufEntry
921
- from google .cloud .logging import StructEntry
922
- from google .cloud .logging import Logger
923
- from google .cloud .logging import Client
920
+ from google .cloud .logging import (
921
+ DESCENDING ,
922
+ Client ,
923
+ Logger ,
924
+ ProtobufEntry ,
925
+ StructEntry ,
926
+ )
924
927
925
928
PROJECT1 = "PROJECT1"
926
929
PROJECT2 = "PROJECT2"
@@ -1010,8 +1013,7 @@ def test_list_entries_limit(self):
1010
1013
)
1011
1014
1012
1015
def test_list_entries_folder (self ):
1013
- from google .cloud .logging import TextEntry
1014
- from google .cloud .logging import Client
1016
+ from google .cloud .logging import Client , TextEntry
1015
1017
1016
1018
client = Client (
1017
1019
project = self .PROJECT , credentials = _make_credentials (), _use_grpc = False
@@ -1042,11 +1044,11 @@ def test_list_entries_folder(self):
1042
1044
self .assertEqual (entry .log_name , LOG_NAME )
1043
1045
1044
1046
def test_first_log_emits_instrumentation (self ):
1047
+ import google .cloud .logging_v2
1048
+ from google .cloud .logging_v2 ._instrumentation import _create_diagnostic_entry
1045
1049
from google .cloud .logging_v2 .handlers ._monitored_resources import (
1046
1050
detect_resource ,
1047
1051
)
1048
- from google .cloud .logging_v2 ._instrumentation import _create_diagnostic_entry
1049
- import google .cloud .logging_v2
1050
1052
1051
1053
google .cloud .logging_v2 ._instrumentation_emitted = False
1052
1054
DEFAULT_LABELS = {"foo" : "spam" }
@@ -1116,8 +1118,8 @@ def test_log_empty_defaults(self):
1116
1118
1117
1119
def test_log_empty_explicit (self ):
1118
1120
import datetime
1119
- from google . cloud . logging import Resource
1120
- from google .cloud .logging import LogEntry
1121
+
1122
+ from google .cloud .logging import LogEntry , Resource
1121
1123
1122
1124
LABELS = {"foo" : "bar" , "baz" : "qux" }
1123
1125
IID = "IID"
@@ -1161,8 +1163,8 @@ def test_log_empty_explicit(self):
1161
1163
self .assertEqual (batch .entries , [ENTRY ])
1162
1164
1163
1165
def test_log_text_defaults (self ):
1164
- from google .cloud .logging_v2 .entries import _GLOBAL_RESOURCE
1165
1166
from google .cloud .logging import TextEntry
1167
+ from google .cloud .logging_v2 .entries import _GLOBAL_RESOURCE
1166
1168
1167
1169
TEXT = "This is the entry text"
1168
1170
ENTRY = TextEntry (payload = TEXT , resource = _GLOBAL_RESOURCE )
@@ -1174,8 +1176,8 @@ def test_log_text_defaults(self):
1174
1176
1175
1177
def test_log_text_explicit (self ):
1176
1178
import datetime
1177
- from google . cloud . logging import Resource
1178
- from google .cloud .logging import TextEntry
1179
+
1180
+ from google .cloud .logging import Resource , TextEntry
1179
1181
1180
1182
TEXT = "This is the entry text"
1181
1183
LABELS = {"foo" : "bar" , "baz" : "qux" }
@@ -1222,8 +1224,8 @@ def test_log_text_explicit(self):
1222
1224
self .assertEqual (batch .entries , [ENTRY ])
1223
1225
1224
1226
def test_log_struct_defaults (self ):
1225
- from google .cloud .logging_v2 .entries import _GLOBAL_RESOURCE
1226
1227
from google .cloud .logging import StructEntry
1228
+ from google .cloud .logging_v2 .entries import _GLOBAL_RESOURCE
1227
1229
1228
1230
STRUCT = {"message" : "Message text" , "weather" : "partly cloudy" }
1229
1231
ENTRY = StructEntry (payload = STRUCT , resource = _GLOBAL_RESOURCE )
@@ -1235,8 +1237,8 @@ def test_log_struct_defaults(self):
1235
1237
1236
1238
def test_log_struct_explicit (self ):
1237
1239
import datetime
1238
- from google . cloud . logging import Resource
1239
- from google .cloud .logging import StructEntry
1240
+
1241
+ from google .cloud .logging import Resource , StructEntry
1240
1242
1241
1243
STRUCT = {"message" : "Message text" , "weather" : "partly cloudy" }
1242
1244
LABELS = {"foo" : "bar" , "baz" : "qux" }
@@ -1283,10 +1285,9 @@ def test_log_struct_explicit(self):
1283
1285
self .assertEqual (batch .entries , [ENTRY ])
1284
1286
1285
1287
def test_log_proto_defaults (self ):
1286
- from google .cloud .logging_v2 .entries import _GLOBAL_RESOURCE
1287
1288
from google .cloud .logging import ProtobufEntry
1288
- from google .protobuf . struct_pb2 import Struct
1289
- from google .protobuf .struct_pb2 import Value
1289
+ from google .cloud . logging_v2 . entries import _GLOBAL_RESOURCE
1290
+ from google .protobuf .struct_pb2 import Struct , Value
1290
1291
1291
1292
message = Struct (fields = {"foo" : Value (bool_value = True )})
1292
1293
ENTRY = ProtobufEntry (payload = message , resource = _GLOBAL_RESOURCE )
@@ -1298,10 +1299,9 @@ def test_log_proto_defaults(self):
1298
1299
1299
1300
def test_log_proto_explicit (self ):
1300
1301
import datetime
1301
- from google .cloud .logging import Resource
1302
- from google .cloud .logging import ProtobufEntry
1303
- from google .protobuf .struct_pb2 import Struct
1304
- from google .protobuf .struct_pb2 import Value
1302
+
1303
+ from google .cloud .logging import ProtobufEntry , Resource
1304
+ from google .protobuf .struct_pb2 import Struct , Value
1305
1305
1306
1306
message = Struct (fields = {"foo" : Value (bool_value = True )})
1307
1307
LABELS = {"foo" : "bar" , "baz" : "qux" }
@@ -1365,8 +1365,8 @@ def test_log_inference_text(self):
1365
1365
When calling batch.log with text input, it should
1366
1366
call batch.log_text
1367
1367
"""
1368
- from google .cloud .logging_v2 .entries import _GLOBAL_RESOURCE
1369
1368
from google .cloud .logging import TextEntry
1369
+ from google .cloud .logging_v2 .entries import _GLOBAL_RESOURCE
1370
1370
1371
1371
TEXT = "This is the entry text"
1372
1372
ENTRY = TextEntry (payload = TEXT , resource = _GLOBAL_RESOURCE )
@@ -1381,8 +1381,8 @@ def test_log_inference_struct(self):
1381
1381
When calling batch.struct with text input, it should
1382
1382
call batch.log_struct
1383
1383
"""
1384
- from google .cloud .logging_v2 .entries import _GLOBAL_RESOURCE
1385
1384
from google .cloud .logging import StructEntry
1385
+ from google .cloud .logging_v2 .entries import _GLOBAL_RESOURCE
1386
1386
1387
1387
STRUCT = {"message" : "Message text" , "weather" : "partly cloudy" }
1388
1388
ENTRY = StructEntry (payload = STRUCT , resource = _GLOBAL_RESOURCE )
@@ -1397,10 +1397,9 @@ def test_log_inference_proto(self):
1397
1397
When calling batch.log with proto input, it should
1398
1398
call batch.log_proto
1399
1399
"""
1400
- from google .cloud .logging_v2 .entries import _GLOBAL_RESOURCE
1401
1400
from google .cloud .logging import ProtobufEntry
1402
- from google .protobuf . struct_pb2 import Struct
1403
- from google .protobuf .struct_pb2 import Value
1401
+ from google .cloud . logging_v2 . entries import _GLOBAL_RESOURCE
1402
+ from google .protobuf .struct_pb2 import Struct , Value
1404
1403
1405
1404
message = Struct (fields = {"foo" : Value (bool_value = True )})
1406
1405
ENTRY = ProtobufEntry (payload = message , resource = _GLOBAL_RESOURCE )
@@ -1416,8 +1415,8 @@ def test_log_inference_struct_explicit(self):
1416
1415
call batch.log_struct, along with input arguments
1417
1416
"""
1418
1417
import datetime
1419
- from google . cloud . logging import Resource
1420
- from google .cloud .logging import StructEntry
1418
+
1419
+ from google .cloud .logging import Resource , StructEntry
1421
1420
1422
1421
STRUCT = {"message" : "Message text" , "weather" : "partly cloudy" }
1423
1422
LABELS = {"foo" : "bar" , "baz" : "qux" }
@@ -1464,15 +1463,15 @@ def test_log_inference_struct_explicit(self):
1464
1463
self .assertEqual (batch .entries , [ENTRY ])
1465
1464
1466
1465
def test_commit_w_unknown_entry_type (self ):
1467
- from google .cloud .logging_v2 .entries import _GLOBAL_RESOURCE
1468
1466
from google .cloud .logging import LogEntry
1467
+ from google .cloud .logging_v2 .entries import _GLOBAL_RESOURCE
1469
1468
1470
1469
logger = _Logger ()
1471
1470
client = _Client (project = self .PROJECT , connection = _make_credentials ())
1472
1471
api = client .logging_api = _DummyLoggingAPI ()
1473
1472
batch = self ._make_one (logger , client )
1474
1473
batch .entries .append (LogEntry (severity = "blah" ))
1475
- ENTRY = {"severity" : "blah " , "resource" : _GLOBAL_RESOURCE ._to_dict ()}
1474
+ ENTRY = {"severity" : "BLAH " , "resource" : _GLOBAL_RESOURCE ._to_dict ()}
1476
1475
1477
1476
batch .commit ()
1478
1477
@@ -1482,9 +1481,35 @@ def test_commit_w_unknown_entry_type(self):
1482
1481
([ENTRY ], logger .full_name , None , None , True ),
1483
1482
)
1484
1483
1485
- def test_commit_w_resource_specified (self ):
1484
+ def test_commit_w_lowercase_severity_type (self ):
1485
+ from google .cloud .logging import LogEntry
1486
1486
from google .cloud .logging_v2 .entries import _GLOBAL_RESOURCE
1487
+
1488
+ logger = _Logger ()
1489
+ client = _Client (project = self .PROJECT , connection = _make_credentials ())
1490
+ api = client .logging_api = _DummyLoggingAPI ()
1491
+ batch = self ._make_one (logger , client )
1492
+ batch .entries .append (LogEntry (severity = "info" ))
1493
+ batch .entries .append (LogEntry (severity = "warn" ))
1494
+ batch .entries .append (LogEntry (severity = "error" ))
1495
+ batch .entries .append (LogEntry (severity = "fatal" ))
1496
+ ENTRIES = [
1497
+ {"severity" : "INFO" , "resource" : _GLOBAL_RESOURCE ._to_dict ()},
1498
+ {"severity" : "WARN" , "resource" : _GLOBAL_RESOURCE ._to_dict ()},
1499
+ {"severity" : "ERROR" , "resource" : _GLOBAL_RESOURCE ._to_dict ()},
1500
+ {"severity" : "FATAL" , "resource" : _GLOBAL_RESOURCE ._to_dict ()},
1501
+ ]
1502
+
1503
+ batch .commit ()
1504
+ self .assertEqual (list (batch .entries ), [])
1505
+ self .assertEqual (
1506
+ api ._write_entries_called_with ,
1507
+ (ENTRIES , logger .full_name , None , None , True ),
1508
+ )
1509
+
1510
+ def test_commit_w_resource_specified (self ):
1487
1511
from google .cloud .logging import Resource
1512
+ from google .cloud .logging_v2 .entries import _GLOBAL_RESOURCE
1488
1513
1489
1514
logger = _Logger ()
1490
1515
client = _Client (project = self .PROJECT , connection = _make_credentials ())
@@ -1508,13 +1533,13 @@ def test_commit_w_resource_specified(self):
1508
1533
)
1509
1534
1510
1535
def test_commit_w_bound_client (self ):
1511
- import json
1512
1536
import datetime
1513
- from google .protobuf .json_format import MessageToJson
1514
- from google .protobuf .struct_pb2 import Struct
1515
- from google .protobuf .struct_pb2 import Value
1537
+ import json
1538
+
1516
1539
from google .cloud ._helpers import _datetime_to_rfc3339
1517
1540
from google .cloud .logging_v2 .entries import _GLOBAL_RESOURCE
1541
+ from google .protobuf .json_format import MessageToJson
1542
+ from google .protobuf .struct_pb2 import Struct , Value
1518
1543
1519
1544
TEXT = "This is the entry text"
1520
1545
STRUCT = {"message" : TEXT , "weather" : "partly cloudy" }
@@ -1599,11 +1624,11 @@ def test_commit_w_bound_client(self):
1599
1624
1600
1625
def test_commit_w_alternate_client (self ):
1601
1626
import json
1602
- from google .protobuf .json_format import MessageToJson
1603
- from google .protobuf .struct_pb2 import Struct
1604
- from google .protobuf .struct_pb2 import Value
1627
+
1605
1628
from google .cloud .logging import Logger
1606
1629
from google .cloud .logging_v2 .entries import _GLOBAL_RESOURCE
1630
+ from google .protobuf .json_format import MessageToJson
1631
+ from google .protobuf .struct_pb2 import Struct , Value
1607
1632
1608
1633
TEXT = "This is the entry text"
1609
1634
STRUCT = {"message" : TEXT , "weather" : "partly cloudy" }
@@ -1651,11 +1676,11 @@ def test_commit_w_alternate_client(self):
1651
1676
1652
1677
def test_context_mgr_success (self ):
1653
1678
import json
1654
- from google .protobuf .json_format import MessageToJson
1655
- from google .protobuf .struct_pb2 import Struct
1656
- from google .protobuf .struct_pb2 import Value
1679
+
1657
1680
from google .cloud .logging import Logger
1658
1681
from google .cloud .logging_v2 .entries import _GLOBAL_RESOURCE
1682
+ from google .protobuf .json_format import MessageToJson
1683
+ from google .protobuf .struct_pb2 import Struct , Value
1659
1684
1660
1685
TEXT = "This is the entry text"
1661
1686
STRUCT = {"message" : TEXT , "weather" : "partly cloudy" }
@@ -1702,11 +1727,9 @@ def test_context_mgr_success(self):
1702
1727
1703
1728
def test_context_mgr_failure (self ):
1704
1729
import datetime
1705
- from google .protobuf .struct_pb2 import Struct
1706
- from google .protobuf .struct_pb2 import Value
1707
- from google .cloud .logging import TextEntry
1708
- from google .cloud .logging import StructEntry
1709
- from google .cloud .logging import ProtobufEntry
1730
+
1731
+ from google .cloud .logging import ProtobufEntry , StructEntry , TextEntry
1732
+ from google .protobuf .struct_pb2 import Struct , Value
1710
1733
1711
1734
TEXT = "This is the entry text"
1712
1735
STRUCT = {"message" : TEXT , "weather" : "partly cloudy" }
@@ -1752,8 +1775,8 @@ def test_append_context_to_error(self):
1752
1775
exception should be unchanged
1753
1776
"""
1754
1777
from google .api_core .exceptions import InvalidArgument
1755
- from google .rpc .error_details_pb2 import DebugInfo
1756
1778
from google .cloud .logging import TextEntry
1779
+ from google .rpc .error_details_pb2 import DebugInfo
1757
1780
1758
1781
logger = _Logger ()
1759
1782
client = _Client (project = self .PROJECT )
@@ -1803,8 +1826,8 @@ def test_batch_error_gets_context(self):
1803
1826
_append_context_to_error is thrown
1804
1827
"""
1805
1828
from google .api_core .exceptions import InvalidArgument
1806
- from google .rpc .error_details_pb2 import DebugInfo
1807
1829
from google .cloud .logging import TextEntry
1830
+ from google .rpc .error_details_pb2 import DebugInfo
1808
1831
1809
1832
logger = _Logger ()
1810
1833
client = _Client (project = self .PROJECT )
0 commit comments