mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Update net.py and bgp.py (#55421)
* Update bgp.py to look for new napalm module name Changed NAPALM_BASE to NAPALM (napalm_base to napalm.base) * Update net.py 1.- Reflect new module name napalm instead of napalm-base. 2.- Catch key-exception for IPv6 if the interface does not have IPv6 enabled 3.- Support for IP secondary address 4.- the module was trowing an exemption because a variable list named "compare" was initiated with [None]. After adding items to the list there was a max(compare), and None is not supported for comparing of the list with types None and IPNetwork. So changing the initial value to the worst value as IPNetwork('0.0.0.0/0'), solved the issue. * Update net.py * blackened changes * removed napalm_base code from naplm_utils * Created rudimentary tests for module * fixed pylint error * passing pre-commit * require napalm libraries to run test Co-authored-by: Tyler Johnson <tjohnson@saltstack.com> Co-authored-by: Daniel Wozniak <dwozniak@saltstack.com>
This commit is contained in:
parent
5eebe18816
commit
2eb7c9051d
5 changed files with 113 additions and 24 deletions
|
@ -110,12 +110,12 @@ try:
|
|||
from netaddr import IPAddress
|
||||
|
||||
# pylint: disable=unused-import
|
||||
from napalm_base import helpers as napalm_helpers
|
||||
from napalm.base import helpers as napalm_helpers
|
||||
|
||||
# pylint: enable=unused-import
|
||||
HAS_NAPALM_BASE = True
|
||||
HAS_NAPALM = True
|
||||
except ImportError:
|
||||
HAS_NAPALM_BASE = False
|
||||
HAS_NAPALM = False
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
|
@ -150,9 +150,9 @@ _DEFAULT_LABELS_MAPPING = {
|
|||
|
||||
|
||||
def __virtual__():
|
||||
if HAS_NAPALM_BASE:
|
||||
if HAS_NAPALM:
|
||||
return __virtualname__
|
||||
return (False, "The napalm-base module could not be imported")
|
||||
return (False, "The napalm module could not be imported")
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
|
|
|
@ -78,13 +78,13 @@ from salt.ext.six.moves import map
|
|||
|
||||
# Import third party libs
|
||||
try:
|
||||
from netaddr import IPNetwork # netaddr is already required by napalm-base
|
||||
from netaddr import IPNetwork # netaddr is already required by napalm
|
||||
from netaddr.core import AddrFormatError
|
||||
from napalm_base import helpers as napalm_helpers
|
||||
from napalm.base import helpers as napalm_helpers
|
||||
|
||||
HAS_NAPALM_BASE = True
|
||||
HAS_NAPALM = True
|
||||
except ImportError:
|
||||
HAS_NAPALM_BASE = False
|
||||
HAS_NAPALM = False
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# module properties
|
||||
|
@ -114,9 +114,9 @@ __virtualname__ = "net"
|
|||
|
||||
|
||||
def __virtual__():
|
||||
if HAS_NAPALM_BASE:
|
||||
if HAS_NAPALM:
|
||||
return __virtualname__
|
||||
return (False, "The napalm-base module could not be imported")
|
||||
return (False, "The napalm module could not be imported")
|
||||
|
||||
|
||||
def _get_net_runner_opts():
|
||||
|
@ -236,8 +236,8 @@ def _find_interfaces_mac(ip): # pylint: disable=invalid-name
|
|||
for interface, interface_ipaddrs in six.iteritems(
|
||||
device_ipaddrs.get("out", {})
|
||||
):
|
||||
ip_addresses = interface_ipaddrs.get("ipv4", {}).keys()
|
||||
ip_addresses.extend(interface_ipaddrs.get("ipv6", {}).keys())
|
||||
ip_addresses = set(interface_ipaddrs.get("ipv4", {}).keys())
|
||||
ip_addresses.update(set(interface_ipaddrs.get("ipv6", {}).keys()))
|
||||
for ipaddr in ip_addresses:
|
||||
if ip != ipaddr:
|
||||
continue
|
||||
|
@ -372,7 +372,7 @@ def interfaces(
|
|||
ipnet = _get_network_obj(ipnet)
|
||||
|
||||
best_row = {}
|
||||
best_net_match = None
|
||||
best_net_match = IPNetwork("0.0.0.0/0")
|
||||
for device, net_interfaces_out in six.iteritems(
|
||||
all_interfaces
|
||||
): # pylint: disable=too-many-nested-blocks
|
||||
|
|
|
@ -41,19 +41,12 @@ try:
|
|||
|
||||
# pylint: enable=unused-import,no-name-in-module
|
||||
HAS_NAPALM = True
|
||||
HAS_NAPALM_BASE = False # doesn't matter anymore, but needed for the logic below
|
||||
try:
|
||||
NAPALM_MAJOR = int(napalm.__version__.split(".")[0])
|
||||
except AttributeError:
|
||||
NAPALM_MAJOR = 0
|
||||
except ImportError:
|
||||
HAS_NAPALM = False
|
||||
try:
|
||||
import napalm_base
|
||||
|
||||
HAS_NAPALM_BASE = True
|
||||
except ImportError:
|
||||
HAS_NAPALM_BASE = False
|
||||
|
||||
try:
|
||||
# try importing ConnectionClosedException
|
||||
|
@ -103,9 +96,7 @@ def virtual(opts, virtualname, filename):
|
|||
"""
|
||||
Returns the __virtual__.
|
||||
"""
|
||||
if ((HAS_NAPALM and NAPALM_MAJOR >= 2) or HAS_NAPALM_BASE) and (
|
||||
is_proxy(opts) or is_minion(opts)
|
||||
):
|
||||
if (HAS_NAPALM and NAPALM_MAJOR >= 2) and (is_proxy(opts) or is_minion(opts)):
|
||||
return virtualname
|
||||
else:
|
||||
return (
|
||||
|
|
34
tests/unit/runners/test_bgp.py
Normal file
34
tests/unit/runners/test_bgp.py
Normal file
|
@ -0,0 +1,34 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Import Python Libs
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
|
||||
# Import salt libs
|
||||
import salt.runners.bgp as bgp
|
||||
|
||||
# Import Salt Testing Libs
|
||||
from tests.support.mixins import LoaderModuleMockMixin
|
||||
from tests.support.unit import TestCase, skipIf
|
||||
|
||||
|
||||
@skipIf(not bgp.HAS_NAPALM, "napalm module required for this test")
|
||||
class BGPTest(TestCase, LoaderModuleMockMixin):
|
||||
"""
|
||||
Test the bgp runner
|
||||
"""
|
||||
|
||||
def setup_loader_modules(self):
|
||||
return {
|
||||
bgp: {
|
||||
"__opts__": {
|
||||
"optimization_order": [0, 1, 2],
|
||||
"renderer": "yaml",
|
||||
"renderer_blacklist": [],
|
||||
"renderer_whitelist": [],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def test_neighbors(self):
|
||||
ret = bgp.neighbors()
|
||||
self.assertEqual(ret, [])
|
64
tests/unit/runners/test_net.py
Normal file
64
tests/unit/runners/test_net.py
Normal file
|
@ -0,0 +1,64 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Import Python Libs
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
|
||||
import shutil
|
||||
import tempfile
|
||||
|
||||
# Import salt libs
|
||||
import salt.runners.net as net
|
||||
|
||||
# Import Salt Testing Libs
|
||||
from tests.support.mixins import LoaderModuleMockMixin
|
||||
from tests.support.mock import MagicMock
|
||||
from tests.support.runtests import RUNTIME_VARS
|
||||
from tests.support.unit import TestCase, skipIf
|
||||
|
||||
|
||||
@skipIf(not net.HAS_NAPALM, "napalm module required for this test")
|
||||
class NetTest(TestCase, LoaderModuleMockMixin):
|
||||
"""
|
||||
Test the net runner
|
||||
"""
|
||||
|
||||
def setup_loader_modules(self):
|
||||
mock_get = MagicMock(return_value={})
|
||||
self.extmods_dir = tempfile.mkdtemp(dir=RUNTIME_VARS.TMP)
|
||||
self.addCleanup(shutil.rmtree, self.extmods_dir, ignore_errors=True)
|
||||
return {
|
||||
net: {
|
||||
"__opts__": {
|
||||
"optimization_order": [0, 1, 2],
|
||||
"renderer": "yaml",
|
||||
"renderer_blacklist": [],
|
||||
"renderer_whitelist": [],
|
||||
"extension_modules": self.extmods_dir,
|
||||
},
|
||||
"__salt__": {"mine.get": mock_get},
|
||||
}
|
||||
}
|
||||
|
||||
def test_interfaces(self):
|
||||
ret = net.interfaces()
|
||||
self.assertEqual(None, ret)
|
||||
|
||||
def test_findarp(self):
|
||||
ret = net.findarp()
|
||||
self.assertEqual(None, ret)
|
||||
|
||||
def test_findmac(self):
|
||||
ret = net.findmac()
|
||||
self.assertEqual(None, ret)
|
||||
|
||||
def test_lldp(self):
|
||||
ret = net.lldp()
|
||||
self.assertEqual(None, ret)
|
||||
|
||||
def test_find(self):
|
||||
ret = net.find("")
|
||||
self.assertEqual({}, ret)
|
||||
|
||||
def test_multi_find(self):
|
||||
ret = net.multi_find()
|
||||
self.assertEqual(None, ret)
|
Loading…
Add table
Reference in a new issue