diff --git a/platform/mellanox/mlnx-platform-api/sonic_platform/chassis.py b/platform/mellanox/mlnx-platform-api/sonic_platform/chassis.py index b83ee33cc3..b9fa259317 100644 --- a/platform/mellanox/mlnx-platform-api/sonic_platform/chassis.py +++ b/platform/mellanox/mlnx-platform-api/sonic_platform/chassis.py @@ -112,7 +112,8 @@ class Chassis(ChassisBase): self.sfp_module = None # Build the RJ45 port list from platform.json and hwsku.json - self.RJ45_port_list = extract_RJ45_ports_index() + self._RJ45_port_inited = False + self._RJ45_port_list = None logger.log_info("Chassis loaded successfully") @@ -124,6 +125,13 @@ class Chassis(ChassisBase): if self.sfp_module.SFP.shared_sdk_handle: self.sfp_module.deinitialize_sdk_handle(sfp_module.SFP.shared_sdk_handle) + @property + def RJ45_port_list(self): + if not self._RJ45_port_inited: + self._RJ45_port_list = extract_RJ45_ports_index() + self._RJ45_port_inited = True + return self._RJ45_port_list + ############################################## # PSU methods ############################################## diff --git a/platform/mellanox/mlnx-platform-api/sonic_platform/utils.py b/platform/mellanox/mlnx-platform-api/sonic_platform/utils.py index b35ef313df..adf931d37e 100644 --- a/platform/mellanox/mlnx-platform-api/sonic_platform/utils.py +++ b/platform/mellanox/mlnx-platform-api/sonic_platform/utils.py @@ -177,12 +177,12 @@ def is_host(): """ Test whether current process is running on the host or an docker return True for host and False for docker - """ + """ try: - proc = subprocess.Popen("docker --version 2>/dev/null", - stdout=subprocess.PIPE, - shell=True, - stderr=subprocess.STDOUT, + proc = subprocess.Popen("docker --version 2>/dev/null", + stdout=subprocess.PIPE, + shell=True, + stderr=subprocess.STDOUT, universal_newlines=True) stdout = proc.communicate()[0] proc.wait() @@ -239,9 +239,13 @@ def load_json_file(filename, log_func=logger.log_error): def extract_RJ45_ports_index(): # Cross check 'platform.json' and 'hwsku.json' to extract the RJ45 port index if exists. hwsku_path = device_info.get_path_to_hwsku_dir() + hwsku_file = os.path.join(hwsku_path, HWSKU_JSON) + if not os.path.exists(hwsku_file): + # Platforms having no hwsku.json do not have RJ45 port + return None + platform_file = device_info.get_path_to_port_config_file() platform_dict = load_json_file(platform_file)['interfaces'] - hwsku_file = os.path.join(hwsku_path, HWSKU_JSON) hwsku_dict = load_json_file(hwsku_file)['interfaces'] port_name_to_index_map_dict = {} RJ45_port_index_list = [] diff --git a/platform/mellanox/mlnx-platform-api/tests/test_utils.py b/platform/mellanox/mlnx-platform-api/tests/test_utils.py index bbc3ab28e5..e1052202d4 100644 --- a/platform/mellanox/mlnx-platform-api/tests/test_utils.py +++ b/platform/mellanox/mlnx-platform-api/tests/test_utils.py @@ -113,10 +113,15 @@ class TestUtils: @utils.default_return(100, log_func=mock_log) def func(): raise RuntimeError('') - + assert func() == 100 assert mock_log.call_count == 1 def test_run_command(self): output = utils.run_command('ls') assert output + + @mock.patch('sonic_py_common.device_info.get_path_to_hwsku_dir', mock.MagicMock(return_value='/tmp')) + def test_extract_RJ45_ports_index(self): + rj45_list = utils.extract_RJ45_ports_index() + assert rj45_list is None