From 78aa28d8f86d4f4701afca0460bcd51d0459c955 Mon Sep 17 00:00:00 2001 From: Vivek Reddy Date: Wed, 18 Dec 2024 01:50:07 +0000 Subject: [PATCH] [hostcfgd] Fix the state machine during eth0 default route check failure Signed-off-by: Vivek Reddy --- scripts/hostcfgd | 11 +++++------ tests/hostcfgd/hostcfgd_test.py | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/scripts/hostcfgd b/scripts/hostcfgd index 3c247c22..67c3989a 100644 --- a/scripts/hostcfgd +++ b/scripts/hostcfgd @@ -125,7 +125,7 @@ def run_cmd_pipe(cmd0, cmd1, cmd2, log_err=True, raise_exception=False): check_output_pipe(cmd0, cmd1, cmd2) except Exception as err: if log_err: - syslog.syslog(syslog.LOG_ERR, "{} - failed: return code - {}, output:\n{}" + syslog.syslog(syslog.LOG_WARNING, "{} - failed: return code - {}, output:\n{}" .format(err.cmd, err.returncode, err.output)) if raise_exception: raise @@ -1537,6 +1537,9 @@ class MgmtIfaceCfg(object): 'services') return + # Update cache + self.mgmt_vrf_enabled = enabled + # Remove mgmt if route if enabled == 'true': """ @@ -1555,16 +1558,12 @@ class MgmtIfaceCfg(object): cmd2 = ['wc', '-l'] run_cmd_pipe(cmd0, cmd1, cmd2, True, True) except subprocess.CalledProcessError: - syslog.syslog(syslog.LOG_ERR, 'MgmtIfaceCfg: Could not delete ' + syslog.syslog(syslog.LOG_WARNING, 'MgmtIfaceCfg: Could not delete ' 'eth0 route') return run_cmd(["ip", "-4", "route", "del", "default", "dev", "eth0", "metric", "202"], False) - # Update cache - self.mgmt_vrf_enabled = enabled - - class RSyslogCfg(object): """Remote syslog config daemon diff --git a/tests/hostcfgd/hostcfgd_test.py b/tests/hostcfgd/hostcfgd_test.py index fbb432f7..441171ff 100644 --- a/tests/hostcfgd/hostcfgd_test.py +++ b/tests/hostcfgd/hostcfgd_test.py @@ -4,6 +4,7 @@ import signal import psutil import swsscommon as swsscommon_package +from subprocess import CalledProcessError from sonic_py_common import device_info from swsscommon import swsscommon from parameterized import parameterized @@ -324,6 +325,29 @@ def test_mgmtiface_event(self): ] mocked_check_output.assert_has_calls(expected) + def test_mgmtvrf_route_check_failed(self): + mgmtiface = hostcfgd.MgmtIfaceCfg() + mgmtiface.load({}, {'mgmtVrfEnabled' : "false"}) + with mock.patch('hostcfgd.check_output_pipe') as mocked_check_output: + 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.CalledProcessError = CalledProcessError + # Simulate the case where there is no default route + mocked_check_output.side_effect = CalledProcessError(returncode=1, cmd="", output="") + + mgmtiface.update_mgmt_vrf({'mgmtVrfEnabled' : "true"}) + expected = [ + 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) + assert mgmtiface.mgmt_vrf_enabled == "true" + + mgmtiface.update_mgmt_vrf({'mgmtVrfEnabled' : "false"}) + assert mgmtiface.mgmt_vrf_enabled == "false" + def test_dns_events(self): MockConfigDb.set_config_db(HOSTCFG_DAEMON_CFG_DB) MockConfigDb.event_queue = [('DNS_NAMESERVER', '1.1.1.1')]