From 9a7a70ca2a50907f30181209f666fc685973d592 Mon Sep 17 00:00:00 2001 From: Qi Luo Date: Mon, 29 Jan 2018 16:08:07 -0800 Subject: [PATCH] Fix entPhysicalSerialNum OID (#63) * Fix entPhysicalSerialNum OID Signed-off-by: Qi Luo * Add unit test for PhysicalTableMIB Signed-off-by: Qi Luo --- src/sonic_ax_impl/mibs/ietf/rfc2737.py | 4 +- tests/mock_tables/dbconnector.py | 29 +++++++------ tests/mock_tables/state_db.json | 5 +++ tests/test_sn.py | 56 ++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 17 deletions(-) create mode 100644 tests/mock_tables/state_db.json create mode 100644 tests/test_sn.py diff --git a/src/sonic_ax_impl/mibs/ietf/rfc2737.py b/src/sonic_ax_impl/mibs/ietf/rfc2737.py index f77a896e731e..3ce7e09f39e9 100644 --- a/src/sonic_ax_impl/mibs/ietf/rfc2737.py +++ b/src/sonic_ax_impl/mibs/ietf/rfc2737.py @@ -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): """ @@ -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) diff --git a/tests/mock_tables/dbconnector.py b/tests/mock_tables/dbconnector.py index a9ca4a400d6f..88d2cfc2ed4b 100644 --- a/tests/mock_tables/dbconnector.py +++ b/tests/mock_tables/dbconnector.py @@ -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 '/' diff --git a/tests/mock_tables/state_db.json b/tests/mock_tables/state_db.json new file mode 100644 index 000000000000..86edfa8c9b0e --- /dev/null +++ b/tests/mock_tables/state_db.json @@ -0,0 +1,5 @@ +{ + "DEVICE_METADATA|localhost": { + "chassis_serial_number": "SAMPLETESTSN" + } +} diff --git a/tests/test_sn.py b/tests/test_sn.py new file mode 100644 index 000000000000..88d75a9a0f04 --- /dev/null +++ b/tests/test_sn.py @@ -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")