Skip to content

Commit

Permalink
Fix bug: if one mgmt port does not exist in StateDB, treat the oper/a…
Browse files Browse the repository at this point in the history
…dmin status as unknown (sonic-net#118)

* Fix bug: if one mgmt port does not exist in StateDB, treat the
oper/admin status as unknown
* Add unit test
  • Loading branch information
qiluo-msft authored Nov 18, 2019
1 parent 28f9e5d commit 862e51a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/sonic_ax_impl/mibs/ietf/rfc1213.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand All @@ -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
Expand All @@ -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"])
Expand Down
6 changes: 6 additions & 0 deletions tests/mock_tables/config_db.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,11 @@
"admin_status": "up",
"alias": "mgmt1",
"speed": 1000
},
"MGMT_PORT|eth2": {
"description": "NotExistInStateDB",
"admin_status": "up",
"alias": "mgmt2",
"speed": 1000
}
}
19 changes: 19 additions & 0 deletions tests/test_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 862e51a

Please sign in to comment.