Skip to content

Commit

Permalink
Support to get MEDIA_SETTING and OPTICS_SI from both platform folder …
Browse files Browse the repository at this point in the history
…and HWSKU folder (#456)

* support to get MEDIA_SETTING and OPTICS_SI from both platform folder and HWSKU folder

* Add unit test cases

Signed-off-by: Kebo Liu <kebol@nvidia.com>

* fix unit test failure

Signed-off-by: Kebo Liu <kebol@nvidia.com>

---------

Signed-off-by: Kebo Liu <kebol@nvidia.com>
  • Loading branch information
keboliu authored Apr 17, 2024
1 parent 805c76a commit 25e22cd
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
25 changes: 22 additions & 3 deletions sonic-xcvrd/tests/test_xcvrd.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,10 +579,14 @@ def test_init_port_sfp_status_tbl(self):
task = SfpStateUpdateTask(DEFAULT_NAMESPACE, port_mapping, stop_event, sfp_error_event)
task._init_port_sfp_status_tbl(port_mapping, xcvr_table_helper, stop_event)

@patch('sonic_py_common.device_info.get_paths_to_platform_and_hwsku_dirs', MagicMock(return_value=('/invalid/path', None)))
@patch('sonic_py_common.device_info.get_paths_to_platform_and_hwsku_dirs', MagicMock(return_value=('/invalid/path', '/invalid/path')))
def test_load_media_settings_missing_file(self):
assert media_settings_parser.load_media_settings() == {}

@patch('sonic_py_common.device_info.get_paths_to_platform_and_hwsku_dirs', MagicMock(return_value=('/invalid/path', '/invalid/path')))
def test_load_optical_si_settings_missing_file(self):
assert optics_si_parser.load_optics_si_settings() == {}

@patch('xcvrd.xcvrd.platform_chassis')
@patch('xcvrd.xcvrd.is_cmis_api')
def test_get_media_settings_key(self, mock_is_cmis_api, mock_chassis):
Expand Down Expand Up @@ -2508,7 +2512,7 @@ class MockPortMapping:

@patch('xcvrd.xcvrd.DaemonXcvrd.load_platform_util', MagicMock())
@patch('xcvrd.xcvrd_utilities.port_event_helper.get_port_mapping', MagicMock(return_value=MockPortMapping))
@patch('sonic_py_common.device_info.get_paths_to_platform_and_hwsku_dirs', MagicMock(return_value=('/tmp', None)))
@patch('sonic_py_common.device_info.get_paths_to_platform_and_hwsku_dirs', MagicMock(return_value=('/tmp', '/tmp')))
@patch('swsscommon.swsscommon.WarmStart', MagicMock())
@patch('xcvrd.xcvrd.DaemonXcvrd.wait_for_port_config_done', MagicMock())
def test_DaemonXcvrd_init_deinit_fastboot_enabled(self):
Expand All @@ -2531,7 +2535,7 @@ def test_DaemonXcvrd_init_deinit_fastboot_enabled(self):

@patch('xcvrd.xcvrd.DaemonXcvrd.load_platform_util', MagicMock())
@patch('xcvrd.xcvrd_utilities.port_event_helper.get_port_mapping', MagicMock(return_value=MockPortMapping))
@patch('sonic_py_common.device_info.get_paths_to_platform_and_hwsku_dirs', MagicMock(return_value=('/tmp', None)))
@patch('sonic_py_common.device_info.get_paths_to_platform_and_hwsku_dirs', MagicMock(return_value=('/tmp', '/tmp')))
@patch('xcvrd.xcvrd.is_warm_reboot_enabled', MagicMock(return_value=False))
@patch('xcvrd.xcvrd.DaemonXcvrd.wait_for_port_config_done', MagicMock())
@patch('subprocess.check_output', MagicMock(return_value='false'))
Expand All @@ -2556,6 +2560,21 @@ def test_DaemonXcvrd_init_deinit_cold(self):

status_tbl.hdel.assert_called()

@patch('sonic_py_common.device_info.get_paths_to_platform_and_hwsku_dirs', MagicMock(return_value=(test_path, '/invalid/path')))
def test_load_optical_si_file_from_platform_folder(self):
assert optics_si_parser.load_optics_si_settings() != {}

@patch('sonic_py_common.device_info.get_paths_to_platform_and_hwsku_dirs', MagicMock(return_value=('/invalid/path', test_path)))
def test_load_optical_si_file_from_hwsku_folder(self):
assert optics_si_parser.load_optics_si_settings() != {}

@patch('sonic_py_common.device_info.get_paths_to_platform_and_hwsku_dirs', MagicMock(return_value=(test_path, '/invalid/path')))
def test_load_media_settings_file_from_platform_folder(self):
assert media_settings_parser.load_media_settings() != {}

@patch('sonic_py_common.device_info.get_paths_to_platform_and_hwsku_dirs', MagicMock(return_value=('/invalid/path', test_path)))
def test_load_media_settings_file_from_hwsku_folder(self):
assert media_settings_parser.load_media_settings() != {}

def wait_until(total_wait_time, interval, call_back, *args, **kwargs):
wait_time = 0
Expand Down
13 changes: 10 additions & 3 deletions sonic-xcvrd/xcvrd/xcvrd_utilities/media_settings_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,17 @@

def load_media_settings():
global g_dict
(platform_path, _) = device_info.get_paths_to_platform_and_hwsku_dirs()
(platform_path, hwsku_path) = device_info.get_paths_to_platform_and_hwsku_dirs()

media_settings_file_path = os.path.join(platform_path, "media_settings.json")
if not os.path.isfile(media_settings_file_path):
# Support to fetch media_settings.json both from platform folder and HWSKU folder
media_settings_file_path_platform = os.path.join(platform_path, "media_settings.json")
media_settings_file_path_hwsku = os.path.join(hwsku_path, "media_settings.json")

if os.path.isfile(media_settings_file_path_hwsku):
media_settings_file_path = media_settings_file_path_hwsku
elif os.path.isfile(media_settings_file_path_platform):
media_settings_file_path = media_settings_file_path_platform
else:
helper_logger.log_info("xcvrd: No media file exists")
return {}

Expand Down
13 changes: 10 additions & 3 deletions sonic-xcvrd/xcvrd/xcvrd_utilities/optics_si_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,17 @@ def fetch_optics_si_setting(physical_port, lane_speed, sfp):

def load_optics_si_settings():
global g_optics_si_dict
(platform_path, _) = device_info.get_paths_to_platform_and_hwsku_dirs()
(platform_path, hwsku_path) = device_info.get_paths_to_platform_and_hwsku_dirs()

optics_si_settings_file_path = os.path.join(platform_path, "optics_si_settings.json")
if not os.path.isfile(optics_si_settings_file_path):
# Support to fetch optics_si_settings.json both from platform folder and HWSKU folder
optics_si_settings_file_path_platform = os.path.join(platform_path, "optics_si_settings.json")
optics_si_settings_file_path_hwsku = os.path.join(hwsku_path, "optics_si_settings.json")

if os.path.isfile(optics_si_settings_file_path_hwsku):
optics_si_settings_file_path = optics_si_settings_file_path_hwsku
elif os.path.isfile(optics_si_settings_file_path_platform):
optics_si_settings_file_path = optics_si_settings_file_path_platform
else:
helper_logger.log_info("No optics SI file exists")
return {}

Expand Down

0 comments on commit 25e22cd

Please sign in to comment.