Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_commit(self, mock_request):
session = ncclient.transport.SSHSession(self.device_handler)
session._server_capabilities = [':candidate', ":confirmed-commit"]
obj = Commit(session, self.device_handler, raise_mode=RaiseMode.ALL)
obj.request(confirmed=True, timeout="0")
node = new_ele("commit")
sub_ele(node, "confirmed")
sub_ele(node, "confirm-timeout").text = "0"
xml = ElementTree.tostring(node)
call = mock_request.call_args_list[0][0][0]
call = ElementTree.tostring(call)
self.assertEqual(call, xml)
def test_loadconf_json(self, mock_request, mock_session):
device_handler = manager.make_device_handler({'name': 'junos'})
session = ncclient.transport.SSHSession(device_handler)
obj = LoadConfiguration(
session,
device_handler,
raise_mode=RaiseMode.ALL)
location = '{ "configuration": { "system": { "location": { "floor": "7" }}}}'
config_json = json.loads(location)
config = json.dumps(config_json)
obj.request(format='json', action='merge', config=config)
node = new_ele('load-configuration', {'format': 'json', 'action': 'merge'})
sub_ele(node, 'configuration-json').text = config
call = mock_request.call_args_list[0][0][0]
self.assertEqual(call.tag, node.tag)
self.assertEqual(call.attrib, node.attrib)
def test_command(self, mock_request, mock_session):
device_handler = manager.make_device_handler({'name': 'junos'})
session = ncclient.transport.SSHSession(device_handler)
obj = Command(session, device_handler, raise_mode=RaiseMode.ALL)
command = 'show system users'
format = 'text'
obj.request(command=command, format=format)
node = new_ele('command', {'format': format})
node.text = command
call = mock_request.call_args_list[0][0][0]
self.assertEqual(call.tag, node.tag)
self.assertEqual(call.text, node.text)
def test_subscribe_all(self, mock_request):
session = ncclient.transport.SSHSession(self.device_handler)
session._server_capabilities = [":notification"]
obj = CreateSubscription(
session,
self.device_handler,
raise_mode=RaiseMode.ALL)
obj.request()
node = new_ele_ns("create-subscription", NETCONF_NOTIFICATION_NS)
xml = ElementTree.tostring(node)
call = mock_request.call_args_list[0][0][0]
call = ElementTree.tostring(call)
self.assertEqual(call, xml)
def test_rpc_capability_error(self, mock_thread, mock_send):
device_handler = manager.make_device_handler({'name': 'junos'})
capabilities = Capabilities(device_handler.get_capabilities())
session = ncclient.transport.Session(capabilities)
session._server_capabilities = [':running']
obj = RPC(session, device_handler, raise_mode=RaiseMode.ALL, timeout=0)
obj._assert(':running')
self.assertRaises(MissingCapabilityError,
obj._assert, ':candidate')
mock_session, mock_recv, mock_close,
mock_send, mock_send_ready,
mock_connected):
mock_send.return_value = True
mock_send_ready.return_value = -1
mock_uuid4.return_value = type('dummy', (), {'urn': "urn:uuid:e0a7abe3-fffa-11e5-b78e-b8e85604f858"})
device_handler = manager.make_device_handler({'name': 'junos', 'use_filter': True})
rpc = ''
mock_recv.side_effect = self._read_file('get-software-information.xml')[:-1] + [b"]]>",
b"]]>"] + \
self._read_file('get-software-information.xml')[1:]
session = SSHSession(device_handler)
session._connected = True
session._channel = paramiko.Channel("c100")
session.parser = session._device_handler.get_xml_parser(session)
obj = ExecuteRpc(session, device_handler, raise_mode=RaiseMode.ALL)
obj._filter_xml = ''
session.run()
resp = obj.request(rpc)._NCElement__doc[0]
self.assertEqual(len(resp.xpath('multi-routing-engine-item/re-name')), 2)
self.assertEqual(len(resp.xpath('multi-routing-engine-item/software-information')), 0)
@unittest.skipIf(sys.version_info.major == 2, "test not supported < Python3")
@patch('ncclient.transport.SSHSession.connected')
@patch('paramiko.channel.Channel.send_ready')
@patch('paramiko.channel.Channel.send')
@patch('ncclient.transport.ssh.SSHSession.close')
@patch('paramiko.channel.Channel.recv')
@patch('ncclient.transport.SSHSession')
@patch('selectors.DefaultSelector.select')
@patch('ncclient.operations.rpc.uuid4')
def test_filter_xml_delimiter_multiple_rpc_in_parallel(self, mock_uuid4, mock_select,
def __set_raise_mode(self, mode):
assert(mode in (operations.RaiseMode.NONE, operations.RaiseMode.ERRORS, operations.RaiseMode.ALL))
self._raise_mode = mode