From 5a2a95875cc551f1556b3c67c4836635d809f6d9 Mon Sep 17 00:00:00 2001 From: dgsudharsan Date: Tue, 27 Feb 2024 21:41:18 +0000 Subject: [PATCH 1/5] [ipintutil]Handle exception in show ip interfaces command --- scripts/ipintutil | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/scripts/ipintutil b/scripts/ipintutil index 5535bce7e7..2429163bbc 100755 --- a/scripts/ipintutil +++ b/scripts/ipintutil @@ -150,7 +150,10 @@ def get_ip_intfs_in_namespace(af, namespace, display): ip_intf_attr = [] if namespace != constants.DEFAULT_NAMESPACE and skip_ip_intf_display(iface, display): continue - ipaddresses = multi_asic_util.multi_asic_get_ip_intf_addr_from_ns(namespace, iface) + try: + ipaddresses = multi_asic_util.multi_asic_get_ip_intf_addr_from_ns(namespace, iface) + except ValueError: + continue if af in ipaddresses: ifaddresses = [] bgp_neighs = {} From 9fd110ada7987dc5997a5791674b0478e31d595f Mon Sep 17 00:00:00 2001 From: dgsudharsan Date: Wed, 28 Feb 2024 06:00:49 +0000 Subject: [PATCH 2/5] Adding UT --- tests/show_ip_int_test.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/show_ip_int_test.py b/tests/show_ip_int_test.py index 16160df75d..9dd7553f8d 100644 --- a/tests/show_ip_int_test.py +++ b/tests/show_ip_int_test.py @@ -96,6 +96,8 @@ def verify_output(output, expected_output): print(new_output) assert new_output == expected_output +def upd_mock_single_asic_get_ip_intf_from_ns(namespace): + return mock_single_asic_get_ip_intf_from_ns(namespace).append("Loopback0") @pytest.mark.usefixtures('setup_teardown_single_asic') class TestShowIpInt(object): @@ -116,6 +118,13 @@ def test_show_intf_invalid_af_option(self): assert return_code == 1 assert result == show_error_invalid_af + def test_show_ip_intf_v4_exception(self): + from .mock_tables import mock_single_asic + mock_single_asic.multi_asic_util.multi_asic_get_ip_intf_from_ns = upd_mock_single_asic_get_ip_intf_from_ns + return_code, result = get_result_and_return_code(["ipintutil"]) + assert return_code == 0 + verify_output(result, show_ipv4_intf_with_multple_ips) + mock_single_asic.multi_asic_util.multi_asic_get_ip_intf_from_ns = mock_single_asic.mock_single_asic_get_ip_intf_from_ns @pytest.mark.usefixtures('setup_teardown_multi_asic') class TestMultiAsicShowIpInt(object): From cd8a66c46d063024bad674b4a9b836ed5d604afc Mon Sep 17 00:00:00 2001 From: dgsudharsan Date: Wed, 28 Feb 2024 19:37:17 +0000 Subject: [PATCH 3/5] Adding coverage --- scripts/ipintutil | 1 + tests/mock_tables/mock_single_asic.py | 4 ++++ tests/show_ip_int_test.py | 10 ---------- 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/scripts/ipintutil b/scripts/ipintutil index 2429163bbc..85879972e0 100755 --- a/scripts/ipintutil +++ b/scripts/ipintutil @@ -29,6 +29,7 @@ try: mock_tables.dbconnector.load_namespace_config() else: import mock_tables.mock_single_asic + mock_tables.mock_single_asic.add_unknown_intf=True except KeyError: pass diff --git a/tests/mock_tables/mock_single_asic.py b/tests/mock_tables/mock_single_asic.py index 08c2157c9d..3ff4125e2f 100644 --- a/tests/mock_tables/mock_single_asic.py +++ b/tests/mock_tables/mock_single_asic.py @@ -4,6 +4,8 @@ from sonic_py_common import multi_asic from utilities_common import multi_asic as multi_asic_util +add_unknown_intf=False + mock_intf_table = { '': { 'eth0': { @@ -60,6 +62,8 @@ def mock_single_asic_get_ip_intf_from_ns(namespace): interfaces = [] try: interfaces = list(mock_intf_table[namespace].keys()) + if add_unknown_intf: + interfaces.append("unknownintf") except KeyError: pass return interfaces diff --git a/tests/show_ip_int_test.py b/tests/show_ip_int_test.py index 9dd7553f8d..0f5b480490 100644 --- a/tests/show_ip_int_test.py +++ b/tests/show_ip_int_test.py @@ -96,8 +96,6 @@ def verify_output(output, expected_output): print(new_output) assert new_output == expected_output -def upd_mock_single_asic_get_ip_intf_from_ns(namespace): - return mock_single_asic_get_ip_intf_from_ns(namespace).append("Loopback0") @pytest.mark.usefixtures('setup_teardown_single_asic') class TestShowIpInt(object): @@ -118,14 +116,6 @@ def test_show_intf_invalid_af_option(self): assert return_code == 1 assert result == show_error_invalid_af - def test_show_ip_intf_v4_exception(self): - from .mock_tables import mock_single_asic - mock_single_asic.multi_asic_util.multi_asic_get_ip_intf_from_ns = upd_mock_single_asic_get_ip_intf_from_ns - return_code, result = get_result_and_return_code(["ipintutil"]) - assert return_code == 0 - verify_output(result, show_ipv4_intf_with_multple_ips) - mock_single_asic.multi_asic_util.multi_asic_get_ip_intf_from_ns = mock_single_asic.mock_single_asic_get_ip_intf_from_ns - @pytest.mark.usefixtures('setup_teardown_multi_asic') class TestMultiAsicShowIpInt(object): From b7bc4d30eb8941a10a395bb09e82afdf281eb75b Mon Sep 17 00:00:00 2001 From: dgsudharsan Date: Wed, 28 Feb 2024 19:38:21 +0000 Subject: [PATCH 4/5] Removing the unnecessary diff --- tests/show_ip_int_test.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/show_ip_int_test.py b/tests/show_ip_int_test.py index 0f5b480490..16160df75d 100644 --- a/tests/show_ip_int_test.py +++ b/tests/show_ip_int_test.py @@ -116,6 +116,7 @@ def test_show_intf_invalid_af_option(self): assert return_code == 1 assert result == show_error_invalid_af + @pytest.mark.usefixtures('setup_teardown_multi_asic') class TestMultiAsicShowIpInt(object): From 7f97968eab78eaf8a9cd32b45aee7801183ffb6f Mon Sep 17 00:00:00 2001 From: dgsudharsan Date: Wed, 28 Feb 2024 20:49:12 +0000 Subject: [PATCH 5/5] Adding changes for coverage --- tests/mock_tables/mock_single_asic.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/mock_tables/mock_single_asic.py b/tests/mock_tables/mock_single_asic.py index 3ff4125e2f..ac97c4bc79 100644 --- a/tests/mock_tables/mock_single_asic.py +++ b/tests/mock_tables/mock_single_asic.py @@ -74,7 +74,8 @@ def mock_single_asic_get_ip_intf_addr_from_ns(namespace, iface): try: ipaddresses = mock_intf_table[namespace][iface] except KeyError: - pass + if add_unknown_intf: + raise ValueError("Unknow interface") return ipaddresses