Skip to content

Commit

Permalink
[Mellanox] Fix issue: failed to decode Json while there is no hwsku.j…
Browse files Browse the repository at this point in the history
…son (sonic-net#11436)

- Why I did it
Fix bug: pmon report error on start up because some SKUs do not have hwsku.json

- How I did it
If hwsku.json, do not extract RJ45 port information

- How to verify it
Manual test.
Unit test.
  • Loading branch information
Junchao-Mellanox authored and skbarista committed Aug 17, 2022
1 parent df5e4a2 commit 23852b6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
10 changes: 9 additions & 1 deletion platform/mellanox/mlnx-platform-api/sonic_platform/chassis.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ def __init__(self):
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")

Expand All @@ -124,6 +125,13 @@ def __del__(self):
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
##############################################
Expand Down
16 changes: 10 additions & 6 deletions platform/mellanox/mlnx-platform-api/sonic_platform/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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 = []
Expand Down
7 changes: 6 additions & 1 deletion platform/mellanox/mlnx-platform-api/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,15 @@ def test_default_return(self, mock_log):
@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

0 comments on commit 23852b6

Please sign in to comment.