Skip to content

Commit

Permalink
Fix SubtreeMIBEntry iter() (sonic-net#33)
Browse files Browse the repository at this point in the history
There is a bug if query a SubtreeMIBEntry with a short OID, it will not
return the first element in the subtree. For example:

1.3.6.1.2.1.4.24.4.1.1: work
1.3.6.1.2.1.4.24.4.1: not work
  • Loading branch information
qiluo-msft authored Jun 12, 2017
1 parent ef78249 commit 7c2c18c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/ax_interface/mib.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def __iter__(self):
sub_id = self.iterator.get_next(sub_id)
if sub_id is None:
break
yield self.subtree + sub_id
yield sub_id

def __call__(self, sub_id):
assert isinstance(sub_id, tuple)
Expand Down Expand Up @@ -352,7 +352,7 @@ def get_next(self, sr):

val1 = mib_entry(key1)
if val1 is None:
# handler returned None, which implies there's no data, keep walking.
logger.error('MIBTable.get_next found an invalid key: {}+{}'.format(mib_entry.subtree, key1))
remaining_oids = remaining_oids[1:]
continue

Expand Down
19 changes: 19 additions & 0 deletions tests/test_forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,25 @@ def test_network_order(self):
ips = ".".join(str(int(x)) for x in list(ipb))
self.assertEqual(ips, "0.1.2.3")

def test_getnextpdu_first(self):
# oid.include = 1
oid = ObjectIdentifier(10, 0, 1, 0, (1, 3, 6, 1, 2, 1, 4, 24, 4, 1))
get_pdu = GetNextPDU(
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)

n = len(response.values)
# self.assertEqual(n, 7)
value0 = response.values[0]
self.assertEqual(value0.type_, ValueType.IP_ADDRESS)
self.assertEqual(str(value0.name), '.1.3.6.1.2.1.4.24.4.1.1.0.0.0.0.0.0.0.0.0.10.0.0.1')
self.assertEqual(str(value0.data), ipaddress.ip_address("0.0.0.0").packed.decode())

def test_getpdu(self):
oid = ObjectIdentifier(24, 0, 1, 0, (1, 3, 6, 1, 2, 1, 4, 24, 4, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 0, 15))
get_pdu = GetPDU(
Expand Down
24 changes: 24 additions & 0 deletions tests/test_hc_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@

from unittest import TestCase

from ax_interface import ValueType
from ax_interface.pdu import PDU
from ax_interface.mib import MIBTable
from ax_interface.pdu_implementations import GetPDU, GetNextPDU
from ax_interface.encodings import ObjectIdentifier
from ax_interface.constants import PduTypes
from ax_interface.pdu import PDU, PDUHeader
from sonic_ax_impl.mibs.ietf import rfc2863


Expand All @@ -19,6 +24,25 @@ class TestGetNextPDU(TestCase):
def setUpClass(cls):
cls.lut = MIBTable(rfc2863.InterfaceMIBObjects)

def test_getnextpdu_firstifalias(self):
# oid.include = 1
oid = ObjectIdentifier(10, 0, 1, 0, (1, 3, 6, 1, 2, 1, 31, 1, 1, 1, 18))
get_pdu = GetNextPDU(
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)

n = len(response.values)
# self.assertEqual(n, 7)
value0 = response.values[0]
self.assertEqual(value0.type_, ValueType.OCTET_STRING)
self.assertEqual(str(value0.name), str(ObjectIdentifier(11, 0, 1, 0, (1, 3, 6, 1, 2, 1, 31, 1, 1, 1, 18, 1))))
self.assertEqual(str(value0.data), 'Ethernet0')

def test_get_next_alias(self):
if_alias = b'\x01\x06\x10\x00\x00\x00\x00o\x00\x01\xcc4\x00\x01\xcc5\x00\x00\x000\x07\x02\x00\x00\x00\x00\x00\x01\x00\x00\x00\x1f\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x11\x00\x00\x00}\x03\x02\x00\x00\x00\x00\x00\x01\x00\x00\x00\x1f\x00\x00\x00\x02'
pdu = PDU.decode(if_alias)
Expand Down
3 changes: 0 additions & 3 deletions tests/test_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def test_getnextpdu_noneifindex(self):
# self.assertEqual(n, 7)
value0 = response.values[0]
self.assertEqual(value0.type_, ValueType.INTEGER)
print("test_getnextpdu_exactmatch: ", str(oid))
self.assertEqual(str(value0.name), str(ObjectIdentifier(11, 0, 1, 0, (1, 3, 6, 1, 2, 1, 2, 2, 1, 1, 1))))
self.assertEqual(value0.data, 0)

Expand All @@ -58,7 +57,6 @@ def test_getnextpdu_firstifindex(self):
# self.assertEqual(n, 7)
value0 = response.values[0]
self.assertEqual(value0.type_, ValueType.INTEGER)
print("test_getnextpdu_exactmatch: ", str(oid))
self.assertEqual(str(value0.name), str(ObjectIdentifier(11, 0, 1, 0, (1, 3, 6, 1, 2, 1, 2, 2, 1, 1, 1))))
self.assertEqual(value0.data, 0)

Expand All @@ -77,7 +75,6 @@ def test_getnextpdu_secondifindex(self):
# self.assertEqual(n, 7)
value0 = response.values[0]
self.assertEqual(value0.type_, ValueType.INTEGER)
print("test_getnextpdu_exactmatch: ", str(oid))
self.assertEqual(str(value0.name), str(ObjectIdentifier(11, 0, 1, 0, (1, 3, 6, 1, 2, 1, 2, 2, 1, 1, 5))))
self.assertEqual(value0.data, 4)

Expand Down

0 comments on commit 7c2c18c

Please # to comment.