Skip to content

Commit c1ee4b1

Browse files
keboliuyxieca
authored andcommitted
[CMIS] Catch Exception to avoid CMIS code crash (#299)
* catch Exception to avoid CMIS code crash Signed-off-by: Kebo Liu <[email protected]> * fix review comments, add more UT test Signed-off-by: Kebo Liu <[email protected]> Signed-off-by: Kebo Liu <[email protected]>
1 parent 638d225 commit c1ee4b1

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

sonic_platform_base/sonic_xcvr/api/public/cmis.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,9 @@ def get_transceiver_bulk_status(self):
203203
bulk_status["tx%dpower" % i] = self.mw_to_dbm(tx_power[i - 1]) if self.get_tx_power_support() else 'N/A'
204204

205205
laser_temp_dict = self.get_laser_temperature()
206-
bulk_status['laser_temperature'] = laser_temp_dict['monitor value']
207206
self.vdm_dict = self.get_vdm()
208207
try:
208+
bulk_status['laser_temperature'] = laser_temp_dict['monitor value']
209209
bulk_status['prefec_ber'] = self.vdm_dict['Pre-FEC BER Average Media Input'][1][0]
210210
bulk_status['postfec_ber'] = self.vdm_dict['Errored Frames Average Media Input'][1][0]
211211
except (KeyError, TypeError):
@@ -1842,7 +1842,11 @@ def get_application_advertisement(self):
18421842

18431843
if not self.is_flat_memory():
18441844
# Read the application advertisement in page01
1845-
dic.update(self.xcvr_eeprom.read(consts.APPLS_ADVT_FIELD_PAGE01))
1845+
try:
1846+
dic.update(self.xcvr_eeprom.read(consts.APPLS_ADVT_FIELD_PAGE01))
1847+
except TypeError as e:
1848+
logger.error('Failed to read APPLS_ADVT_FIELD_PAGE01: ' + str(e))
1849+
return ret
18461850

18471851
for app in range(1, 16):
18481852
buf = {}

sonic_platform_base/sonic_xcvr/api/public/cmisVDM.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ def get_vdm_page(self, page, VDM_flag_page):
5050
if page not in [0x20, 0x21, 0x22, 0x23]:
5151
raise ValueError('Page not in VDM Descriptor range!')
5252
vdm_descriptor = self.xcvr_eeprom.read_raw(page * PAGE_SIZE + PAGE_OFFSET, PAGE_SIZE)
53-
53+
if not vdm_descriptor:
54+
return {}
55+
5456
# Odd Adress VDM observable type ID, real-time monitored value in Page + 4
5557
vdm_typeID = vdm_descriptor[1::2]
5658
# Even Address
@@ -83,6 +85,9 @@ def get_vdm_page(self, page, VDM_flag_page):
8385
vdm_thrsh_low_alarm_raw = self.xcvr_eeprom.read_raw(vdm_low_alarm_offset, VDM_SIZE, True)
8486
vdm_thrsh_high_warn_raw = self.xcvr_eeprom.read_raw(vdm_high_warn_offset, VDM_SIZE, True)
8587
vdm_thrsh_low_warn_raw = self.xcvr_eeprom.read_raw(vdm_low_warn_offset, VDM_SIZE, True)
88+
if not vdm_value_raw or not vdm_thrsh_high_alarm_raw or not vdm_thrsh_low_alarm_raw \
89+
or not vdm_high_warn_offset or not vdm_thrsh_low_warn_raw:
90+
return {}
8691
if vdm_format == 'S16':
8792
vdm_value = struct.unpack('>h',vdm_value_raw)[0] * scale
8893
vdm_thrsh_high_alarm = struct.unpack('>h', vdm_thrsh_high_alarm_raw)[0] * scale

tests/sonic_xcvr/test_cmisVDM.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,54 @@ def test_get_vdm_page(self, input_param, mock_response, expected):
6464
result = self.api.get_vdm_page(*input_param)
6565
assert result == expected
6666

67+
@pytest.mark.parametrize("input_param, mock_response, expected", [
68+
(
69+
[0x20, [0]*128], # input_param
70+
[ # mock_response
71+
(
72+
16, 9, 16, 11, 16, 13, 16, 15, 32, 10, 33, 10, 0, 0, 0, 0,
73+
80,128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
74+
160,143,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
75+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
76+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
77+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
78+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
79+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
80+
),
81+
82+
None,
83+
None,
84+
None,
85+
None,
86+
None,
87+
None,
88+
None,
89+
None,
90+
],
91+
{}
92+
)
93+
])
94+
def test_get_vdm_page_none_vdm_value_raw(self, input_param, mock_response, expected):
95+
self.api.xcvr_eeprom.read_raw = MagicMock()
96+
self.api.xcvr_eeprom.read_raw.side_effect = mock_response
97+
result = self.api.get_vdm_page(*input_param)
98+
assert result == expected
99+
100+
@pytest.mark.parametrize("input_param, mock_response, expected", [
101+
(
102+
[0x20, [0]*128], # input_param
103+
[ # mock_response
104+
None,
105+
],
106+
{}
107+
)
108+
])
109+
def test_get_vdm_page_none_vdm_descriptor(self, input_param, mock_response, expected):
110+
self.api.xcvr_eeprom.read_raw = MagicMock()
111+
self.api.xcvr_eeprom.read_raw.side_effect = mock_response
112+
result = self.api.get_vdm_page(*input_param)
113+
assert result == expected
114+
67115
@pytest.mark.parametrize("mock_response, expected", [
68116
(
69117
[ # mock_response

0 commit comments

Comments
 (0)