From fbf82b93b7c5812bc87664b76dca98eb8ed465ab Mon Sep 17 00:00:00 2001 From: Stephen Sun <5379172+stephenxs@users.noreply.github.com> Date: Mon, 8 Aug 2022 20:24:17 +0800 Subject: [PATCH] Fix the issue that sonic_platform is not installed on vs image (#2300) Method is_rj45_port references sonic_platform which has not been implemented on vs platform However, the method is referenced by show interface status which is widely used in kvm test in azure pipeline checkers - What I did - How I did it True is returned in is_rj45_port if sonic_platform can not be imported - How to verify it Run vs tests Signed-off-by: Stephen Sun --- tests/sfp_test.py | 7 +++++++ utilities_common/platform_sfputil_helper.py | 18 ++++++++++++------ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/tests/sfp_test.py b/tests/sfp_test.py index b894b39469..d762b9f8ae 100644 --- a/tests/sfp_test.py +++ b/tests/sfp_test.py @@ -495,6 +495,13 @@ 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): + import utilities_common.platform_sfputil_helper as platform_sfputil_helper + platform_sfputil_helper.platform_chassis = None + if 'sonic_platform' in sys.modules: + sys.modules.pop('sonic_platform') + assert platform_sfputil_helper.is_rj45_port("Ethernet0") == False + @classmethod def teardown_class(cls): print("TEARDOWN") diff --git a/utilities_common/platform_sfputil_helper.py b/utilities_common/platform_sfputil_helper.py index 8b6afd5ef5..b4c819b4d8 100644 --- a/utilities_common/platform_sfputil_helper.py +++ b/utilities_common/platform_sfputil_helper.py @@ -111,12 +111,18 @@ def is_rj45_port(port_name): global platform_sfp_base global platform_sfputil_loaded - if not platform_chassis: - import sonic_platform - platform_chassis = sonic_platform.platform.Platform().get_chassis() - if not platform_sfp_base: - import sonic_platform_base - platform_sfp_base = sonic_platform_base.sfp_base.SfpBase + try: + if not platform_chassis: + import sonic_platform + platform_chassis = sonic_platform.platform.Platform().get_chassis() + if not platform_sfp_base: + import sonic_platform_base + platform_sfp_base = sonic_platform_base.sfp_base.SfpBase + except ModuleNotFoundError as e: + # This method is referenced by intfutil which is called on vs image + # However, there is no platform API supported on vs image + # So False is returned in such case + return False if platform_chassis and platform_sfp_base: if not platform_sfputil: