def test_filter_xml_sax_on_junos_rfc_compliant(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 = '<get-software-information/>' 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 = '<multi-routing-engine-results><multi-routing-engine-item><re-name/></multi-routing-engine-item></multi-routing-engine-results>' session.run() resp = obj.request(rpc)._NCElement__doc[0] from lxml import etree print(resp) 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_xml_without_sax_input(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 = '<get-software-information/>' 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 = None 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')), 2)
def test_run_recieve(self, mock_error, mock_select, mock_recv, mock_close): mock_select.return_value = True, None, None mock_recv.return_value = 0 device_handler = JunosDeviceHandler({'name': 'junos'}) obj = SSHSession(device_handler) obj._channel = paramiko.Channel("c100") obj.run() self.assertTrue( isinstance(mock_error.call_args_list[0][0][0], SessionCloseError))
def test_run_send(self, mock_error, mock_send, mock_ready, mock_close): mock_ready.return_value = True mock_send.return_value = -1 device_handler = JunosDeviceHandler({'name': 'junos'}) obj = SSHSession(device_handler) obj._channel = paramiko.Channel("c100") obj._q.put("rpc") obj.run() self.assertEqual(mock_send.call_args_list[0][0][0], "rpc") self.assertTrue( isinstance(mock_error.call_args_list[0][0][0], SessionCloseError))
def test_handle_connection_exceptions(self, mock_open, mock_init, mock_channel): session = ncclient.transport.SSHSession(self.obj) session._channel_id = 100 mock_init.return_value = None session._transport = paramiko.Transport() channel = paramiko.Channel(100) mock_open.return_value = channel self.obj.handle_connection_exceptions(session) self.assertEqual(channel._name, "netconf-command-100") self.assertEqual(mock_channel.call_args_list[0][0][0], "xml-mode netconf need-trailer")
def test_create_channel(self): client = paramiko.SSHClient() channel = paramiko.Channel(123) self.mox.StubOutWithMock(client, 'invoke_shell') self.mox.StubOutWithMock(channel, 'resize_pty') client.invoke_shell().AndReturn(channel) channel.resize_pty(600, 800) self.mox.ReplayAll() utils.create_channel(client, 600, 800) self.mox.VerifyAll()
def _test_run_send_py3(self, base, chunker, mock_error, mock_selector, mock_send, mock_ready, mock_close): mock_selector.return_value = False mock_ready.return_value = True mock_send.return_value = -1 device_handler = JunosDeviceHandler({'name': 'junos'}) obj = SSHSession(device_handler) obj._channel = paramiko.Channel("c100") msg = "naïve garçon" obj._q.put(msg) obj._base = base obj.run() self.assertEqual(mock_send.call_args_list[0][0][0], chunker(msg)) self.assertTrue( isinstance(mock_error.call_args_list[0][0][0], SessionCloseError))
def test_filter_xml_delimiter_multiple_rpc_in_parallel( 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.side_effect = [ type('xyz', (), {'urn': "urn:uuid:ddef40cb-5745-481d-974d-7188f9f2bb33"}), type('pqr', (), {'urn': "urn:uuid:549ef9d1-024a-4fd0-88bf-047d25f0870d"}) ] device_handler = manager.make_device_handler({ 'name': 'junos', 'use_filter': True }) rpc = '<get-software-information/>' mock_recv.side_effect = [ b""" <rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:junos="http://xml.juniper.net/junos/19.2I0/junos" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="urn:uuid:ddef40cb-5745-481d-974d-7188f9f2bb33"> <ospf-neighbor-information xmlns="http://xml.juniper.net/junos/19.2I0/junos-routing"> <ospf-neighbor> <neighbor-address>13.1.1.2</neighbor-address> <interface-name>ge-0/0/0.1</interface-name> <ospf-neighbor-state>Exchange</ospf-neighbor-state> <neighbor-id>2.2.2.2</neighbor-id> <neighbor-priority>128</neighbor-priority> <activity-timer>36</activity-timer> <ospf-area>0.0.0.0</ospf-area> <options>0x52</options> <dr-address>13.1.1.1</dr-address> <bdr-address>13.1.1.2</bdr-address> <neighbor-up-time junos:seconds="17812"> 04:56:52 </neighbor-up-time> <neighbor-adjacency-time junos:seconds="17812"> 04:56:52 </neighbor-adjacency-time> <master-slave>slave</master-slave> <sequence-number>0x204b6fd</sequence-number> <dbd-retransmit-time>3</dbd-retransmit-time> <lsreq-retransmit-time>0</lsreq-retransmit-time> <lsa-list> Link state retransmission list: Type LSA ID Adv rtr Seq Router 1.1.1.1 1.1.1.1 0x80000019 OpaqArea 1.0.0.1 1.1.1.1 0x80000011 Router 3.3.3.3 3.3.3.3 0x80000004 Network 23.1.1.2 3.3.3.3 0x80000001 OpaqArea 1.0.0.1 2.2.2.2 0x80000002 OpaqArea 1.0.0.1 3.3.3.3 0x80000002 OpaqArea 1.0.0.3 1.1.1.1 0x80000002 OpaqArea 1.0.0.3 3.3.3.3 0x80000001 OpaqArea 1.0.0.4 2.2.2.2 0x80000001 </lsa-list> <ospf-neighbor-topology> <ospf-topology-name>default</ospf-topology-name> <ospf-topology-id>0</ospf-topology-id> <ospf-neighbor-topology-state>Forward Only</ospf-neighbor-topology-state> </ospf-neighbor-topology> </ospf-neighbor> </ospf-neighbor-information> </rpc-reply>]]>]]><rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:junos="http://xml.juniper.net/junos/19.2I0/junos" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="urn:uuid:549ef9d1-024a-4fd0-88bf-047d25f0870d"> <pfe-statistics> <pfe-traffic-statistics> <pfe-input-packets>22450</pfe-input-packets> <input-pps>0</input-pps> <pfe-output-packets>31992</pfe-output-packets> <output-pps>0</output-pps> <pfe-fabric-input>0</pfe-fabric-input> <pfe-fabric-input-pps>0</pfe-fabric-input-pps> <pfe-fabric-output>0</pfe-fabric-output> <pfe-fabric-output-pps>0</pfe-fabric-output-pps> </pfe-traffic-statistics></pfe-statistics></rpc-reply>]]>]]>""" ] 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 = ExecuteRpc(session, device_handler, raise_mode=RaiseMode.ALL) obj._filter_xml = '<multi-routing-engine-results><multi-routing-engine-item><re-name/></multi-routing-engine-item></multi-routing-engine-results>' session.run() resp = obj.request(rpc)._NCElement__doc[0] self.assertEqual(len(resp.xpath('pfe-traffic-statistics')), 1)
def open_unix_socket(self, socket_path: str, window_size: int = common.DEFAULT_WINDOW_SIZE, max_packet_size: int = common.DEFAULT_MAX_PACKET_SIZE, timeout: tobiko.Seconds = None) \ -> paramiko.Channel: # pylint: disable=protected-access transport: typing.Any = self.connect().get_transport() if transport is None or not transport.active: raise paramiko.SSHException("SSH session not active") timeout = 3600 if timeout is None else timeout transport.lock.acquire() try: window_size = transport._sanitize_window_size(window_size) max_packet_size = transport._sanitize_packet_size(max_packet_size) chanid = transport._next_channel() # Documented here: # https://github.com/openssh/openssh-portable/blob/master/PROTOCOL m = paramiko.Message() m.add_byte(b'Z') m.add_string('*****@*****.**') m.add_int(chanid) m.add_int(window_size) m.add_int(max_packet_size) m.add_string(socket_path) # Reserved stuff m.add_string('') m.add_int(0) sock: typing.Any = paramiko.Channel(chanid) transport._channels.put(chanid, sock) transport.channel_events[chanid] = event = threading.Event() transport.channels_seen[chanid] = True sock._set_transport(transport) sock._set_window(window_size, max_packet_size) finally: transport.lock.release() transport._send_user_message(m) start_ts = tobiko.time() while True: event.wait(0.1) if not transport.active: e = transport.get_exception() if e is None: e = paramiko.SSHException("Unable to open channel.") raise e if event.is_set(): break elif start_ts + timeout < time.time(): raise paramiko.SSHException("Timeout opening channel.") sock = transport._channels.get(chanid) if sock is None: e = transport.get_exception() if e is None: e = paramiko.SSHException("Unable to open channel.") raise e return sock