Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CMIS] Catch Exception to avoid CMIS code crash #299

Merged
merged 2 commits into from
Aug 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions sonic_platform_base/sonic_xcvr/api/public/cmis.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,9 @@ def get_transceiver_bulk_status(self):
bulk_status["tx%dpower" % i] = self.mw_to_dbm(tx_power[i - 1]) if self.get_tx_power_support() else 'N/A'

laser_temp_dict = self.get_laser_temperature()
bulk_status['laser_temperature'] = laser_temp_dict['monitor value']
self.vdm_dict = self.get_vdm()
try:
bulk_status['laser_temperature'] = laser_temp_dict['monitor value']
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure I understand, I moved bulk_status['laser_temperature'] = laser_temp_dict['monitor value'] to try section to avoid the case that laser_temp_dict is none

bulk_status['prefec_ber'] = self.vdm_dict['Pre-FEC BER Average Media Input'][1][0]
bulk_status['postfec_ber'] = self.vdm_dict['Errored Frames Average Media Input'][1][0]
except (KeyError, TypeError):
Expand Down Expand Up @@ -1856,7 +1856,11 @@ def get_application_advertisement(self):

if not self.is_flat_memory():
# Read the application advertisement in page01
dic.update(self.xcvr_eeprom.read(consts.APPLS_ADVT_FIELD_PAGE01))
try:
dic.update(self.xcvr_eeprom.read(consts.APPLS_ADVT_FIELD_PAGE01))
except TypeError as e:
logger.error('Failed to read APPLS_ADVT_FIELD_PAGE01: ' + str(e))
return ret

for app in range(1, 16):
buf = {}
Expand Down
7 changes: 6 additions & 1 deletion sonic_platform_base/sonic_xcvr/api/public/cmisVDM.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ def get_vdm_page(self, page, VDM_flag_page):
if page not in [0x20, 0x21, 0x22, 0x23]:
raise ValueError('Page not in VDM Descriptor range!')
vdm_descriptor = self.xcvr_eeprom.read_raw(page * PAGE_SIZE + PAGE_OFFSET, PAGE_SIZE)

if not vdm_descriptor:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are other eeprom read in this function. can you take care of them also?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

return {}

# Odd Adress VDM observable type ID, real-time monitored value in Page + 4
vdm_typeID = vdm_descriptor[1::2]
# Even Address
Expand Down Expand Up @@ -83,6 +85,9 @@ def get_vdm_page(self, page, VDM_flag_page):
vdm_thrsh_low_alarm_raw = self.xcvr_eeprom.read_raw(vdm_low_alarm_offset, VDM_SIZE, True)
vdm_thrsh_high_warn_raw = self.xcvr_eeprom.read_raw(vdm_high_warn_offset, VDM_SIZE, True)
vdm_thrsh_low_warn_raw = self.xcvr_eeprom.read_raw(vdm_low_warn_offset, VDM_SIZE, True)
if not vdm_value_raw or not vdm_thrsh_high_alarm_raw or not vdm_thrsh_low_alarm_raw \
or not vdm_high_warn_offset or not vdm_thrsh_low_warn_raw:
return {}
if vdm_format == 'S16':
vdm_value = struct.unpack('>h',vdm_value_raw)[0] * scale
vdm_thrsh_high_alarm = struct.unpack('>h', vdm_thrsh_high_alarm_raw)[0] * scale
Expand Down
48 changes: 48 additions & 0 deletions tests/sonic_xcvr/test_cmisVDM.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,54 @@ def test_get_vdm_page(self, input_param, mock_response, expected):
result = self.api.get_vdm_page(*input_param)
assert result == expected

@pytest.mark.parametrize("input_param, mock_response, expected", [
(
[0x20, [0]*128], # input_param
[ # mock_response
(
16, 9, 16, 11, 16, 13, 16, 15, 32, 10, 33, 10, 0, 0, 0, 0,
80,128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
160,143,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
),

None,
None,
None,
None,
None,
None,
None,
None,
],
{}
)
])
def test_get_vdm_page_none_vdm_value_raw(self, input_param, mock_response, expected):
self.api.xcvr_eeprom.read_raw = MagicMock()
self.api.xcvr_eeprom.read_raw.side_effect = mock_response
result = self.api.get_vdm_page(*input_param)
assert result == expected

@pytest.mark.parametrize("input_param, mock_response, expected", [
(
[0x20, [0]*128], # input_param
[ # mock_response
None,
],
{}
)
])
def test_get_vdm_page_none_vdm_descriptor(self, input_param, mock_response, expected):
self.api.xcvr_eeprom.read_raw = MagicMock()
self.api.xcvr_eeprom.read_raw.side_effect = mock_response
result = self.api.get_vdm_page(*input_param)
assert result == expected

@pytest.mark.parametrize("mock_response, expected", [
(
[ # mock_response
Expand Down