Browse Source

advance the check changes a bit, and add a function to fetch pvids

ssh-lenovo
John-Mark Gurney 5 years ago
parent
commit
1833266506
1 changed files with 34 additions and 7 deletions
  1. +34
    -7
      vlanmang.py

+ 34
- 7
vlanmang.py View File

@@ -59,6 +59,14 @@ def checkchanges(module):
for i in mods: for i in mods:
switch = SNMPSwitch(i.host, i.community) switch = SNMPSwitch(i.host, i.community)
portmapping = switch.getportmapping() 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): def getpvidmapping(data, lookupfun):
'''Return a mapping from vlan based table to a port: vlan '''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 '''Return a port name mapping. Keys are the port index
and the value is the name from the ifName entry.''' 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): def findport(self, name):
'''Look up a port name and return it's port index. This '''Look up a port name and return it's port index. This
@@ -193,16 +201,23 @@ class SNMPSwitch(object):
int(vlan)), 6) # destroy(6) int(vlan)), 6) # destroy(6)


def getvlans(self): 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')) return (x[0][-1] for x in self._walk('Q-BRIDGE-MIB', 'dot1qVlanStatus'))


def staticvlans(self): 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')) 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): class _TestMisc(unittest.TestCase):
def setUp(self): def setUp(self):
import test_data import test_data
@@ -242,16 +257,24 @@ class _TestMisc(unittest.TestCase):
@mock.patch('importlib.import_module') @mock.patch('importlib.import_module')
def test_checkchanges(self, imprt, portmapping): def test_checkchanges(self, imprt, portmapping):
def tmp(*args, **kwargs): def tmp(*args, **kwargs):
print `args`
return self._test_data return self._test_data
imprt.side_effect = tmp 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') imprt.assert_called_with('data')
portmapping.assert_called() portmapping.assert_called()


_skipSwitchTests = True
del ports[31]

checkchanges('data')

_skipSwitchTests = False


class _TestSwitch(unittest.TestCase): class _TestSwitch(unittest.TestCase):
def setUp(self): def setUp(self):
@@ -294,6 +317,10 @@ class _TestSwitch(unittest.TestCase):


self.assertTrue(set(switch.staticvlans()).issubset(existingvlans)) 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' testname = 'Sometestname'


# Create test vlan # Create test vlan


Loading…
Cancel
Save