[Git][NTPsec/ntpsec][master] MIB tree/flat conversion functions now use OIDs instead of tuples

Ian Bruene gitlab at mg.gitlab.com
Sun Oct 22 16:38:25 UTC 2017


Ian Bruene pushed to branch master at NTPsec / ntpsec


Commits:
83985602 by Ian Bruene at 2017-10-22T11:37:15-05:00
MIB tree/flat conversion functions now use OIDs instead of tuples

- - - - -


2 changed files:

- pylib/agentx.py
- tests/pylib/test_agentx.py


Changes:

=====================================
pylib/agentx.py
=====================================
--- a/pylib/agentx.py
+++ b/pylib/agentx.py
@@ -1263,15 +1263,20 @@ def mibTree2List(mibtree, currentPath=()):
     branches = list(mibtree.keys())
     branches.sort()
     for branch in branches:
-        paths.append(currentPath + (branch,))
+        paths.append(OID(currentPath + (branch,)))
         branchPath = currentPath + (branch,)
         paths += mibTree2List(mibtree[branch], branchPath)
     return tuple(paths)
 
-def mibList2Tree(miblist):
+def mibList2Tree(miblist, rootPath=()):
     "Takes a list of OIDs and inflates it into a tree"
     tree = {}
-    for node in miblist:
+    for oid in miblist:
+        node = oid.subids
+        rootlen = len(rootPath)
+        if node[:rootlen] != rootPath:  # OID is not decended from the root, bail
+            raise ValueError("Node %s does not have root %s" % (node, rootPath))
+        node = node[rootlen:]  # clip the root off for the tree
         branch = tree
         nodePos = 0
         nodeSize = len(node)


=====================================
tests/pylib/test_agentx.py
=====================================
--- a/tests/pylib/test_agentx.py
+++ b/tests/pylib/test_agentx.py
@@ -2450,36 +2450,51 @@ class TestNtpclientsNtpsnmpd(unittest.TestCase):
         self.assertEqual(fail, True)
 
     def test_mibTree2List(self):
-        f = ntp.agentx.mibTree2List
+        x = ntp.agentx
+        f = x.mibTree2List
 
         # Test empty tree
         self.assertEqual(f({}), ())
         # Test flat tree
         self.assertEqual(f({0: None, 1: None, 3: None, 4: None}),
-                         ((0,), (1,), (3,), (4,)))
+                         (x.OID((0,)), x.OID((1,)), x.OID((3,)), x.OID((4,))))
+        # Test flat tree with root path
+        self.assertEqual(f({0: None, 1: None, 3: None, 4: None}, (42, 23)),
+                         (x.OID((42, 23, 0)), x.OID((42, 23, 1)),
+                          x.OID((42, 23, 3)), x.OID((42, 23, 4))))
         # Test nested tree
         self.assertEqual(f({0: None,
                             2: {0: None, 1: None},
                             3: {5: {0: None, 2: None}, 6: None},
                             4: None}),
-                         ((0,),
-                          (2,), (2, 0), (2, 1),
-                          (3,), (3, 5), (3, 5, 0), (3, 5, 2), (3, 6),
-                          (4,)))
+                         (x.OID((0,)),
+                          x.OID((2,)), x.OID((2, 0)), x.OID((2, 1)),
+                          x.OID((3,)),
+                          x.OID((3, 5)), x.OID((3, 5, 0)), x.OID((3, 5, 2)),
+                          x.OID((3, 6)),
+                          x.OID((4,))))
 
     def test_mibList2Tree(self):
-        f = ntp.agentx.mibList2Tree
+        x = ntp.agentx
+        f = x.mibList2Tree
 
         # Test empty tree
         self.assertEqual(f(tuple()), {})
         # Test flat tree
-        self.assertEqual(f(((0,), (1,), (3,), (4,))),
+        self.assertEqual(f((x.OID((0,)), x.OID((1,)),
+                            x.OID((3,)), x.OID((4,)))),
+                         {0: None, 1: None, 3: None, 4:None})
+        # Test flat tree with root path
+        self.assertEqual(f((x.OID((42, 23, 0)), x.OID((42, 23, 1)),
+                            x.OID((42, 23, 3)), x.OID((42, 23, 4))), (42, 23)),
                          {0: None, 1: None, 3: None, 4:None})
         # Test nested tree
-        self.assertEqual(f(((0,),
-                            (2,), (2, 0), (2, 1),
-                            (3,), (3, 5), (3, 5, 0), (3, 5, 2), (3, 6),
-                            (4,))),
+        self.assertEqual(f((x.OID((0,)),
+                            x.OID((2,)), x.OID((2, 0)), x.OID((2, 1)),
+                            x.OID((3,)),
+                            x.OID((3, 5)), x.OID((3, 5, 0)), x.OID((3, 5, 2)),
+                            x.OID((3, 6)),
+                            x.OID((4,)))),
                          {0: None,
                           2: {0: None, 1: None},
                           3: {5: {0: None, 2: None}, 6: None},



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/839856022968cb644b134cd5ba8062927338b3d8

---
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/839856022968cb644b134cd5ba8062927338b3d8
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20171022/ea5824d9/attachment.html>


More information about the vc mailing list