Browse Source

add the starting work on checking for changes..

ssh-lenovo
John-Mark Gurney 5 years ago
parent
commit
74fe8f5efb
3 changed files with 96 additions and 4 deletions
  1. +1
    -0
      requirements.txt
  2. +29
    -0
      test_data.py
  3. +66
    -4
      vlanmang.py

+ 1
- 0
requirements.txt View File

@@ -1,2 +1,3 @@
pysnmp pysnmp
coverage coverage
mock

+ 29
- 0
test_data.py View File

@@ -0,0 +1,29 @@
import vlanmang

##############
### Data ###
##############
vlans = {
1: 'def',
5: 'vlana',
283: 'vlanb',
}

def rng(s, e):
return range(s, e + 1)

distributionswitch = {
1: {
'u': [ 'lag1', 20, 21 ],
},
5: {
'u': rng(1,8),
't': [ 'lag1', 20, 21],
},
283: {
'u': rng(9,19) + [ 22, 23 ],
't': [ 'lag1' ]
},
}

distswitch = vlanmang.SwitchConfig('192.168.0.58', 'private', distributionswitch)

+ 66
- 4
vlanmang.py View File

@@ -5,7 +5,9 @@ from pysnmp.hlapi import *
from pysnmp.smi.builder import MibBuilder from pysnmp.smi.builder import MibBuilder
from pysnmp.smi.view import MibViewController from pysnmp.smi.view import MibViewController


import importlib
import itertools import itertools
import mock
import random import random
import unittest import unittest


@@ -32,6 +34,32 @@ _mvc = MibViewController(_mbuilder)
# LLDP: # LLDP:
# 1.0.8802.1.1.2.1.4.1.1 aka LLDP-MIB, lldpRemTable # 1.0.8802.1.1.2.1.4.1.1 aka LLDP-MIB, lldpRemTable


class SwitchConfig(object):
def __init__(self, host, community, vlanconf):
self._host = host
self._community = community
self._vlanconf = vlanconf

@property
def host(self):
return self._host

@property
def community(self):
return self._community

@property
def vlanconf(self):
return self._vlanconf

def checkchanges(module):
mod = importlib.import_module(module)
mods = [ i for i in mod.__dict__.itervalues() if isinstance(i, SwitchConfig) ]

for i in mods:
switch = SNMPSwitch(i.host, i.community)
portmapping = switch.getportmapping()

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
dictionary.''' dictionary.'''
@@ -134,6 +162,12 @@ class SNMPSwitch(object):
for varBind in varBinds: for varBind in varBinds:
yield varBind yield varBind


def getportmapping(self):
'''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'))

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
looks up via the ifName table in IF-MIB.''' looks up via the ifName table in IF-MIB.'''
@@ -170,6 +204,11 @@ class SNMPSwitch(object):
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'))


class _TestMisc(unittest.TestCase): class _TestMisc(unittest.TestCase):
def setUp(self):
import test_data

self._test_data = test_data

def test_pvid(self): def test_pvid(self):
data = { data = {
1: { 1: {
@@ -199,6 +238,19 @@ class _TestMisc(unittest.TestCase):
self.assertEqual(getportlist(data, lookup.__getitem__), self.assertEqual(getportlist(data, lookup.__getitem__),
set(xrange(1, 11)) | set(xrange(13, 20)) | set([30])) set(xrange(1, 11)) | set(xrange(13, 20)) | set([30]))


@mock.patch('vlanmang.SNMPSwitch.getportmapping')
@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')

imprt.assert_called_with('data')
portmapping.assert_called()

_skipSwitchTests = True _skipSwitchTests = True


class _TestSwitch(unittest.TestCase): class _TestSwitch(unittest.TestCase):
@@ -206,17 +258,27 @@ class _TestSwitch(unittest.TestCase):
args = open('test.creds').read().split() args = open('test.creds').read().split()
self.switch = SNMPSwitch(*args) self.switch = SNMPSwitch(*args)


def test_unpacktable(self):
pass
switchmodel = self.switch._get(('ENTITY-MIB',
'entPhysicalModelName', 1))
if switchmodel != 'GS108T smartSwitch' or \
_skipSwitchTests: # pragma: no cover
self.skipTest('Need a GS108T switch to run these tests')


@unittest.skipIf(_skipSwitchTests, 'slow')
def test_misc(self): def test_misc(self):
switch = self.switch switch = self.switch


self.assertEqual(switch.findport('g1'), 1) self.assertEqual(switch.findport('g1'), 1)
self.assertEqual(switch.findport('l1'), 14) self.assertEqual(switch.findport('l1'), 14)


@unittest.skipIf(_skipSwitchTests, 'slow')
def test_portnames(self):
switch = self.switch

resp = dict((x, 'g%d' % x) for x in xrange(1, 9))
resp.update({ 13: 'cpu' })
resp.update((x, 'l%d' % (x - 13)) for x in xrange(14, 18))

self.assertEqual(switch.getportmapping(), resp)

def test_vlan(self): def test_vlan(self):
switch = self.switch switch = self.switch




Loading…
Cancel
Save