Skip to content
Snippets Groups Projects
Commit 591412b5 authored by Taya Snijder's avatar Taya Snijder
Browse files

Added test pymib file and tests

parent 09b0df9d
No related branches found
No related tags found
1 merge request!288Resolve L2SS-446 "Extend snmp client to support mib files"
...@@ -3,6 +3,7 @@ from tangostationcontrol.clients.comms_client import CommClient ...@@ -3,6 +3,7 @@ from tangostationcontrol.clients.comms_client import CommClient
from pysnmp import hlapi from pysnmp import hlapi
from pysnmp.smi import builder from pysnmp.smi import builder
from pathlib import Path
import numpy import numpy
import logging import logging
...@@ -232,20 +233,26 @@ class snmp_attribute: ...@@ -232,20 +233,26 @@ class snmp_attribute:
return vals return vals
from pysmi import debug
debug.setLogger(debug.Debug('all'))
class mib_loader: class mib_loader:
def __init__(self): def __init__(self):
self.mibBuilder = builder.MibBuilder() self.mibBuilder = builder.MibBuilder()
def set_pymib_dir(self, mib_dir): def set_pymib_dir(self, mib_dir : str):
mib_source = builder.DirMibSource(mib_dir) abs_path = str(Path().absolute()).replace("\\", "/")
if mib_dir[0] != "/":
mib_dir = "/" + mib_dir
dir_path = abs_path + mib_dir
mib_source = builder.DirMibSource(dir_path)
self.mibBuilder.addMibSources(mib_source) self.mibBuilder.addMibSources(mib_source)
def load_pymib(self, mib_name): def load_pymib(self, mib_name):
self.mibBuilder.loadModule(mib_name) self.mibBuilder.loadModule(mib_name)
def compile_mibs(self, path):
from pysnmp.smi import compiler
compiler.addMibCompiler(self.mibBuilder, sources=[path, ])
Integer32, MibScalar, MibTable, MibTableRow, MibTableColumn, TimeTicks, iso, Gauge32, MibIdentifier, Bits, Counter32 = mibBuilder.importSymbols("SNMPv2-SMI", "Integer32", "MibScalar", "MibTable", "MibTableRow", "MibTableColumn", "TimeTicks", "iso", "Gauge32", "MibIdentifier", "Bits","Counter32")
ConstraintsIntersection, ConstraintsUnion, ValueSizeConstraint, SingleValueConstraint, ValueRangeConstraint = mibBuilder.importSymbols("ASN1-REFINEMENT", "ConstraintsIntersection", "ConstraintsUnion", "ValueSizeConstraint", "SingleValueConstraint", "ValueRangeConstraint")
NamedValues, = mibBuilder.importSymbols("ASN1-ENUMERATION", "NamedValues")
testNamedValue = MibScalar((9, 8, 7, 6, 5, 4, 3, 2, 1), Integer32().subtype(subtypeSpec=ConstraintsUnion(SingleValueConstraint(1, 2, 3, 4, 5, 6, 7))).clone(namedValues=NamedValues(("A", 1), ("B", 2), ("C", 3), ("D", 4)))).setMaxAccess("readonly")
mibBuilder.exportSymbols("TEST-MIB", testNamedValue=testNamedValue)
from pysnmp import hlapi
import numpy
from unittest import mock from unittest import mock
from tangostationcontrol.test import base from tangostationcontrol.test import base
from tangostationcontrol.clients.snmp_client import mib_loader
from tangostationcontrol.clients.snmp_client import mib_loader
from pysnmp.smi import builder, view
import pysnmp.hlapi as pysnmp
from pysnmp.smi.rfc1902 import ObjectIdentity
class TestMibLoading(base.TestCase): class TestMibLoading(base.TestCase):
#name and directory of the pymib file
mib = "TEST-MIB"
# entry point is 'tango/tangostationcontrol'
rel_dir = "/tangostationcontrol/test/clients/SNMP_mib_loading"
def test_load(self): def test_load(self):
pass """"
This test tests the loading of pymib files.
It does this by trying to load a test pymib file named TEST-MIB.py in /SNMP_pymibs
"""
# if any of this fails the test is considered unsuccessful
loader = mib_loader()
loader.set_pymib_dir(self.rel_dir)
loader.load_pymib(self.mib)
self.assertTrue(True)
def test_content(self):
"""
This file contains a 1 variable named: testNamedValue with oid "9.8.7.6.5.4.3.2.1" with named values: ("A", 1), ("B", 2), ("C", 3), ("D", 4)
In order to confirm that the mib is indeed loaded correctly this test has to get the oids, the values and the named values
"""
# same as before
loader = mib_loader()
loader.set_pymib_dir(self.rel_dir)
loader.load_pymib(self.mib)
# used to view mibs client side
mibView = view.MibViewController(loader.mibBuilder)
# The expected testNamedValue parameters as written in TEST-MIB.py
testNamedValue = "testNamedValue"
testNamedValue_oid = "9.8.7.6.5.4.3.2.1"
testNamedValue_named = "A"
testNamedValue_value = 1
# get testValue and set a value of 1
obj_T = pysnmp.ObjectType(ObjectIdentity(self.mib, testNamedValue), pysnmp.Integer32(1))
obj_T.resolveWithMib(mibView)
# get the oid
self.assertEqual(str(obj_T[0]), testNamedValue_oid)
# get the name format: mib::name
self.assertEqual(obj_T[0].prettyPrint(), f"{self.mib}::{testNamedValue}")
# get the namedValue
self.assertEqual(str(obj_T[1]), testNamedValue_named)
# get the numerical value
self.assertEqual(int(obj_T[1]), testNamedValue_value)
...@@ -5,7 +5,7 @@ from unittest import mock ...@@ -5,7 +5,7 @@ from unittest import mock
from tangostationcontrol.test import base from tangostationcontrol.test import base
from tangostationcontrol.clients.snmp_client import SNMP_client, snmp_attribute from tangostationcontrol.clients.snmp_client import SNMP_client, snmp_attribute, mib_loader
class server_imitator: class server_imitator:
...@@ -196,3 +196,6 @@ class TestSNMP(base.TestCase): ...@@ -196,3 +196,6 @@ class TestSNMP(base.TestCase):
# check to make sure the value is indeed 2 # check to make sure the value is indeed 2
self.assertEqual(ret_val, 2, f"Expected: to get {2}, got: {ret_val} of type {type(ret_val)}") self.assertEqual(ret_val, 2, f"Expected: to get {2}, got: {ret_val} of type {type(ret_val)}")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment