From 2ac5432a1d559d57fd3596db0eccb93e1055784f Mon Sep 17 00:00:00 2001 From: liuh-80 Date: Wed, 23 Aug 2023 02:37:12 +0000 Subject: [PATCH 1/3] Fix key missing exception when invalied transiver info in STATE_DB --- src/sonic_ax_impl/mibs/ietf/rfc3433.py | 11 ++++++++++ tests/test_rfc3433.py | 28 ++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 tests/test_rfc3433.py diff --git a/src/sonic_ax_impl/mibs/ietf/rfc3433.py b/src/sonic_ax_impl/mibs/ietf/rfc3433.py index b389e6813..80b0f2f0b 100644 --- a/src/sonic_ax_impl/mibs/ietf/rfc3433.py +++ b/src/sonic_ax_impl/mibs/ietf/rfc3433.py @@ -372,6 +372,7 @@ def __init__(self): self.fan_sensor = [] self.psu_sensor = [] self.thermal_sensor = [] + self.broken_transceiver_info = [] def reinit_data(self): """ @@ -424,6 +425,16 @@ def update_xcvr_dom_data(self): # 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.error( + "Invalid interface {} in STATE_DB, \ + attribute 'type' missing in transceiver_info '{}'".format(interface, transceiver_info_entry_data)) + self.broken_transceiver_info.append(interface) + continue + if transceiver_info_entry_data['type'] == RJ45_PORT_TYPE: continue diff --git a/tests/test_rfc3433.py b/tests/test_rfc3433.py new file mode 100644 index 000000000..17608a3a0 --- /dev/null +++ b/tests/test_rfc3433.py @@ -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.error') as mocked_error: + updater.update_data() + + # check warning + mocked_error.assert_called() + + self.assertTrue(len(updater.sub_ids) == 0) \ No newline at end of file From 8f88ee20b4820ec961a18fa39107ae5762c3e42e Mon Sep 17 00:00:00 2001 From: liuh-80 Date: Wed, 30 Aug 2023 06:52:33 +0000 Subject: [PATCH 2/3] Fix PR comments --- src/sonic_ax_impl/mibs/ietf/rfc3433.py | 6 +++--- tests/test_rfc3433.py | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/sonic_ax_impl/mibs/ietf/rfc3433.py b/src/sonic_ax_impl/mibs/ietf/rfc3433.py index 80b0f2f0b..90021017e 100644 --- a/src/sonic_ax_impl/mibs/ietf/rfc3433.py +++ b/src/sonic_ax_impl/mibs/ietf/rfc3433.py @@ -385,6 +385,7 @@ def reinit_data(self): self.ent_phy_sensor_precision_map = {} self.ent_phy_sensor_value_map = {} self.ent_phy_sensor_oper_state_map = {} + self.broken_transceiver_info.clear() transceiver_dom_encoded = Namespace.dbs_keys(self.statedb, mibs.STATE_DB, self.TRANSCEIVER_DOM_KEY_PATTERN) if transceiver_dom_encoded: @@ -423,18 +424,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.error( + 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 diff --git a/tests/test_rfc3433.py b/tests/test_rfc3433.py index 17608a3a0..dd6e94f40 100644 --- a/tests/test_rfc3433.py +++ b/tests/test_rfc3433.py @@ -19,10 +19,10 @@ 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.error') as mocked_error: + with mock.patch('sonic_ax_impl.mibs.logger.warn') as mocked_warn: updater.update_data() # check warning - mocked_error.assert_called() + mocked_warn.assert_called() self.assertTrue(len(updater.sub_ids) == 0) \ No newline at end of file From 89a1aff59e43ae7dbeff3b75efb49a2e6570c8e6 Mon Sep 17 00:00:00 2001 From: liuh-80 Date: Thu, 31 Aug 2023 05:42:41 +0000 Subject: [PATCH 3/3] Revert unecessary change --- src/sonic_ax_impl/mibs/ietf/rfc3433.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/sonic_ax_impl/mibs/ietf/rfc3433.py b/src/sonic_ax_impl/mibs/ietf/rfc3433.py index 90021017e..89be78327 100644 --- a/src/sonic_ax_impl/mibs/ietf/rfc3433.py +++ b/src/sonic_ax_impl/mibs/ietf/rfc3433.py @@ -385,7 +385,6 @@ def reinit_data(self): self.ent_phy_sensor_precision_map = {} self.ent_phy_sensor_value_map = {} self.ent_phy_sensor_oper_state_map = {} - self.broken_transceiver_info.clear() transceiver_dom_encoded = Namespace.dbs_keys(self.statedb, mibs.STATE_DB, self.TRANSCEIVER_DOM_KEY_PATTERN) if transceiver_dom_encoded: