[Git][NTPsec/ntpsec][master] Added tests for MIBControl.

Ian Bruene gitlab at mg.gitlab.com
Mon Mar 19 13:56:47 UTC 2018


Ian Bruene pushed to branch master at NTPsec / ntpsec


Commits:
968f07e3 by Ian Bruene at 2018-03-19T13:55:57Z
Added tests for MIBControl.

- - - - -


2 changed files:

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


Changes:

=====================================
pylib/agentx.py
=====================================
--- a/pylib/agentx.py
+++ b/pylib/agentx.py
@@ -23,9 +23,11 @@ pingTime = 60
 
 
 class MIBControl:
-    def __init__(self, oidTree={}, mibRoot=(), rangeSubid=0, upperBound=None,
+    def __init__(self, oidTree=None, mibRoot=(), rangeSubid=0, upperBound=None,
                  mibContext=None):
-        self.oidTree = oidTree  # contains callbacks for the MIB
+        self.oidTree = {}  # contains callbacks for the MIB
+        if oidTree is not None:
+            self.oidTree = oidTree
         # The undo system is only for the last operation
         self.inSetP = False  # Are we currently in the set procedure?
         self.setVarbinds = []  # Varbind of the current set operation
@@ -55,16 +57,12 @@ class MIBControl:
         currentLevel = self.oidTree
         remainingOID = oid
         while True:
-            print(remainingOID)
             node, remainingOID = ntp.util.slicedata(remainingOID, 1)
             node = node[0]
-            print("Nodes at this level:", currentLevel.keys())
             if node not in currentLevel.keys():
-                print("Adding node:", node)
                 currentLevel[node] = {"reader":None, "writer":None,
                                       "subids":None}
             if len(remainingOID) == 0:  # We have reached the target node
-                print("Reached target, filling it")
                 currentLevel[node]["reader"] = reader
                 currentLevel[node]["writer"] = writer
                 if dynamic is not None:
@@ -128,6 +126,10 @@ class MIBControl:
                         continue
                 elif oid > oidrange.start:
                     # If we are here it means we hit the start but skipped
+                    if (oidrange.end.isNull() is False) and \
+                       (oid >= oidrange.end):
+                        # We fell off the range
+                        return []
                     oids.append((oid, reader, writer))
                     break
             except StopIteration:
@@ -138,10 +140,11 @@ class MIBControl:
         # Start filling in the rest of the range
         while True:
             try:
-                oid, reader = gen.next()
+                oid, reader, writer = gen.next()
                 if reader is None:
                     continue  # skip unimplemented OIDs
-                elif (oidrange.end is not None) and (oid >= oidrange.end):
+                elif (oidrange.end.isNull() is False) and \
+                     (oid >= oidrange.end):
                     break  # past the end of a bounded range
                 else:
                     oids.append((oid, reader, writer))


=====================================
tests/pylib/test_agentx.py
=====================================
--- a/tests/pylib/test_agentx.py
+++ b/tests/pylib/test_agentx.py
@@ -2,8 +2,142 @@
 # -*- coding: utf-8 -*-
 
 import ntp.agentx as AX
+import ntp.agentx_packet as AP
 
 import unittest
+import types
+
+class TestMIBControl(unittest.TestCase):
+    def test___init__(self):
+        c = AX.MIBControl(1, 2, 3, 4, 5)
+        self.assertEqual(c.oidTree, 1)
+        self.assertEqual(c.inSetP, False)
+        self.assertEqual(c.setVarbinds, [])
+        self.assertEqual(c.setHandlers, [])
+        self.assertEqual(c.setUndoData, [])
+        self.assertEqual(c.mibRoot, 2)
+        self.assertEqual(c.rangeSubid, 3)
+        self.assertEqual(c.upperBound, 4)
+        self.assertEqual(c.context, 5)
+
+    def test_mib_rootOID(self):
+        c = AX.MIBControl(mibRoot="foo")
+        self.assertEqual(c.mib_rootOID(), "foo")
+
+    def test_mib_rangeSubid(self):
+        c = AX.MIBControl(rangeSubid="foo")
+        self.assertEqual(c.mib_rangeSubid(), "foo")
+
+    def test_mib_upperBound(self):
+        c = AX.MIBControl(upperBound="foo")
+        self.assertEqual(c.mib_upperBound(), "foo")
+
+    def test_mib_context(self):
+        c = AX.MIBControl(mibContext="foo")
+        self.assertEqual(c.mib_context(), "foo")
+
+    def test_addNode(self):
+        c = AX.MIBControl()
+        # Top level, non-dynamic
+        c.addNode((0,), 1, 2)
+        self.assertEqual(c.oidTree,
+                         {0:{"reader":1, "writer":2, "subids":None}})
+        # Top level, non-dynamic, class OID
+        c.addNode(AP.OID((1,)), 1, 2)
+        self.assertEqual(c.oidTree,
+                         {0:{"reader":1, "writer":2, "subids":None},
+                          1:{"reader":1, "writer":2, "subids":None}})
+        # Top level, dynamic
+        c.addNode((2,), dynamic=3)
+        self.assertEqual(c.oidTree,
+                         {0:{"reader":1, "writer":2, "subids":None},
+                          1:{"reader":1, "writer":2, "subids":None},
+                          2:{"reader":None, "writer":None, "subids":3}})
+        # Sub level
+        c.addNode((1, 2, 3), 1, 2)
+        self.assertEqual(c.oidTree,
+                         {0:{"reader":1, "writer":2, "subids":None},
+                          1:{"reader":1, "writer":2, "subids":
+                             {2:{"reader":None, "writer":None, "subids":{
+                                 3:{"reader":1, "writer":2, "subids":None}}}}},
+                          2:{"reader":None, "writer":None, "subids":3}})
+
+    def test_getOID_andNext(self):
+        c = AX.MIBControl()
+        c.addNode((0, 1))
+        c.addNode((0, 2, 0), 10, 11)
+        c.addNode((0, 2, 1), 20, 21)
+        c.addNode((0, 2, 2), 30, 31)
+        c.addNode((0, 2, 3), 40, 41)
+        c.addNode((0, 4, 1, 0), 50, 51)
+        # Test getOID
+        # Test non-existent, no generator
+        self.assertEqual(c.getOID(AP.OID((0, 0))), (None, None, None))
+        # Test empty, no generator
+        self.assertEqual(c.getOID(AP.OID((0, 1))),
+                         (None, None, None))
+        # Test empty, with generator
+        self.assertEqual(c.getOID(AP.OID((0, 1)), True),
+                         (None, None, None, None))
+        # Test existent, no generator
+        self.assertEqual(c.getOID(AP.OID((0, 2, 1))),
+                         (AP.OID((0, 2, 1)), 20, 21))
+        # Test existent, with generator
+        r = c.getOID(AP.OID((0, 2, 1)), True)
+        self.assertEqual(r[:3], (AP.OID((0, 2, 1)), 20, 21))
+        self.assertEqual(isinstance(r[3], types.GeneratorType), True)
+        # Test getNextOID
+        # Test non-existent, no generator
+        self.assertEqual(c.getNextOID(AP.OID((0, 0))),
+                         (AP.OID((0, 2, 0)), 10, 11))
+        # Test empty, no generator
+        self.assertEqual(c.getNextOID(AP.OID((0, 1))),
+                         (AP.OID((0, 2, 0)), 10, 11))
+        # Test empty, with generator
+        r = c.getNextOID(AP.OID((0, 1)), True)
+        self.assertEqual(r[:3], (AP.OID((0, 2, 0)), 10, 11))
+        self.assertEqual(isinstance(r[3], types.GeneratorType), True)
+        # Test existent, no generator
+        self.assertEqual(c.getNextOID(AP.OID((0, 2, 1))),
+                         (AP.OID((0, 2, 2)), 30, 31))
+        # Test existent, with generator
+        r = c.getNextOID(AP.OID((0, 2, 1)), True)
+        self.assertEqual(r[:3], (AP.OID((0, 2, 2)), 30, 31))
+        self.assertEqual(isinstance(r[3], types.GeneratorType), True)
+        # Test walking off the end
+        self.assertEqual(c.getNextOID(AP.OID((0, 4, 1, 0))),
+                         (None, None, None))
+
+    def test_getOIDsInRange(self):
+        c = AX.MIBControl()
+        c.addNode((0, 1))
+        c.addNode((0, 2, 0), 10, 11)
+        c.addNode((0, 2, 1), 20, 21)
+        c.addNode((0, 2, 2), 30, 31)
+        c.addNode((0, 2, 3), 40, 41)
+        c.addNode((0, 4, 1, 0), 50, 51)
+        # Test range too early
+        rng = AP.SearchRange((0, 0, 0), (0, 0, 5))
+        self.assertEqual(c.getOIDsInRange(rng), [])
+        # Test range too late
+        rng = AP.SearchRange((6, 0, 0), (6, 0, 5))
+        self.assertEqual(c.getOIDsInRange(rng), [])
+        # Test nothing implemented in range
+        rng = AP.SearchRange((0,), (0, 1, 5))
+        self.assertEqual(c.getOIDsInRange(rng), [])
+        # Test in range
+        rng = AP.SearchRange((0, 2, 0), (0, 2, 3))
+        self.assertEqual(c.getOIDsInRange(rng),
+                         [(AP.OID((0, 2, 1)), 20, 21),
+                          (AP.OID((0, 2, 2)), 30, 31)])
+        # Test unbounded range
+        rng = AP.SearchRange((0, 2, 0), ())
+        self.assertEqual(c.getOIDsInRange(rng),
+                         [(AP.OID((0, 2, 1)), 20, 21),
+                          (AP.OID((0, 2, 2)), 30, 31),
+                          (AP.OID((0, 2, 3)), 40, 41),
+                          (AP.OID((0, 4, 1, 0)), 50, 51)])
+
 
 class TestAgentx(unittest.TestCase):
     def test_walkMIBTree(self):
@@ -19,10 +153,10 @@ class TestAgentx(unittest.TestCase):
                                       "subids": None},
                                   5: {"reader": None, "writer": None,
                                       "subids": None}})),
-                         ((AX.OID((0,)), None, None),
-                          (AX.OID((1,)), None, None),
-                          (AX.OID((2,)), None, None),
-                          (AX.OID((5,)), None, None)))
+                         ((AP.OID((0,)), None, None),
+                          (AP.OID((1,)), None, None),
+                          (AP.OID((2,)), None, None),
+                          (AP.OID((5,)), None, None)))
         # Test nested, fully static tree
         self.assertEqual(tuple(f({0: {"reader": None, "writer": None,
                                       "subids": None},
@@ -37,12 +171,12 @@ class TestAgentx(unittest.TestCase):
                                                  "subids": None}}}}},
                                   5: {"reader": None, "writer": None,
                                       "subids": None}})),
-                         ((AX.OID((0,)), None, None),
-                          (AX.OID((1,)), None, None),
-                          (AX.OID((1, 0)), None, None),
-                          (AX.OID((1, 1)), None, None),
-                          (AX.OID((1, 1, 42)), None, None),
-                          (AX.OID((5,)), None, None)))
+                         ((AP.OID((0,)), None, None),
+                          (AP.OID((1,)), None, None),
+                          (AP.OID((1, 0)), None, None),
+                          (AP.OID((1, 1)), None, None),
+                          (AP.OID((1, 1, 42)), None, None),
+                          (AP.OID((5,)), None, None)))
         # Test nested, fully static tree, with rootpath
         self.assertEqual(tuple(f({0: {"reader": None, "writer": None,
                                       "subids": None},
@@ -58,12 +192,12 @@ class TestAgentx(unittest.TestCase):
                                   5: {"reader": None, "writer": None,
                                       "subids": None}},
                                  (23,))),
-                         ((AX.OID((23, 0)), None, None),
-                          (AX.OID((23, 1)), None, None),
-                          (AX.OID((23, 1, 0)), None, None),
-                          (AX.OID((23, 1, 1)), None, None),
-                          (AX.OID((23, 1, 1, 42)), None, None),
-                          (AX.OID((23, 5)), None, None)))
+                         ((AP.OID((23, 0)), None, None),
+                          (AP.OID((23, 1)), None, None),
+                          (AP.OID((23, 1, 0)), None, None),
+                          (AP.OID((23, 1, 1)), None, None),
+                          (AP.OID((23, 1, 1, 42)), None, None),
+                          (AP.OID((23, 5)), None, None)))
         # subid lambda for dynamic tree testing
         submaker = (lambda: {0: {"reader": None, "writer": None,
                                  "subids": None},
@@ -80,13 +214,13 @@ class TestAgentx(unittest.TestCase):
                                       "subids": submaker},
                                   2: {"reader": None, "writer": None,
                                       "subids": None}})),
-                         ((AX.OID((0,)), None, None),
-                          (AX.OID((1,)), None, None),
-                          (AX.OID((1, 0)), None, None),
-                          (AX.OID((1, 1)), None, None),
-                          (AX.OID((1, 1, 0)), None, None),
-                          (AX.OID((1, 2)), None, None),
-                          (AX.OID((2,)), None, None)))
+                         ((AP.OID((0,)), None, None),
+                          (AP.OID((1,)), None, None),
+                          (AP.OID((1, 0)), None, None),
+                          (AP.OID((1, 1)), None, None),
+                          (AP.OID((1, 1, 0)), None, None),
+                          (AP.OID((1, 2)), None, None),
+                          (AP.OID((2,)), None, None)))
         # Test tree with dynamic nodes and root path
         self.assertEqual(tuple(f({0: {"reader": None, "writer": None,
                                       "static": True, "subids": None},
@@ -95,10 +229,14 @@ class TestAgentx(unittest.TestCase):
                                   2: {"reader": None, "writer": None,
                                       "static": True, "subids": None}},
                                  (23,))),
-                         ((AX.OID((23, 0)), None, None),
-                          (AX.OID((23, 1)), None, None),
-                          (AX.OID((23, 1, 0)), None, None),
-                          (AX.OID((23, 1, 1)), None, None),
-                          (AX.OID((23, 1, 1, 0)), None, None),
-                          (AX.OID((23, 1, 2)), None, None),
-                          (AX.OID((23, 2)), None, None)))
+                         ((AP.OID((23, 0)), None, None),
+                          (AP.OID((23, 1)), None, None),
+                          (AP.OID((23, 1, 0)), None, None),
+                          (AP.OID((23, 1, 1)), None, None),
+                          (AP.OID((23, 1, 1, 0)), None, None),
+                          (AP.OID((23, 1, 2)), None, None),
+                          (AP.OID((23, 2)), None, None)))
+
+
+if __name__ == '__main__':
+    unittest.main()



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/968f07e3e7cbe1bad13c04fb35be2cf49b00621e

---
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/968f07e3e7cbe1bad13c04fb35be2cf49b00621e
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/20180319/4b28c0c0/attachment.html>


More information about the vc mailing list