Skip to content

Commit

Permalink
native json support
Browse files Browse the repository at this point in the history
  • Loading branch information
vnitinv committed Sep 1, 2015
1 parent b6ca428 commit 36b0006
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
10 changes: 9 additions & 1 deletion lib/jnpr/junos/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,15 @@ def execute(self, rpc_cmd, **kvargs):
# dev.rpc.get_route_engine_information({'format': 'json'})

if rpc_cmd_e.attrib.get('format') in ['json', 'JSON']:
return json.loads(rpc_rsp_e.text)
if self._facts == {}:
self.facts_refresh()
ver_info = self._facts['version_info']
if ver_info.major[0] >= 15 or \
(ver_info.major[0] == 14 and ver_info.major[1] >= 2):
return json.loads(rpc_rsp_e.text)
else:
warnings.warn("Native JSON support is only from 14.2 onwards",
RuntimeWarning)

# This section is here for the possible use of something other than ncclient
# for RPCs that have embedded rpc-errors, need to check for those now
Expand Down
22 changes: 20 additions & 2 deletions tests/unit/test_rpcmeta.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

from jnpr.junos.device import Device
from jnpr.junos.rpcmeta import _RpcMetaExec
from jnpr.junos.facts.swver import version_info
from ncclient.manager import Manager, make_device_handler
from ncclient.transport import SSHSession

from mock import patch, MagicMock
from mock import patch, MagicMock, call
from lxml import etree


Expand Down Expand Up @@ -87,13 +88,30 @@ def test_rpcmeta_get_config(self, mock_execute_fn):
self.assertEqual(mock_execute_fn.call_args[0][0].tag,
'get-configuration')

def test_rpcmeta_exec_rpc_format_json(self):
def test_rpcmeta_exec_rpc_format_json_14_2(self):
self.dev._conn.rpc = MagicMock(side_effect=self._mock_manager)
self.dev._facts['version_info'] = version_info('14.2X46-D15.3')
op = self.rpc.get_system_users_information(dict(format='json'))
self.assertEqual(op['system-users-information'][0]
['uptime-information'][0]['date-time'][0]['data'],
u'4:43AM')

def test_rpcmeta_exec_rpc_format_json_gt_14_2(self):
self.dev._conn.rpc = MagicMock(side_effect=self._mock_manager)
self.dev._facts['version_info'] = version_info('15.1X46-D15.3')
op = self.rpc.get_system_users_information(dict(format='json'))
self.assertEqual(op['system-users-information'][0]
['uptime-information'][0]['date-time'][0]['data'],
u'4:43AM')

@patch('jnpr.junos.device.warnings')
def test_rpcmeta_exec_rpc_format_json_lt_14_2(self, mock_warn):
self.dev._conn.rpc = MagicMock(side_effect=self._mock_manager)
self.dev._facts['version_info'] = version_info('13.1X46-D15.3')
self.rpc.get_system_users_information(dict(format='json'))
mock_warn.assert_has_calls(call.warn(
'Native JSON support is only from 14.2 onwards', RuntimeWarning))

def _mock_manager(self, *args, **kwargs):
if kwargs:
if 'normalize' in kwargs and args:
Expand Down

0 comments on commit 36b0006

Please sign in to comment.