From 862e51ab85d48290082adfcbb801bfbbe3a95bf3 Mon Sep 17 00:00:00 2001 From: Qi Luo Date: Mon, 18 Nov 2019 11:02:28 -0800 Subject: [PATCH] Fix bug: if one mgmt port does not exist in StateDB, treat the oper/admin status as unknown (#118) * Fix bug: if one mgmt port does not exist in StateDB, treat the oper/admin status as unknown * Add unit test --- src/sonic_ax_impl/mibs/ietf/rfc1213.py | 14 ++++++++++---- tests/mock_tables/config_db.json | 6 ++++++ tests/test_interfaces.py | 19 +++++++++++++++++++ 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/sonic_ax_impl/mibs/ietf/rfc1213.py b/src/sonic_ax_impl/mibs/ietf/rfc1213.py index 6c383e48e1c2..30191656e018 100644 --- a/src/sonic_ax_impl/mibs/ietf/rfc1213.py +++ b/src/sonic_ax_impl/mibs/ietf/rfc1213.py @@ -337,7 +337,7 @@ def _get_if_entry_state_db(self, sub_id): else: return None - return self.db_conn.get_all(db, if_table, blocking=True) + return self.db_conn.get_all(db, if_table, blocking=False) def _get_status(self, sub_id, key): """ @@ -347,7 +347,12 @@ def _get_status(self, sub_id, key): """ status_map = { b"up": 1, - b"down": 2 + b"down": 2, + b"testing": 3, + b"unknown": 4, + b"dormant": 5, + b"notPresent": 6, + b"lowerLayerDown": 7 } # Once PORT_TABLE will be moved to CONFIG DB @@ -357,11 +362,12 @@ def _get_status(self, sub_id, key): entry = self._get_if_entry_state_db(sub_id) else: entry = self._get_if_entry(sub_id) + if not entry: - return + return status_map.get(b"unknown") # Note: If interface never become up its state won't be reflected in DB entry - # If state is not in DB entry assume interface is down + # If state key is not in DB entry assume interface is down state = entry.get(key, b"down") return status_map.get(state, status_map[b"down"]) diff --git a/tests/mock_tables/config_db.json b/tests/mock_tables/config_db.json index f5b09c4279ab..a61777fea344 100644 --- a/tests/mock_tables/config_db.json +++ b/tests/mock_tables/config_db.json @@ -8,5 +8,11 @@ "admin_status": "up", "alias": "mgmt1", "speed": 1000 + }, + "MGMT_PORT|eth2": { + "description": "NotExistInStateDB", + "admin_status": "up", + "alias": "mgmt2", + "speed": 1000 } } diff --git a/tests/test_interfaces.py b/tests/test_interfaces.py index 1e0d7b988a0d..5b54dec3d35d 100644 --- a/tests/test_interfaces.py +++ b/tests/test_interfaces.py @@ -289,6 +289,25 @@ def test_mgmt_iface_oper_status(self): self.assertEqual(str(value0.name), str(ObjectIdentifier(11, 0, 1, 0, (1, 3, 6, 1, 2, 1, 2, 2, 1, 8, 10001)))) self.assertEqual(value0.data, 1) + def test_mgmt_iface_oper_status_unknown(self): + """ + Test mgmt port operative status + """ + oid = ObjectIdentifier(11, 0, 0, 0, (1, 3, 6, 1, 2, 1, 2, 2, 1, 8, 10002)) + get_pdu = GetPDU( + header=PDUHeader(1, PduTypes.GET, 16, 0, 42, 0, 0, 0), + oids=[oid] + ) + + encoded = get_pdu.encode() + response = get_pdu.make_response(self.lut) + print(response) + + value0 = response.values[0] + self.assertEqual(value0.type_, ValueType.INTEGER) + self.assertEqual(str(value0.name), str(ObjectIdentifier(11, 0, 1, 0, (1, 3, 6, 1, 2, 1, 2, 2, 1, 8, 10002)))) + self.assertEqual(value0.data, 4) + def test_mgmt_iface_admin_status(self): """ Test mgmt port admin status