@@ -896,4 +896,83 @@ def test_handler_functionality(self):
896896 mock .patch ('hostcfgd.syslog.syslog' ) as mock_syslog :
897897 daemon .memory_statistics_handler ('enabled' , None , 'true' )
898898 mock_syslog .assert_any_call (mock .ANY ,
899- "MemoryStatisticsCfg: Error while handling memory statistics update: Handler error" )
899+ "MemoryStatisticsCfg: Error while handling memory statistics update: Handler error" )
900+
901+
902+
903+
904+ def test_reload_memory_statistics (self ):
905+ """Test reloading the daemon configuration via SIGHUP."""
906+ with mock .patch ('hostcfgd.os.kill' ) as mock_kill , \
907+ mock .patch ('hostcfgd.syslog.syslog' ) as mock_syslog , \
908+ mock .patch .object (self .mem_stat_cfg , 'get_memory_statistics_pid' , return_value = 123 ):
909+ # Successful reload
910+ self .mem_stat_cfg .reload_memory_statistics ()
911+ mock_kill .assert_called_with (123 , signal .SIGHUP )
912+ mock_syslog .assert_any_call (mock .ANY , "MemoryStatisticsCfg: Sent SIGHUP to reload daemon configuration" )
913+
914+ # Reload failure
915+ mock_kill .side_effect = Exception ("Reload error" )
916+ self .mem_stat_cfg .reload_memory_statistics ()
917+ mock_syslog .assert_any_call (mock .ANY , "MemoryStatisticsCfg: Failed to reload MemoryStatisticsDaemon: Reload error" )
918+
919+ def test_restart_memory_statistics_exceptions (self ):
920+ """Test restarting the daemon with exceptions."""
921+ # Restart failure after shutdown
922+ with mock .patch ('hostcfgd.os.kill' ) as mock_kill , \
923+ mock .patch ('hostcfgd.subprocess.Popen' , side_effect = Exception ("Start error" )), \
924+ mock .patch .object (self .mem_stat_cfg , 'get_memory_statistics_pid' , return_value = 123 ), \
925+ mock .patch ('hostcfgd.syslog.syslog' ) as mock_syslog :
926+ self .mem_stat_cfg .restart_memory_statistics ()
927+ mock_kill .assert_called_with (123 , signal .SIGTERM )
928+ mock_syslog .assert_any_call (mock .ANY , "MemoryStatisticsCfg: Failed to start MemoryStatisticsDaemon: Start error" )
929+
930+ def test_shutdown_memory_statistics_exceptions (self ):
931+ """Test shutdown with various exceptions."""
932+ with mock .patch ('hostcfgd.os.kill' ) as mock_kill , \
933+ mock .patch ('hostcfgd.syslog.syslog' ) as mock_syslog , \
934+ mock .patch .object (self .mem_stat_cfg , 'get_memory_statistics_pid' , return_value = 123 ):
935+ # SIGTERM failure
936+ mock_kill .side_effect = Exception ("Shutdown error" )
937+ self .mem_stat_cfg .shutdown_memory_statistics ()
938+ mock_syslog .assert_any_call (mock .ANY , "MemoryStatisticsCfg: Failed to shutdown MemoryStatisticsDaemon: Shutdown error" )
939+
940+ # No running PID
941+ with mock .patch .object (self .mem_stat_cfg , 'get_memory_statistics_pid' , return_value = None ):
942+ self .mem_stat_cfg .shutdown_memory_statistics ()
943+ mock_kill .assert_not_called ()
944+
945+ def test_wait_for_shutdown_exceptions (self ):
946+ """Test waiting for daemon shutdown with exceptions."""
947+ mock_process = mock .Mock ()
948+ with mock .patch ('hostcfgd.psutil.Process' , return_value = mock_process ), \
949+ mock .patch ('hostcfgd.syslog.syslog' ) as mock_syslog :
950+ # Generic exception
951+ mock_process .wait .side_effect = Exception ("Wait error" )
952+ self .mem_stat_cfg .wait_for_shutdown (123 )
953+ mock_syslog .assert_any_call (mock .ANY , "MemoryStatisticsCfg: Exception in wait_for_shutdown(): Wait error" )
954+
955+ def test_apply_setting_exceptions (self ):
956+ """Test error handling in apply_setting."""
957+ with mock .patch .object (self .mem_stat_cfg , 'restart_memory_statistics' , side_effect = Exception ("Restart error" )), \
958+ mock .patch ('hostcfgd.syslog.syslog' ) as mock_syslog :
959+ self .mem_stat_cfg .apply_setting ('enabled' , 'true' )
960+ mock_syslog .assert_any_call (mock .ANY , "MemoryStatisticsCfg: Exception in apply_setting() for key 'enabled': Restart error" )
961+
962+ def test_get_memory_statistics_pid_exceptions (self ):
963+ """Test exception handling in PID retrieval."""
964+ # Generic exception during PID file read
965+ with mock .patch ('builtins.open' , side_effect = Exception ("PID file error" )), \
966+ mock .patch ('hostcfgd.syslog.syslog' ) as mock_syslog :
967+ pid = self .mem_stat_cfg .get_memory_statistics_pid ()
968+ self .assertIsNone (pid )
969+ mock_syslog .assert_any_call (mock .ANY , "MemoryStatisticsCfg: Exception failed to retrieve MemoryStatisticsDaemon PID: PID file error" )
970+
971+ def test_memory_statistics_update_reload_logic (self ):
972+ """Test logic path for triggering reload instead of restart."""
973+ with mock .patch .object (self .mem_stat_cfg , 'reload_memory_statistics' ) as mock_reload , \
974+ mock .patch .object (self .mem_stat_cfg , 'restart_memory_statistics' ) as mock_restart :
975+ self .mem_stat_cfg .cache ['sampling_interval' ] = '10'
976+ self .mem_stat_cfg .memory_statistics_update ('sampling_interval' , '10' )
977+ mock_reload .assert_called_once ()
978+ mock_restart .assert_not_called ()
0 commit comments