Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def setUp(self):
class FakeThing(NetworkDriver):
def __init__(self):
pass
self.device = FakeThing()
assert expected_report == actual_report, yaml.safe_dump(actual_report)
def test_immutable_validation_source(self):
"""Test validation_source is not modified."""
mocked_data = os.path.join(BASEPATH, "mocked_data", "strict_pass_skip")
device = FakeDriver(mocked_data)
source = _read_yaml(os.path.join(mocked_data, "validate.yml"))
witness = _read_yaml(os.path.join(mocked_data, "validate.yml"))
device.compliance_report(validation_source=source)
assert source == witness, yaml.safe_dump(source)
class FakeDriver(NetworkDriver):
"""This is a fake NetworkDriver."""
def __init__(self, path):
self.path = path
def __getattribute__(self, name):
def load_json(filename):
def func(**kwargs):
with open(filename, "r") as f:
return json.loads(f.read())
return func
if name.startswith("get_") or name in C.ACTION_TYPE_METHODS:
filename = os.path.join(self.path, "{}.json".format(name))
return load_json(filename)
def test_get_network_driver(self, driver):
"""Check that we can get the desired driver and is instance of NetworkDriver."""
self.assertTrue(issubclass(get_network_driver(driver), NetworkDriver))
from napalm.junos import constants as C
from napalm.base.exceptions import ConnectionException
from napalm.base.exceptions import MergeConfigException
from napalm.base.exceptions import CommandErrorException
from napalm.base.exceptions import ReplaceConfigException
from napalm.base.exceptions import CommandTimeoutException
from napalm.base.exceptions import LockError
from napalm.base.exceptions import UnlockError
# import local modules
from napalm.junos.utils import junos_views
log = logging.getLogger(__file__)
class JunOSDriver(NetworkDriver):
"""JunOSDriver class - inherits NetworkDriver from napalm.base."""
def __init__(self, hostname, username, password, timeout=60, optional_args=None):
"""
Initialise JunOS driver.
Optional args:
* config_lock (True/False): lock configuration DB after the connection is established.
* lock_disable (True/False): force configuration lock to be disabled (for external lock
management).
* port (int): custom port
* key_file (string): SSH key file path
* keepalive (int): Keepalive interval
* ignore_warning (boolean): not generate warning exceptions
"""
self.hostname = hostname
from napalm.base.exceptions import (
ConnectionException,
MergeConfigException,
ReplaceConfigException,
SessionLockedException,
CommandErrorException,
)
from napalm.eos.constants import LLDP_CAPAB_TRANFORM_TABLE
import napalm.base.constants as c
# local modules
# here add local imports
# e.g. import napalm.eos.helpers etc.
class EOSDriver(NetworkDriver):
"""Napalm driver for Arista EOS."""
SUPPORTED_OC_MODELS = []
HEREDOC_COMMANDS = [
("banner login", 1),
("banner motd", 1),
("comment", 1),
("protocol https certificate", 2),
]
_RE_BGP_INFO = re.compile(
r"BGP neighbor is (?P.*?), remote AS (?P.*?), .*"
) # noqa
_RE_BGP_RID_INFO = re.compile(
r".*BGP version 4, remote router ID (?P.*?), VRF (?P.*?)$"
import vyattaconfparser
from netmiko import __version__ as netmiko_version
from netmiko import ConnectHandler
from netmiko import SCPConn
# NAPALM base
import napalm.base.constants as C
from napalm.base.utils import py23_compat
from napalm.base.base import NetworkDriver
from napalm.base.exceptions import ConnectionException, \
MergeConfigException, ReplaceConfigException
class VyOSDriver(NetworkDriver):
_MINUTE_SECONDS = 60
_HOUR_SECONDS = 60 * _MINUTE_SECONDS
_DAY_SECONDS = 24 * _HOUR_SECONDS
_WEEK_SECONDS = 7 * _DAY_SECONDS
_YEAR_SECONDS = 365 * _DAY_SECONDS
_DEST_FILENAME = "/var/tmp/candidate_running.conf"
_BACKUP_FILENAME = "/var/tmp/backup_running.conf"
_BOOT_FILENAME = "/config/config.boot"
def __init__(self, hostname, username, password, timeout=60, optional_args=None):
self.hostname = hostname
self.username = username
self.password = password
self.timeout = timeout
self.device = None
AFI_COMMAND_MAP = {
"IPv4 Unicast": "ipv4 unicast",
"IPv6 Unicast": "ipv6 unicast",
"VPNv4 Unicast": "vpnv4 all",
"VPNv6 Unicast": "vpnv6 unicast all",
"IPv4 Multicast": "ipv4 multicast",
"IPv6 Multicast": "ipv6 multicast",
"L2VPN E-VPN": "l2vpn evpn",
"MVPNv4 Unicast": "ipv4 mvpn all",
"MVPNv6 Unicast": "ipv6 mvpn all",
"VPNv4 Flowspec": "ipv4 flowspec",
"VPNv6 Flowspec": "ipv6 flowspec",
}
class IOSDriver(NetworkDriver):
"""NAPALM Cisco IOS Handler."""
def __init__(self, hostname, username, password, timeout=60, optional_args=None):
"""NAPALM Cisco IOS Handler."""
if optional_args is None:
optional_args = {}
self.hostname = hostname
self.username = username
self.password = password
self.timeout = timeout
self.transport = optional_args.get("transport", "ssh")
# Retrieve file names
self.candidate_cfg = optional_args.get("candidate_cfg", "candidate_config.txt")
self.merge_cfg = optional_args.get("merge_cfg", "merge_config.txt")
class MockDevice(object):
def __init__(self, parent, profile):
self.parent = parent
self.profile = profile
def run_commands(self, commands):
"""Mock for EOS"""
return list(self.parent.cli(commands).values())[0]
def show(self, command):
"""Mock for nxos"""
return self.run_commands([command])
class MockDriver(NetworkDriver):
def __init__(self, hostname, username, password, timeout=60, optional_args=None):
"""
Supported optional_args:
* path(str) - path to where the mocked files are located
* profile(list) - List of profiles to assign
"""
self.hostname = hostname
self.username = username
self.password = password
self.path = optional_args.get("path", "")
self.profile = optional_args.get("profile", [])
self.fail_on_open = optional_args.get("fail_on_open", False)
self.opened = False
self.calls = {}
self.device = MockDevice(self, self.profile)
from napalm.base.exceptions import (
ConnectionException,
MergeConfigException,
ReplaceConfigException,
CommandErrorException,
CommitError,
)
# Easier to store these as constants
HOUR_SECONDS = 3600
DAY_SECONDS = 24 * HOUR_SECONDS
WEEK_SECONDS = 7 * DAY_SECONDS
YEAR_SECONDS = 365 * DAY_SECONDS
class CEDriver(NetworkDriver):
"""Napalm driver for HUAWEI CloudEngine."""
def __init__(self, hostname, username, password, timeout=60, optional_args=None):
"""Constructor."""
self.device = None
self.hostname = hostname
self.username = username
self.password = password
self.timeout = timeout
# Get optional arguments
if optional_args is None:
optional_args = {}
# Netmiko possible arguments
netmiko_argument_map = {