Skip to content

Commit 755d480

Browse files
committed
update hostcfgd test file
Signed-off-by: Arham-Nasir <arqamnasir719@gmail.com>
1 parent 759f3da commit 755d480

File tree

1 file changed

+203
-21
lines changed

1 file changed

+203
-21
lines changed

tests/hostcfgd/hostcfgd_test.py

Lines changed: 203 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -547,8 +547,191 @@ def test_banner_message(self, mock_run_cmd):
547547
import psutil
548548
# from hostcfgd import MemoryStatisticsCfg
549549

550+
# class MockConfigDb:
551+
# CONFIG_DB = {}
552+
553+
# class TestMemoryStatisticsCfgd(TestCase):
554+
# """Test MemoryStatisticsCfg functionalities."""
555+
556+
# def setUp(self):
557+
# # Initial configuration for Memory Statistics
558+
# MockConfigDb.CONFIG_DB['MEMORY_STATISTICS'] = {
559+
# 'enabled': 'false',
560+
# 'sampling_interval': '5',
561+
# 'retention_period': '15'
562+
# }
563+
# self.mem_stat_cfg = hostcfgd.MemoryStatisticsCfg(MockConfigDb.CONFIG_DB)
564+
565+
# def tearDown(self):
566+
# MockConfigDb.CONFIG_DB = {}
567+
568+
# # Test initial loading
569+
# def test_load_with_invalid_key(self):
570+
# """Test loading configuration with invalid key"""
571+
# config = {'invalid_key': 'value', 'enabled': 'true'}
572+
# with mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
573+
# self.mem_stat_cfg.load(config)
574+
# mock_syslog.assert_any_call(mock.ANY, "MemoryStatisticsCfg: Invalid key 'invalid_key' in initial configuration.")
575+
576+
# def test_load_with_empty_config(self):
577+
# """Test loading empty configuration"""
578+
# with mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
579+
# self.mem_stat_cfg.load(None)
580+
# mock_syslog.assert_any_call(mock.ANY, "MemoryStatisticsCfg: Loading initial configuration")
581+
582+
# # Test validation
583+
# def test_memory_statistics_update_invalid_key(self):
584+
# """Test update with invalid key"""
585+
# with mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
586+
# self.mem_stat_cfg.memory_statistics_update('invalid_key', 'value')
587+
# mock_syslog.assert_any_call(mock.ANY, "MemoryStatisticsCfg: Invalid key 'invalid_key' received.")
588+
589+
# def test_memory_statistics_update_invalid_numeric_value(self):
590+
# """Test update with invalid numeric value"""
591+
# with mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
592+
# self.mem_stat_cfg.memory_statistics_update('sampling_interval', '-1')
593+
# mock_syslog.assert_any_call(mock.ANY, "MemoryStatisticsCfg: Invalid value '-1' for key 'sampling_interval'. Must be a positive integer.")
594+
595+
# # Test daemon management
596+
# @mock.patch('hostcfgd.subprocess.Popen')
597+
# @mock.patch('hostcfgd.os.kill')
598+
# def test_restart_memory_statistics_success(self, mock_kill, mock_popen):
599+
# """Test successful daemon restart"""
600+
# with mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
601+
# with mock.patch.object(self.mem_stat_cfg, 'get_memory_statistics_pid', return_value=123):
602+
# self.mem_stat_cfg.restart_memory_statistics()
603+
# mock_kill.assert_called_with(123, signal.SIGTERM)
604+
# mock_popen.assert_called_once()
605+
606+
# @mock.patch('hostcfgd.subprocess.Popen')
607+
# def test_restart_memory_statistics_failure(self, mock_popen):
608+
# """Test failed daemon restart"""
609+
# mock_popen.side_effect = Exception("Failed to start")
610+
# with mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
611+
# self.mem_stat_cfg.restart_memory_statistics()
612+
# mock_syslog.assert_any_call(mock.ANY, "MemoryStatisticsCfg: Failed to start MemoryStatisticsDaemon: Failed to start")
613+
614+
# # Test PID management
615+
# def test_get_memory_statistics_pid_file_not_found(self):
616+
# """Test PID retrieval when file doesn't exist"""
617+
# with mock.patch('builtins.open', side_effect=FileNotFoundError):
618+
# with mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
619+
# pid = self.mem_stat_cfg.get_memory_statistics_pid()
620+
# self.assertIsNone(pid)
621+
# mock_syslog.assert_any_call(mock.ANY, "MemoryStatisticsCfg: PID file not found. Daemon might not be running.")
622+
623+
# def test_get_memory_statistics_pid_invalid_content(self):
624+
# """Test PID retrieval with invalid file content"""
625+
# mock_open = mock.mock_open(read_data="invalid")
626+
# with mock.patch('builtins.open', mock_open):
627+
# with mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
628+
# pid = self.mem_stat_cfg.get_memory_statistics_pid()
629+
# self.assertIsNone(pid)
630+
# mock_syslog.assert_any_call(mock.ANY, "MemoryStatisticsCfg: PID file contents invalid.")
631+
632+
# @mock.patch('hostcfgd.psutil.pid_exists', return_value=True)
633+
# @mock.patch('hostcfgd.psutil.Process')
634+
# def test_get_memory_statistics_pid_wrong_process(self, mock_process, mock_pid_exists):
635+
# """Test PID retrieval when process name doesn't match"""
636+
# mock_process_instance = mock.Mock()
637+
# mock_process_instance.name.return_value = "wrong_process"
638+
# mock_process.return_value = mock_process_instance
639+
640+
# mock_open = mock.mock_open(read_data="123")
641+
# with mock.patch('builtins.open', mock_open):
642+
# with mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
643+
# pid = self.mem_stat_cfg.get_memory_statistics_pid()
644+
# self.assertIsNone(pid)
645+
# mock_syslog.assert_any_call(mock.ANY, "MemoryStatisticsCfg: PID 123 does not correspond to memory_statistics_service.py.")
646+
647+
# # Test daemon shutdown
648+
# @mock.patch('hostcfgd.psutil.Process')
649+
# def test_wait_for_shutdown_timeout(self, mock_process):
650+
# """Test shutdown waiting timeout"""
651+
# mock_process_instance = mock.Mock()
652+
# mock_process_instance.wait.side_effect = psutil.TimeoutExpired(123, 10)
653+
# mock_process.return_value = mock_process_instance
654+
655+
# with mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
656+
# self.mem_stat_cfg.wait_for_shutdown(123)
657+
# mock_syslog.assert_any_call(mock.ANY, "MemoryStatisticsCfg: Timed out while waiting for daemon (PID 123) to shut down.")
658+
659+
# @mock.patch('hostcfgd.psutil.Process')
660+
# def test_wait_for_shutdown_no_process(self, mock_process):
661+
# """Test shutdown waiting when process doesn't exist"""
662+
# mock_process.side_effect = psutil.NoSuchProcess(123)
663+
664+
# with mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
665+
# self.mem_stat_cfg.wait_for_shutdown(123)
666+
# mock_syslog.assert_any_call(mock.ANY, "MemoryStatisticsCfg: MemoryStatisticsDaemon process not found.")
667+
668+
# # Test enable/disable functionality
669+
# def test_memory_statistics_enable(self):
670+
# """Test enabling memory statistics"""
671+
# with mock.patch.object(self.mem_stat_cfg, 'restart_memory_statistics') as mock_restart:
672+
# self.mem_stat_cfg.memory_statistics_update('enabled', 'true')
673+
# mock_restart.assert_called_once()
674+
# self.assertEqual(self.mem_stat_cfg.cache['enabled'], 'true')
675+
676+
# def test_memory_statistics_disable(self):
677+
# """Test disabling memory statistics"""
678+
# with mock.patch.object(self.mem_stat_cfg, 'shutdown_memory_statistics') as mock_shutdown:
679+
# self.mem_stat_cfg.memory_statistics_update('enabled', 'false')
680+
# mock_shutdown.assert_called_once()
681+
# self.assertEqual(self.mem_stat_cfg.cache['enabled'], 'false')
682+
683+
# # Additional test cases
684+
# def test_reload_memory_statistics(self):
685+
# """Test reloading memory statistics configuration"""
686+
# with mock.patch.object(self.mem_stat_cfg, 'get_memory_statistics_pid', return_value=123):
687+
# with mock.patch('hostcfgd.os.kill') as mock_kill:
688+
# self.mem_stat_cfg.reload_memory_statistics()
689+
# mock_kill.assert_called_with(123, signal.SIGHUP)
690+
691+
# def test_reload_memory_statistics_failure(self):
692+
# """Test failure in reloading memory statistics configuration"""
693+
# with mock.patch.object(self.mem_stat_cfg, 'get_memory_statistics_pid', return_value=123):
694+
# with mock.patch('hostcfgd.os.kill', side_effect=Exception("Failed to send SIGHUP")):
695+
# with mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
696+
# self.mem_stat_cfg.reload_memory_statistics()
697+
# mock_syslog.assert_any_call(mock.ANY, "MemoryStatisticsCfg: Failed to reload MemoryStatisticsDaemon: Failed to send SIGHUP")
698+
699+
# def test_shutdown_memory_statistics(self):
700+
# """Test shutting down memory statistics daemon"""
701+
# with mock.patch.object(self.mem_stat_cfg, 'get_memory_statistics_pid', return_value=123):
702+
# with mock.patch('hostcfgd.os.kill') as mock_kill:
703+
# with mock.patch.object(self.mem_stat_cfg, 'wait_for_shutdown') as mock_wait:
704+
# self.mem_stat_cfg.shutdown_memory_statistics()
705+
# mock_kill.assert_called_with(123, signal.SIGTERM)
706+
# mock_wait.assert_called_with(123)
707+
708+
# def test_shutdown_memory_statistics_failure(self):
709+
# """Test failure in shutting down memory statistics daemon"""
710+
# with mock.patch.object(self.mem_stat_cfg, 'get_memory_statistics_pid', return_value=123):
711+
# with mock.patch('hostcfgd.os.kill', side_effect=Exception("Failed to send SIGTERM")):
712+
# with mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
713+
# self.mem_stat_cfg.shutdown_memory_statistics()
714+
# mock_syslog.assert_any_call(mock.ANY, "MemoryStatisticsCfg: Failed to shutdown MemoryStatisticsDaemon: Failed to send SIGTERM")
715+
716+
717+
718+
from unittest import TestCase
719+
# import mock
720+
import signal
721+
import psutil
722+
import hostcfgd
723+
550724
class MockConfigDb:
551725
CONFIG_DB = {}
726+
727+
@classmethod
728+
def set_config_db(cls, config):
729+
"""Set the configuration database for testing.
730+
731+
Args:
732+
config: Dictionary containing configuration data
733+
"""
734+
cls.CONFIG_DB = config
552735

553736
class TestMemoryStatisticsCfgd(TestCase):
554737
"""Test MemoryStatisticsCfg functionalities."""
@@ -644,27 +827,6 @@ def test_get_memory_statistics_pid_wrong_process(self, mock_process, mock_pid_ex
644827
self.assertIsNone(pid)
645828
mock_syslog.assert_any_call(mock.ANY, "MemoryStatisticsCfg: PID 123 does not correspond to memory_statistics_service.py.")
646829

647-
# Test daemon shutdown
648-
@mock.patch('hostcfgd.psutil.Process')
649-
def test_wait_for_shutdown_timeout(self, mock_process):
650-
"""Test shutdown waiting timeout"""
651-
mock_process_instance = mock.Mock()
652-
mock_process_instance.wait.side_effect = psutil.TimeoutExpired(123, 10)
653-
mock_process.return_value = mock_process_instance
654-
655-
with mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
656-
self.mem_stat_cfg.wait_for_shutdown(123)
657-
mock_syslog.assert_any_call(mock.ANY, "MemoryStatisticsCfg: Timed out while waiting for daemon (PID 123) to shut down.")
658-
659-
@mock.patch('hostcfgd.psutil.Process')
660-
def test_wait_for_shutdown_no_process(self, mock_process):
661-
"""Test shutdown waiting when process doesn't exist"""
662-
mock_process.side_effect = psutil.NoSuchProcess(123)
663-
664-
with mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
665-
self.mem_stat_cfg.wait_for_shutdown(123)
666-
mock_syslog.assert_any_call(mock.ANY, "MemoryStatisticsCfg: MemoryStatisticsDaemon process not found.")
667-
668830
# Test enable/disable functionality
669831
def test_memory_statistics_enable(self):
670832
"""Test enabling memory statistics"""
@@ -712,3 +874,23 @@ def test_shutdown_memory_statistics_failure(self):
712874
with mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
713875
self.mem_stat_cfg.shutdown_memory_statistics()
714876
mock_syslog.assert_any_call(mock.ANY, "MemoryStatisticsCfg: Failed to shutdown MemoryStatisticsDaemon: Failed to send SIGTERM")
877+
878+
@mock.patch('hostcfgd.psutil.Process')
879+
def test_wait_for_shutdown_timeout(self, mock_process):
880+
"""Test shutdown waiting timeout"""
881+
mock_process_instance = mock.Mock()
882+
mock_process_instance.wait.side_effect = psutil.TimeoutExpired(123, 10)
883+
mock_process.return_value = mock_process_instance
884+
885+
with mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
886+
self.mem_stat_cfg.wait_for_shutdown(123)
887+
mock_syslog.assert_any_call(mock.ANY, "MemoryStatisticsCfg: Timed out while waiting for daemon (PID 123) to shut down.")
888+
889+
@mock.patch('hostcfgd.psutil.Process')
890+
def test_wait_for_shutdown_no_process(self, mock_process):
891+
"""Test shutdown waiting when process doesn't exist"""
892+
mock_process.side_effect = psutil.NoSuchProcess(123)
893+
894+
with mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
895+
self.mem_stat_cfg.wait_for_shutdown(123)
896+
mock_syslog.assert_any_call(mock.ANY, "MemoryStatisticsCfg: MemoryStatisticsDaemon process not found.")

0 commit comments

Comments
 (0)