@@ -61,7 +61,7 @@ def set_up(
6161 is_diagnostics_enabled ,
6262 subscription_id_env_var = TEST_SUBSCRIPTION_ID_PLUS ,
6363) -> None :
64- diagnostic_logger ._logger .handlers .clear ()
64+ diagnostic_logger ._diagnostic_file_logger .handlers .clear ()
6565 patch .dict (
6666 "os.environ" ,
6767 {
@@ -165,3 +165,86 @@ def test_subscription_id_no_plus(self, temp_file_path):
165165 diagnostic_logger .AzureDiagnosticLogging .info (MESSAGE1 , "4200" )
166166 diagnostic_logger .AzureDiagnosticLogging .info (MESSAGE2 , "4301" )
167167 check_file_for_messages (temp_file_path , "INFO" , ((MESSAGE1 , "4200" ), (MESSAGE2 , "4301" )))
168+
169+ def test_initialize_file_handler_exception (self , temp_file_path ):
170+ """Test that initialization fails gracefully when FileHandler creation raises an exception."""
171+ set_up (temp_file_path , is_diagnostics_enabled = True )
172+ # Mock FileHandler to raise an exception
173+ with patch ("azure.monitor.opentelemetry._diagnostics.diagnostic_logging.logging.FileHandler" ) as mock_file_handler , \
174+ patch ("azure.monitor.opentelemetry._diagnostics.diagnostic_logging._logger" ) as mock_logger :
175+ mock_file_handler .side_effect = OSError ("Permission denied" )
176+ # Attempt to log, which will trigger initialization
177+ diagnostic_logger .AzureDiagnosticLogging .info (MESSAGE1 , "4200" )
178+ # Verify that initialization failed
179+ assert diagnostic_logger .AzureDiagnosticLogging ._initialized is False
180+ check_file_is_empty (temp_file_path )
181+ # Verify that the error was logged
182+ mock_logger .error .assert_called_once ()
183+
184+ def test_initialize_makedirs_exception_not_file_exists (self , temp_file_path ):
185+ """Test that initialization fails gracefully when makedirs raises a non-FileExistsError exception."""
186+ set_up (temp_file_path , is_diagnostics_enabled = True )
187+ # Mock makedirs to raise a PermissionError
188+ with patch ("azure.monitor.opentelemetry._diagnostics.diagnostic_logging.makedirs" ) as mock_makedirs , \
189+ patch ("azure.monitor.opentelemetry._diagnostics.diagnostic_logging.exists" , return_value = False ), \
190+ patch ("azure.monitor.opentelemetry._diagnostics.diagnostic_logging._logger" ) as mock_logger :
191+ mock_makedirs .side_effect = PermissionError ("Permission denied" )
192+ # Attempt to log, which will trigger initialization
193+ diagnostic_logger .AzureDiagnosticLogging .info (MESSAGE1 , "4200" )
194+ # Verify that initialization failed
195+ assert diagnostic_logger .AzureDiagnosticLogging ._initialized is False
196+ check_file_is_empty (temp_file_path )
197+ # Verify that the error was logged
198+ mock_logger .error .assert_called_once ()
199+
200+ def test_initialize_makedirs_file_exists_error_handled (self , temp_file_path ):
201+ """Test that FileExistsError from makedirs is handled gracefully and initialization continues."""
202+ set_up (temp_file_path , is_diagnostics_enabled = True )
203+ # Mock makedirs to raise FileExistsError (this should be handled gracefully)
204+ with patch ("azure.monitor.opentelemetry._diagnostics.diagnostic_logging.makedirs" ) as mock_makedirs , \
205+ patch ("azure.monitor.opentelemetry._diagnostics.diagnostic_logging.exists" , return_value = False ), \
206+ patch ("azure.monitor.opentelemetry._diagnostics.diagnostic_logging._logger" ) as mock_logger :
207+ mock_makedirs .side_effect = FileExistsError ("Directory already exists" )
208+ # Attempt to log, which will trigger initialization
209+ diagnostic_logger .AzureDiagnosticLogging .info (MESSAGE1 , "4200" )
210+ # Verify that initialization succeeded despite FileExistsError
211+ assert diagnostic_logger .AzureDiagnosticLogging ._initialized is True
212+ check_file_for_messages (temp_file_path , "INFO" , ((MESSAGE1 , "4200" ),))
213+
214+ def test_initialize_formatter_exception (self , temp_file_path ):
215+ """Test that initialization fails gracefully when Formatter creation raises an exception."""
216+ set_up (temp_file_path , is_diagnostics_enabled = True )
217+ # Mock Formatter to raise an exception
218+ with patch ("azure.monitor.opentelemetry._diagnostics.diagnostic_logging.logging.Formatter" ) as mock_formatter , \
219+ patch ("azure.monitor.opentelemetry._diagnostics.diagnostic_logging._logger" ) as mock_logger :
220+ mock_formatter .side_effect = ValueError ("Invalid format string" )
221+ # Attempt to log, which will trigger initialization
222+ diagnostic_logger .AzureDiagnosticLogging .info (MESSAGE1 , "4200" )
223+ # Verify that initialization failed
224+ assert diagnostic_logger .AzureDiagnosticLogging ._initialized is False
225+ # Verify that the error was logged
226+ mock_logger .error .assert_called_once ()
227+
228+ def test_singleton_pattern (self , temp_file_path ):
229+ """Test that AzureDiagnosticLogging follows the singleton pattern."""
230+ set_up (temp_file_path , is_diagnostics_enabled = True )
231+
232+ # Create multiple instances
233+ instance1 = diagnostic_logger .AzureDiagnosticLogging ()
234+ instance2 = diagnostic_logger .AzureDiagnosticLogging ()
235+ instance3 = diagnostic_logger .AzureDiagnosticLogging ()
236+
237+ # Verify all instances are the same object
238+ assert instance1 is instance2
239+ assert instance2 is instance3
240+ assert instance1 is instance3
241+
242+ # Verify they all have the same id (memory address)
243+ assert id (instance1 ) == id (instance2 ) == id (instance3 )
244+
245+ # Verify class-level access still works
246+ diagnostic_logger .AzureDiagnosticLogging .info (MESSAGE1 , "4200" )
247+ assert diagnostic_logger .AzureDiagnosticLogging ._initialized is True
248+
249+ # Verify instance methods work (if they exist)
250+ # Since this is primarily a class-based API, we just verify the singleton behavior
0 commit comments