Skip to content

Commit

Permalink
Fix key missing exception when invalied transiver info in STATE_DB (#289
Browse files Browse the repository at this point in the history
)

Fix PhysicalSensorTableMIBUpdater crash when 'type' attribute missing.

#### Work item tracking
Microsoft ADO (number only): 24869277

**- What I did**
Fix PhysicalSensorTableMIBUpdater crash when 'type' attribute missing.

**- How I did it**
Check 'type' attribute, if missing write error log in PhysicalSensorTableMIBUpdater.

**- How to verify it**
Manually test.
Pass all UT

**- Description for the changelog**
Fix PhysicalSensorTableMIBUpdater crash when 'type' attribute missing.
  • Loading branch information
liuh-80 authored and StormLiangMS committed Oct 17, 2023
1 parent ba7ff87 commit ead6e0f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/sonic_ax_impl/mibs/ietf/rfc3433.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ def __init__(self):
self.fan_sensor = []
self.psu_sensor = []
self.thermal_sensor = []
self.broken_transceiver_info = []

def reinit_data(self):
"""
Expand Down Expand Up @@ -422,8 +423,17 @@ def update_xcvr_dom_data(self):
in STATE_DB, skipping".format(transceiver_dom_entry))
continue

# skip RJ45 port
transceiver_info_entry_data = Namespace.dbs_get_all(self.statedb, mibs.STATE_DB, mibs.transceiver_info_table(interface))
if 'type' not in transceiver_info_entry_data:
# Only write error log once
if interface not in self.broken_transceiver_info:
mibs.logger.warn(
"Invalid interface {} in STATE_DB, \
attribute 'type' missing in transceiver_info '{}'".format(interface, transceiver_info_entry_data))
self.broken_transceiver_info.append(interface)
continue

# skip RJ45 port
if transceiver_info_entry_data['type'] == RJ45_PORT_TYPE:
continue

Expand Down
28 changes: 28 additions & 0 deletions tests/test_rfc3433.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import os
import sys
from unittest import TestCase

if sys.version_info.major == 3:
from unittest import mock
else:
import mock

modules_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(modules_path, 'src'))

from sonic_ax_impl.mibs.ietf.rfc3433 import PhysicalSensorTableMIBUpdater

class TestPhysicalSensorTableMIBUpdater(TestCase):

@mock.patch('sonic_ax_impl.mibs.Namespace.dbs_get_all', mock.MagicMock(return_value=({"hardwarerev": "1.0"})))
def test_PhysicalSensorTableMIBUpdater_transceiver_info_key_missing(self):
updater = PhysicalSensorTableMIBUpdater()
updater.transceiver_dom.append("TRANSCEIVER_INFO|Ethernet0")

with mock.patch('sonic_ax_impl.mibs.logger.warn') as mocked_warn:
updater.update_data()

# check warning
mocked_warn.assert_called()

self.assertTrue(len(updater.sub_ids) == 0)

0 comments on commit ead6e0f

Please sign in to comment.