diff --git a/vlanmang.py b/vlanmang.py index 5a314b7..d3413b1 100644 --- a/vlanmang.py +++ b/vlanmang.py @@ -59,6 +59,14 @@ def checkchanges(module): for i in mods: switch = SNMPSwitch(i.host, i.community) portmapping = switch.getportmapping() + invportmap = { y: x for x, y in portmapping.iteritems() } + + portlist = getportlist(i._vlanconf, invportmap.__getitem__) + + ports = set(portmapping.iterkeys()) + + if ports != portlist: + raise ValueError('missing or extra ports found: %s' % `ports.symmetric_difference(portlist)`) def getpvidmapping(data, lookupfun): '''Return a mapping from vlan based table to a port: vlan @@ -166,7 +174,7 @@ class SNMPSwitch(object): '''Return a port name mapping. Keys are the port index and the value is the name from the ifName entry.''' - return dict((x[0][-1], str(x[1])) for x in self._walk('IF-MIB', 'ifName')) + return { x[0][-1]: str(x[1]) for x in self._walk('IF-MIB', 'ifName') } def findport(self, name): '''Look up a port name and return it's port index. This @@ -193,16 +201,23 @@ class SNMPSwitch(object): int(vlan)), 6) # destroy(6) def getvlans(self): - '''Return all the vlans.''' + '''Return an iterator with all the vlan ids.''' return (x[0][-1] for x in self._walk('Q-BRIDGE-MIB', 'dot1qVlanStatus')) def staticvlans(self): - '''Return the staticly defined/configured vlans. This - sometimes excludes special built in vlans, like vlan 1.''' + '''Return an iterator of the staticly defined/configured + vlans. This sometimes excludes special built in vlans, + like vlan 1.''' return (x[0][-1] for x in self._walk('Q-BRIDGE-MIB', 'dot1qVlanStaticName')) + def getpvid(self): + '''Returns a dictionary w/ the interface index as the key, + and the pvid of the interface.''' + + return { x[0][-1]: int(x[1]) for x in self._walk('Q-BRIDGE-MIB', 'dot1qPvid') } + class _TestMisc(unittest.TestCase): def setUp(self): import test_data @@ -242,16 +257,24 @@ class _TestMisc(unittest.TestCase): @mock.patch('importlib.import_module') def test_checkchanges(self, imprt, portmapping): def tmp(*args, **kwargs): - print `args` return self._test_data imprt.side_effect = tmp - checkchanges('data') + ports = { x: 'g%d' % x for x in xrange(1, 24) } + ports[30] = 'lag1' + ports[31] = 'lag2' + portmapping.side_effect = [ ports, ports ] + + self.assertRaises(ValueError, checkchanges, 'data') imprt.assert_called_with('data') portmapping.assert_called() -_skipSwitchTests = True + del ports[31] + + checkchanges('data') + +_skipSwitchTests = False class _TestSwitch(unittest.TestCase): def setUp(self): @@ -294,6 +317,10 @@ class _TestSwitch(unittest.TestCase): self.assertTrue(set(switch.staticvlans()).issubset(existingvlans)) + pvidres = { x: 1 for x in xrange(1, 9) } + pvidres.update({ x: 1 for x in xrange(14, 18) }) + self.assertEqual(switch.getpvid(), pvidres) + testname = 'Sometestname' # Create test vlan