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_rpc_send(self, mock_thread, mock_send):
device_handler = manager.make_device_handler({'name': 'junos'})
capabilities = Capabilities(device_handler.get_capabilities())
session = ncclient.transport.Session(capabilities)
obj = RPC(session, device_handler, raise_mode=RaiseMode.ALL, timeout=0)
reply = RPCReply(xml1)
obj._reply = reply
node = new_ele("commit")
sub_ele(node, "confirmed")
sub_ele(node, "confirm-timeout").text = "50"
sub_ele(node, "log").text = "message"
result = obj._request(node)
ele = new_ele("rpc",
{"message-id": obj._id},
**device_handler.get_xml_extra_prefix_kwargs())
ele.append(node)
node = to_xml(ele)
mock_send.assert_called_once_with(node)
self.assertEqual(
result.data_xml,
(NCElement(
def test_kill_session(self, mock_request, mock_session):
session = ncclient.transport.SSHSession(self.device_handler)
obj = KillSession(
session,
self.device_handler,
raise_mode=RaiseMode.ALL)
obj.request("100")
node = new_ele("kill-session")
sub_ele(node, "session-id").text = "100"
xml = ElementTree.tostring(node)
call = mock_request.call_args_list[0][0][0]
call = ElementTree.tostring(call)
self.assertEqual(call, xml)
def test_edit_config(self, mock_request):
session = ncclient.transport.SSHSession(self.device_handler)
obj = EditConfig(
session,
self.device_handler,
raise_mode=RaiseMode.ALL)
root = new_ele('config')
configuration = sub_ele(root, 'configuration')
system = sub_ele(configuration, 'system')
location = sub_ele(system, 'location')
sub_ele(location, 'building').text = "Main Campus, A"
sub_ele(location, 'floor').text = "5"
sub_ele(location, 'rack').text = "27"
obj.request(copy.deepcopy(root))
node = new_ele("edit-config")
node.append(util.datastore_or_url("target", "candidate"))
node.append(validated_element(root, ("config", qualify("config"))))
xml = ElementTree.tostring(node)
call = mock_request.call_args_list[0][0][0]
call = ElementTree.tostring(call)
self.assertEqual(call, xml)
def test_delete_config(self, mock_request):
session = ncclient.transport.SSHSession(self.device_handler)
obj = DeleteConfig(
session,
self.device_handler,
raise_mode=RaiseMode.ALL)
obj.request("candidate")
node = new_ele("delete-config")
node.append(util.datastore_or_url("target", "candidate"))
xml = ElementTree.tostring(node)
call = mock_request.call_args_list[0][0][0]
call = ElementTree.tostring(call)
self.assertEqual(call, xml)
def test_lock_default_param(self, mock_request, mock_session):
session = ncclient.transport.SSHSession(self.device_handler)
obj = Lock(session, self.device_handler, raise_mode=RaiseMode.ALL)
obj.request()
node = new_ele("lock")
sub_ele(sub_ele(node, "target"), "candidate")
xml = ElementTree.tostring(node)
call = mock_request.call_args_list[0][0][0]
call = ElementTree.tostring(call)
self.assertEqual(call, xml)
def test_subscribe_times(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(filter=None, start_time=start_time, stop_time=stop_time)
node = new_ele_ns("create-subscription", NETCONF_NOTIFICATION_NS)
sub_ele_ns(node, "startTime", NETCONF_NOTIFICATION_NS).text = start_time
sub_ele_ns(node, "stopTime", NETCONF_NOTIFICATION_NS).text = stop_time
xml = ElementTree.tostring(node)
call = mock_request.call_args_list[0][0][0]
call = ElementTree.tostring(call)
self.assertEqual(call, xml)
def test_get(self, mock_request):
session = ncclient.transport.SSHSession(self.device_handler)
obj = Get(session, self.device_handler, raise_mode=RaiseMode.ALL)
root_filter = new_ele('filter')
config_filter = sub_ele(root_filter, 'configuration')
system_filter = sub_ele(config_filter, 'system')
sub_ele(system_filter, 'services')
obj.request(copy.deepcopy(root_filter))
node = new_ele("get")
node.append(util.build_filter(root_filter))
xml = ElementTree.tostring(node)
call = mock_request.call_args_list[0][0][0]
call = ElementTree.tostring(call)
self.assertEqual(call, xml)
def test_filter_xml_sax_on(self, mock_uuid4, mock_select, 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')
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)
# as filter_xml is not having software-information, response wont contain it
self.assertEqual(len(resp.xpath('multi-routing-engine-item/software-information')), 0)
def test_use_filter_False(self, mock_uuid4, mock_select,
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': False})
rpc = ''
mock_recv.side_effect = self._read_file('get-software-information.xml')
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)
# as filter_xml is not having software-information, response wont contain it
self.assertEqual(len(resp.xpath('multi-routing-engine-item/software-information')), 2)
self.assertIsInstance(session.parser, DefaultXMLParser)
def __init__(self, session, device_handler, timeout=30):
self._session = session
self._async_mode = False
self._timeout = timeout
self._raise_mode = operations.RaiseMode.ALL
self._huge_tree = self.HUGE_TREE_DEFAULT
self._device_handler = device_handler