<s> import json <EOL> import logging <EOL> import os <EOL> import unittest <EOL> import mock <EOL> from vmruntime import cloud_logging <EOL> class CloudLoggingTestCase ( unittest . TestCase ) : <EOL> EXPECTED_TRACE_ID = None <EOL> EXPECTED_OVERRIDDEN_TRACE_ID = '<STR_LIT>' <EOL> EXPECTED_MESSAGE = '<STR_LIT>' <EOL> TEST_TIME = <NUM_LIT> <EOL> EXPECTED_SECONDS = <NUM_LIT> <EOL> EXPECTED_NANOS = <NUM_LIT> <EOL> @ mock . patch . object ( cloud_logging . CloudLoggingHandler , '<STR_LIT>' ) <EOL> def setUp ( self , mock_open ) : <EOL> self . handler = cloud_logging . CloudLoggingHandler ( ) <EOL> with mock . patch ( '<STR_LIT>' , return_value = self . TEST_TIME ) : <EOL> self . record = logging . makeLogRecord ( { '<STR_LIT>' : self . EXPECTED_MESSAGE , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . record_with_extra = logging . makeLogRecord ( <EOL> { '<STR_LIT>' : self . EXPECTED_MESSAGE , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . EXPECTED_OVERRIDDEN_TRACE_ID , } ) <EOL> def test_file_name_is_correct ( self ) : <EOL> self . assertTrue ( self . handler . baseFilename . startswith ( <EOL> '<STR_LIT>' ) ) <EOL> self . assertTrue ( self . handler . baseFilename . endswith ( '<STR_LIT>' ) ) <EOL> def test_format ( self ) : <EOL> msg = self . handler . format ( self . record ) <EOL> payload = json . loads ( msg ) <EOL> if self . EXPECTED_TRACE_ID : <EOL> self . assertEquals ( payload [ '<STR_LIT>' ] , self . EXPECTED_TRACE_ID ) <EOL> else : <EOL> self . assertNotIn ( '<STR_LIT>' , payload ) <EOL> def test_format_with_trace_id_as_extra ( self ) : <EOL> msg = self . handler . format ( self . record_with_extra ) <EOL> payload = json . loads ( msg ) <EOL> self . assertEquals ( payload [ '<STR_LIT>' ] , <EOL> self . EXPECTED_OVERRIDDEN_TRACE_ID ) <EOL> def test_format_timestamp ( self ) : <EOL> msg = self . handler . format ( self . record ) <EOL> payload = json . loads ( msg ) <EOL> self . assertEquals ( payload [ '<STR_LIT>' ] [ '<STR_LIT>' ] , <EOL> self . EXPECTED_SECONDS ) <EOL> self . assertEquals ( payload [ '<STR_LIT>' ] [ '<STR_LIT>' ] , self . EXPECTED_NANOS ) <EOL> class CloudLoggingTestCaseWithTraceIdEnv ( CloudLoggingTestCase ) : <EOL> EXPECTED_TRACE_ID = '<STR_LIT>' <EOL> def setUp ( self ) : <EOL> super ( CloudLoggingTestCaseWithTraceIdEnv , self ) . setUp ( ) <EOL> os . environ [ '<STR_LIT>' ] = '<STR_LIT>' . format ( <EOL> self . EXPECTED_TRACE_ID ) <EOL> def tearDown ( self ) : <EOL> os . unsetenv ( '<STR_LIT>' ) <EOL> class CloudLoggingTestCaseWithTraceIdEnvNoOptions ( CloudLoggingTestCase ) : <EOL> EXPECTED_TRACE_ID = '<STR_LIT>' <EOL> def setUp ( self ) : <EOL> super ( CloudLoggingTestCaseWithTraceIdEnvNoOptions , self ) . setUp ( ) <EOL> os . environ [ '<STR_LIT>' ] = self . EXPECTED_TRACE_ID <EOL> def tearDown ( self ) : <EOL> os . unsetenv ( '<STR_LIT>' ) </s>
