Skip to content

Commit

Permalink
Fix entPhysicalSerialNum OID (sonic-net#63)
Browse files Browse the repository at this point in the history
* Fix entPhysicalSerialNum OID

Signed-off-by: Qi Luo <qiluo-msft@users.noreply.github.com>

* Add unit test for PhysicalTableMIB

Signed-off-by: Qi Luo <qiluo-msft@users.noreply.github.com>
  • Loading branch information
qiluo-msft committed Jan 30, 2018
1 parent 20f3bce commit 9a7a70c
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/sonic_ax_impl/mibs/ietf/rfc2737.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def reinit_data(self):
self.physical_classes = [(self.CHASSIS_ID, )]
self.physical_classes_map = {
(self.CHASSIS_ID, ): (PhysicalClass.CHASSIS, chassis_serial_number.decode("utf-8"))
}
}

def update_data(self):
"""
Expand Down Expand Up @@ -107,4 +107,4 @@ class PhysicalTableMIB(metaclass=MIBMeta, prefix='.1.3.6.1.2.1.47.1.1.1'):
SubtreeMIBEntry('1.5', updater, ValueType.INTEGER, updater.get_physical_class)

entPhysicalSerialNum = \
SubtreeMIBEntry('1.1.1.11', updater, ValueType.OCTET_STRING, updater.get_serial_number)
SubtreeMIBEntry('1.11', updater, ValueType.OCTET_STRING, updater.get_serial_number)
29 changes: 14 additions & 15 deletions tests/mock_tables/dbconnector.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,22 @@ def __init__(self, *args, **kwargs):
super(SwssSyncClient, self).__init__(strict=True, *args, **kwargs)
db = kwargs.pop('db')
if db == 0:
with open(INPUT_DIR + '/appl_db.json') as f:
db = json.load(f)
for h, table in db.items():
for k, v in table.items():
self.hset(h, k, v)
fname = 'appl_db.json'
elif db == 1:
with open(INPUT_DIR + '/asic_db.json') as f:
db = json.load(f)
for h, table in db.items():
for k, v in table.items():
self.hset(h, k, v)
fname = 'asic_db.json'
elif db == 2:
with open(INPUT_DIR + '/counters_db.json') as f:
db = json.load(f)
for h, table in db.items():
for k, v in table.items():
self.hset(h, k, v)
fname = 'counters_db.json'
elif db == 6:
fname = 'state_db.json'
else:
raise ValueError("Invalid db")

fname = os.path.join(INPUT_DIR, fname)
with open(fname) as f:
js = json.load(f)
for h, table in js.items():
for k, v in table.items():
self.hset(h, k, v)

# Patch mockredis/mockredis/client.py
# The official implementation will filter out keys with a slash '/'
Expand Down
5 changes: 5 additions & 0 deletions tests/mock_tables/state_db.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"DEVICE_METADATA|localhost": {
"chassis_serial_number": "SAMPLETESTSN"
}
}
56 changes: 56 additions & 0 deletions tests/test_sn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import os
import sys

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

from unittest import TestCase

# noinspection PyUnresolvedReferences
import tests.mock_tables.dbconnector

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

class TestSonicMIB(TestCase):
@classmethod
def setUpClass(cls):
cls.lut = MIBTable(SonicMIB)

def test_getnextpdu_entPhysicalClass(self):
get_pdu = GetNextPDU(
header=PDUHeader(1, PduTypes.GET, 16, 0, 42, 0, 0, 0),
oids=(
ObjectIdentifier(12, 0, 0, 0, (1, 3, 6, 1, 2, 1, 47, 1, 1, 1, 1, 5)),
)
)

encoded = get_pdu.encode()
response = get_pdu.make_response(self.lut)

value0 = response.values[0]
self.assertEqual(str(value0.name), str(ObjectIdentifier(12, 0, 1, 0, (1, 3, 6, 1, 2, 1, 47, 1, 1, 1, 1, 5, 1))))
self.assertEqual(value0.type_, ValueType.INTEGER)
self.assertEqual(value0.data, 3)

def test_getnextpdu_entPhysicalSerialNum(self):
get_pdu = GetNextPDU(
header=PDUHeader(1, PduTypes.GET, 16, 0, 42, 0, 0, 0),
oids=(
ObjectIdentifier(20, 0, 0, 0, (1, 3, 6, 1, 2, 1, 47, 1, 1, 1, 1, 11)),
)
)

encoded = get_pdu.encode()
response = get_pdu.make_response(self.lut)

value0 = response.values[0]
self.assertEqual(str(value0.name), str(ObjectIdentifier(12, 0, 1, 0, (1, 3, 6, 1, 2, 1, 47, 1, 1, 1, 1, 11, 1))))
self.assertEqual(value0.type_, ValueType.OCTET_STRING)
self.assertEqual(str(value0.data), "SAMPLETESTSN")

0 comments on commit 9a7a70c

Please sign in to comment.