Skip to content

Commit

Permalink
update hostcfgd test file
Browse files Browse the repository at this point in the history
Signed-off-by: Arham-Nasir <[email protected]>
  • Loading branch information
Arham-Nasir committed Nov 16, 2024
1 parent 759f3da commit 755d480
Showing 1 changed file with 203 additions and 21 deletions.
224 changes: 203 additions & 21 deletions tests/hostcfgd/hostcfgd_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,8 +547,191 @@ def test_banner_message(self, mock_run_cmd):
import psutil
# from hostcfgd import MemoryStatisticsCfg

# class MockConfigDb:
# CONFIG_DB = {}

# class TestMemoryStatisticsCfgd(TestCase):
# """Test MemoryStatisticsCfg functionalities."""

# def setUp(self):
# # Initial configuration for Memory Statistics
# MockConfigDb.CONFIG_DB['MEMORY_STATISTICS'] = {
# 'enabled': 'false',
# 'sampling_interval': '5',
# 'retention_period': '15'
# }
# self.mem_stat_cfg = hostcfgd.MemoryStatisticsCfg(MockConfigDb.CONFIG_DB)

# def tearDown(self):
# MockConfigDb.CONFIG_DB = {}

# # Test initial loading
# def test_load_with_invalid_key(self):
# """Test loading configuration with invalid key"""
# config = {'invalid_key': 'value', 'enabled': 'true'}
# with mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
# self.mem_stat_cfg.load(config)
# mock_syslog.assert_any_call(mock.ANY, "MemoryStatisticsCfg: Invalid key 'invalid_key' in initial configuration.")

# def test_load_with_empty_config(self):
# """Test loading empty configuration"""
# with mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
# self.mem_stat_cfg.load(None)
# mock_syslog.assert_any_call(mock.ANY, "MemoryStatisticsCfg: Loading initial configuration")

# # Test validation
# def test_memory_statistics_update_invalid_key(self):
# """Test update with invalid key"""
# with mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
# self.mem_stat_cfg.memory_statistics_update('invalid_key', 'value')
# mock_syslog.assert_any_call(mock.ANY, "MemoryStatisticsCfg: Invalid key 'invalid_key' received.")

# def test_memory_statistics_update_invalid_numeric_value(self):
# """Test update with invalid numeric value"""
# with mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
# self.mem_stat_cfg.memory_statistics_update('sampling_interval', '-1')
# mock_syslog.assert_any_call(mock.ANY, "MemoryStatisticsCfg: Invalid value '-1' for key 'sampling_interval'. Must be a positive integer.")

# # Test daemon management
# @mock.patch('hostcfgd.subprocess.Popen')
# @mock.patch('hostcfgd.os.kill')
# def test_restart_memory_statistics_success(self, mock_kill, mock_popen):
# """Test successful daemon restart"""
# with mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
# with mock.patch.object(self.mem_stat_cfg, 'get_memory_statistics_pid', return_value=123):
# self.mem_stat_cfg.restart_memory_statistics()
# mock_kill.assert_called_with(123, signal.SIGTERM)
# mock_popen.assert_called_once()

# @mock.patch('hostcfgd.subprocess.Popen')
# def test_restart_memory_statistics_failure(self, mock_popen):
# """Test failed daemon restart"""
# mock_popen.side_effect = Exception("Failed to start")
# with mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
# self.mem_stat_cfg.restart_memory_statistics()
# mock_syslog.assert_any_call(mock.ANY, "MemoryStatisticsCfg: Failed to start MemoryStatisticsDaemon: Failed to start")

# # Test PID management
# def test_get_memory_statistics_pid_file_not_found(self):
# """Test PID retrieval when file doesn't exist"""
# with mock.patch('builtins.open', side_effect=FileNotFoundError):
# with 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 file not found. Daemon might not be running.")

# def test_get_memory_statistics_pid_invalid_content(self):
# """Test PID retrieval with invalid file content"""
# mock_open = mock.mock_open(read_data="invalid")
# with mock.patch('builtins.open', mock_open):
# with 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 file contents invalid.")

# @mock.patch('hostcfgd.psutil.pid_exists', return_value=True)
# @mock.patch('hostcfgd.psutil.Process')
# def test_get_memory_statistics_pid_wrong_process(self, mock_process, mock_pid_exists):
# """Test PID retrieval when process name doesn't match"""
# mock_process_instance = mock.Mock()
# mock_process_instance.name.return_value = "wrong_process"
# mock_process.return_value = mock_process_instance

# mock_open = mock.mock_open(read_data="123")
# with mock.patch('builtins.open', mock_open):
# with 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 123 does not correspond to memory_statistics_service.py.")

# # Test daemon shutdown
# @mock.patch('hostcfgd.psutil.Process')
# def test_wait_for_shutdown_timeout(self, mock_process):
# """Test shutdown waiting timeout"""
# mock_process_instance = mock.Mock()
# mock_process_instance.wait.side_effect = psutil.TimeoutExpired(123, 10)
# mock_process.return_value = mock_process_instance

# with mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
# self.mem_stat_cfg.wait_for_shutdown(123)
# mock_syslog.assert_any_call(mock.ANY, "MemoryStatisticsCfg: Timed out while waiting for daemon (PID 123) to shut down.")

# @mock.patch('hostcfgd.psutil.Process')
# def test_wait_for_shutdown_no_process(self, mock_process):
# """Test shutdown waiting when process doesn't exist"""
# mock_process.side_effect = psutil.NoSuchProcess(123)

# with mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
# self.mem_stat_cfg.wait_for_shutdown(123)
# mock_syslog.assert_any_call(mock.ANY, "MemoryStatisticsCfg: MemoryStatisticsDaemon process not found.")

# # Test enable/disable functionality
# def test_memory_statistics_enable(self):
# """Test enabling memory statistics"""
# with mock.patch.object(self.mem_stat_cfg, 'restart_memory_statistics') as mock_restart:
# self.mem_stat_cfg.memory_statistics_update('enabled', 'true')
# mock_restart.assert_called_once()
# self.assertEqual(self.mem_stat_cfg.cache['enabled'], 'true')

# def test_memory_statistics_disable(self):
# """Test disabling memory statistics"""
# with mock.patch.object(self.mem_stat_cfg, 'shutdown_memory_statistics') as mock_shutdown:
# self.mem_stat_cfg.memory_statistics_update('enabled', 'false')
# mock_shutdown.assert_called_once()
# self.assertEqual(self.mem_stat_cfg.cache['enabled'], 'false')

# # Additional test cases
# def test_reload_memory_statistics(self):
# """Test reloading memory statistics configuration"""
# with mock.patch.object(self.mem_stat_cfg, 'get_memory_statistics_pid', return_value=123):
# with mock.patch('hostcfgd.os.kill') as mock_kill:
# self.mem_stat_cfg.reload_memory_statistics()
# mock_kill.assert_called_with(123, signal.SIGHUP)

# def test_reload_memory_statistics_failure(self):
# """Test failure in reloading memory statistics configuration"""
# with mock.patch.object(self.mem_stat_cfg, 'get_memory_statistics_pid', return_value=123):
# with mock.patch('hostcfgd.os.kill', side_effect=Exception("Failed to send SIGHUP")):
# with 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: Failed to send SIGHUP")

# def test_shutdown_memory_statistics(self):
# """Test shutting down memory statistics daemon"""
# with mock.patch.object(self.mem_stat_cfg, 'get_memory_statistics_pid', return_value=123):
# with mock.patch('hostcfgd.os.kill') as mock_kill:
# with mock.patch.object(self.mem_stat_cfg, 'wait_for_shutdown') as mock_wait:
# self.mem_stat_cfg.shutdown_memory_statistics()
# mock_kill.assert_called_with(123, signal.SIGTERM)
# mock_wait.assert_called_with(123)

# def test_shutdown_memory_statistics_failure(self):
# """Test failure in shutting down memory statistics daemon"""
# with mock.patch.object(self.mem_stat_cfg, 'get_memory_statistics_pid', return_value=123):
# with mock.patch('hostcfgd.os.kill', side_effect=Exception("Failed to send SIGTERM")):
# with 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: Failed to send SIGTERM")



from unittest import TestCase
# import mock
import signal
import psutil
import hostcfgd

class MockConfigDb:
CONFIG_DB = {}

@classmethod
def set_config_db(cls, config):
"""Set the configuration database for testing.
Args:
config: Dictionary containing configuration data
"""
cls.CONFIG_DB = config

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

# Test daemon shutdown
@mock.patch('hostcfgd.psutil.Process')
def test_wait_for_shutdown_timeout(self, mock_process):
"""Test shutdown waiting timeout"""
mock_process_instance = mock.Mock()
mock_process_instance.wait.side_effect = psutil.TimeoutExpired(123, 10)
mock_process.return_value = mock_process_instance

with mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
self.mem_stat_cfg.wait_for_shutdown(123)
mock_syslog.assert_any_call(mock.ANY, "MemoryStatisticsCfg: Timed out while waiting for daemon (PID 123) to shut down.")

@mock.patch('hostcfgd.psutil.Process')
def test_wait_for_shutdown_no_process(self, mock_process):
"""Test shutdown waiting when process doesn't exist"""
mock_process.side_effect = psutil.NoSuchProcess(123)

with mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
self.mem_stat_cfg.wait_for_shutdown(123)
mock_syslog.assert_any_call(mock.ANY, "MemoryStatisticsCfg: MemoryStatisticsDaemon process not found.")

# Test enable/disable functionality
def test_memory_statistics_enable(self):
"""Test enabling memory statistics"""
Expand Down Expand Up @@ -712,3 +874,23 @@ def test_shutdown_memory_statistics_failure(self):
with 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: Failed to send SIGTERM")

@mock.patch('hostcfgd.psutil.Process')
def test_wait_for_shutdown_timeout(self, mock_process):
"""Test shutdown waiting timeout"""
mock_process_instance = mock.Mock()
mock_process_instance.wait.side_effect = psutil.TimeoutExpired(123, 10)
mock_process.return_value = mock_process_instance

with mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
self.mem_stat_cfg.wait_for_shutdown(123)
mock_syslog.assert_any_call(mock.ANY, "MemoryStatisticsCfg: Timed out while waiting for daemon (PID 123) to shut down.")

@mock.patch('hostcfgd.psutil.Process')
def test_wait_for_shutdown_no_process(self, mock_process):
"""Test shutdown waiting when process doesn't exist"""
mock_process.side_effect = psutil.NoSuchProcess(123)

with mock.patch('hostcfgd.syslog.syslog') as mock_syslog:
self.mem_stat_cfg.wait_for_shutdown(123)
mock_syslog.assert_any_call(mock.ANY, "MemoryStatisticsCfg: MemoryStatisticsDaemon process not found.")

0 comments on commit 755d480

Please sign in to comment.