Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update portadmin tests to use fixtures #2668

Merged
merged 11 commits into from
Aug 30, 2023
199 changes: 89 additions & 110 deletions tests/unittests/portadmin/portadmin_test.py
Original file line number Diff line number Diff line change
@@ -1,152 +1,131 @@
from mock import Mock

import unittest
import pytest

from nav.oids import OID
from nav.enterprise.ids import VENDOR_ID_HEWLETT_PACKARD, VENDOR_ID_CISCOSYSTEMS
from nav.portadmin.management import *
from nav.portadmin.management import ManagementFactory
from nav.portadmin.snmp.hp import HP
from nav.portadmin.snmp.cisco import Cisco

###############################################################################
from nav.portadmin.vlan import FantasyVlan

class TestPortadminManagementFactory:
def test_get_hp(self, netbox_hp):
handler = ManagementFactory.get_instance(netbox_hp)
assert handler is not None, "Could not get handler-object"
assert isinstance(handler, HP), "Wrong handler-type"

class PortadminResponseTest(unittest.TestCase):
def setUp(self):
self.profile = Mock()
self.profile.snmp_version = 2
self.profile.snmp_community = "public"
def test_get_cisco(self, netbox_cisco):
handler = ManagementFactory.get_instance(netbox_cisco)
assert handler is not None, "Could not get handler-object"
assert isinstance(handler, Cisco), "Wrong handler-type"

self.hpVendor = Mock()
self.hpVendor.id = u'hp'

self.ciscoVendor = Mock()
self.ciscoVendor.id = u'cisco'

self.hpType = Mock()
self.hpType.vendor = self.hpVendor
self.hpType.get_enterprise_id.return_value = VENDOR_ID_HEWLETT_PACKARD

self.ciscoType = Mock()
self.ciscoType.vendor = self.ciscoVendor
self.ciscoType.get_enterprise_id.return_value = VENDOR_ID_CISCOSYSTEMS

self.netboxHP = Mock()
self.netboxHP.type = self.hpType
self.netboxHP.ip = '10.240.160.39'
self.netboxHP.get_preferred_snmp_management_profile.return_value = self.profile

self.netboxCisco = Mock()
self.netboxCisco.type = self.ciscoType
self.netboxCisco.ip = '10.240.160.38'
self.netboxCisco.get_preferred_snmp_management_profile.return_value = (
self.profile
)

self.snmpReadOnlyHandler = None
self.handler = None

def tearDown(self):
self.hpVendor = None
self.ciscoVendor = None
self.hpType = None
self.ciscoType = None
self.netboxHP = None
self.netboxCisco = None
self.handler = None
self.snmpReadOnlyHandler = None
self.snmpReadWriteHandler = None

####################################################################
# HP-netbox

def test_management_factory_get_hp(self):
self.handler = ManagementFactory.get_instance(self.netboxHP)
self.assertNotEqual(self.handler, None, 'Could not get handler-object')
self.assertIsInstance(self.handler, HP, msg='Wrong handler-type')

def test_get_vlan_hp(self):
self.handler = ManagementFactory.get_instance(self.netboxHP)
class TestPortadminResponseHP:
def test_get_vlan_hp(self, handler_hp):
# get hold of the read-only Snmp-object
self.snmpReadOnlyHandler = self.handler._get_read_only_handle()
snmp_read_only_handler = handler_hp._get_read_only_handle()
# replace get-method on Snmp-object with a mock-method
# this get-method returns a vlan-number
self.snmpReadOnlyHandler.get = Mock(return_value=666)
snmp_read_only_handler.get = Mock(return_value=666)
ifc = Mock(baseport=1)
self.assertEqual(
self.handler.get_interface_native_vlan(ifc), 666, "getVlan-test failed"
)
self.snmpReadOnlyHandler.get.assert_called_with(
assert handler_hp.get_interface_native_vlan(ifc) == 666, "getVlan-test failed"
snmp_read_only_handler.get.assert_called_with(
OID('.1.3.6.1.2.1.17.7.1.4.5.1.1.1')
)

def test_get_ifaliases_hp(self):
self.handler = ManagementFactory.get_instance(self.netboxHP)
def test_get_ifaliases_hp(self, handler_hp):
# get hold of the read-only Snmp-object
self.snmpReadOnlyHandler = self.handler._get_read_only_handle()
snmp_read_only_handler = handler_hp._get_read_only_handle()
# replace get-method on Snmp-object with a mock-method
# for getting all IfAlias
walkdata = [('.1', b'hjalmar'), ('.2', b'snorre'), ('.3', b'bjarne')]
expected = {1: 'hjalmar', 2: 'snorre', 3: 'bjarne'}
self.snmpReadOnlyHandler.bulkwalk = Mock(return_value=walkdata)
self.assertEqual(
self.handler._get_all_ifaliases(), expected, "getAllIfAlias failed."
)
snmp_read_only_handler.bulkwalk = Mock(return_value=walkdata)
assert handler_hp._get_all_ifaliases() == expected, "getAllIfAlias failed."

def test_set_ifalias_hp(self):
self.handler = ManagementFactory.get_instance(self.netboxHP)
def test_set_ifalias_hp(self, handler_hp):
# get hold of the read-write Snmp-object
self.snmpReadWriteHandler = self.handler._get_read_write_handle()

snmp_read_only_handler = handler_hp._get_read_write_handle()
# replace set-method on Snmp-object with a mock-method
# all set-methods return None
self.snmpReadWriteHandler.set = Mock(return_value=None)
snmp_read_only_handler.set = Mock(return_value=None)
interface = Mock()
interface.ifindex = 1
self.assertEqual(
self.handler.set_interface_description(interface, "punkt1"),
None,
"setIfAlias failed",
)

####################################################################
# CISCO-netbox
assert (
handler_hp.set_interface_description(interface, "punkt1") is None
), "setIfAlias failed"

def test_management_factory_get_cisco(self):
####################################################################
# cisco-netbox
self.handler = ManagementFactory.get_instance(self.netboxCisco)
self.assertNotEqual(self.handler, None, 'Could not get handler-object')
self.assertIsInstance(self.handler, Cisco, 'Wrong handler-type')

def test_get_vlan_cisco(self):
self.handler = ManagementFactory.get_instance(self.netboxCisco)
assert type(self.handler) == Cisco
class TestPortadminResponseCisco:
def test_get_vlan_cisco(self, handler_cisco):
# get hold of the read-only Snmp-object
self.snmpReadOnlyHandler = self.handler._get_read_only_handle()
snmp_read_only_handler = handler_cisco._get_read_only_handle()
# replace get-method on Snmp-object with a mock-method
# this get-method returns a vlan-number
self.snmpReadOnlyHandler.get = Mock(return_value=77)
snmp_read_only_handler.get = Mock(return_value=77)
ifc = Mock(ifindex=1)
self.assertEqual(
self.handler.get_interface_native_vlan(ifc), 77, "getVlan-test failed"
)
self.snmpReadOnlyHandler.get.assert_called_with(
'1.3.6.1.4.1.9.9.68.1.2.2.1.2.1'
)
assert handler_cisco.get_interface_native_vlan(ifc) == 77, "getVlan-test failed"
snmp_read_only_handler.get.assert_called_with('1.3.6.1.4.1.9.9.68.1.2.2.1.2.1')

def test_get_ifaliases_cisco(self):
self.handler = ManagementFactory.get_instance(self.netboxCisco)
def test_get_ifaliases_cisco(self, handler_cisco):
# get hold of the read-only Snmp-object
self.snmpReadOnlyHandler = self.handler._get_read_only_handle()
snmp_read_only_handler = handler_cisco._get_read_only_handle()
# replace get-method on Snmp-object with a mock-method
# for getting all IfAlias
walkdata = [('.1', b'jomar'), ('.2', b'knut'), ('.3', b'hjallis')]
expected = {1: 'jomar', 2: 'knut', 3: 'hjallis'}
self.snmpReadOnlyHandler.bulkwalk = Mock(return_value=walkdata)
self.assertEqual(
self.handler._get_all_ifaliases(), expected, "getAllIfAlias failed."
)
snmp_read_only_handler.bulkwalk = Mock(return_value=walkdata)
assert handler_cisco._get_all_ifaliases() == expected, "getAllIfAlias failed."


@pytest.fixture
def profile():
profile = Mock()
profile.snmp_version = 2
profile.snmp_community = "public"
return profile


@pytest.fixture
def netbox_hp(profile):
vendor = Mock()
vendor.id = u'hp'

netbox_type = Mock()
netbox_type.vendor = vendor
netbox_type.get_enterprise_id.return_value = VENDOR_ID_HEWLETT_PACKARD

netbox = Mock()
netbox.type = netbox_type
netbox.ip = '10.240.160.39'
netbox.get_preferred_snmp_management_profile.return_value = profile

return netbox


@pytest.fixture
def netbox_cisco(profile):
vendor = Mock()
vendor.id = u'cisco'

netbox_type = Mock()
netbox_type.vendor = vendor
netbox_type.get_enterprise_id.return_value = VENDOR_ID_CISCOSYSTEMS

netbox = Mock()
netbox.type = netbox_type
netbox.ip = '10.240.160.38'
netbox.get_preferred_snmp_management_profile.return_value = profile

return netbox


@pytest.fixture
def handler_hp(netbox_hp):
return ManagementFactory.get_instance(netbox_hp)


if __name__ == '__main__':
unittest.main()
@pytest.fixture
def handler_cisco(netbox_cisco):
return ManagementFactory.get_instance(netbox_cisco)
Loading