@@ -656,18 +656,6 @@ def test_get_memory_statistics_pid_nonexistent(self, mock_pid_exists):
656656 self .assertIsNone (pid )
657657 mock_syslog .assert_any_call (mock .ANY , "MemoryStatisticsCfg: PID does not exist." )
658658
659- # @mock.patch('hostcfgd.psutil.Process')
660- # def test_get_memory_statistics_pid_exception(self, mock_process):
661- # """Test general exception handling in get_memory_statistics_pid"""
662- # mock_process.side_effect = Exception("Unexpected error")
663- # mock_open = mock.mock_open(read_data="123")
664- # with mock.patch('builtins.open', mock_open):
665- # with mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
666- # pid = self.mem_stat_cfg.get_memory_statistics_pid()
667- # self.assertIsNone(pid)
668- # mock_syslog.assert_any_call(mock.ANY,
669- # "MemoryStatisticsCfg: Exception failed to retrieve MemoryStatisticsDaemon PID: Unexpected error")
670-
671659 @mock .patch ('hostcfgd.psutil.Process' )
672660 def test_get_memory_statistics_pid_exception (self , mock_process ):
673661 """Test general exception handling in get_memory_statistics_pid"""
@@ -706,4 +694,54 @@ def test_wait_for_shutdown_general_exception(self, mock_process):
706694 with mock .patch ('hostcfgd.syslog.syslog' ) as mock_syslog :
707695 self .mem_stat_cfg .wait_for_shutdown (123 )
708696 mock_syslog .assert_any_call (mock .ANY ,
709- "MemoryStatisticsCfg: Exception in wait_for_shutdown(): Unexpected shutdown error" )
697+ "MemoryStatisticsCfg: Exception in wait_for_shutdown(): Unexpected shutdown error" )
698+
699+ def test_memory_statistics_disable (self ):
700+ """Test disabling memory statistics"""
701+ # First set the initial state to enabled
702+ self .mem_stat_cfg .cache ['enabled' ] = 'true'
703+
704+ # Mock the apply_setting method to avoid actual system calls
705+ with mock .patch .object (self .mem_stat_cfg , 'apply_setting' ) as mock_apply :
706+ self .mem_stat_cfg .memory_statistics_update ('enabled' , 'false' )
707+
708+ # Verify apply_setting was called with correct parameters
709+ mock_apply .assert_called_once_with ('enabled' , 'false' )
710+
711+ # Verify the cache was updated
712+ self .assertEqual (self .mem_stat_cfg .cache ['enabled' ], 'false' )
713+
714+ def test_memory_statistics_disable_with_shutdown (self ):
715+ """Test disabling memory statistics with full shutdown chain"""
716+ # First set the initial state to enabled
717+ self .mem_stat_cfg .cache ['enabled' ] = 'true'
718+
719+ # Mock both get_memory_statistics_pid and os.kill to simulate full shutdown
720+ with mock .patch .object (self .mem_stat_cfg , 'get_memory_statistics_pid' , return_value = 123 ) as mock_get_pid , \
721+ mock .patch ('hostcfgd.os.kill' ) as mock_kill , \
722+ mock .patch .object (self .mem_stat_cfg , 'wait_for_shutdown' ) as mock_wait :
723+
724+ self .mem_stat_cfg .memory_statistics_update ('enabled' , 'false' )
725+
726+ # Verify the shutdown sequence
727+ mock_get_pid .assert_called_once ()
728+ mock_kill .assert_called_once_with (123 , signal .SIGTERM )
729+ mock_wait .assert_called_once_with (123 )
730+
731+ # Verify the cache was updated
732+ self .assertEqual (self .mem_stat_cfg .cache ['enabled' ], 'false' )
733+
734+ def test_memory_statistics_disable_no_running_daemon (self ):
735+ """Test disabling memory statistics when daemon is not running"""
736+ # First set the initial state to enabled
737+ self .mem_stat_cfg .cache ['enabled' ] = 'true'
738+
739+ # Mock get_memory_statistics_pid to return None (no running daemon)
740+ with mock .patch .object (self .mem_stat_cfg , 'get_memory_statistics_pid' , return_value = None ) as mock_get_pid :
741+ self .mem_stat_cfg .memory_statistics_update ('enabled' , 'false' )
742+
743+ # Verify get_pid was called
744+ mock_get_pid .assert_called_once ()
745+
746+ # Verify the cache was updated despite no running daemon
747+ self .assertEqual (self .mem_stat_cfg .cache ['enabled' ], 'false' )
0 commit comments