Skip to content

Commit

Permalink
update test cases
Browse files Browse the repository at this point in the history
Signed-off-by: Arham-Nasir <arqamnasir719@gmail.com>
  • Loading branch information
Arham-Nasir committed Nov 17, 2024
1 parent 3d6c1be commit 4486515
Showing 1 changed file with 85 additions and 1 deletion.
86 changes: 85 additions & 1 deletion tests/hostcfgd/hostcfgd_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -896,4 +896,88 @@ def test_handler_functionality(self):
mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
daemon.memory_statistics_handler('enabled', None, 'true')
mock_syslog.assert_any_call(mock.ANY,
"MemoryStatisticsCfg: Error while handling memory statistics update: Handler error")
"MemoryStatisticsCfg: Error while handling memory statistics update: Handler error")

def test_apply_setting_functionality(self):
"""Test apply_setting method scenarios"""
# Test restart case when enabled is true
with mock.patch.object(self.mem_stat_cfg, 'restart_memory_statistics') as mock_restart, \
mock.patch.object(self.mem_stat_cfg, 'shutdown_memory_statistics') as mock_shutdown, \
mock.patch.object(self.mem_stat_cfg, 'reload_memory_statistics') as mock_reload:
self.mem_stat_cfg.cache['enabled'] = 'true'
self.mem_stat_cfg.apply_setting('sampling_interval', '10')
mock_restart.assert_called_once()
mock_shutdown.assert_not_called()
mock_reload.assert_not_called()

# Test shutdown case when enabled is false
with mock.patch.object(self.mem_stat_cfg, 'restart_memory_statistics') as mock_restart, \
mock.patch.object(self.mem_stat_cfg, 'shutdown_memory_statistics') as mock_shutdown, \
mock.patch.object(self.mem_stat_cfg, 'reload_memory_statistics') as mock_reload:
self.mem_stat_cfg.cache['enabled'] = 'false'
self.mem_stat_cfg.apply_setting('enabled', 'false')
mock_shutdown.assert_called_once()
mock_restart.assert_not_called()
mock_reload.assert_not_called()

# Test reload case for other settings when enabled
with mock.patch.object(self.mem_stat_cfg, 'restart_memory_statistics') as mock_restart, \
mock.patch.object(self.mem_stat_cfg, 'shutdown_memory_statistics') as mock_shutdown, \
mock.patch.object(self.mem_stat_cfg, 'reload_memory_statistics') as mock_reload:
self.mem_stat_cfg.cache['enabled'] = 'true'
self.mem_stat_cfg.apply_setting('retention_period', '20')
mock_reload.assert_called_once()
mock_restart.assert_not_called()
mock_shutdown.assert_not_called()

# Test exception handling
with mock.patch.object(self.mem_stat_cfg, 'reload_memory_statistics',
side_effect=Exception("Test error")), \
mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
self.mem_stat_cfg.apply_setting('retention_period', '20')
mock_syslog.assert_any_call(mock.ANY,
"MemoryStatisticsCfg: Exception in apply_setting() for key 'retention_period': Test error")

def test_reload_memory_statistics_error(self):
"""Test error handling in reload_memory_statistics"""
with mock.patch.object(self.mem_stat_cfg, 'get_memory_statistics_pid', return_value=123), \
mock.patch('hostcfgd.os.kill', side_effect=Exception("Reload failed")), \
mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
self.mem_stat_cfg.reload_memory_statistics()
mock_syslog.assert_any_call(mock.ANY,
"MemoryStatisticsCfg: Failed to reload MemoryStatisticsDaemon: Reload failed")

def test_shutdown_memory_statistics_error(self):
"""Test error handling in shutdown_memory_statistics"""
with mock.patch.object(self.mem_stat_cfg, 'get_memory_statistics_pid', return_value=123), \
mock.patch('hostcfgd.os.kill', side_effect=Exception("Shutdown failed")), \
mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
self.mem_stat_cfg.shutdown_memory_statistics()
mock_syslog.assert_any_call(mock.ANY,
"MemoryStatisticsCfg: Failed to shutdown MemoryStatisticsDaemon: Shutdown failed")

def test_wait_for_shutdown_general_error(self):
"""Test general exception handling in wait_for_shutdown"""
with mock.patch('hostcfgd.psutil.Process', side_effect=Exception("Unexpected error")), \
mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
self.mem_stat_cfg.wait_for_shutdown(123)
mock_syslog.assert_any_call(mock.ANY,
"MemoryStatisticsCfg: Exception in wait_for_shutdown(): Unexpected error")

def test_get_memory_statistics_pid_additional_scenarios(self):
"""Test additional scenarios for get_memory_statistics_pid"""
# Test non-existent PID
with mock.patch('builtins.open', mock.mock_open(read_data="123")), \
mock.patch('hostcfgd.psutil.pid_exists', return_value=False), \
mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
pid = self.mem_stat_cfg.get_memory_statistics_pid()
self.assertIsNone(pid)
mock_syslog.assert_any_call(mock.ANY, "MemoryStatisticsCfg: PID does not exist.")

# Test general exception
with mock.patch('builtins.open', side_effect=Exception("Unexpected error")), \
mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
pid = self.mem_stat_cfg.get_memory_statistics_pid()
self.assertIsNone(pid)
mock_syslog.assert_any_call(mock.ANY,
"MemoryStatisticsCfg: Exception failed to retrieve MemoryStatisticsDaemon PID: Unexpected error")

0 comments on commit 4486515

Please sign in to comment.