Skip to content

Commit

Permalink
Merge pull request #35 from mirceaulinic/TSTFW-PY3-TOX
Browse files Browse the repository at this point in the history
One more down
  • Loading branch information
mirceaulinic committed Dec 29, 2016
2 parents 70cc1d8 + 990178f commit d73ee78
Show file tree
Hide file tree
Showing 40 changed files with 539 additions and 103 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ env
*.swp

test/unit/test_devices.py

report.json
27 changes: 8 additions & 19 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ language: python

python:
- 2.7
- 3.4
- 3.5

install:
- pip install -r requirements-dev.txt
- pip install .
- pip install -e git+https://github.com/napalm-automation/napalm-base.git@develop#egg=napalm-base
- pip install tox-travis
- pip install coveralls

deploy:
Expand All @@ -19,19 +19,8 @@ deploy:
branch: master

script:
- pylama .
- cd test/unit
- nosetests --with-coverage --cover-package napalm_pluribus -v TestPluribusDriver:TestGetterPluribusDriver.test_get_facts
- nosetests --with-coverage --cover-package napalm_pluribus -v TestPluribusDriver:TestGetterPluribusDriver.test_get_interfaces
- nosetests --with-coverage --cover-package napalm_pluribus -v TestPluribusDriver:TestGetterPluribusDriver.test_get_lldp_neighbors
- nosetests --with-coverage --cover-package napalm_pluribus -v TestPluribusDriver:TestGetterPluribusDriver.test_get_lldp_neighbors_detail
- nosetests --with-coverage --cover-package napalm_pluribus -v TestPluribusDriver:TestGetterPluribusDriver.test_get_mac_address_table
- nosetests --with-coverage --cover-package napalm_pluribus -v TestPluribusDriver:TestGetterPluribusDriver.test_get_ntp_stats
- nosetests --with-coverage --cover-package napalm_pluribus -v TestPluribusDriver:TestGetterPluribusDriver.test_get_ntp_servers
- nosetests --with-coverage --cover-package napalm_pluribus -v TestPluribusDriver:TestGetterPluribusDriver.test_get_snmp_information
- nosetests --with-coverage --cover-package napalm_pluribus -v TestPluribusDriver:TestGetterPluribusDriver.test_get_users
- nosetests --with-coverage --cover-package napalm_pluribus -v TestPluribusDriver:TestGetterPluribusDriver.test_traceroute
- nosetests --with-coverage --cover-package napalm_pluribus -v TestPluribusDriver:TestGetterPluribusDriver.test_get_config
- cd ../..
- coverage combine test/unit/.coverage
after_success: coveralls
- tox
after_success:
- coveralls
- if [ $TRAVIS_TAG ]; then curl -X POST https://readthedocs.org/build/napalm; fi

63 changes: 37 additions & 26 deletions napalm_pluribus/pluribus.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
# NAPALM base
import napalm_base.helpers
import napalm_base.exceptions
import napalm_base.constants as C
from napalm_base.utils import py23_compat
from napalm_base.base import NetworkDriver

Expand Down Expand Up @@ -56,13 +57,18 @@ def open(self):
except pyPluribus.exceptions.ConnectionError as connerr:
raise napalm_base.exceptions.ConnectionException(connerr.message)

def is_alive(self):
return{
'is_alive': self.device._connection.transport.is_alive()
}

def close(self):
self.device.close()

def load_merge_candidate(self, filename=None, config=None):
return self.device.config.load_candidate(filename=filename, config=config)

def config_compare(self):
def compare_config(self):
return self.device.config.compare()

def commit_config(self):
Expand Down Expand Up @@ -110,26 +116,26 @@ def get_facts(self):

facts = {
'vendor': u'Pluribus',
'os_version': unicode(os_ver),
'hostname': unicode(hostname),
'os_version': py23_compat.text_type(os_ver),
'hostname': py23_compat.text_type(hostname),
'uptime': uptime,
'model': unicode(model),
'serial_number': unicode(serial),
'model': py23_compat.text_type(model),
'serial_number': py23_compat.text_type(serial),
'interface_list': interfaces,
'fqdn': u''
}

return facts

def cli(self, commands=None):
def cli(self, commands):

cli_output = {}

if type(commands) is not list:
raise TypeError('Please provide a valid list of commands!')

for command in commands:
cli_output[unicode(command)] = self.device.cli(command)
cli_output[py23_compat.text_type(command)] = self.device.cli(command)

return cli_output

Expand All @@ -142,16 +148,16 @@ def get_interfaces(self):

for line in interfaces_lines:
interface_details = line.split('@$@')
interface_name = unicode(interface_details[1])
interface_name = py23_compat.text_type(interface_details[1])
up = (interface_details[4] != 'disable')
enabled = (interface_details[8] == 'on')
speed = 0
if up and interface_details[4].replace('g', '').isdigit():
speed = int(1e3 * int(interface_details[4].replace('g', '')))
# > 1G interfaces
last_flap = 0.0
description = unicode(interface_details[17])
mac_address = unicode(interface_details[28])
description = py23_compat.text_type(interface_details[17])
mac_address = py23_compat.text_type(interface_details[28])
interfaces[interface_name] = {
'is_up': up,
'is_enabled': enabled,
Expand All @@ -175,7 +181,7 @@ def get_mac_address_table(self):
mac_details = line.split('@$@')
mac_raw = mac_details[2].strip()
vlan = int(mac_details[3].strip())
ports = unicode(mac_details[8].strip())
ports = py23_compat.text_type(mac_details[8].strip())
active = (mac_details[9].strip == 'active')
mac_table.append({
'mac': napalm_base.helpers.convert(
Expand All @@ -199,9 +205,9 @@ def get_lldp_neighbors(self):

for line in lines:
neighbor_details = line.split('@$@')
port = unicode(neighbor_details[1].strip())
port_id = unicode(neighbor_details[3].strip())
system_name = unicode(neighbor_details[6].strip())
port = py23_compat.text_type(neighbor_details[1].strip())
port_id = py23_compat.text_type(neighbor_details[3].strip())
system_name = py23_compat.text_type(neighbor_details[6].strip())
if port_id not in lldp_neighbors.keys():
lldp_neighbors[port_id] = []
lldp_neighbors[port_id].append({
Expand All @@ -211,7 +217,7 @@ def get_lldp_neighbors(self):

return lldp_neighbors

def get_lldp_neighbors_detail(self):
def get_lldp_neighbors_detail(self, interface=''):

lldp_neighbors = {}

Expand All @@ -220,12 +226,14 @@ def get_lldp_neighbors_detail(self):

for line in lines:
neighbor_details = line.split('@$@')
port = unicode(neighbor_details[1].strip())
port = py23_compat.text_type(neighbor_details[1].strip())
if interface and port != interface:
continue
chassis = napalm_base.helpers.convert(
napalm_base.helpers.mac, neighbor_details[2].strip())
port_id = unicode(neighbor_details[3].strip())
port_descr = unicode(neighbor_details[4].strip())
system_name = unicode(neighbor_details[6].strip())
port_id = py23_compat.text_type(neighbor_details[3].strip())
port_descr = py23_compat.text_type(neighbor_details[4].strip())
system_name = py23_compat.text_type(neighbor_details[6].strip())
if port not in lldp_neighbors.keys():
lldp_neighbors[port] = []
lldp_neighbors[port].append({
Expand Down Expand Up @@ -254,7 +262,7 @@ def get_ntp_stats(self):
ntp_stats = []

sw_setup_show = self.device.show('switch setup', delim='@$@')
ntp_server = unicode(sw_setup_show.splitlines()[9].split('@$@')[-1])
ntp_server = py23_compat.text_type(sw_setup_show.splitlines()[9].split('@$@')[-1])

ntp_stats.append({
'remote': ntp_server,
Expand Down Expand Up @@ -284,7 +292,7 @@ def get_snmp_information(self):
switch_info = self.device.show('switch info', delim='@$@')
chassis_id = switch_info.splitlines()[2].split('@$@')[-1]

snmp_information['chassis_id'] = unicode(chassis_id)
snmp_information['chassis_id'] = py23_compat.text_type(chassis_id)
snmp_information['contact'] = u''
snmp_information['location'] = u''
snmp_information['community'] = {}
Expand All @@ -294,7 +302,7 @@ def get_snmp_information(self):

for snmp_line in snmp_lines:
snmp_line_details = snmp_line.split('@$@')
snmp_community = unicode(snmp_line_details[1])
snmp_community = py23_compat.text_type(snmp_line_details[1])
snmp_mode = _SNMP_MODE_MAP_.get(snmp_line_details[2], u'ro')
snmp_acl = u''
snmp_information['community'][snmp_community] = {
Expand Down Expand Up @@ -348,8 +356,11 @@ def get_users(self):

return users

def traceroute(self, destination, source='', ttl=0, timeout=0):

def traceroute(self,
destination,
source=C.TRACEROUTE_SOURCE,
ttl=C.TRACEROUTE_TTL,
timeout=C.TRACEROUTE_TIMEOUT):
# same method as on EOS, different command send to CLI

_HOP_ENTRY_PROBE = [
Expand Down Expand Up @@ -429,8 +440,8 @@ def traceroute(self, destination, source='', ttl=0, timeout=0):
host_name = '*'
ip_address = '*'
traceroute_result['success'][hop_index]['probes'][probe_index+1] = {
'host_name': unicode(host_name),
'ip_address': unicode(ip_address),
'host_name': py23_compat.text_type(host_name),
'ip_address': py23_compat.text_type(ip_address),
'rtt': rtt
}
previous_probe_host_name = host_name
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
coveralls
pytest
pytest-cov
pytest-json
Expand Down
10 changes: 10 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
[pylama]
linters = mccabe,pep8,pyflakes
ignore = D203,C901
skip = build/*,.tox/*

[pylama:pep8]
max_line_length = 100

[pytest]
addopts = --cov=napalm_pluribus --cov-report term-missing -vs --pylama
json_report = report.json
jsonapi = true

[coverage:run]
source = napalm_pluribus

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

setup(
name="napalm-pluribus",
version="0.4.2",
version="0.5.0",
packages=find_packages(),
author="David Barroso, Mircea Ulinic",
author_email="dbarrosop@dravetech.com, mircea@cloudflare.com",
Expand Down
57 changes: 1 addition & 56 deletions test/unit/TestPluribusDriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
import unittest

from napalm_pluribus import pluribus
from napalm_base.test.base import TestConfigNetworkDriver, TestGettersNetworkDriver
import re
from napalm_base.test.base import TestConfigNetworkDriver


class TestConfigPluribusDriver(unittest.TestCase, TestConfigNetworkDriver):
Expand All @@ -30,57 +29,3 @@ def setUpClass(cls):

cls.device = pluribus.PluribusDriver(hostname, username, password, timeout=60)
cls.device.open()


class TestGetterPluribusDriver(unittest.TestCase, TestGettersNetworkDriver):

@classmethod
def setUpClass(cls):
cls.mock = True

hostname = '172.17.17.1'
username = 'mircea'
password = 'password'
cls.vendor = 'pluribus'

cls.device = pluribus.PluribusDriver(hostname, username, password, timeout=60)

if cls.mock:
cls.device.device = FakePluribusDevice()
else:
cls.device.open()


class FakePluribusConfig:

def __init__(self, device):
self._device = device

def _download_running_config(self):
return self._device.show('running config')


class FakePluribusDevice:

def __init__(self):
self.config = FakePluribusConfig(self)

@staticmethod
def read_txt_file(filename):
with open(filename) as data_file:
return data_file.read()

def execute_show(self, command):

cmd = re.sub(r'[\[\]\*\^\+\s\|]', '_', command)
return self.read_txt_file('pluribus/mock_data/{}.txt'.format(cmd))

def show(self, command, delim='@$@'):
if not command.endswith('-show'):
command += '-show'
command = command.replace(' ', '-')

return self.execute_show(command)

def cli(self, command):
return self.execute_show(command)

0 comments on commit d73ee78

Please sign in to comment.