1# Copyright 2016 Google LLC All Rights Reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15import unittest 16 17 18class TestContainerEngineHandler(unittest.TestCase): 19 PROJECT = "PROJECT" 20 21 def _get_target_class(self): 22 from google.cloud.logging.handlers import ContainerEngineHandler 23 24 return ContainerEngineHandler 25 26 def _make_one(self, *args, **kw): 27 return self._get_target_class()(*args, **kw) 28 29 def test_ctor_defaults(self): 30 handler = self._make_one() 31 self.assertIsNone(handler.name) 32 33 def test_ctor_w_name(self): 34 handler = self._make_one(name="foo") 35 self.assertEqual(handler.name, "foo") 36 37 def test_format(self): 38 import logging 39 import json 40 41 handler = self._make_one() 42 logname = "loggername" 43 message = "hello world,嗨 世界" 44 record = logging.LogRecord( 45 logname, logging.INFO, None, None, message, None, None 46 ) 47 record.created = 5.03 48 expected_payload = { 49 "message": message, 50 "timestamp": {"seconds": 5, "nanos": int(0.03 * 1e9)}, 51 "thread": record.thread, 52 "severity": record.levelname, 53 } 54 payload = handler.format(record) 55 56 self.assertEqual(payload, json.dumps(expected_payload, ensure_ascii=False)) 57