From a6c403fb7df2eecc81395da2ca5ba9e831d835d7 Mon Sep 17 00:00:00 2001 From: Stephen Sun Date: Tue, 23 Aug 2022 03:12:02 +0000 Subject: [PATCH 1/2] Handle non-front-panel ports in is_rj45_port Handle the cases that a port is not a front-panel port in is_rj45_port Add mock test to cover the logic Signed-off-by: Stephen Sun --- tests/sfp_test.py | 8 +++++++- utilities_common/platform_sfputil_helper.py | 7 +++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/tests/sfp_test.py b/tests/sfp_test.py index d762b9f8ae..0a171a0cb2 100644 --- a/tests/sfp_test.py +++ b/tests/sfp_test.py @@ -495,9 +495,15 @@ def test_sfp_eeprom_dom_all(self): assert result.exit_code == 0 assert "\n".join([ l.rstrip() for l in result.output.split('\n')]) == test_sfp_eeprom_dom_all_output - def test_is_rj45_port(self): + def test_is_rj45_port_sad(self): import utilities_common.platform_sfputil_helper as platform_sfputil_helper + from sonic_py_common.interface import front_panel_prefix, SONIC_INTERFACE_PREFIXES + platform_sfputil_helper.platform_chassis = None + + for _, prefix in SONIC_INTERFACE_PREFIXES.items(): + assert prefix == front_panel_prefix() or not platform_sfputil_helper.is_rj45_port(prefix + '0') + if 'sonic_platform' in sys.modules: sys.modules.pop('sonic_platform') assert platform_sfputil_helper.is_rj45_port("Ethernet0") == False diff --git a/utilities_common/platform_sfputil_helper.py b/utilities_common/platform_sfputil_helper.py index d73e9dd131..980310ad14 100644 --- a/utilities_common/platform_sfputil_helper.py +++ b/utilities_common/platform_sfputil_helper.py @@ -4,6 +4,7 @@ from . import cli as clicommon from sonic_py_common import multi_asic, device_info +from sonic_py_common.interface import front_panel_prefix, SONIC_INTERFACE_PREFIXES platform_sfputil = None platform_chassis = None @@ -111,6 +112,12 @@ def is_rj45_port(port_name): global platform_sfp_base global platform_sfputil_loaded + if not port_name or not port_name.startswith(front_panel_prefix()): + return False + for _, prefix in SONIC_INTERFACE_PREFIXES.items(): + if prefix != front_panel_prefix() and port_name.startswith(prefix): + return False + try: if not platform_chassis: import sonic_platform From d9aaf7486b9336e830dab613df34992a5e427116 Mon Sep 17 00:00:00 2001 From: Stephen Sun Date: Wed, 24 Aug 2022 01:36:18 +0000 Subject: [PATCH 2/2] Use APIs in sonic_platform_base Signed-off-by: Stephen Sun --- tests/sfp_test.py | 8 +------- utilities_common/platform_sfputil_helper.py | 9 +-------- 2 files changed, 2 insertions(+), 15 deletions(-) diff --git a/tests/sfp_test.py b/tests/sfp_test.py index 0a171a0cb2..d762b9f8ae 100644 --- a/tests/sfp_test.py +++ b/tests/sfp_test.py @@ -495,15 +495,9 @@ def test_sfp_eeprom_dom_all(self): assert result.exit_code == 0 assert "\n".join([ l.rstrip() for l in result.output.split('\n')]) == test_sfp_eeprom_dom_all_output - def test_is_rj45_port_sad(self): + def test_is_rj45_port(self): import utilities_common.platform_sfputil_helper as platform_sfputil_helper - from sonic_py_common.interface import front_panel_prefix, SONIC_INTERFACE_PREFIXES - platform_sfputil_helper.platform_chassis = None - - for _, prefix in SONIC_INTERFACE_PREFIXES.items(): - assert prefix == front_panel_prefix() or not platform_sfputil_helper.is_rj45_port(prefix + '0') - if 'sonic_platform' in sys.modules: sys.modules.pop('sonic_platform') assert platform_sfputil_helper.is_rj45_port("Ethernet0") == False diff --git a/utilities_common/platform_sfputil_helper.py b/utilities_common/platform_sfputil_helper.py index 980310ad14..89ade3fc04 100644 --- a/utilities_common/platform_sfputil_helper.py +++ b/utilities_common/platform_sfputil_helper.py @@ -4,7 +4,6 @@ from . import cli as clicommon from sonic_py_common import multi_asic, device_info -from sonic_py_common.interface import front_panel_prefix, SONIC_INTERFACE_PREFIXES platform_sfputil = None platform_chassis = None @@ -112,12 +111,6 @@ def is_rj45_port(port_name): global platform_sfp_base global platform_sfputil_loaded - if not port_name or not port_name.startswith(front_panel_prefix()): - return False - for _, prefix in SONIC_INTERFACE_PREFIXES.items(): - if prefix != front_panel_prefix() and port_name.startswith(prefix): - return False - try: if not platform_chassis: import sonic_platform @@ -140,7 +133,7 @@ def is_rj45_port(port_name): port_type = None try: - physical_port = logical_port_name_to_physical_port_list(port_name) + physical_port = platform_sfputil.logical_port_name_to_physical_port_list(port_name) if physical_port: port_type = platform_chassis.get_port_or_cage_type(physical_port[0]) except Exception as e: