diff --git a/scripts/hostcfgd b/scripts/hostcfgd index e5e52aea..7a591743 100644 --- a/scripts/hostcfgd +++ b/scripts/hostcfgd @@ -1775,7 +1775,7 @@ class Memory_StatisticsCfg(object): def memory_statistics_update(self, key, data): """ - Handle update to the memory statistics configuration. + Handle updates to the memory statistics configuration. Args: key: Key identifying the config type. data: Updated configuration data. @@ -1792,9 +1792,10 @@ class Memory_StatisticsCfg(object): """ try: output = subprocess.check_output(cmd, stderr=subprocess.STDOUT) - syslog.syslog(syslog.LOG_INFO, output.decode('utf-8')) # Ensure proper decoding - except subprocess.CalledProcessError as e: # Make sure subprocess is imported correctly - syslog.syslog(syslog.LOG_ERR, str(e)) + 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 SerialConsoleCfg: @@ -2140,7 +2141,7 @@ class HostConfigDaemon: # Handle FIPS changes self.config_db.subscribe('FIPS', make_callback(self.fips_config_handler)) - # Handle NTP, NTP_SERVER, and NTP_KEY updates + # Handle NTP, NTP_SERVER, and NTP_KEY updates self.config_db.subscribe(swsscommon.CFG_NTP_GLOBAL_TABLE_NAME, make_callback(self.ntp_global_handler)) self.config_db.subscribe(swsscommon.CFG_NTP_SERVER_TABLE_NAME, diff --git a/tests/hostcfgd/hostcfgd_test.py b/tests/hostcfgd/hostcfgd_test.py index 3adb44a4..449675b6 100644 --- a/tests/hostcfgd/hostcfgd_test.py +++ b/tests/hostcfgd/hostcfgd_test.py @@ -216,6 +216,7 @@ def test_kdump_event(self): call(['sonic-kdump-config', '--memory', '0M-2G:256M,2G-4G:320M,4G-8G:384M,8G-:448M'])] mocked_subprocess.check_call.assert_has_calls(expected, any_order=True) + def test_devicemeta_event(self): """ Test handling DEVICE_METADATA events. @@ -323,54 +324,8 @@ def test_mgmtiface_event(self): call(['cat', '/proc/net/route'], ['grep', '-E', r"eth0\s+00000000\s+[0-9A-Z]+\s+[0-9]+\s+[0-9]+\s+[0-9]+\s+202"], ['wc', '-l']) ] mocked_check_output.assert_has_calls(expected) - - def test_dns_events(self): - MockConfigDb.set_config_db(HOSTCFG_DAEMON_CFG_DB) - MockConfigDb.event_queue = [('DNS_NAMESERVER', '1.1.1.1')] - daemon = hostcfgd.HostConfigDaemon() - daemon.register_callbacks() - with mock.patch('hostcfgd.run_cmd') as mocked_run_cmd: - try: - daemon.start() - except TimeoutError: - pass - mocked_run_cmd.assert_has_calls([call(['systemctl', 'restart', 'resolv-config'], True, False)]) - - def load(init_data): - # Debug print to inspect init_data during runtime - print("init_data content:", init_data) - - # Safely accessing 'MEMORY_STATISTICS' with a fallback to an empty dict if the key is missing - memory_statistics = init_data.get('MEMORY_STATISTICS', {}) - - # Proceed with loading other parts of the config - aaa = init_data.get('AAA', {}) - tacacs_global = init_data.get('TACPLUS', {}) - tacacs_server = init_data.get('TACPLUS_SERVER', {}) - radius_global = init_data.get('RADIUS', {}) - radius_server = init_data.get('RADIUS_SERVER', {}) - ldap_global = init_data.get('LDAP', {}) - ldap_server = init_data.get('LDAP_SERVER', {}) - lpbk_table = init_data.get('LOOPBACK_INTERFACE', {}) - kdump = init_data.get('KDUMP', {}) - - # Debugging memory_statistics - if memory_statistics: - print("Memory statistics configuration found:", memory_statistics) - else: - print("Memory statistics configuration not found.") - + def test_memory_statistics_event(self): - HOSTCFG_DAEMON_INIT_CFG_DB = { - 'MEMORY_STATISTICS': { - 'config': { - 'enabled': 'true', - 'retention_time': '15', - 'sampling_interval': '5' - } - }, - } - MockConfigDb.set_config_db(HOSTCFG_DAEMON_CFG_DB) daemon = hostcfgd.HostConfigDaemon() daemon.register_callbacks() @@ -396,6 +351,18 @@ def test_memory_statistics_event(self): mocked_subprocess.check_call.assert_has_calls(expected_calls, any_order=True) + def test_dns_events(self): + MockConfigDb.set_config_db(HOSTCFG_DAEMON_CFG_DB) + MockConfigDb.event_queue = [('DNS_NAMESERVER', '1.1.1.1')] + daemon = hostcfgd.HostConfigDaemon() + daemon.register_callbacks() + with mock.patch('hostcfgd.run_cmd') as mocked_run_cmd: + try: + daemon.start() + except TimeoutError: + pass + mocked_run_cmd.assert_has_calls([call(['systemctl', 'restart', 'resolv-config'], True, False)]) + class TestDnsHandler: @@ -413,4 +380,4 @@ def test_load(self): data = {} dns_cfg.load(data) - dns_cfg.dns_update.assert_called() + dns_cfg.dns_update.assert_called() \ No newline at end of file