Skip to content

Commit d502eae

Browse files
committed
update test cases
Signed-off-by: Arham-Nasir <arqamnasir719@gmail.com>
1 parent 4486515 commit d502eae

File tree

1 file changed

+1
-85
lines changed

1 file changed

+1
-85
lines changed

tests/hostcfgd/hostcfgd_test.py

Lines changed: 1 addition & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -896,88 +896,4 @@ 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")
900-
901-
def test_apply_setting_functionality(self):
902-
"""Test apply_setting method scenarios"""
903-
# Test restart case when enabled is true
904-
with mock.patch.object(self.mem_stat_cfg, 'restart_memory_statistics') as mock_restart, \
905-
mock.patch.object(self.mem_stat_cfg, 'shutdown_memory_statistics') as mock_shutdown, \
906-
mock.patch.object(self.mem_stat_cfg, 'reload_memory_statistics') as mock_reload:
907-
self.mem_stat_cfg.cache['enabled'] = 'true'
908-
self.mem_stat_cfg.apply_setting('sampling_interval', '10')
909-
mock_restart.assert_called_once()
910-
mock_shutdown.assert_not_called()
911-
mock_reload.assert_not_called()
912-
913-
# Test shutdown case when enabled is false
914-
with mock.patch.object(self.mem_stat_cfg, 'restart_memory_statistics') as mock_restart, \
915-
mock.patch.object(self.mem_stat_cfg, 'shutdown_memory_statistics') as mock_shutdown, \
916-
mock.patch.object(self.mem_stat_cfg, 'reload_memory_statistics') as mock_reload:
917-
self.mem_stat_cfg.cache['enabled'] = 'false'
918-
self.mem_stat_cfg.apply_setting('enabled', 'false')
919-
mock_shutdown.assert_called_once()
920-
mock_restart.assert_not_called()
921-
mock_reload.assert_not_called()
922-
923-
# Test reload case for other settings when enabled
924-
with mock.patch.object(self.mem_stat_cfg, 'restart_memory_statistics') as mock_restart, \
925-
mock.patch.object(self.mem_stat_cfg, 'shutdown_memory_statistics') as mock_shutdown, \
926-
mock.patch.object(self.mem_stat_cfg, 'reload_memory_statistics') as mock_reload:
927-
self.mem_stat_cfg.cache['enabled'] = 'true'
928-
self.mem_stat_cfg.apply_setting('retention_period', '20')
929-
mock_reload.assert_called_once()
930-
mock_restart.assert_not_called()
931-
mock_shutdown.assert_not_called()
932-
933-
# Test exception handling
934-
with mock.patch.object(self.mem_stat_cfg, 'reload_memory_statistics',
935-
side_effect=Exception("Test error")), \
936-
mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
937-
self.mem_stat_cfg.apply_setting('retention_period', '20')
938-
mock_syslog.assert_any_call(mock.ANY,
939-
"MemoryStatisticsCfg: Exception in apply_setting() for key 'retention_period': Test error")
940-
941-
def test_reload_memory_statistics_error(self):
942-
"""Test error handling in reload_memory_statistics"""
943-
with mock.patch.object(self.mem_stat_cfg, 'get_memory_statistics_pid', return_value=123), \
944-
mock.patch('hostcfgd.os.kill', side_effect=Exception("Reload failed")), \
945-
mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
946-
self.mem_stat_cfg.reload_memory_statistics()
947-
mock_syslog.assert_any_call(mock.ANY,
948-
"MemoryStatisticsCfg: Failed to reload MemoryStatisticsDaemon: Reload failed")
949-
950-
def test_shutdown_memory_statistics_error(self):
951-
"""Test error handling in shutdown_memory_statistics"""
952-
with mock.patch.object(self.mem_stat_cfg, 'get_memory_statistics_pid', return_value=123), \
953-
mock.patch('hostcfgd.os.kill', side_effect=Exception("Shutdown failed")), \
954-
mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
955-
self.mem_stat_cfg.shutdown_memory_statistics()
956-
mock_syslog.assert_any_call(mock.ANY,
957-
"MemoryStatisticsCfg: Failed to shutdown MemoryStatisticsDaemon: Shutdown failed")
958-
959-
def test_wait_for_shutdown_general_error(self):
960-
"""Test general exception handling in wait_for_shutdown"""
961-
with mock.patch('hostcfgd.psutil.Process', side_effect=Exception("Unexpected error")), \
962-
mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
963-
self.mem_stat_cfg.wait_for_shutdown(123)
964-
mock_syslog.assert_any_call(mock.ANY,
965-
"MemoryStatisticsCfg: Exception in wait_for_shutdown(): Unexpected error")
966-
967-
def test_get_memory_statistics_pid_additional_scenarios(self):
968-
"""Test additional scenarios for get_memory_statistics_pid"""
969-
# Test non-existent PID
970-
with mock.patch('builtins.open', mock.mock_open(read_data="123")), \
971-
mock.patch('hostcfgd.psutil.pid_exists', return_value=False), \
972-
mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
973-
pid = self.mem_stat_cfg.get_memory_statistics_pid()
974-
self.assertIsNone(pid)
975-
mock_syslog.assert_any_call(mock.ANY, "MemoryStatisticsCfg: PID does not exist.")
976-
977-
# Test general exception
978-
with mock.patch('builtins.open', side_effect=Exception("Unexpected error")), \
979-
mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
980-
pid = self.mem_stat_cfg.get_memory_statistics_pid()
981-
self.assertIsNone(pid)
982-
mock_syslog.assert_any_call(mock.ANY,
983-
"MemoryStatisticsCfg: Exception failed to retrieve MemoryStatisticsDaemon PID: Unexpected error")
899+
"MemoryStatisticsCfg: Error while handling memory statistics update: Handler error")

0 commit comments

Comments
 (0)