Freeze napalm test objects

This commit is contained in:
Pedro Algarvio 2020-08-17 20:19:08 +01:00 committed by Daniel Wozniak
parent aed3a3e23a
commit ed9ebba9bd
13 changed files with 652 additions and 603 deletions

View file

@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""
Utils for the NAPALM modules and proxy.
@ -15,8 +14,6 @@ Utils for the NAPALM modules and proxy.
.. versionadded:: 2017.7.0
"""
# Import Python libs
from __future__ import absolute_import, print_function, unicode_literals
import copy
import importlib
@ -28,10 +25,6 @@ import salt.output
import salt.utils.args
import salt.utils.platform
# Import Salt libs
from salt.ext import six
# Import third party libs
try:
# will try to import NAPALM
# https://github.com/napalm-automation/napalm
@ -170,7 +163,7 @@ def call(napalm_device, method, *args, **kwargs):
# if connected will try to execute desired command
kwargs_copy = {}
kwargs_copy.update(kwargs)
for karg, warg in six.iteritems(kwargs_copy):
for karg, warg in kwargs_copy.items():
# lets clear None arguments
# to not be sent to NAPALM methods
if warg is None:
@ -427,7 +420,7 @@ def proxy_napalm_wrap(func):
except napalm_base.exceptions.ConnectionException as nce:
log.error(nce)
return "{base_msg}. See log for details.".format(
base_msg=six.text_type(nce.msg)
base_msg=str(nce.msg)
)
else:
# in case the `inherit_napalm_device` is set
@ -496,7 +489,7 @@ def proxy_napalm_wrap(func):
except napalm_base.exceptions.ConnectionException as nce:
log.error(nce)
return "{base_msg}. See log for details.".format(
base_msg=six.text_type(nce.msg)
base_msg=str(nce.msg)
)
else:
# in case the `inherit_napalm_device` is set

View file

@ -1,447 +1,492 @@
# -*- coding: utf-8 -*-
"""
Base classes for napalm unit tests
:codeauthor: :email:`Anthony Shaw <anthonyshaw@apache.org>`
"""
from __future__ import absolute_import
from functools import wraps
TEST_INTERFACES = {
"Management1": {
"is_up": False,
"is_enabled": False,
"description": u"",
"last_flapped": -1,
"speed": 1000,
"mac_address": u"dead:beef:dead",
from salt.utils.immutabletypes import freeze
TEST_INTERFACES = freeze(
{
"Management1": {
"is_up": False,
"is_enabled": False,
"description": "",
"last_flapped": -1,
"speed": 1000,
"mac_address": "dead:beef:dead",
}
}
}
)
# Test data
TEST_FACTS = {
"__opts__": {},
"OPTIONAL_ARGS": {},
"uptime": "Forever",
"UP": True,
"HOSTNAME": "test-device.com",
"hostname": "test-device.com",
"username": "admin",
"os_version": "1.2.3",
"model": "test_model",
"serial_number": "123456",
"vendor": "cisco",
"interface_list": TEST_INTERFACES,
}
TEST_FACTS = freeze(
{
"__opts__": {},
"OPTIONAL_ARGS": {},
"uptime": "Forever",
"UP": True,
"HOSTNAME": "test-device.com",
"hostname": "test-device.com",
"username": "admin",
"os_version": "1.2.3",
"model": "test_model",
"serial_number": "123456",
"vendor": "cisco",
"interface_list": TEST_INTERFACES,
}
)
TEST_ENVIRONMENT = {"hot": "yes"}
TEST_ENVIRONMENT = freeze({"hot": "yes"})
TEST_COMMAND_RESPONSE = {"show run": "all the command output"}
TEST_COMMAND_RESPONSE = freeze({"show run": "all the command output"})
TEST_TRACEROUTE_RESPONSE = {
"success": {
1: {
"probes": {
1: {
"rtt": 1.123,
"ip_address": u"206.223.116.21",
"host_name": u"eqixsj-google-gige.google.com",
TEST_TRACEROUTE_RESPONSE = freeze(
{
"success": {
1: {
"probes": {
1: {
"rtt": 1.123,
"ip_address": "206.223.116.21",
"host_name": "eqixsj-google-gige.google.com",
}
}
}
}
}
}
)
TEST_PING_RESPONSE = {
"success": {
"probes_sent": 5,
"packet_loss": 0,
"rtt_min": 72.158,
"rtt_max": 72.433,
"rtt_avg": 72.268,
"rtt_stddev": 0.094,
"results": [{"ip_address": "1.1.1.1", "rtt": 72.248}],
}
}
TEST_ARP_TABLE = [
TEST_PING_RESPONSE = freeze(
{
"interface": "MgmtEth0/RSP0/CPU0/0",
"mac": "5C:5E:AB:DA:3C:F0",
"ip": "172.17.17.1",
"age": 1454496274.84,
"success": {
"probes_sent": 5,
"packet_loss": 0,
"rtt_min": 72.158,
"rtt_max": 72.433,
"rtt_avg": 72.268,
"rtt_stddev": 0.094,
"results": [{"ip_address": "1.1.1.1", "rtt": 72.248}],
}
}
]
)
TEST_IPADDRS = {"FastEthernet8": {"ipv4": {"10.66.43.169": {"prefix_length": 22}}}}
TEST_ARP_TABLE = freeze(
[
{
"interface": "MgmtEth0/RSP0/CPU0/0",
"mac": "5C:5E:AB:DA:3C:F0",
"ip": "172.17.17.1",
"age": 1454496274.84,
}
]
)
TEST_INTERFACES = {
"Management1": {
"is_up": False,
"is_enabled": False,
"description": u"",
"last_flapped": -1,
"speed": 1000,
"mac_address": u"dead:beef:dead",
}
}
TEST_IPADDRS = freeze(
{"FastEthernet8": {"ipv4": {"10.66.43.169": {"prefix_length": 22}}}}
)
TEST_LLDP_NEIGHBORS = {u"Ethernet2": [{"hostname": u"junos-unittest", "port": u"520"}]}
TEST_MAC_TABLE = [
TEST_INTERFACES = freeze(
{
"mac": "00:1C:58:29:4A:71",
"interface": "Ethernet47",
"vlan": 100,
"static": False,
"active": True,
"moves": 1,
"last_move": 1454417742.58,
"Management1": {
"is_up": False,
"is_enabled": False,
"description": "",
"last_flapped": -1,
"speed": 1000,
"mac_address": "dead:beef:dead",
}
}
]
)
TEST_RUNNING_CONFIG = {"one": "two"}
TEST_LLDP_NEIGHBORS = freeze(
{"Ethernet2": [{"hostname": "junos-unittest", "port": "520"}]}
)
TEST_OPTICS = {
"et1": {
"physical_channels": {
"channel": [
TEST_MAC_TABLE = freeze(
[
{
"mac": "00:1C:58:29:4A:71",
"interface": "Ethernet47",
"vlan": 100,
"static": False,
"active": True,
"moves": 1,
"last_move": 1454417742.58,
}
]
)
TEST_RUNNING_CONFIG = freeze({"one": "two"})
TEST_OPTICS = freeze(
{
"et1": {
"physical_channels": {
"channel": [
{
"index": 0,
"state": {
"input_power": {
"instant": 0.0,
"avg": 0.0,
"min": 0.0,
"max": 0.0,
},
"output_power": {
"instant": 0.0,
"avg": 0.0,
"min": 0.0,
"max": 0.0,
},
"laser_bias_current": {
"instant": 0.0,
"avg": 0.0,
"min": 0.0,
"max": 0.0,
},
},
}
]
}
}
}
)
TEST_BGP_CONFIG = freeze({"test": "value"})
TEST_BGP_NEIGHBORS = freeze(
{
"default": {
8121: [
{
"index": 0,
"state": {
"input_power": {
"instant": 0.0,
"avg": 0.0,
"min": 0.0,
"max": 0.0,
},
"output_power": {
"instant": 0.0,
"avg": 0.0,
"min": 0.0,
"max": 0.0,
},
"laser_bias_current": {
"instant": 0.0,
"avg": 0.0,
"min": 0.0,
"max": 0.0,
},
},
"up": True,
"local_as": 13335,
"remote_as": 8121,
"local_address": "172.101.76.1",
"local_address_configured": True,
"local_port": 179,
"remote_address": "192.247.78.0",
"router_id": "192.168.0.1",
"remote_port": 58380,
"multihop": False,
"import_policy": "4-NTT-TRANSIT-IN",
"export_policy": "4-NTT-TRANSIT-OUT",
"input_messages": 123,
"output_messages": 13,
"input_updates": 123,
"output_updates": 5,
"messages_queued_out": 23,
"connection_state": "Established",
"previous_connection_state": "EstabSync",
"last_event": "RecvKeepAlive",
"suppress_4byte_as": False,
"local_as_prepend": False,
"holdtime": 90,
"configured_holdtime": 90,
"keepalive": 30,
"configured_keepalive": 30,
"active_prefix_count": 132808,
"received_prefix_count": 566739,
"accepted_prefix_count": 566479,
"suppressed_prefix_count": 0,
"advertise_prefix_count": 0,
"flap_count": 27,
}
]
}
}
}
)
TEST_BGP_CONFIG = {"test": "value"}
TEST_TERM_CONFIG = freeze({"result": True, "already_configured": False})
TEST_BGP_NEIGHBORS = {
"default": {
8121: [
TEST_NTP_PEERS = freeze(
{
"192.168.0.1": 1,
"172.17.17.1": 2,
"172.17.17.2": 3,
"2400:cb00:6:1024::c71b:840a": 4,
}
)
TEST_NTP_SERVERS = freeze(
{
"192.168.0.1": 1,
"172.17.17.1": 2,
"172.17.17.2": 3,
"2400:cb00:6:1024::c71b:840a": 4,
}
)
TEST_NTP_STATS = freeze(
[
{
"remote": "188.114.101.4",
"referenceid": "188.114.100.1",
"synchronized": True,
"stratum": 4,
"type": "-",
"when": "107",
"hostpoll": 256,
"reachability": 377,
"delay": 164.228,
"offset": -13.866,
"jitter": 2.695,
}
]
)
TEST_PROBES_CONFIG = freeze(
{
"probe1": {
"test1": {
"probe_type": "icmp-ping",
"target": "192.168.0.1",
"source": "192.168.0.2",
"probe_count": 13,
"test_interval": 3,
},
"test2": {
"probe_type": "http-ping",
"target": "172.17.17.1",
"source": "192.17.17.2",
"probe_count": 5,
"test_interval": 60,
},
}
}
)
TEST_PROBES_RESULTS = freeze(
{
"probe1": {
"test1": {
"last_test_min_delay": 63.120,
"global_test_min_delay": 62.912,
"current_test_avg_delay": 63.190,
"global_test_max_delay": 177.349,
"current_test_max_delay": 63.302,
"global_test_avg_delay": 63.802,
"last_test_avg_delay": 63.438,
"last_test_max_delay": 65.356,
"probe_type": "icmp-ping",
"rtt": 63.138,
"last_test_loss": 0,
"round_trip_jitter": -59.0,
"target": "192.168.0.1",
"source": "192.168.0.2",
"probe_count": 15,
"current_test_min_delay": 63.138,
},
"test2": {
"last_test_min_delay": 176.384,
"global_test_min_delay": 169.226,
"current_test_avg_delay": 177.098,
"global_test_max_delay": 292.628,
"current_test_max_delay": 180.055,
"global_test_avg_delay": 177.959,
"last_test_avg_delay": 177.178,
"last_test_max_delay": 184.671,
"probe_type": "icmp-ping",
"rtt": 176.449,
"last_test_loss": 0,
"round_trip_jitter": -34.0,
"target": "172.17.17.1",
"source": "172.17.17.2",
"probe_count": 15,
"current_test_min_delay": 176.402,
},
}
}
)
TEST_ROUTE = freeze(
{
"172.16.0.0/25": [
{
"up": True,
"local_as": 13335,
"remote_as": 8121,
"local_address": u"172.101.76.1",
"local_address_configured": True,
"local_port": 179,
"remote_address": u"192.247.78.0",
"router_id": u"192.168.0.1",
"remote_port": 58380,
"multihop": False,
"import_policy": u"4-NTT-TRANSIT-IN",
"export_policy": u"4-NTT-TRANSIT-OUT",
"input_messages": 123,
"output_messages": 13,
"input_updates": 123,
"output_updates": 5,
"messages_queued_out": 23,
"connection_state": u"Established",
"previous_connection_state": u"EstabSync",
"last_event": u"RecvKeepAlive",
"suppress_4byte_as": False,
"local_as_prepend": False,
"holdtime": 90,
"configured_holdtime": 90,
"keepalive": 30,
"configured_keepalive": 30,
"active_prefix_count": 132808,
"received_prefix_count": 566739,
"accepted_prefix_count": 566479,
"suppressed_prefix_count": 0,
"advertise_prefix_count": 0,
"flap_count": 27,
}
"protocol": "BGP",
"last_active": True,
"current_active": True,
"age": 1178693,
"routing_table": "inet.0",
"next_hop": "192.168.0.11",
"outgoing_interface": "xe-1/1/1.100",
"preference": 170,
"selected_next_hop": False,
"protocol_attributes": {
"remote_as": 65001,
"metric": 5,
"local_as": 13335,
"as_path": "",
"remote_address": "192.168.0.11",
"metric2": 0,
"local_preference": 0,
"communities": ["0:2", "no-export"],
"preference2": -1,
},
"inactive_reason": "",
},
{
"protocol": "BGP",
"last_active": False,
"current_active": False,
"age": 2359429,
"routing_table": "inet.0",
"next_hop": "192.168.0.17",
"outgoing_interface": "xe-1/1/1.100",
"preference": 170,
"selected_next_hop": True,
"protocol_attributes": {
"remote_as": 65001,
"metric": 5,
"local_as": 13335,
"as_path": "",
"remote_address": "192.168.0.17",
"metric2": 0,
"local_preference": 0,
"communities": ["0:3", "no-export"],
"preference2": -1,
},
"inactive_reason": "Not Best in its group - Router ID",
},
]
}
}
)
TEST_TERM_CONFIG = {"result": True, "already_configured": False}
TEST_SNMP_INFO = freeze({"test_": "value"})
TEST_NTP_PEERS = {
"192.168.0.1": 1,
"172.17.17.1": 2,
"172.17.17.2": 3,
"2400:cb00:6:1024::c71b:840a": 4,
}
TEST_NTP_SERVERS = {
"192.168.0.1": 1,
"172.17.17.1": 2,
"172.17.17.2": 3,
"2400:cb00:6:1024::c71b:840a": 4,
}
TEST_NTP_STATS = [
TEST_USERS = freeze(
{
"remote": u"188.114.101.4",
"referenceid": u"188.114.100.1",
"synchronized": True,
"stratum": 4,
"type": u"-",
"when": u"107",
"hostpoll": 256,
"reachability": 377,
"delay": 164.228,
"offset": -13.866,
"jitter": 2.695,
}
]
TEST_PROBES_CONFIG = {
"probe1": {
"test1": {
"probe_type": "icmp-ping",
"target": "192.168.0.1",
"source": "192.168.0.2",
"probe_count": 13,
"test_interval": 3,
},
"test2": {
"probe_type": "http-ping",
"target": "172.17.17.1",
"source": "192.17.17.2",
"probe_count": 5,
"test_interval": 60,
},
}
}
TEST_PROBES_RESULTS = {
"probe1": {
"test1": {
"last_test_min_delay": 63.120,
"global_test_min_delay": 62.912,
"current_test_avg_delay": 63.190,
"global_test_max_delay": 177.349,
"current_test_max_delay": 63.302,
"global_test_avg_delay": 63.802,
"last_test_avg_delay": 63.438,
"last_test_max_delay": 65.356,
"probe_type": "icmp-ping",
"rtt": 63.138,
"last_test_loss": 0,
"round_trip_jitter": -59.0,
"target": "192.168.0.1",
"source": "192.168.0.2",
"probe_count": 15,
"current_test_min_delay": 63.138,
},
"test2": {
"last_test_min_delay": 176.384,
"global_test_min_delay": 169.226,
"current_test_avg_delay": 177.098,
"global_test_max_delay": 292.628,
"current_test_max_delay": 180.055,
"global_test_avg_delay": 177.959,
"last_test_avg_delay": 177.178,
"last_test_max_delay": 184.671,
"probe_type": "icmp-ping",
"rtt": 176.449,
"last_test_loss": 0,
"round_trip_jitter": -34.0,
"target": "172.17.17.1",
"source": "172.17.17.2",
"probe_count": 15,
"current_test_min_delay": 176.402,
},
}
}
TEST_ROUTE = {
"172.16.0.0/25": [
{
"protocol": "BGP",
"last_active": True,
"current_active": True,
"age": 1178693,
"routing_table": "inet.0",
"next_hop": "192.168.0.11",
"outgoing_interface": "xe-1/1/1.100",
"preference": 170,
"selected_next_hop": False,
"protocol_attributes": {
"remote_as": 65001,
"metric": 5,
"local_as": 13335,
"as_path": "",
"remote_address": "192.168.0.11",
"metric2": 0,
"local_preference": 0,
"communities": ["0:2", "no-export"],
"preference2": -1,
},
"inactive_reason": "",
},
{
"protocol": "BGP",
"last_active": False,
"current_active": False,
"age": 2359429,
"routing_table": "inet.0",
"next_hop": "192.168.0.17",
"outgoing_interface": "xe-1/1/1.100",
"preference": 170,
"selected_next_hop": True,
"protocol_attributes": {
"remote_as": 65001,
"metric": 5,
"local_as": 13335,
"as_path": "",
"remote_address": "192.168.0.17",
"metric2": 0,
"local_preference": 0,
"communities": ["0:3", "no-export"],
"preference2": -1,
},
"inactive_reason": "Not Best in its group - Router ID",
},
]
}
TEST_SNMP_INFO = {"test_": "value"}
TEST_USERS = {
"mircea": {
"level": 15,
"password": "$1$0P70xKPa$4jt5/10cBTckk6I/w/",
"sshkeys": [
"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC4pFn+shPwTb2yELO4L7NtQrKOJXNeCl1je\
"mircea": {
"level": 15,
"password": "$1$0P70xKPa$4jt5/10cBTckk6I/w/",
"sshkeys": [
"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC4pFn+shPwTb2yELO4L7NtQrKOJXNeCl1je\
l9STXVaGnRAnuc2PXl35vnWmcUq6YbUEcgUTRzzXfmelJKuVJTJIlMXii7h2xkbQp0YZIEs4P\
8ipwnRBAxFfk/ZcDsdfsdfsdfsdN56ejk345jhk345jk345jk341p3A/9LIL7l6YewLBCwJj6\
D+fWSJ0/YW+7oH17Fk2HH+tw0L5PcWLHkwA4t60iXn16qDbIk/ze6jv2hDGdCdz7oYQeCE55C\
CHOHMJWYfN3jcL4s0qv8/u6Ka1FVkV7iMmro7ChThoV/5snI4Ljf2wKqgHH7TfNaCfpU0WvHA\
nTs8zhOrGScSrtb mircea@master-roshi"
],
],
}
}
}
)
class MockNapalmDevice(object):
class MockNapalmDevice:
"""Setup a mock device for our tests"""
def __init__(self):
self._d = {}
def __getitem__(self, key):
return self._d[key]
def __setitem__(self, key, value):
self._d[key] = value
def get_facts(self):
return TEST_FACTS
return TEST_FACTS.copy()
def get_environment(self):
return TEST_ENVIRONMENT
return TEST_ENVIRONMENT.copy()
def get_arp_table(self):
return TEST_ARP_TABLE
return TEST_ARP_TABLE.copy()
def get(self, key, default=None, *args, **kwargs):
try:
if key == "DRIVER":
return self
return TEST_FACTS[key]
return TEST_FACTS.copy()[key]
except KeyError:
return default
def cli(self, commands, *args, **kwargs):
assert commands[0] == "show run"
return TEST_COMMAND_RESPONSE
return TEST_COMMAND_RESPONSE.copy()
def traceroute(self, destination, **kwargs):
assert destination == "destination.com"
return TEST_TRACEROUTE_RESPONSE
return TEST_TRACEROUTE_RESPONSE.copy()
def ping(self, destination, **kwargs):
assert destination == "destination.com"
return TEST_PING_RESPONSE
return TEST_PING_RESPONSE.copy()
def get_config(self, retrieve="all"):
assert retrieve == "running"
return TEST_RUNNING_CONFIG
return TEST_RUNNING_CONFIG.copy()
def get_interfaces_ip(self, **kwargs):
return TEST_IPADDRS
return TEST_IPADDRS.copy()
def get_interfaces(self, **kwargs):
return TEST_INTERFACES
return TEST_INTERFACES.copy()
def get_lldp_neighbors_detail(self, **kwargs):
return TEST_LLDP_NEIGHBORS
return TEST_LLDP_NEIGHBORS.copy()
def get_mac_address_table(self, **kwargs):
return TEST_MAC_TABLE
return TEST_MAC_TABLE.copy()
def get_optics(self, **kwargs):
return TEST_OPTICS
return TEST_OPTICS.copy()
def load_merge_candidate(self, filename=None, config=None):
assert config == "new config"
return TEST_RUNNING_CONFIG
return TEST_RUNNING_CONFIG.copy()
def load_replace_candidate(self, filename=None, config=None):
assert config == "new config"
return TEST_RUNNING_CONFIG
return TEST_RUNNING_CONFIG.copy()
def commit_config(self, **kwargs):
return TEST_RUNNING_CONFIG
return TEST_RUNNING_CONFIG.copy()
def discard_config(self, **kwargs):
return TEST_RUNNING_CONFIG
return TEST_RUNNING_CONFIG.copy()
def compare_config(self, **kwargs):
return TEST_RUNNING_CONFIG
return TEST_RUNNING_CONFIG.copy()
def rollback(self, **kwargs):
return TEST_RUNNING_CONFIG
return TEST_RUNNING_CONFIG.copy()
def get_bgp_config(self, **kwargs):
return TEST_BGP_CONFIG
return TEST_BGP_CONFIG.copy()
def get_bgp_neighbors_detail(self, neighbor_address=None, **kwargs):
assert neighbor_address is None or "test_address"
return TEST_BGP_NEIGHBORS
return TEST_BGP_NEIGHBORS.copy()
def get_ntp_peers(self, **kwargs):
return TEST_NTP_PEERS
return TEST_NTP_PEERS.copy()
def get_ntp_servers(self, **kwargs):
return TEST_NTP_SERVERS
return TEST_NTP_SERVERS.copy()
def get_ntp_stats(self, **kwargs):
return TEST_NTP_STATS
return TEST_NTP_STATS.copy()
def get_probes_config(self, **kwargs):
return TEST_PROBES_CONFIG
return TEST_PROBES_CONFIG.copy()
def get_probes_results(self, **kwargs):
return TEST_PROBES_RESULTS
return TEST_PROBES_RESULTS.copy()
def get_route_to(self, destination, protocol=None, **kwargs):
assert destination == "1.2.3.4"
return TEST_ROUTE
return TEST_ROUTE.copy()
def get_snmp_information(self, **kwargs):
return TEST_SNMP_INFO
return TEST_SNMP_INFO.copy()
def get_users(self, **kwargs):
return TEST_USERS
return TEST_USERS.copy()
def mock_proxy_napalm_wrap(func):
@ -458,11 +503,6 @@ def mock_proxy_napalm_wrap(func):
return func_wrapper
import salt.utils.napalm as napalm_utils # NOQA
napalm_utils.proxy_napalm_wrap = mock_proxy_napalm_wrap # NOQA
def true(name):
return True

View file

@ -1,41 +1,42 @@
# -*- coding: utf-8 -*-
"""
:codeauthor: :email:`Anthony Shaw <anthonyshaw@apache.org>`
"""
# Import Python Libs
from __future__ import absolute_import, print_function, unicode_literals
import salt.modules.napalm_bgp as napalm_bgp
import tests.support.napalm as napalm_test_support
# Import Salt Testing Libs
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.mock import MagicMock
from tests.support.mock import MagicMock, patch
from tests.support.unit import TestCase
import salt.modules.napalm_bgp as napalm_bgp # NOQA
class NapalmBgpModuleTestCase(TestCase, LoaderModuleMockMixin):
def setup_loader_modules(self):
patcher = patch(
"salt.utils.napalm.get_device",
MagicMock(return_value=napalm_test_support.MockNapalmDevice()),
)
patcher.start()
self.addCleanup(patcher.stop)
module_globals = {
"__salt__": {
"config.option": MagicMock(
"config.get": MagicMock(
return_value={"test": {"driver": "test", "key": "2orgk34kgk34g"}}
),
"file.file_exists": napalm_test_support.true,
"file.join": napalm_test_support.join,
"file.get_managed": napalm_test_support.get_managed_file,
"random.hash": napalm_test_support.random_hash,
}
},
}
return {napalm_bgp: module_globals}
def test_config(self):
print(123,)
ret = napalm_bgp.config("test_group")
assert ret["out"] is napalm_test_support.TEST_BGP_CONFIG
assert ret["out"] == napalm_test_support.TEST_BGP_CONFIG.copy()
def test_neighbors(self):
ret = napalm_bgp.neighbors("test_address")
assert ret["out"] is napalm_test_support.TEST_BGP_NEIGHBORS
assert ret["out"] == napalm_test_support.TEST_BGP_NEIGHBORS.copy()

View file

@ -1,88 +1,91 @@
# -*- coding: utf-8 -*-
"""
Test the napalm_formula execution module.
"""
from __future__ import absolute_import, print_function, unicode_literals
# Import Python libs
import textwrap
# Import Salt modules
import salt.modules.napalm_formula as napalm_formula
# Import Salt Testing libs
from salt.utils.immutabletypes import freeze
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.mock import patch
from tests.support.mock import MagicMock, patch
from tests.support.unit import TestCase
class TestModulesNAPALMFormula(TestCase, LoaderModuleMockMixin):
model = {
"interfaces": {
"interface": {
"Ethernet1": {
"config": {
"name": "Ethernet1",
"description": "Interface Ethernet1",
},
"subinterfaces": {
"subinterface": {
"0": {
"config": {
"index": 0,
"description": "Subinterface Ethernet1.0",
@classmethod
def setUpClass(cls):
cls.model = freeze(
{
"interfaces": {
"interface": {
"Ethernet1": {
"config": {
"name": "Ethernet1",
"description": "Interface Ethernet1",
},
"subinterfaces": {
"subinterface": {
"0": {
"config": {
"index": 0,
"description": "Subinterface Ethernet1.0",
}
},
"100": {
"config": {
"index": 100,
"description": "Subinterface Ethernet1.100",
}
},
"900": {
"config": {
"index": 900,
"description": "Subinterface Ethernet1.900",
}
},
}
},
"100": {
"config": {
"index": 100,
"description": "Subinterface Ethernet1.100",
},
"Ethernet2": {
"config": {
"name": "Ethernet2",
"description": "Interface Ethernet2",
},
"subinterfaces": {
"subinterface": {
"400": {
"config": {
"index": 400,
"description": "Subinterface Ethernet2.400",
}
}
}
},
"900": {
"config": {
"index": 900,
"description": "Subinterface Ethernet1.900",
}
},
}
},
},
"Ethernet2": {
"config": {
"name": "Ethernet2",
"description": "Interface Ethernet2",
},
"subinterfaces": {
"subinterface": {
"400": {
"config": {
"index": 400,
"description": "Subinterface Ethernet2.400",
}
}
}
},
},
}
}
}
defaults = {
"interfaces": {
"interface": {
"*": {
"config": {"mtu": 2048, "enabled": True},
"subinterfaces": {
"subinterface": {"*": {"config": {"enabled": True}}}
},
},
}
}
}
}
}
)
cls.defaults = freeze(
{
"interfaces": {
"interface": {
"*": {
"config": {"mtu": 2048, "enabled": True},
"subinterfaces": {
"subinterface": {"*": {"config": {"enabled": True}}}
},
}
}
}
}
)
def setup_loader_modules(self):
patcher = patch("salt.utils.napalm.is_proxy", MagicMock(return_value=True))
patcher.start()
self.addCleanup(patcher.stop)
return {napalm_formula: {"__grains__": {"os": "eos"}}}
def test_container_path(self):
@ -94,7 +97,7 @@ class TestModulesNAPALMFormula(TestCase, LoaderModuleMockMixin):
"interfaces:interface:Ethernet1:subinterfaces:subinterface:900:config",
"interfaces:interface:Ethernet2:config",
]
ret = napalm_formula.container_path(self.model)
ret = napalm_formula.container_path(self.model.copy())
self.assertEqual(set(ret), set(paths))
def test_setval(self):
@ -160,7 +163,7 @@ class TestModulesNAPALMFormula(TestCase, LoaderModuleMockMixin):
}
}
}
ret = napalm_formula.defaults(self.model, self.defaults)
ret = napalm_formula.defaults(self.model.copy(), self.defaults.copy())
self.assertEqual(ret, expected_result)
def test_render_field(self):

View file

@ -1,20 +1,14 @@
# -*- coding: utf-8 -*-
"""
:codeauthor: :email:`Anthony Shaw <anthonyshaw@apache.org>`
"""
# Import Python Libs
from __future__ import absolute_import, print_function, unicode_literals
import salt.modules.napalm_netacl as napalm_acl
import tests.support.napalm as napalm_test_support
# Import Salt Testing Libs
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.mock import MagicMock
from tests.support.mock import MagicMock, patch
from tests.support.unit import TestCase
import salt.modules.napalm_netacl as napalm_acl # NOQA
def mock_capirca_term_config(platform, filter_name, term_name, *args, **kwargs):
assert platform == "cisco"
@ -52,9 +46,15 @@ def mock_capirca_get_term_pillar(filter_, term, *args, **kwargs):
class NapalmAclModuleTestCase(TestCase, LoaderModuleMockMixin):
def setup_loader_modules(self):
patcher = patch(
"salt.utils.napalm.get_device",
MagicMock(return_value=napalm_test_support.MockNapalmDevice()),
)
patcher.start()
self.addCleanup(patcher.stop)
module_globals = {
"__salt__": {
"config.option": MagicMock(
"config.get": MagicMock(
return_value={"test": {"driver": "test", "key": "2orgk34kgk34g"}}
),
"file.file_exists": napalm_test_support.true,

View file

@ -1,26 +1,26 @@
# -*- coding: utf-8 -*-
"""
:codeauthor: Anthony Shaw <anthonyshaw@apache.org>
"""
# Import Python Libs
from __future__ import absolute_import, print_function, unicode_literals
import salt.modules.napalm_network as napalm_network
import tests.support.napalm as napalm_test_support
# Import Salt Testing Libs
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.mock import MagicMock
from tests.support.mock import MagicMock, patch
from tests.support.unit import TestCase
import salt.modules.napalm_network as napalm_network # NOQA
class NapalmNetworkModuleTestCase(TestCase, LoaderModuleMockMixin):
def setup_loader_modules(self):
patcher = patch(
"salt.utils.napalm.get_device",
MagicMock(return_value=napalm_test_support.MockNapalmDevice()),
)
patcher.start()
self.addCleanup(patcher.stop)
module_globals = {
"__salt__": {
"config.option": MagicMock(
"config.get": MagicMock(
return_value={"test": {"driver": "test", "key": "2orgk34kgk34g"}}
),
"file.file_exists": napalm_test_support.true,
@ -38,25 +38,25 @@ class NapalmNetworkModuleTestCase(TestCase, LoaderModuleMockMixin):
def test_facts(self):
ret = napalm_network.facts()
assert ret["out"] == napalm_test_support.TEST_FACTS
assert ret["out"] == napalm_test_support.TEST_FACTS.copy()
def test_environment(self):
ret = napalm_network.environment()
assert ret["out"] == napalm_test_support.TEST_ENVIRONMENT
assert ret["out"] == napalm_test_support.TEST_ENVIRONMENT.copy()
def test_cli_single_command(self):
"""
Test that CLI works with 1 arg
"""
ret = napalm_network.cli("show run")
assert ret["out"] == napalm_test_support.TEST_COMMAND_RESPONSE
assert ret["out"] == napalm_test_support.TEST_COMMAND_RESPONSE.copy()
def test_cli_multi_command(self):
"""
Test that CLI works with 2 arg
"""
ret = napalm_network.cli("show run", "show run")
assert ret["out"] == napalm_test_support.TEST_COMMAND_RESPONSE
assert ret["out"] == napalm_test_support.TEST_COMMAND_RESPONSE.copy()
def test_traceroute(self):
ret = napalm_network.traceroute("destination.com")
@ -68,31 +68,31 @@ class NapalmNetworkModuleTestCase(TestCase, LoaderModuleMockMixin):
def test_arp(self):
ret = napalm_network.arp()
assert ret["out"] == napalm_test_support.TEST_ARP_TABLE
assert ret["out"] == napalm_test_support.TEST_ARP_TABLE.copy()
def test_ipaddrs(self):
ret = napalm_network.ipaddrs()
assert ret["out"] == napalm_test_support.TEST_IPADDRS
assert ret["out"] == napalm_test_support.TEST_IPADDRS.copy()
def test_interfaces(self):
ret = napalm_network.interfaces()
assert ret["out"] == napalm_test_support.TEST_INTERFACES
assert ret["out"] == napalm_test_support.TEST_INTERFACES.copy()
def test_lldp(self):
ret = napalm_network.lldp()
assert ret["out"] == napalm_test_support.TEST_LLDP_NEIGHBORS
assert ret["out"] == napalm_test_support.TEST_LLDP_NEIGHBORS.copy()
def test_mac(self):
ret = napalm_network.mac()
assert ret["out"] == napalm_test_support.TEST_MAC_TABLE
assert ret["out"] == napalm_test_support.TEST_MAC_TABLE.copy()
def test_config(self):
ret = napalm_network.config("running")
assert ret["out"] == napalm_test_support.TEST_RUNNING_CONFIG
assert ret["out"] == napalm_test_support.TEST_RUNNING_CONFIG.copy()
def test_optics(self):
ret = napalm_network.optics()
assert ret["out"] == napalm_test_support.TEST_OPTICS
assert ret["out"] == napalm_test_support.TEST_OPTICS.copy()
def test_load_config(self):
ret = napalm_network.load_config(text="new config")
@ -108,19 +108,19 @@ class NapalmNetworkModuleTestCase(TestCase, LoaderModuleMockMixin):
def test_commit(self):
ret = napalm_network.commit()
assert ret["out"] == napalm_test_support.TEST_RUNNING_CONFIG
assert ret["out"] == napalm_test_support.TEST_RUNNING_CONFIG.copy()
def test_discard_config(self):
ret = napalm_network.discard_config()
assert ret["out"] == napalm_test_support.TEST_RUNNING_CONFIG
assert ret["out"] == napalm_test_support.TEST_RUNNING_CONFIG.copy()
def test_compare_config(self):
ret = napalm_network.compare_config()
assert ret["out"] == napalm_test_support.TEST_RUNNING_CONFIG
assert ret["out"] == napalm_test_support.TEST_RUNNING_CONFIG.copy()
def test_rollback(self):
ret = napalm_network.rollback()
assert ret["out"] == napalm_test_support.TEST_RUNNING_CONFIG
assert ret["out"] == napalm_test_support.TEST_RUNNING_CONFIG.copy()
def test_config_changed(self):
ret = napalm_network.config_changed()

View file

@ -1,20 +1,14 @@
# -*- coding: utf-8 -*-
"""
:codeauthor: :email:`Anthony Shaw <anthonyshaw@apache.org>`
"""
# Import Python Libs
from __future__ import absolute_import, print_function, unicode_literals
import salt.modules.napalm_ntp as napalm_ntp
import tests.support.napalm as napalm_test_support
# Import Salt Testing Libs
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.mock import MagicMock
from tests.support.mock import MagicMock, patch
from tests.support.unit import TestCase
import salt.modules.napalm_ntp as napalm_ntp # NOQA
def mock_net_load_template(template, *args, **kwargs):
if template == "set_ntp_peers" or template == "delete_ntp_peers":
@ -25,9 +19,15 @@ def mock_net_load_template(template, *args, **kwargs):
class NapalmNtpModuleTestCase(TestCase, LoaderModuleMockMixin):
def setup_loader_modules(self):
patcher = patch(
"salt.utils.napalm.get_device",
MagicMock(return_value=napalm_test_support.MockNapalmDevice()),
)
patcher.start()
self.addCleanup(patcher.stop)
module_globals = {
"__salt__": {
"config.option": MagicMock(
"config.get": MagicMock(
return_value={"test": {"driver": "test", "key": "2orgk34kgk34g"}}
),
"file.file_exists": napalm_test_support.true,

View file

@ -1,58 +1,63 @@
# -*- coding: utf-8 -*-
"""
:codeauthor: :email:`Anthony Shaw <anthonyshaw@apache.org>`
"""
# Import Python Libs
from __future__ import absolute_import, print_function, unicode_literals
import salt.modules.napalm_probes as napalm_probes
import tests.support.napalm as napalm_test_support
# Import Salt Testing Libs
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.mock import MagicMock
from tests.support.mock import MagicMock, patch
from tests.support.unit import TestCase
import salt.modules.napalm_probes as napalm_probes # NOQA
TEST_PROBES = {
"new_probe": {
"new_test1": {
"probe_type": "icmp-ping",
"target": "192.168.0.1",
"source": "192.168.0.2",
"probe_count": 13,
"test_interval": 3,
}
}
}
TEST_DELETE_PROBES = {"existing_probe": {"existing_test1": {}, "existing_test2": {}}}
TEST_SCHEDULE_PROBES = {"test_probe": {"existing_test1": {}, "existing_test2": {}}}
def mock_net_load(template, *args, **kwargs):
if template == "set_probes":
assert kwargs["probes"] == TEST_PROBES
return napalm_test_support.TEST_TERM_CONFIG
if template == "delete_probes":
assert kwargs["probes"] == TEST_DELETE_PROBES
return napalm_test_support.TEST_TERM_CONFIG
if template == "schedule_probes":
assert kwargs["probes"] == TEST_SCHEDULE_PROBES
return napalm_test_support.TEST_TERM_CONFIG
raise ValueError("incorrect template {0}".format(template))
class NapalmProbesModuleTestCase(TestCase, LoaderModuleMockMixin):
@classmethod
def setUpClass(cls):
cls._test_probes = {
"new_probe": {
"new_test1": {
"probe_type": "icmp-ping",
"target": "192.168.0.1",
"source": "192.168.0.2",
"probe_count": 13,
"test_interval": 3,
}
}
}
cls._test_delete_probes = {
"existing_probe": {"existing_test1": {}, "existing_test2": {}}
}
cls._test_schedule_probes = {
"test_probe": {"existing_test1": {}, "existing_test2": {}}
}
@classmethod
def tearDownClass(cls):
cls._test_probes = cls._test_delete_probes = cls._test_schedule_probes = None
def setup_loader_modules(self):
patcher = patch(
"salt.utils.napalm.get_device",
MagicMock(return_value=napalm_test_support.MockNapalmDevice()),
)
patcher.start()
self.addCleanup(patcher.stop)
def mock_net_load(template, *args, **kwargs):
if template == "set_probes":
assert kwargs["probes"] == self._test_probes.copy()
return napalm_test_support.TEST_TERM_CONFIG.copy()
if template == "delete_probes":
assert kwargs["probes"] == self._test_delete_probes.copy()
return napalm_test_support.TEST_TERM_CONFIG.copy()
if template == "schedule_probes":
assert kwargs["probes"] == self._test_schedule_probes.copy()
return napalm_test_support.TEST_TERM_CONFIG.copy()
raise ValueError("incorrect template {}".format(template))
module_globals = {
"__salt__": {
"config.option": MagicMock(
"config.get": MagicMock(
return_value={"test": {"driver": "test", "key": "2orgk34kgk34g"}}
),
"file.file_exists": napalm_test_support.true,
@ -67,20 +72,20 @@ class NapalmProbesModuleTestCase(TestCase, LoaderModuleMockMixin):
def test_probes_config(self):
ret = napalm_probes.config()
assert ret["out"] == napalm_test_support.TEST_PROBES_CONFIG
assert ret["out"] == napalm_test_support.TEST_PROBES_CONFIG.copy()
def test_probes_results(self):
ret = napalm_probes.results()
assert ret["out"] == napalm_test_support.TEST_PROBES_RESULTS
assert ret["out"] == napalm_test_support.TEST_PROBES_RESULTS.copy()
def test_set_probes(self):
ret = napalm_probes.set_probes(TEST_PROBES)
ret = napalm_probes.set_probes(self._test_probes.copy())
assert ret["result"] is True
def test_delete_probes(self):
ret = napalm_probes.delete_probes(TEST_DELETE_PROBES)
ret = napalm_probes.delete_probes(self._test_delete_probes.copy())
assert ret["result"] is True
def test_schedule_probes(self):
ret = napalm_probes.schedule_probes(TEST_SCHEDULE_PROBES)
ret = napalm_probes.schedule_probes(self._test_schedule_probes.copy())
assert ret["result"] is True

View file

@ -1,30 +1,30 @@
# -*- coding: utf-8 -*-
"""
:codeauthor: :email:`Anthony Shaw <anthonyshaw@apache.org>`
"""
# Import Python Libs
from __future__ import absolute_import, print_function, unicode_literals
import salt.modules.napalm_route as napalm_route
import tests.support.napalm as napalm_test_support
# Import Salt Testing Libs
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.mock import MagicMock
from tests.support.mock import MagicMock, patch
from tests.support.unit import TestCase
import salt.modules.napalm_route as napalm_route # NOQA
def mock_net_load(template, *args, **kwargs):
raise ValueError("incorrect template {0}".format(template))
raise ValueError("incorrect template {}".format(template))
class NapalmRouteModuleTestCase(TestCase, LoaderModuleMockMixin):
def setup_loader_modules(self):
patcher = patch(
"salt.utils.napalm.get_device",
MagicMock(return_value=napalm_test_support.MockNapalmDevice()),
)
patcher.start()
self.addCleanup(patcher.stop)
module_globals = {
"__salt__": {
"config.option": MagicMock(
"config.get": MagicMock(
return_value={"test": {"driver": "test", "key": "2orgk34kgk34g"}}
),
"file.file_exists": napalm_test_support.true,
@ -39,4 +39,4 @@ class NapalmRouteModuleTestCase(TestCase, LoaderModuleMockMixin):
def test_show(self):
ret = napalm_route.show("1.2.3.4")
assert ret["out"] == napalm_test_support.TEST_ROUTE
assert ret["out"] == napalm_test_support.TEST_ROUTE.copy()

View file

@ -1,27 +1,27 @@
# -*- coding: utf-8 -*-
"""
:codeauthor: :email:`Anthony Shaw <anthonyshaw@apache.org>`
"""
# Import Python Libs
from __future__ import absolute_import, print_function, unicode_literals
import salt.modules.napalm_network as napalm_network
import salt.modules.napalm_snmp as napalm_snmp
import tests.support.napalm as napalm_test_support
# Import Salt Testing Libs
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.mock import MagicMock
from tests.support.mock import MagicMock, patch
from tests.support.unit import TestCase
import salt.modules.napalm_snmp as napalm_snmp # NOQA
import salt.modules.napalm_network as napalm_network # NOQA
class NapalmSnmpModuleTestCase(TestCase, LoaderModuleMockMixin):
def setup_loader_modules(self):
patcher = patch(
"salt.utils.napalm.get_device",
MagicMock(return_value=napalm_test_support.MockNapalmDevice()),
)
patcher.start()
self.addCleanup(patcher.stop)
module_globals = {
"__salt__": {
"config.option": MagicMock(
"config.get": MagicMock(
return_value={"test": {"driver": "test", "key": "2orgk34kgk34g"}}
),
"file.file_exists": napalm_test_support.true,
@ -36,7 +36,7 @@ class NapalmSnmpModuleTestCase(TestCase, LoaderModuleMockMixin):
def test_config(self):
ret = napalm_snmp.config()
assert ret["out"] == napalm_test_support.TEST_SNMP_INFO
assert ret["out"] == napalm_test_support.TEST_SNMP_INFO.copy()
def test_remove_config(self):
ret = napalm_snmp.remove_config("1.2.3.4")

View file

@ -1,27 +1,27 @@
# -*- coding: utf-8 -*-
"""
:codeauthor: :email:`Anthony Shaw <anthonyshaw@apache.org>`
"""
# Import Python Libs
from __future__ import absolute_import, print_function, unicode_literals
import salt.modules.napalm_network as napalm_network
import salt.modules.napalm_users as napalm_users
import tests.support.napalm as napalm_test_support
# Import Salt Testing Libs
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.mock import MagicMock
from tests.support.mock import MagicMock, patch
from tests.support.unit import TestCase
import salt.modules.napalm_users as napalm_users # NOQA
import salt.modules.napalm_network as napalm_network # NOQA
class NapalmUsersModuleTestCase(TestCase, LoaderModuleMockMixin):
def setup_loader_modules(self):
patcher = patch(
"salt.utils.napalm.get_device",
MagicMock(return_value=napalm_test_support.MockNapalmDevice()),
)
patcher.start()
self.addCleanup(patcher.stop)
module_globals = {
"__salt__": {
"config.option": MagicMock(
"config.get": MagicMock(
return_value={"test": {"driver": "test", "key": "2orgk34kgk34g"}}
),
"file.file_exists": napalm_test_support.true,
@ -36,7 +36,7 @@ class NapalmUsersModuleTestCase(TestCase, LoaderModuleMockMixin):
def test_config(self):
ret = napalm_users.config()
assert ret["out"] == napalm_test_support.TEST_USERS
assert ret["out"] == napalm_test_support.TEST_USERS.copy()
def test_set_users(self):
ret = napalm_users.set_users({"mircea": {}})

View file

@ -1,62 +1,72 @@
# -*- coding: utf-8 -*-
"""
:codeauthor: :email:`Anthony Shaw <anthonyshaw@apache.org>`
"""
# Import Python Libs
from __future__ import absolute_import, print_function, unicode_literals
import salt.modules.napalm_network as napalm_network
import salt.modules.napalm_yang_mod as napalm_yang_mod
import tests.support.napalm as napalm_test_support
# Import Salt Testing Libs
from salt.utils.immutabletypes import freeze
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.mock import MagicMock
from tests.support.mock import MagicMock, patch
from tests.support.unit import TestCase
import salt.modules.napalm_yang_mod as napalm_yang_mod # NOQA
import salt.modules.napalm_network as napalm_network # NOQA
TEST_DIFF = {"diff1": "value"}
class MockNapalmYangModel(object):
class MockNapalmYangModel:
def Root(self):
return MagicMock()
class MockNapalmYangModels(object):
class MockNapalmYangModels:
openconfig_interfaces = MockNapalmYangModel()
class MockUtils(object):
class MockUtils:
def __init__(self, test_diff):
self.test_diff = test_diff
def diff(self, *args):
return TEST_DIFF
return self.test_diff
class MockNapalmYangModule(object):
base = MockNapalmYangModel()
models = MockNapalmYangModels()
utils = MockUtils()
TEST_CONFIG = {
"comment": "Configuration discarded.",
"already_configured": False,
"result": True,
"diff": '[edit interfaces xe-0/0/5]+ description "Adding a description";',
}
def mock_net_load_config(**kwargs):
return TEST_CONFIG
class MockNapalmYangModule:
def __init__(self, test_diff):
self.base = MockNapalmYangModel()
self.models = MockNapalmYangModels()
self.utils = MockUtils(test_diff)
class NapalmYangModModuleTestCase(TestCase, LoaderModuleMockMixin):
@classmethod
def setUpClass(cls):
cls._test_config = freeze(
{
"comment": "Configuration discarded.",
"already_configured": False,
"result": True,
"diff": '[edit interfaces xe-0/0/5]+ description "Adding a description";',
}
)
cls._test_diff = freeze({"diff1": "value"})
@classmethod
def tearDownClass(cls):
cls._test_config = cls._test_diff = None
def setup_loader_modules(self):
patcher = patch(
"salt.utils.napalm.get_device",
MagicMock(return_value=napalm_test_support.MockNapalmDevice()),
)
patcher.start()
self.addCleanup(patcher.stop)
def mock_net_load_config(**kwargs):
return self._test_config.copy()
module_globals = {
"__salt__": {
"config.option": MagicMock(
"config.get": MagicMock(
return_value={"test": {"driver": "test", "key": "2orgk34kgk34g"}}
),
"file.file_exists": napalm_test_support.true,
@ -65,22 +75,22 @@ class NapalmYangModModuleTestCase(TestCase, LoaderModuleMockMixin):
"random.hash": napalm_test_support.random_hash,
"net.load_template": napalm_network.load_template,
"net.load_config": mock_net_load_config,
}
},
"napalm_yang": MockNapalmYangModule(self._test_diff.copy()),
}
module_globals["napalm_yang"] = MockNapalmYangModule()
return {napalm_yang_mod: module_globals, napalm_network: module_globals}
def test_diff(self):
ret = napalm_yang_mod.diff({}, {"test": True}, "models.openconfig_interfaces")
assert ret == TEST_DIFF
assert ret == self._test_diff.copy()
def test_diff_list(self):
"""
Test it with an actual list
"""
ret = napalm_yang_mod.diff({}, {"test": True}, ["models.openconfig_interfaces"])
assert ret == TEST_DIFF
assert ret == self._test_diff.copy()
def test_parse(self):
ret = napalm_yang_mod.parse("models.openconfig_interfaces")
@ -92,7 +102,7 @@ class NapalmYangModModuleTestCase(TestCase, LoaderModuleMockMixin):
def test_load_config(self):
ret = napalm_yang_mod.load_config({}, "models.openconfig_interfaces")
assert ret is TEST_CONFIG
assert ret == self._test_config.copy()
def test_compliance_report(self):
ret = napalm_yang_mod.compliance_report({}, "models.openconfig_interfaces")

View file

@ -1,32 +1,29 @@
# -*- coding: utf-8 -*-
"""
:codeauthor: :email:`Anthony Shaw <anthonyshaw@apache.org>`
"""
# Import Python Libs
from __future__ import absolute_import, print_function, unicode_literals
import salt.proxy.napalm as napalm_proxy
import tests.support.napalm as napalm_test_support
# Import Salt Testing Libs
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.mock import MagicMock, patch
from tests.support.unit import TestCase
import salt.proxy.napalm as napalm_proxy # NOQA
TEST_OPTS = {"proxytype": "napalm", "driver": "junos", "host": "core05.nrt02"}
def mock_get_device(opts, *args, **kwargs):
assert opts == TEST_OPTS
return {"DRIVER": napalm_test_support.MockNapalmDevice(), "UP": True}
@patch("salt.utils.napalm.get_device", mock_get_device)
class NapalmProxyTestCase(TestCase, LoaderModuleMockMixin):
def setup_loader_modules(self):
def mock_get_device(opts, *args, **kwargs):
assert opts == self.test_opts
return {"DRIVER": napalm_test_support.MockNapalmDevice(), "UP": True}
patcher = patch("salt.utils.napalm.get_device", mock_get_device)
patcher.start()
self.addCleanup(patcher.stop)
self.test_opts = {
"proxytype": "napalm",
"driver": "junos",
"host": "core05.nrt02",
}
module_globals = {
"__salt__": {
"config.option": MagicMock(
@ -38,47 +35,47 @@ class NapalmProxyTestCase(TestCase, LoaderModuleMockMixin):
return {napalm_proxy: module_globals}
def test_init(self):
ret = napalm_proxy.init(TEST_OPTS)
ret = napalm_proxy.init(self.test_opts)
assert ret is True
def test_alive(self):
ret = napalm_proxy.alive(TEST_OPTS)
ret = napalm_proxy.alive(self.test_opts)
assert ret is True
def test_ping(self):
napalm_proxy.init(TEST_OPTS)
napalm_proxy.init(self.test_opts)
ret = napalm_proxy.ping()
assert ret is True
def test_initialized(self):
napalm_proxy.init(TEST_OPTS)
napalm_proxy.init(self.test_opts)
ret = napalm_proxy.initialized()
assert ret is True
def test_get_device(self):
napalm_proxy.init(TEST_OPTS)
napalm_proxy.init(self.test_opts)
ret = napalm_proxy.get_device()
assert ret["UP"] is True
def test_get_grains(self):
napalm_proxy.init(TEST_OPTS)
napalm_proxy.init(self.test_opts)
ret = napalm_proxy.get_grains()
assert ret["out"] == napalm_test_support.TEST_FACTS
assert ret["out"] == napalm_test_support.TEST_FACTS.copy()
def test_grains_refresh(self):
napalm_proxy.init(TEST_OPTS)
napalm_proxy.init(self.test_opts)
ret = napalm_proxy.grains_refresh()
assert ret["out"] == napalm_test_support.TEST_FACTS
assert ret["out"] == napalm_test_support.TEST_FACTS.copy()
def test_fns(self):
ret = napalm_proxy.fns()
assert "details" in ret.keys()
def test_shutdown(self):
ret = napalm_proxy.shutdown(TEST_OPTS)
ret = napalm_proxy.shutdown(self.test_opts)
assert ret is True
def test_call(self):
napalm_proxy.init(TEST_OPTS)
napalm_proxy.init(self.test_opts)
ret = napalm_proxy.call("get_arp_table")
assert ret["out"] == napalm_test_support.TEST_ARP_TABLE
assert ret["out"] == napalm_test_support.TEST_ARP_TABLE.copy()