Skip to content

Commit

Permalink
chaged hostcfgd
Browse files Browse the repository at this point in the history
  • Loading branch information
kanza-latif committed Sep 13, 2024
1 parent beac5a5 commit bbdfee9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
10 changes: 6 additions & 4 deletions scripts/hostcfgd
Original file line number Diff line number Diff line change
Expand Up @@ -1703,22 +1703,23 @@ class Memory_StatisticsCfg(object):
3) Handle change of sampling interval
"""

def __init__(self):
def __init__(self, CfgDb):
self.config_db = CfgDb
self.cache = {}
self.memory_statistics_defaults = {
"enabled": "false",
"retention_time": "15 days",
"sampling_interval": "5 minutes"
}

def load(self, memory_stats_config: dict):
def load(self, memory_statistics_table):
"""
Load memory statistics configuration when the daemon starts.
Args:
memory_stats_config: Configured memory statistics settings.
"""
syslog.syslog(syslog.LOG_INFO, "Memory_StatisticsCfg init ...")
memory_statistics_conf = memory_stats_config.get("config", {})
memory_statistics_conf = memory_statistics_table.get("config", {})

# Apply default configurations if not present
for row, value in self.memory_statistics_defaults.items():
Expand Down Expand Up @@ -1771,7 +1772,8 @@ class Memory_StatisticsCfg(object):
syslog.syslog(syslog.LOG_INFO, output.decode('utf-8'))
except subprocess.CalledProcessError as e:
syslog.syslog(syslog.LOG_ERR, e.output.decode('utf-8'))



class HostConfigDaemon:
def __init__(self):
self.state_db_conn = DBConnector(STATE_DB, 0)
Expand Down
46 changes: 23 additions & 23 deletions tests/hostcfgd/hostcfgd_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,31 +295,31 @@ def test_dns_events(self):
mocked_run_cmd.assert_has_calls([call(['systemctl', 'restart', 'resolv-config'], True, False)])

def test_memory_statistics_event(self):
MockConfigDb.set_config_db(HOSTCFG_DAEMON_CFG_DB)
daemon = hostcfgd.HostConfigDaemon()
daemon.register_callbacks()
MockConfigDb.event_queue = [('MEMORY_STATISTICS', 'config')]
MockConfigDb.set_config_db(HOSTCFG_DAEMON_CFG_DB)
daemon = hostcfgd.HostConfigDaemon()
daemon.register_callbacks()
MockConfigDb.event_queue = [('MEMORY_STATISTICS', 'config')]

with mock.patch('hostcfgd.subprocess') as mocked_subprocess:
popen_mock = mock.Mock()
attrs = {'communicate.return_value': ('output', 'error')}
popen_mock.configure_mock(**attrs)
mocked_subprocess.Popen.return_value = popen_mock
mocked_subprocess.check_call = mock.Mock()

try:
daemon.start()
except TimeoutError:
pass

expected = [
mock.call(['sonic-memory_statistics-config', '--enable']),
mock.call(['sonic-memory_statistics-config', '--retention_time', '15']),
mock.call(['sonic-memory_statistics-config', '--sampling_interval', '5'])
]

with mock.patch('hostcfgd.subprocess') as mocked_subprocess:
popen_mock = mock.Mock()
attrs = {'communicate.return_value': ('output', 'error')}
popen_mock.configure_mock(**attrs)
mocked_subprocess.Popen.return_value = popen_mock
mocked_subprocess.check_call = mock.Mock()
mocked_subprocess.check_call.assert_has_calls(expected, any_order=True)

try:
daemon.start()
except TimeoutError:
pass

expected = [
mock.call(['sonic-memory_statistics-config', '--enable']),
mock.call(['sonic-memory_statistics-config', '--retention_time', '15']),
mock.call(['sonic-memory_statistics-config', '--sampling_interval', '5'])
]

mocked_subprocess.check_call.assert_has_calls(expected, any_order=True)

class TestDnsHandler:

@mock.patch('hostcfgd.run_cmd')
Expand Down

0 comments on commit bbdfee9

Please sign in to comment.