From 0aeaa3c5ada71bb619264fb541dae5fc343a138b Mon Sep 17 00:00:00 2001 From: Baorong Liu Date: Tue, 30 Jan 2024 22:21:46 -0800 Subject: [PATCH] [staticroutebfd] fix an error in error logging (#17043) Why I did it Fix an error in the log_err call. this error can be triggered by an invalid static route key. usually the code cannot go here with normal config file. but hit this issue with an invalid key by manual testing with redis-cli directly. the file is scanned by Python lint to prevent such errors. Work item tracking Microsoft ADO ():26250268 How I did it fix the format error. How to verify it 1, ran pylint to check the design, make sure no such error in the design file. 2, wrote a separate python program to verify the log call. In the current logging related testing, usually use patch/mock for logging. for this specific error, could not trigger it if we call mock function instead the real function in the design. so need to do lint checking for code change. --- src/sonic-bgpcfgd/staticroutebfd/main.py | 2 +- src/sonic-bgpcfgd/tests/test_static_rt_bfd.py | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/sonic-bgpcfgd/staticroutebfd/main.py b/src/sonic-bgpcfgd/staticroutebfd/main.py index e3b2ed10be30..2d7006420eb9 100644 --- a/src/sonic-bgpcfgd/staticroutebfd/main.py +++ b/src/sonic-bgpcfgd/staticroutebfd/main.py @@ -373,7 +373,7 @@ def static_route_set_handler(self, key, data): valid, is_ipv4, ip = check_ip(ip_prefix) if not valid: - log_err("invalid ip prefix for static route: ", key) + log_err("invalid ip prefix for static route: '%s'"%(key)) return True arg_list = lambda v: [x.strip() for x in v.split(',')] if len(v.strip()) != 0 else None diff --git a/src/sonic-bgpcfgd/tests/test_static_rt_bfd.py b/src/sonic-bgpcfgd/tests/test_static_rt_bfd.py index 11009aae0289..24f88bade1c6 100644 --- a/src/sonic-bgpcfgd/tests/test_static_rt_bfd.py +++ b/src/sonic-bgpcfgd/tests/test_static_rt_bfd.py @@ -95,6 +95,23 @@ def intf_setup(dut): {} ) +@patch('staticroutebfd.main.log_err') +def test_invalid_key(mocked_log_err): + dut = constructor() + intf_setup(dut) + + set_del_test(dut, "srt", + "SET", + ("2.2.2/24", { + "bfd": "true", + "nexthop": "192.168.1.2 , 192.168.2.2, 192.168.3.2", + "ifname": "if1, if2, if3", + }), + {}, + {} + ) + mocked_log_err.assert_called_with("invalid ip prefix for static route: '2.2.2/24'") + def test_set_del(): dut = constructor() intf_setup(dut)