Skip to content

Commit

Permalink
handle facts gathering and raise warning in case of exception
Browse files Browse the repository at this point in the history
  • Loading branch information
vnitinv committed Dec 4, 2015
1 parent 78ed9c0 commit 6bc42c8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 11 deletions.
18 changes: 16 additions & 2 deletions lib/jnpr/junos/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import types
import platform
import warnings
import traceback

# stdlib, in support of the the 'probe' method
import socket
Expand Down Expand Up @@ -764,12 +765,25 @@ def bind(self, *vargs, **kvargs):
# facts
# ------------------------------------------------------------------------

def facts_refresh(self):
def facts_refresh(self, exception_on_failure=False):
"""
Reload the facts from the Junos device into :attr:`facts` property.
:param bool exception_on_failure: To raise exception or warning when
facts gathering errors out.
"""
for gather in FACT_LIST:
gather(self, self._facts)
try:
gather(self, self._facts)
except:
if exception_on_failure:
raise
warnings.warn('Facts gathering is incomplete. '
'To know the reason call "dev.facts_refresh(exception_on_failure=True)"', RuntimeWarning)
return



# ------------------------------------------------------------------------
# probe
Expand Down
47 changes: 38 additions & 9 deletions tests/unit/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

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

def setUp(self):
from jnpr.junos.device import _MyTemplateLoader
self.template_loader = _MyTemplateLoader()
Expand Down Expand Up @@ -83,7 +84,8 @@ def test_device_ConnectRefusedError(self, mock_manager):
@patch('jnpr.junos.device.netconf_ssh')
@patch('jnpr.junos.device.datetime')
def test_device_ConnectTimeoutError(self, mock_datetime, mock_manager):
mock_manager.connect.side_effect = NcErrors.SSHError("Could not open socket to 1.1.1.1:830")
mock_manager.connect.side_effect = NcErrors.SSHError(
"Could not open socket to 1.1.1.1:830")
from datetime import timedelta, datetime
currenttime = datetime.now()
mock_datetime.datetime.now.side_effect = [currenttime,
Expand Down Expand Up @@ -195,7 +197,10 @@ def test_device_open(self, mock_connect, mock_execute):
"""
mock_connect.side_effect = self._mock_manager
mock_execute.side_effect = self._mock_manager
self.dev2 = Device(host='2.2.2.2', user='rick', password='password123')
self.dev2 = Device(
host='2.2.2.2',
user='rick',
password='password123')
self.dev2.open()
self.assertEqual(self.dev2.connected, True)

Expand All @@ -211,6 +216,15 @@ def test_device_facts(self, mock_execute):
self.dev.facts_refresh()
assert self.dev.facts['version'] == facts['version']

@patch('jnpr.junos.Device.execute')
@patch('jnpr.junos.device.warnings')
def test_device_facts_error(self, mock_warnings, mock_execute):
with patch('jnpr.junos.utils.fs.FS.cat') as mock_cat:
mock_execute.side_effect = self._mock_manager
mock_cat.side_effect = IOError('File cant be handled')
self.dev.facts_refresh()
self.assertTrue(mock_warnings.warn.called)

def test_device_hostname(self):
self.assertEqual(self.dev.hostname, '1.1.1.1')

Expand Down Expand Up @@ -278,17 +292,25 @@ def test_device_cli_exception(self):
@patch('jnpr.junos.Device.execute')
def test_device_display_xml_rpc(self, mock_execute):
mock_execute.side_effect = self._mock_manager
self.assertEqual(self.dev.display_xml_rpc('show system uptime ').tag, 'get-system-uptime-information')
self.assertEqual(
self.dev.display_xml_rpc('show system uptime ').tag,
'get-system-uptime-information')

@patch('jnpr.junos.Device.execute')
def test_device_display_xml_rpc_text(self, mock_execute):
mock_execute.side_effect = self._mock_manager
self.assertIn('<get-system-uptime-information>', self.dev.display_xml_rpc('show system uptime ', format='text'))
self.assertIn(
'<get-system-uptime-information>',
self.dev.display_xml_rpc(
'show system uptime ',
format='text'))

@patch('jnpr.junos.Device.execute')
def test_device_display_xml_exception(self, mock_execute):
mock_execute.side_effect = self._mock_manager
self.assertEqual(self.dev.display_xml_rpc('show foo'), 'invalid command: show foo| display xml rpc')
self.assertEqual(
self.dev.display_xml_rpc('show foo'),
'invalid command: show foo| display xml rpc')

def test_device_execute(self):
self.dev._conn.rpc = MagicMock(side_effect=self._mock_manager)
Expand Down Expand Up @@ -319,7 +341,9 @@ def test_device_execute_rpc_error(self):

def test_device_execute_permission_error(self):
self.dev._conn.rpc = MagicMock(side_effect=self._mock_manager)
self.assertRaises(EzErrors.PermissionError, self.dev.rpc.get_permission_denied)
self.assertRaises(
EzErrors.PermissionError,
self.dev.rpc.get_permission_denied)

def test_device_execute_index_error(self):
self.dev._conn.rpc = MagicMock(side_effect=self._mock_manager)
Expand All @@ -334,11 +358,15 @@ def test_device_execute_unopened(self):

def test_device_execute_timeout(self):
self.dev._conn.rpc = MagicMock(side_effect=TimeoutExpiredError)
self.assertRaises(EzErrors.RpcTimeoutError, self.dev.rpc.get_rpc_timeout)
self.assertRaises(
EzErrors.RpcTimeoutError,
self.dev.rpc.get_rpc_timeout)

def test_device_execute_closed(self):
self.dev._conn.rpc = MagicMock(side_effect=NcErrors.TransportError)
self.assertRaises(EzErrors.ConnectClosedError, self.dev.rpc.get_rpc_close)
self.assertRaises(
EzErrors.ConnectClosedError,
self.dev.rpc.get_rpc_close)
self.assertFalse(self.dev.connected)

def test_device_rpcmeta(self):
Expand Down Expand Up @@ -400,7 +428,8 @@ def kve():
def test_device_template(self):
# Try to load the template relative to module base
try:
template = self.dev.Template('tests/unit/templates/config-example.xml')
template = self.dev.Template(
'tests/unit/templates/config-example.xml')
except:
# Try to load the template relative to test base
try:
Expand Down

0 comments on commit 6bc42c8

Please sign in to comment.