Skip to content

Commit

Permalink
Merge pull request #191 from Juniper/unit-tests
Browse files Browse the repository at this point in the history
Unit tests up to 70% overall.
  • Loading branch information
shermdog committed May 5, 2014
2 parents b7223e7 + 7faa972 commit c5d691a
Show file tree
Hide file tree
Showing 39 changed files with 1,744 additions and 43 deletions.
4 changes: 4 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[run]
omit =
lib/jnpr/junos/cfg/phyport/*
lib/jnpr/junos/cfg/srx/*
1 change: 1 addition & 0 deletions development.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ coverage # http://nedbatchelder.com/code/coverage/
mock # http://www.voidspace.org.uk/python/mock/
nose # http://nose.readthedocs.org/en/latest/
pep8 # https://github.com/jcrocholl/pep8
pyflakes # https://launchpad.net/pyflakes
2 changes: 1 addition & 1 deletion lib/jnpr/junos/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ def open(self, *vargs, **kvargs):

except socket.gaierror:
# invalid DNS name, so unreachable
raise EzErrors.ConnectUnknownError(self)
raise EzErrors.ConnectUnknownHostError(self)

except Exception as err:
# anything else, we will re-raise as a
Expand Down
2 changes: 1 addition & 1 deletion lib/jnpr/junos/utils/sw.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def _scp_progress(_path, _total, _xfrd):

# execute the secure-copy with the Python SCP module

with SCP(self._dev, progress=_scp_progress) as scp:
with SCP(self._dev, progress=_scp_progress) as scp:
scp.put(package, remote_path)

# -------------------------------------------------------------------------
Expand Down
10 changes: 9 additions & 1 deletion tests/unit/facts/test_chassis.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

import unittest
from nose.plugins.attrib import attr
from mock import patch
from mock import patch, MagicMock
from lxml import etree
import os

from jnpr.junos import Device
from jnpr.junos.facts.chassis import facts_chassis as chassis
from jnpr.junos.exception import ConnectNotMasterError

from ncclient.manager import Manager, make_device_handler
from ncclient.transport import SSHSession
Expand All @@ -30,6 +32,12 @@ def test_2RE_true(self, mock_execute):
chassis(self.dev, self.facts)
self.assertTrue(self.facts['2RE'])

def test_chassis_exception_ConnectNotMasterError(self):
xmldata = etree.XML('<rpc-reply><output>test</output></rpc-reply>')
self.dev.rpc.get_chassis_inventory = MagicMock(side_effect=xmldata)
with self.assertRaises(ConnectNotMasterError):
chassis(self.dev, self.facts)

def _read_file(self, fname):
from ncclient.xml_ import NCElement

Expand Down
42 changes: 42 additions & 0 deletions tests/unit/facts/test_domain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
__author__ = "Nitin Kumar, Rick Sherman"
__credits__ = "Jeremy Schulman"

import unittest
from nose.plugins.attrib import attr
from mock import patch

from jnpr.junos.facts.domain import facts_domain
from jnpr.junos import Device


@attr('unit')
class TestDomain(unittest.TestCase):

def setUp(self):
self.dev = Device(host='1.1.1.1', user='rick', password='password123',
gather_facts=False)
self.facts = {}

@patch('jnpr.junos.facts.domain.FS.cat')
def test_resolv_conf(self, mock_fs_cat):
mock_fs_cat.return_value =\
"""# domain juniper.net
search englab.juniper.net spglab.juniper.net juniper.net jnpr.net
nameserver 10.11.12.13
"""
self.facts['hostname'] = 'test'
facts_domain(self.dev, self.facts)
self.assertEqual(self.facts['domain'], 'juniper.net')
self.assertEqual(self.facts['fqdn'], 'test.juniper.net')

@patch('jnpr.junos.facts.domain.FS.cat')
def test_resolv_conf_no_domain(self, mock_fs_cat):
mock_fs_cat.return_value =\
"""
search englab.juniper.net spglab.juniper.net juniper.net jnpr.net
nameserver 10.11.12.13
"""
self.facts['hostname'] = 'test'
facts_domain(self.dev, self.facts)
self.assertIsNone(self.facts['domain'])
self.assertEqual(self.facts['fqdn'], 'test')
22 changes: 15 additions & 7 deletions tests/unit/facts/test_swver.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,22 @@ def test_version_info_repr(self):
'type=R, minor=7, build=5)')

def test_version_info_lt(self):
self.assertTrue(version_info('13.3-20131120') < (14, 1))
self.assertLess(version_info('13.3-20131120'), (14, 1))

def test_version_info_lt_eq(self):
self.assertTrue(version_info('13.3-20131120') <= (14, 1))
self.assertLessEqual(version_info('13.3-20131120'), (14, 1))

def test_version_info_gt(self):
self.assertTrue(version_info('13.3-20131120') > (12, 1))
self.assertGreater(version_info('13.3-20131120'), (12, 1))

def test_version_info_gt_eq(self):
self.assertTrue(version_info('13.3-20131120') >= (12, 1))
self.assertGreaterEqual(version_info('13.3-20131120'), (12, 1))

def test_version_info_eq(self):
self.assertTrue(version_info('13.3-20131120') == (13, 3))
self.assertEqual(version_info('13.3-20131120'), (13, 3))

def test_version_info_not_eq(self):
self.assertTrue(version_info('13.3-20131120') != (15, 3))
self.assertNotEqual(version_info('13.3-20131120'), (15, 3))


@attr('unit')
Expand All @@ -63,6 +63,14 @@ def test_swver(self, mock_execute):
software_version(self.dev, self.facts)
self.assertEqual(self.facts['version'], '12.3R6.6')

@patch('jnpr.junos.Device.execute')
def test_swver_hostname_none(self, mock_execute):
mock_execute.side_effect = self._mock_manager
self.facts['master'] = 'RE5'
self.facts['version_RE5'] = '15.3R6.6'
software_version(self.dev, self.facts)
self.assertEqual(self.facts['version'], '15.3R6.6')

# --> JLS, there should always be a facts['master'] assigned.
# @patch('jnpr.junos.Device.execute')
# def test_swver_master_none(self, mock_execute):
Expand All @@ -73,7 +81,7 @@ def test_swver(self, mock_execute):

@patch('jnpr.junos.Device.execute')
@patch('jnpr.junos.facts.swver.re.findall')
def test_swver_exception_handling(self, mock_re_findall, mock_execute):
def test_swver_exception_handling(self, mock_re_findall, mock_execute):
mock_execute.side_effect = self._mock_manager
mock_re_findall.side_effect = IndexError
self.facts['master'] = 'RE0'
Expand Down
154 changes: 154 additions & 0 deletions tests/unit/rpc-reply/show-configuration.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<configuration-information>
<configuration-output>
## Last commit: 2014-03-24 16:34:32 UTC by rick
version 12.1X46-D15.3;
system {
host-name firefly;
root-authentication {
encrypted-password "$1$kmSqRIU6$9EogG7ow0DWiww9mev8.b."; ## SECRET-DATA
}
login {
user rick {
uid 2000;
class super-user;
authentication {
encrypted-password "$1$wDDri7eJ$2Ot4pfE29PgVbeutIveov1"; ## SECRET-DATA
}
}
}
services {
ssh;
netconf {
ssh;
}
web-management {
http {
interface ge-0/0/0.0;
}
}
}
syslog {
user * {
any emergency;
}
file messages {
any any;
authorization info;
}
file interactive-commands {
interactive-commands any;
}
}
license {
autoupdate {
url https://ae1.juniper.net/junos/key_retrieval;
}
}
}
interfaces {
ge-0/0/0 {
unit 0 {
family inet {
address 10.0.0.31/24;
}
}
}
ge-0/0/1 {
unit 0 {
family inet {
address 192.168.2.1/24;
}
}
}
}
routing-options {
static {
route 0.0.0.0/0 next-hop 10.0.0.1;
}
}
security {
screen {
ids-option untrust-screen {
icmp {
ping-death;
}
ip {
source-route-option;
tear-drop;
}
tcp {
syn-flood {
alarm-threshold 1024;
attack-threshold 200;
source-threshold 1024;
destination-threshold 2048;
queue-size 2000; ## Warning: 'queue-size' is deprecated
timeout 20;
}
land;
}
}
}
policies {
from-zone trust to-zone trust {
policy default-permit {
match {
source-address any;
destination-address any;
application any;
}
then {
permit;
}
}
}
from-zone trust to-zone untrust {
policy default-permit {
match {
source-address any;
destination-address any;
application any;
}
then {
permit;
}
}
}
from-zone untrust to-zone trust {
policy default-deny {
match {
source-address any;
destination-address any;
application any;
}
then {
deny;
}
}
}
}
zones {
security-zone trust {
tcp-rst;
}
security-zone untrust {
screen untrust-screen;
interfaces {
ge-0/0/0.0 {
host-inbound-traffic {
system-services {
http;
https;
ssh;
telnet;
dhcp;
netconf;
}
}
}
}
}
}
}
</configuration-output>
</configuration-information>
5 changes: 5 additions & 0 deletions tests/unit/rpc-reply/show-system-alarms.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<output>
1 alarms currently active
Alarm time Class Description
2014-04-17 09:09:21 UTC Minor Rescue configuration is not set
</output>
9 changes: 9 additions & 0 deletions tests/unit/rpc-reply/show-system-uptime-rpc.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<rpc-reply xmlns:junos="http://xml.juniper.net/junos/12.1X46/junos">
<rpc>
<get-system-uptime-information>
</get-system-uptime-information>
</rpc>
<cli>
<banner></banner>
</cli>
</rpc-reply>
4 changes: 4 additions & 0 deletions tests/unit/templates/config-example.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
system {
host-name {{ host_name }};
domain-name {{ domain_name }};
}
Loading

0 comments on commit c5d691a

Please sign in to comment.