Ejemplo n.º 1
0
class TestSerial(unittest.TestCase):

    @patch('jnpr.junos.transport.tty_serial.serial.Serial.open')
    @patch('jnpr.junos.transport.tty_serial.serial.Serial.write')
    @patch('jnpr.junos.transport.tty_serial.serial.Serial.flush')
    @patch('jnpr.junos.transport.tty_serial.Serial.read_prompt')
    @patch('jnpr.junos.transport.tty.tty_netconf.open')
    def setUp(self, mock_nc_open, mock_read,
              mock_flush, mock_write, mock_open):
        self.dev = Console(port='USB/ttyUSB0', baud=9600, mode='Serial')
        mock_read.side_effect = [
            ('login', 'login'), ('passwd', 'passwd'), ('shell', 'shell')]
        self.dev.open()

    @patch('jnpr.junos.transport.tty.tty_netconf.close')
    @patch('jnpr.junos.transport.tty_serial.Serial.read_prompt')
    @patch('jnpr.junos.transport.tty_serial.Serial.write')
    @patch('jnpr.junos.transport.tty_serial.Serial._tty_close')
    def tearDown(self, mock_serial_close, mock_write, mock_read, mock_close):
        mock_read.side_effect = [
            ('cli', 'cli'), ('shell', 'shell'), ('login', 'login')]
        self.dev.close()

    def test_console_connected(self):
        self.assertTrue(self.dev.connected)

    def test_close_connection(self):
        self.dev._tty._ser = MagicMock()
        self.dev.close(skip_logout=True)
        self.assertTrue(self.dev._tty._ser.close.called)
Ejemplo n.º 2
0
class TestSerial(unittest.TestCase):
    @patch("jnpr.junos.transport.tty_serial.serial.Serial.open")
    @patch("jnpr.junos.transport.tty_serial.serial.Serial.write")
    @patch("jnpr.junos.transport.tty_serial.serial.Serial.flush")
    @patch("jnpr.junos.transport.tty_serial.Serial.read_prompt")
    @patch("jnpr.junos.transport.tty.tty_netconf.open")
    def setUp(self, mock_nc_open, mock_read, mock_flush, mock_write,
              mock_open):
        self.dev = Console(port="USB/ttyUSB0", baud=9600, mode="Serial")
        mock_read.side_effect = [
            ("login", "login"),
            ("passwd", "passwd"),
            ("shell", "shell"),
        ]
        self.dev.open()

    @patch("jnpr.junos.transport.tty.sleep")
    @patch("jnpr.junos.transport.tty.tty_netconf.close")
    @patch("jnpr.junos.transport.tty_serial.Serial.read_prompt")
    @patch("jnpr.junos.transport.tty_serial.Serial.write")
    @patch("jnpr.junos.transport.tty_serial.Serial._tty_close")
    def tearDown(self, mock_serial_close, mock_write, mock_read, mock_close,
                 mock_sleep):
        mock_read.side_effect = [
            ("shell", "shell"),
            ("login", "login"),
            ("cli", "cli"),
        ]
        self.dev.close()

    def test_console_connected(self):
        self.assertTrue(self.dev.connected)

    def test_close_connection(self):
        self.dev._tty._ser = MagicMock()
        self.dev.close(skip_logout=True)
        self.assertTrue(self.dev._tty._ser.close.called)

    @patch("jnpr.junos.transport.tty_serial.serial.Serial.open")
    def test_tty_serial_open_exception(self, mock_open):
        dev = Console(port="USB/ttyUSB0", baud=9600, mode="Serial")
        mock_open.side_effect = OSError
        self.assertRaises(RuntimeError, dev.open)

    def test_tty_serial_rawwrite(self):
        self.dev._tty._ser = MagicMock()
        self.dev._tty.rawwrite("test")
        self.dev._tty._ser.write.assert_called_with("test")

    def test_tty_serial_read(self):
        self.dev._tty._ser = MagicMock()
        self.dev._tty.read()
        self.dev._tty._ser.readline.assert_called()

    def test_tty_serial_read_prompt(self):
        self.dev._tty._ser = MagicMock()
        self.dev._tty.EXPECT_TIMEOUT = 0.1
        self.dev._tty._ser.readline.side_effect = ["", "test"]
        self.assertEqual(self.dev._tty.read_prompt()[0], None)
Ejemplo n.º 3
0
class TestSerial(unittest.TestCase):
    @patch('jnpr.junos.transport.tty_serial.serial.Serial.open')
    @patch('jnpr.junos.transport.tty_serial.serial.Serial.write')
    @patch('jnpr.junos.transport.tty_serial.serial.Serial.flush')
    @patch('jnpr.junos.transport.tty_serial.Serial.read_prompt')
    @patch('jnpr.junos.transport.tty.tty_netconf.open')
    def setUp(self, mock_nc_open, mock_read, mock_flush, mock_write,
              mock_open):
        self.dev = Console(port='USB/ttyUSB0', baud=9600, mode='Serial')
        mock_read.side_effect = [('login', 'login'), ('passwd', 'passwd'),
                                 ('shell', 'shell')]
        self.dev.open()

    @patch('jnpr.junos.transport.tty.sleep')
    @patch('jnpr.junos.transport.tty.tty_netconf.close')
    @patch('jnpr.junos.transport.tty_serial.Serial.read_prompt')
    @patch('jnpr.junos.transport.tty_serial.Serial.write')
    @patch('jnpr.junos.transport.tty_serial.Serial._tty_close')
    def tearDown(self, mock_serial_close, mock_write, mock_read, mock_close,
                 mock_sleep):
        mock_read.side_effect = [
            ('shell', 'shell'),
            ('login', 'login'),
            ('cli', 'cli'),
        ]
        self.dev.close()

    def test_console_connected(self):
        self.assertTrue(self.dev.connected)

    def test_close_connection(self):
        self.dev._tty._ser = MagicMock()
        self.dev.close(skip_logout=True)
        self.assertTrue(self.dev._tty._ser.close.called)

    @patch('jnpr.junos.transport.tty_serial.serial.Serial.open')
    def test_tty_serial_open_exception(self, mock_open):
        dev = Console(port='USB/ttyUSB0', baud=9600, mode='Serial')
        mock_open.side_effect = OSError
        self.assertRaises(RuntimeError, dev.open)

    def test_tty_serial_rawwrite(self):
        self.dev._tty._ser = MagicMock()
        self.dev._tty.rawwrite('test')
        self.dev._tty._ser.write.assert_called_with('test')

    def test_tty_serial_read(self):
        self.dev._tty._ser = MagicMock()
        self.dev._tty.read()
        self.dev._tty._ser.readline.assert_called()

    def test_tty_serial_read_prompt(self):
        self.dev._tty._ser = MagicMock()
        self.dev._tty.EXPECT_TIMEOUT = 0.1
        self.dev._tty._ser.readline.side_effect = ['', 'test']
        self.assertEqual(self.dev._tty.read_prompt()[0], None)
Ejemplo n.º 4
0
class TestSerial(unittest.TestCase):

    @patch('jnpr.junos.transport.tty_serial.serial.Serial.open')
    @patch('jnpr.junos.transport.tty_serial.serial.Serial.write')
    @patch('jnpr.junos.transport.tty_serial.serial.Serial.flush')
    @patch('jnpr.junos.transport.tty_serial.Serial.read_prompt')
    @patch('jnpr.junos.transport.tty.tty_netconf.open')
    def setUp(self, mock_nc_open, mock_read,
              mock_flush, mock_write, mock_open):
        self.dev = Console(port='USB/ttyUSB0', baud=9600, mode='Serial')
        mock_read.side_effect = [
            ('login', 'login'), ('passwd', 'passwd'), ('shell', 'shell')]
        self.dev.open()

    @patch('jnpr.junos.transport.tty.sleep')
    @patch('jnpr.junos.transport.tty.tty_netconf.close')
    @patch('jnpr.junos.transport.tty_serial.Serial.read_prompt')
    @patch('jnpr.junos.transport.tty_serial.Serial.write')
    @patch('jnpr.junos.transport.tty_serial.Serial._tty_close')
    def tearDown(self, mock_serial_close, mock_write, mock_read, mock_close,
                 mock_sleep):
        # mock_read.side_effect = [('shell', 'shell'), ('login', 'login'),
        mock_read.side_effect = [('shell', 'shell'), ('login', 'login'),
                                 ('cli', 'cli'), ]
        self.dev.close()

    def test_console_connected(self):
        self.assertTrue(self.dev.connected)

    def test_close_connection(self):
        self.dev._tty._ser = MagicMock()
        self.dev.close(skip_logout=True)
        self.assertTrue(self.dev._tty._ser.close.called)

    @patch('jnpr.junos.transport.tty_serial.serial.Serial.open')
    def test_tty_serial_open_exception(self, mock_open):
        dev = Console(port='USB/ttyUSB0', baud=9600, mode='Serial')
        mock_open.side_effect = OSError
        self.assertRaises(RuntimeError, dev.open)

    def test_tty_serial_rawwrite(self):
        self.dev._tty._ser = MagicMock()
        self.dev._tty.rawwrite('test')
        self.dev._tty._ser.write.assert_called_with('test')

    def test_tty_serial_read(self):
        self.dev._tty._ser = MagicMock()
        self.dev._tty.read()
        self.dev._tty._ser.readline.assert_called()

    def test_tty_serial_read_prompt(self):
        self.dev._tty._ser = MagicMock()
        self.dev._tty.EXPECT_TIMEOUT = 0.1
        self.dev._tty._ser.readline.side_effect = ['', 'test']
        self.assertEqual(self.dev._tty.read_prompt()[0], None)
Ejemplo n.º 5
0
class TestSerial(unittest.TestCase):
    @patch("jnpr.junos.transport.tty_serial.serial.Serial.open")
    @patch("jnpr.junos.transport.tty_serial.serial.Serial.write")
    @patch("jnpr.junos.transport.tty_serial.serial.Serial.flush")
    @patch("jnpr.junos.transport.tty_serial.Serial.read_prompt")
    @patch("jnpr.junos.transport.tty.tty_netconf.open")
    def setUp(self, mock_nc_open, mock_read, mock_flush, mock_write, mock_open):
        self.dev = Console(port="USB/ttyUSB0", baud=9600, mode="Serial")
        mock_read.side_effect = [("login", "login"), ("passwd", "passwd"), ("shell", "shell")]
        self.dev.open()

    @patch("jnpr.junos.transport.tty.sleep")
    @patch("jnpr.junos.transport.tty.tty_netconf.close")
    @patch("jnpr.junos.transport.tty_serial.Serial.read_prompt")
    @patch("jnpr.junos.transport.tty_serial.Serial.write")
    @patch("jnpr.junos.transport.tty_serial.Serial._tty_close")
    def tearDown(self, mock_serial_close, mock_write, mock_read, mock_close, mock_sleep):
        # mock_read.side_effect = [('shell', 'shell'), ('login', 'login'),
        mock_read.side_effect = [("shell", "shell"), ("login", "login"), ("cli", "cli")]
        self.dev.close()

    def test_console_connected(self):
        self.assertTrue(self.dev.connected)

    def test_close_connection(self):
        self.dev._tty._ser = MagicMock()
        self.dev.close(skip_logout=True)
        self.assertTrue(self.dev._tty._ser.close.called)

    @patch("jnpr.junos.transport.tty_serial.serial.Serial.open")
    def test_tty_serial_open_exception(self, mock_open):
        dev = Console(port="USB/ttyUSB0", baud=9600, mode="Serial")
        mock_open.side_effect = OSError
        self.assertRaises(RuntimeError, dev.open)

    def test_tty_serial_rawwrite(self):
        self.dev._tty._ser = MagicMock()
        self.dev._tty.rawwrite("test")
        self.dev._tty._ser.write.assert_called_with("test")

    def test_tty_serial_read(self):
        self.dev._tty._ser = MagicMock()
        self.dev._tty.read()
        self.dev._tty._ser.readline.assert_is_called()

    def test_tty_serial_read_prompt(self):
        self.dev._tty._ser = MagicMock()
        self.dev._tty.EXPECT_TIMEOUT = 0.1
        self.dev._tty._ser.readline.side_effect = ["", "test"]
        self.assertEqual(self.dev._tty.read_prompt()[0], None)
Ejemplo n.º 6
0
class TestConsole(unittest.TestCase):
    @patch('jnpr.junos.transport.tty_telnet.Telnet._tty_open')
    @patch('jnpr.junos.transport.tty_telnet.telnetlib.Telnet.expect')
    @patch('jnpr.junos.transport.tty_telnet.Telnet.write')
    def setUp(self, mock_write, mock_expect, mock_open):
        tty_netconf.open = MagicMock()
        mock_expect.side_effect = [
            (1, re.search('(?P<login>ogin:\s*$)',
                          "login: "******"password: "******"junos % "), six.b('\r\r\nroot@device:~ # '))
        ]
        self.dev = Console(host='1.1.1.1',
                           user='******',
                           password='******',
                           mode='Telnet')
        self.dev.open()

    # @patch('jnpr.junos.transport.tty.tty_netconf.close')
    # @patch('jnpr.junos.transport.tty_telnet.telnetlib.Telnet.expect')
    # @patch('jnpr.junos.transport.tty_telnet.Telnet.write')
    # def tearDown(self, mock_write, mock_expect, mock_nc_close):
    #     mock_expect.side_effect = [(1, re.search('(?P<cli>[^\\-"]>\s*$)', "cli>"), six.b('\r\r\nroot@device>')),
    #                                (2,
    # re.search('(?P<shell>%|#\s*$)',
    #                                           "junos %"),
    # six.b('\r\r\nroot@device:~ # ')),
    #                                (3, re.search('(?P<login>ogin:\s*$)', "login: "******"login: "******"password: "******"login incorrect"), six.b('\r\r\nlogin incorrect'))
        ]
        self.dev = Console(host='1.1.1.1',
                           user='******',
                           password='******',
                           mode='Telnet')
        self.assertRaises(StopIteration, self.dev.open)

    @patch('jnpr.junos.console.Console._tty_logout')
    @patch('jnpr.junos.transport.tty_telnet.Telnet._tty_open')
    @patch('jnpr.junos.transport.tty_telnet.telnetlib.Telnet.expect')
    @patch('jnpr.junos.transport.tty_telnet.Telnet.write')
    def test_with_context(self, mock_write, mock_expect, mock_open,
                          mock_logout):
        tty_netconf.open = MagicMock()

        mock_expect.side_effect = [
            (1, re.search('(?P<login>ogin:\s*$)',
                          "login: "******"password: "******"junos % "), six.b('\r\r\nroot@device:~ # '))
        ]
        with Console(host='1.1.1.1',
                     user='******',
                     password='******',
                     mode='Telnet') as dev:
            self.assertTrue(isinstance(self.dev, Console))

    @patch('jnpr.junos.console.Console._tty_login')
    def test_console_open_error(self, mock_tty_login):
        mock_tty_login.side_effect = RuntimeError
        self.assertRaises(RuntimeError, self.dev.open)

    def test_console_connected(self):
        self.assertTrue(self.dev.connected)
        self.assertFalse(self.dev._gather_facts)

    @patch('jnpr.junos.console.Console._tty_logout')
    def test_console_close_error(self, mock_logout):
        mock_logout.side_effect = RuntimeError
        self.assertRaises(RuntimeError, self.dev.close)

    @patch('jnpr.junos.transport.tty_telnet.Telnet')
    @patch('jnpr.junos.console.Console._tty_login')
    def test_console_tty_open_err(self, mock_login, mock_telnet):
        with patch('jnpr.junos.transport.tty_telnet.telnetlib.Telnet.open'
                   ) as mock_open:
            mock_telnet.RETRY_OPEN = 1
            mock_login.side_effect = ValueError
            self.dev._tty.LOGIN_RETRY = self.dev._tty.RETRY_OPEN = 1
            self.assertRaises(ValueError, self.dev.open)

    @patch('jnpr.junos.transport.tty_serial.Serial._tty_open')
    @patch('jnpr.junos.transport.tty_serial.serial.Serial.readline')
    @patch('jnpr.junos.transport.tty_serial.Serial.write')
    def test_console_serial(self, mock_write, mock_expect, mock_open):
        tty_netconf.open = MagicMock()
        mock_expect.side_effect = [
            six.b('\r\r\n Login:'******'\r\r\n password:'******'\r\r\nroot@device:~ # ')
        ]
        self.dev = Console(host='1.1.1.1',
                           user='******',
                           password='******',
                           mode='serial')
        self.dev.open()
        self.assertTrue(self.dev.connected)
        self.assertFalse(self.dev._gather_facts)

    def test_wrong_mode(self):
        dev = Console(host='1.1.1.1',
                      user='******',
                      password='******',
                      mode='testing')
        self.assertRaises(AttributeError, dev.open)

    @patch('jnpr.junos.transport.tty_telnet.Telnet._tty_close')
    def test_console_close_error_skip_logout(self, mock_close):
        mock_close.side_effect = RuntimeError
        self.assertRaises(RuntimeError, self.dev.close, skip_logout=True)

    @patch('jnpr.junos.transport.tty_netconf.tty_netconf.rpc')
    def test_console_zeroize(self, mock_zeroize):
        self.dev.zeroize()
        self.assertTrue(mock_zeroize.called)

    @patch('jnpr.junos.transport.tty_netconf.tty_netconf.rpc')
    @patch('jnpr.junos.console.FACT_LIST')
    @patch('jnpr.junos.device.warnings')
    def test_console_gather_facts(self, mock_warnings, mock_fact_list,
                                  mock_rpc):
        self.dev._fact_style = 'old'
        from jnpr.junos.ofacts.session import facts_session
        mock_fact_list.__iter__.return_value = [facts_session]
        self.dev.facts_refresh()
        self.assertEqual(mock_rpc.call_count, 8)

    @patch('jnpr.junos.console.Console._tty_login')
    @patch('jnpr.junos.console.FACT_LIST')
    @patch('jnpr.junos.device.warnings')
    def test_console_gather_facts_true(self, mock_warnings, mock_fact_list,
                                       tty_login):
        self.dev._fact_style = 'old'
        self.dev.facts = self.dev.ofacts
        from jnpr.junos.ofacts.session import facts_session
        mock_fact_list.__iter__.return_value = [facts_session]
        self.dev._gather_facts = True
        self.dev.open()
        self.assertEqual(
            self.dev.facts, {
                '2RE': False,
                'RE_hw_mi': False,
                'ifd_style': 'CLASSIC',
                'serialnumber': 'UNKNOWN',
                'model': 'UNKNOWN',
                'vc_capable': False,
                'switch_style': 'NONE',
                'personality': 'UNKNOWN'
            })

    @patch('ncclient.operations.rpc.RPCReply.parse')
    @patch('jnpr.junos.transport.tty_telnet.telnetlib.Telnet.write')
    @patch('jnpr.junos.transport.tty_netconf.select.select')
    @patch('jnpr.junos.transport.tty_telnet.telnetlib.Telnet.read_until')
    def test_load_console(self, mock_read_until, mock_select, mock_write,
                          mock_parse):
        mock_select.return_value = ([self.dev._tty._rx], [], [])
        xml = """<policy-options>
                  <policy-statement>
                    <name>F5-in</name>
                    <term>
                        <name>testing</name>
                        <then>
                            <accept/>
                        </then>
                    </term>
                    <from>
                        <protocol>mpls</protocol>
                    </from>
                </policy-statement>
                </policy-options>"""

        mock_read_until.return_value = six.b("""
        <rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:junos="http://xml.juniper.net/junos/15.2I0/junos">
            <load-configuration-results>
            <ok/>
            </load-configuration-results>
            </rpc-reply>
            ]]>]]>""")
        cu = Config(self.dev)
        op = cu.load(xml, format='xml')
        cu.commit()

    @patch('ncclient.operations.rpc.RPCReply.parse')
    @patch('jnpr.junos.transport.tty_netconf.tty_netconf._receive')
    @patch('jnpr.junos.transport.tty_telnet.Telnet.rawwrite')
    def test_console_rpc_call(self, mock_write, mock_rcv, mock_parse):
        self.dev._tty.nc.rpc = MagicMock(side_effect=self._mock_manager)
        op = self.dev.rpc.get_chassis_inventory()
        self.assertEqual(op.tag, 'chassis-inventory')

    @patch('ncclient.operations.rpc.RPCReply.parse')
    @patch('jnpr.junos.transport.tty_netconf.tty_netconf._receive')
    @patch('jnpr.junos.transport.tty_telnet.Telnet.rawwrite')
    def test_console_rpc_call_exception(self, mock_write, mock_rcv,
                                        mock_parse):
        mock_rcv.return_value = '<output>testing</output>'
        op = self.dev.rpc.get_chassis_inventory()
        self.assertEqual(op.tag, 'output')

    def test_timeout_getter_setter(self):
        self.dev.timeout = 1
        self.assertEqual(1, self.dev.timeout)

    # below 2 function will be used in future.
    def _mock_manager(self, *args, **kwargs):
        if args:
            return self._read_file(etree.XML(args[0]).tag + '.xml')

    def _read_file(self, fname):
        from ncclient.xml_ import NCElement

        fpath = os.path.join(os.path.dirname(__file__), 'rpc-reply', fname)
        with open(fpath) as fp:
            return fp.read()
Ejemplo n.º 7
0
class TestConsole(unittest.TestCase):

    @patch('jnpr.junos.transport.tty_telnet.Telnet._tty_open')
    @patch('jnpr.junos.transport.tty_telnet.telnetlib.Telnet.expect')
    @patch('jnpr.junos.transport.tty_telnet.Telnet.write')
    def setUp(self, mock_write, mock_expect, mock_open):
        tty_netconf.open = MagicMock()
        mock_expect.side_effect = [(1, re.search('(?P<login>ogin:\s*$)', "login: "******"password: "******"junos % "), six.b('\r\r\nroot@device:~ # '))]
        self.dev = Console(
            host='1.1.1.1',
            user='******',
            password='******',
            mode='Telnet')
        self.dev.open()

    # @patch('jnpr.junos.transport.tty.tty_netconf.close')
    # @patch('jnpr.junos.transport.tty_telnet.telnetlib.Telnet.expect')
    # @patch('jnpr.junos.transport.tty_telnet.Telnet.write')
    # def tearDown(self, mock_write, mock_expect, mock_nc_close):
    #     mock_expect.side_effect = [(1, re.search('(?P<cli>[^\\-"]>\s*$)', "cli>"), six.b('\r\r\nroot@device>')),
    #                                (2,
    # re.search('(?P<shell>%|#\s*$)',
    #                                           "junos %"),
    # six.b('\r\r\nroot@device:~ # ')),
    #                                (3, re.search('(?P<login>ogin:\s*$)', "login: "******"login: "******"password: "******"login incorrect"),
                                    six.b('\r\r\nlogin incorrect'))]
        self.dev = Console(
            host='1.1.1.1',
            user='******',
            password='******',
            mode='Telnet')
        self.assertRaises(StopIteration, self.dev.open)

    @patch('jnpr.junos.console.Console._tty_logout')
    @patch('jnpr.junos.transport.tty_telnet.Telnet._tty_open')
    @patch('jnpr.junos.transport.tty_telnet.telnetlib.Telnet.expect')
    @patch('jnpr.junos.transport.tty_telnet.Telnet.write')
    def test_with_context(
            self, mock_write, mock_expect, mock_open, mock_logout):
        tty_netconf.open = MagicMock()

        mock_expect.side_effect = [(1, re.search('(?P<login>ogin:\s*$)', "login: "******"password: "******"junos % "),
                                    six.b('\r\r\nroot@device:~ # '))]
        with Console(host='1.1.1.1', user='******', password='******', mode='Telnet') as dev:
            self.assertTrue(isinstance(self.dev, Console))

    @patch('jnpr.junos.console.Console._tty_login')
    def test_console_open_error(self, mock_tty_login):
        mock_tty_login.side_effect = RuntimeError
        self.assertRaises(RuntimeError, self.dev.open)

    def test_console_connected(self):
        self.assertTrue(self.dev.connected)
        self.assertFalse(self.dev._gather_facts)

    @patch('jnpr.junos.console.Console._tty_logout')
    def test_console_close_error(self, mock_logout):
        mock_logout.side_effect = RuntimeError
        self.assertRaises(RuntimeError, self.dev.close)

    @patch('jnpr.junos.transport.tty_telnet.Telnet')
    @patch('jnpr.junos.console.Console._tty_login')
    def test_console_tty_open_err(self, mock_login, mock_telnet):
        with patch('jnpr.junos.transport.tty_telnet.telnetlib.Telnet.open') as mock_open:
            mock_telnet.RETRY_OPEN = 1
            mock_login.side_effect = ValueError
            self.dev._tty.LOGIN_RETRY = self.dev._tty.RETRY_OPEN = 1
            self.assertRaises(ValueError, self.dev.open)

    @patch('jnpr.junos.transport.tty_serial.Serial._tty_open')
    @patch('jnpr.junos.transport.tty_serial.serial.Serial.readline')
    @patch('jnpr.junos.transport.tty_serial.Serial.write')
    def test_console_serial(self, mock_write, mock_expect, mock_open):
        tty_netconf.open = MagicMock()
        mock_expect.side_effect = [six.b('\r\r\n Login:'******'\r\r\n password:'******'\r\r\nroot@device:~ # ')]
        self.dev = Console(
            host='1.1.1.1',
            user='******',
            password='******',
            mode='serial')
        self.dev.open()
        self.assertTrue(self.dev.connected)
        self.assertFalse(self.dev._gather_facts)

    def test_wrong_mode(self):
        dev = Console(host='1.1.1.1', user='******', password='******',
                      mode='testing')
        self.assertRaises(AttributeError, dev.open)

    @patch('jnpr.junos.transport.tty_telnet.Telnet._tty_close')
    def test_console_close_error_skip_logout(self, mock_close):
        mock_close.side_effect = RuntimeError
        self.assertRaises(RuntimeError, self.dev.close, skip_logout=True)

    @patch('jnpr.junos.transport.tty_netconf.tty_netconf.rpc')
    def test_console_zeroize(self, mock_zeroize):
        self.dev.zeroize()
        self.assertTrue(mock_zeroize.called)

    @patch('jnpr.junos.transport.tty_netconf.tty_netconf.rpc')
    @patch('jnpr.junos.console.FACT_LIST')
    @patch('jnpr.junos.device.warnings')
    def test_console_gather_facts(self, mock_warnings, mock_fact_list,
                                  mock_rpc):
        self.dev._fact_style = 'old'
        from jnpr.junos.ofacts.session import facts_session
        mock_fact_list.__iter__.return_value = [facts_session]
        self.dev.facts_refresh()
        self.assertEqual(mock_rpc.call_count, 8)

    @patch('jnpr.junos.console.Console._tty_login')
    @patch('jnpr.junos.console.FACT_LIST')
    @patch('jnpr.junos.device.warnings')
    def test_console_gather_facts_true(self, mock_warnings, mock_fact_list,
                                       tty_login):
        self.dev._fact_style = 'old'
        self.dev.facts = self.dev.ofacts
        from jnpr.junos.ofacts.session import facts_session
        mock_fact_list.__iter__.return_value = [facts_session]
        self.dev._gather_facts = True
        self.dev.open()
        self.assertEqual(self.dev.facts, {'2RE': False,
                                          'RE_hw_mi': False,
                                          'ifd_style': 'CLASSIC',
                                          'serialnumber': 'UNKNOWN',
                                          'model': 'UNKNOWN',
                                          'vc_capable': False,
                                          'switch_style': 'NONE',
                                          'personality': 'UNKNOWN'})

    @patch('ncclient.operations.rpc.RPCReply.parse')
    @patch('jnpr.junos.transport.tty_telnet.telnetlib.Telnet.write')
    @patch('jnpr.junos.transport.tty_netconf.select.select')
    @patch('jnpr.junos.transport.tty_telnet.telnetlib.Telnet.read_until')
    def test_load_console(
            self, mock_read_until, mock_select, mock_write, mock_parse):
        mock_select.return_value = ([self.dev._tty._rx], [], [])
        xml = """<policy-options>
                  <policy-statement>
                    <name>F5-in</name>
                    <term>
                        <name>testing</name>
                        <then>
                            <accept/>
                        </then>
                    </term>
                    <from>
                        <protocol>mpls</protocol>
                    </from>
                </policy-statement>
                </policy-options>"""

        mock_read_until.return_value = six.b("""
        <rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:junos="http://xml.juniper.net/junos/15.2I0/junos">
            <load-configuration-results>
            <ok/>
            </load-configuration-results>
            </rpc-reply>
            ]]>]]>""")
        cu = Config(self.dev)
        op = cu.load(xml, format='xml')
        cu.commit()

    @patch('ncclient.operations.rpc.RPCReply.parse')
    @patch('jnpr.junos.transport.tty_netconf.tty_netconf._receive')
    @patch('jnpr.junos.transport.tty_telnet.Telnet.rawwrite')
    def test_console_rpc_call(self, mock_write, mock_rcv, mock_parse):
        self.dev._tty.nc.rpc = MagicMock(side_effect=self._mock_manager)
        op = self.dev.rpc.get_chassis_inventory()
        self.assertEqual(op.tag, 'chassis-inventory')

    @patch('ncclient.operations.rpc.RPCReply.parse')
    @patch('jnpr.junos.transport.tty_netconf.tty_netconf._receive')
    @patch('jnpr.junos.transport.tty_telnet.Telnet.rawwrite')
    def test_console_rpc_call_exception(
            self, mock_write, mock_rcv, mock_parse):
        mock_rcv.return_value = '<output>testing</output>'
        op = self.dev.rpc.get_chassis_inventory()
        self.assertEqual(op.tag, 'output')

    def test_timeout_getter_setter(self):
        self.dev.timeout = 1
        self.assertEqual(1, self.dev.timeout)

    # below 2 function will be used in future.
    def _mock_manager(self, *args, **kwargs):
        if args:
            return self._read_file(etree.XML(args[0]).tag + '.xml')

    def _read_file(self, fname):
        from ncclient.xml_ import NCElement

        fpath = os.path.join(os.path.dirname(__file__),
                             'rpc-reply', fname)
        with open(fpath) as fp:
            return fp.read()
Ejemplo n.º 8
0
class TestSerialWin(unittest.TestCase):
    @patch("jnpr.junos.transport.tty_serial.serial.Serial.open")
    @patch("jnpr.junos.transport.tty_serial.serial.Serial.read")
    @patch("jnpr.junos.transport.tty_serial.serial.Serial.write")
    @patch("jnpr.junos.transport.tty_serial.serial.Serial.flush")
    @patch("jnpr.junos.transport.tty_serial.Serial.read_prompt")
    def setUp(self, mock_read, mock_flush, mock_write, mock_serial_read,
              mock_open):
        self.dev = Console(port="COM4", baud=9600, mode="Serial")
        mock_read.side_effect = [
            ("login", "login"),
            ("passwd", "passwd"),
            ("shell", "shell"),
        ]
        mock_serial_read.side_effect = [
            six.
            b("<!-- No zombies were killed during the creation of this user interface -->"
              ),
            six.b(""),
            six.b("""<!-- user root, class super-user -->
<hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
  <capabilities>
    <capability>urn:ietf:params:netconf:base:1.0</capability>
    <capability>urn:ietf:params:netconf:capability:candidate:1.0</capability>
    <capability>urn:ietf:params:netconf:capability:confirmed-commit:1.0</capability>
    <capability>urn:ietf:params:netconf:capability:validate:1.0</capability>
    <capability>urn:ietf:params:netconf:capability:url:1.0?scheme=http,ftp,file</capability>
    <capability>urn:ietf:params:xml:ns:netconf:base:1.0</capability>
    <capability>urn:ietf:params:xml:ns:netconf:capability:candidate:1.0</capability>
    <capability>urn:ietf:params:xml:ns:netconf:capability:confirmed-commit:1.0</capability>
    <capability>urn:ietf:params:xml:ns:netconf:capability:validate:1.0</capability>
    <capability>urn:ietf:params:xml:ns:netconf:capability:url:1.0?scheme=http,ftp,file</capability>
    <capability>urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring</capability>
    <capability>http://xml.juniper.net/netconf/junos/1.0</capability>
    <capability>http://xml.juniper.net/dmi/system/1.0</capability>
  </capabilities>
  <session-id>7478</session-id>
</hello>
]]>]]>"""),
            six.b(""),
        ]
        self.dev.open()

    @patch("jnpr.junos.transport.tty.sleep")
    @patch("jnpr.junos.transport.tty.tty_netconf.close")
    @patch("jnpr.junos.transport.tty_serial.Serial.read_prompt")
    @patch("jnpr.junos.transport.tty_serial.Serial.write")
    @patch("jnpr.junos.transport.tty_serial.Serial._tty_close")
    def tearDown(self, mock_serial_close, mock_write, mock_read, mock_close,
                 mock_sleep):
        mock_read.side_effect = [
            ("shell", "shell"),
            ("login", "login"),
            ("cli", "cli"),
        ]
        self.dev.close()

    def test_tty_serial_win_connected(self):
        self.assertTrue(self.dev.connected)

    @patch("jnpr.junos.transport.tty.tty_netconf.close")
    @patch("jnpr.junos.transport.tty_serial.Serial._tty_close")
    def test_tty_serial_win_rpc_call(self, mock_serial_close, mock_close):
        self.dev._tty.read = MagicMock()
        self.dev._tty.rawwrite = MagicMock()
        self.dev._tty.read.side_effect = [
            six.b('<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"'
                  ' xmlns:junos="http://xml.juniper.net/junos/15.1X49/junos">'
                  '<route-engine-information xmlns="http://xml.juniper.net/ju'
                  'nos/15.1X49/junos-chassis"><route-engine><status>OK</statu'
                  's><temperature junos:celsius="45">45 degrees C / 113 degre'
                  'es F</temperature><cpu-temperature junos:celsius="61">61 d'
                  "egrees C / 141 degrees F</cpu-temperature><memory-system-t"
                  "otal>4096</memory-system-total><memory-system-total-used>1"
                  "024</memory-system-total-used><memory-system-total-util>25"
                  "</memory-system-total-util><memory-control-plane>2624</mem"
                  "ory-control-plane><memory-control-plane-used>682</memory-c"
                  "ontrol-plane-used><memory-control-plane-util>26</memory-co"
                  "ntrol-plane-util><memory-data-plane>1472</memory-data-plan"
                  "e><memory-data-plane-used>353</memory-data-plane-used><mem"
                  "ory-data-plane-util>24</memory-data-plane-util><cpu-user>1"
                  "2</cpu-user><cpu-background>0</cpu-background><cpu-system>"
                  "6</cpu-system><cpu-interrupt>0</cpu-interrupt><cpu-idle>83"
                  "</cpu-idle><model>RE-SRX300</model><serial-number>CV0918AF"
                  '1022</serial-number><start-time junos:seconds="1584539305"'
                  ">2020-03-18 08:48:25 CDT</start-time><up-time junos:second"
                  's="137925">1 day, 14 hours, 18 minutes, 45 seconds</up-tim'
                  "e><last-reboot-reason>0x1:power cycle/failure</last-reboot"
                  "-reason><load-average-one>0.12</load-average-one><load-ave"
                  "rage-five>0.08</load-average-five><load-average-fifteen>0."
                  "06</load-average-fifteen></route-engine></route-engine-inf"
                  "ormation></rpc-reply>]]>]]>")
        ]
        res = self.dev.rpc.get_route_engine_information()
        self.assertEqual(res.tag, "route-engine-information")
Ejemplo n.º 9
0
class TestConsole(unittest.TestCase):

    @patch('jnpr.junos.transport.tty_telnet.Telnet._tty_open')
    @patch('jnpr.junos.transport.tty_telnet.telnetlib.Telnet.expect')
    @patch('jnpr.junos.transport.tty_telnet.Telnet.write')
    def setUp(self, mock_write, mock_expect, mock_open):
        tty_netconf.open = MagicMock()
        mock_expect.side_effect=[(1, re.search('(?P<login>ogin:\s*$)', "login: "******"password: "******"junos % "), six.b('\r\r\nroot@device:~ # '))]
        self.dev = Console(host='1.1.1.1', user='******', password='******', mode='Telnet')
        self.dev.open()

    @patch('jnpr.junos.transport.tty.tty_netconf.close')
    @patch('jnpr.junos.transport.tty_telnet.telnetlib.Telnet.expect')
    @patch('jnpr.junos.transport.tty_telnet.Telnet.write')
    def tearDown(self, mock_write, mock_expect, mock_nc_close):
        mock_expect.side_effect = [(1, re.search('(?P<cli>[^\\-"]>\s*$)', "cli>"), six.b('\r\r\nroot@device>')),
                                   (2, re.search('(?P<shell>%|#\s*$)', "junos %"), six.b('\r\r\nroot@device:~ # ')),
                                   (3, re.search('(?P<login>ogin:\s*$)', "login: "******"""<policy-options>
                  <policy-statement>
                    <name>F5-in</name>
                    <term>
                        <name>testing</name>
                        <then>
                            <accept/>
                        </then>
                    </term>
                    <from>
                        <protocol>mpls</protocol>
                    </from>
                </policy-statement>
                </policy-options>"""

        mock_read_until.return_value = six.b("""
        <rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:junos="http://xml.juniper.net/junos/15.2I0/junos">
            <load-configuration-results>
            <ok/>
            </load-configuration-results>
            </rpc-reply>
            ]]>]]>""")
        cu = Config(self.dev)
        cu.load(xml, format='xml')
        cu.commit()


    @patch('jnpr.junos.transport.tty_netconf.tty_netconf._receive')
    @patch('jnpr.junos.transport.tty_telnet.Telnet.rawwrite')
    def test_console_rpc_call(self, mock_write, mock_rcv):
        mock_rcv.side_effect = self._mock_manager
        self.dev.rpc.get_chassis_inventory()
        self.assertTrue(mock_rcv.called)

    @patch('jnpr.junos.transport.tty_netconf.remove_namespaces')
    @patch('jnpr.junos.transport.tty_netconf.tty_netconf._receive')
    @patch('jnpr.junos.transport.tty_telnet.Telnet.rawwrite')
    def test_console_rpc_call_exception(self, mock_write, mock_rcv, mock_ns):
        mock_rcv.return_value = etree.fromstring('<output>testing</output>')
        mock_ns.side_effect = IndexError('testing')
        op = self.dev.rpc.get_chassis_inventory()
        self.assertEqual(op.tag, 'output')

    # below 2 function will be used in future.
    def _mock_manager(self, *args, **kwargs):
        if args:
            return self._read_file(args[0].tag + '.xml')

    def _read_file(self, fname):
        from ncclient.xml_ import NCElement

        fpath = os.path.join(os.path.dirname(__file__),
                             'rpc-reply', fname)
        with open(fpath) as fp:
            foo = fp.read()
        if fname == 'get-system-users-information.xml':
            return NCElement(foo,
                             self.dev._conn._device_handler.transform_reply())
        rpc_reply = NCElement(foo, self.dev._conn.
                              _device_handler.transform_reply()) \
            ._NCElement__doc[0]
        return rpc_reply
Ejemplo n.º 10
0
class TestConsole(unittest.TestCase):

    @patch('jnpr.junos.transport.tty_telnet.Telnet._tty_open')
    @patch('jnpr.junos.transport.tty_telnet.telnetlib.Telnet.expect')
    @patch('jnpr.junos.transport.tty_telnet.Telnet.write')
    def setUp(self, mock_write, mock_expect, mock_open):
        tty_netconf.open = MagicMock()
        mock_expect.side_effect=[(1, re.search('(?P<login>ogin:\s*$)', "login: "******"password: "******"junos % "), '\r\r\nroot@device:~ # ')]
        self.dev = Console(host='1.1.1.1', user='******', password='******', mode = 'Telnet')
        self.dev.open()

    @patch('jnpr.junos.transport.tty.tty_netconf.close')
    @patch('jnpr.junos.transport.tty_telnet.telnetlib.Telnet.expect')
    @patch('jnpr.junos.transport.tty_telnet.Telnet.write')
    def tearDown(self, mock_write, mock_expect, mock_nc_close):
        mock_expect.side_effect = [(1, re.search('(?P<cli>[^\\-"]>\s*$)', "cli>"), '\r\r\nroot@device>'),
                                   (2, re.search('(?P<shell>%|#\s*$)', "junos %"), '\r\r\nroot@device:~ # '),
                                   (3, re.search('(?P<login>ogin:\s*$)', "login: "******"""<policy-options>
                  <policy-statement>
                    <name>F5-in</name>
                    <term>
                        <name>testing</name>
                        <then>
                            <accept/>
                        </then>
                    </term>
                    <from>
                        <protocol>mpls</protocol>
                    </from>
                </policy-statement>
                </policy-options>"""

        mock_read_until.return_value = """
        <rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:junos="http://xml.juniper.net/junos/15.2I0/junos">
            <load-configuration-results>
            <ok/>
            </load-configuration-results>
            </rpc-reply>
            ]]>]]>"""
        cu = Config(self.dev)
        cu.load(xml, format='xml')
        cu.commit()
Ejemplo n.º 11
0
class TestConsole(unittest.TestCase):
    @patch("jnpr.junos.transport.tty_telnet.Telnet._tty_open")
    @patch("jnpr.junos.transport.tty_telnet.telnetlib.Telnet.expect")
    @patch("jnpr.junos.transport.tty_telnet.Telnet.write")
    def setUp(self, mock_write, mock_expect, mock_open):
        tty_netconf.open = MagicMock()
        mock_expect.side_effect = [
            (1, re.search("(?P<login>ogin:\s*$)", "login: "******"\r\r\n ogin:")),
            (
                2,
                re.search("(?P<passwd>assword:\s*$)", "password: "******"\r\r\n password:"******"(?P<shell>%|#\s*$)", "junos % "),
                six.b("\r\r\nroot@device:~ # "),
            ),
        ]
        self.dev = Console(host="1.1.1.1", user="******", password="******", mode="Telnet")
        self.dev.open()

    @patch("jnpr.junos.console.Console._tty_logout")
    def tearDown(self, mock_tty_logout):
        self.dev.close()

    def test_telnet_host_none(self):
        self.dev = Console(host=None, user="******", password="******", mode="Telnet")
        self.assertTrue(self.dev.open()["failed"])

    @patch("jnpr.junos.console.warnings")
    def test_telnet_old_fact_warning(self, mock_warn):
        self.dev = Console(
            host="1.1.1.1",
            user="******",
            password="******",
            mode="Telnet",
            fact_style="old",
        )
        mock_warn.assert_has_calls(
            [
                call.warn(
                    "fact-style old will be removed in a future release.",
                    RuntimeWarning,
                )
            ]
        )

    @patch("jnpr.junos.transport.tty_telnet.Telnet._tty_open")
    @patch("jnpr.junos.transport.tty_telnet.telnetlib.Telnet.expect")
    @patch("jnpr.junos.transport.tty_telnet.Telnet.write")
    def test_login_bad_password(self, mock_write, mock_expect, mock_open):
        tty_netconf.open = MagicMock()
        mock_expect.side_effect = [
            (1, re.search("(?P<login>ogin:\s*$)", "login: "******"\r\r\n ogin:")),
            (
                2,
                re.search("(?P<passwd>assword:\s*$)", "password: "******"\r\r\n password:"******"(?P<badpasswd>ogin incorrect)", "login incorrect"),
                six.b("\r\r\nlogin incorrect"),
            ),
        ]
        self.dev = Console(host="1.1.1.1", user="******", password="******", mode="Telnet")
        self.assertRaises(StopIteration, self.dev.open)

    @patch("jnpr.junos.console.Console._tty_logout")
    @patch("jnpr.junos.transport.tty_telnet.Telnet._tty_open")
    @patch("jnpr.junos.transport.tty_telnet.telnetlib.Telnet.expect")
    @patch("jnpr.junos.transport.tty_telnet.Telnet.write")
    def test_with_context(self, mock_write, mock_expect, mock_open, mock_logout):
        tty_netconf.open = MagicMock()

        mock_expect.side_effect = [
            (1, re.search("(?P<login>ogin:\s*$)", "login: "******"\r\r\n ogin:")),
            (
                2,
                re.search("(?P<passwd>assword:\s*$)", "password: "******"\r\r\n password:"******"(?P<shell>%|#\s*$)", "junos % "),
                six.b("\r\r\nroot@device:~ # "),
            ),
        ]
        with Console(
            host="1.1.1.1", user="******", password="******", mode="Telnet"
        ) as dev:
            self.assertTrue(isinstance(self.dev, Console))

    @patch("jnpr.junos.console.Console._tty_login")
    def test_console_open_error(self, mock_tty_login):
        mock_tty_login.side_effect = RuntimeError
        self.assertRaises(RuntimeError, self.dev.open)

    def test_console_connected(self):
        self.assertTrue(self.dev.connected)
        self.assertFalse(self.dev._gather_facts)

    @patch("jnpr.junos.console.Console._tty_logout")
    def test_console_close_error(self, mock_logout):
        mock_logout.side_effect = RuntimeError
        self.assertRaises(RuntimeError, self.dev.close)

    @patch("jnpr.junos.console.Console._tty_logout")
    def test_console_close_socket_error(self, mock_logout):
        mock_logout.side_effect = socket.error
        self.assertRaises(socket.error, self.dev.close)

    @patch("jnpr.junos.console.Console._tty_logout")
    def test_console_close_socket_conn_reset(self, mock_logout):
        mock_logout.side_effect = socket.error("Connection reset by peer")
        self.dev.close()
        self.assertFalse(self.dev.connected)

    @patch("jnpr.junos.console.Console._tty_logout")
    def test_console_close_telnet_conn_closed(self, mock_logout):
        mock_logout.side_effect = EOFError("telnet connection closed")
        self.dev.close()
        self.assertFalse(self.dev.connected)

    @patch("jnpr.junos.transport.tty_telnet.Telnet")
    @patch("jnpr.junos.console.Console._tty_login")
    def test_console_tty_open_err(self, mock_login, mock_telnet):
        with patch(
            "jnpr.junos.transport.tty_telnet." "telnetlib.Telnet.open"
        ) as mock_open:
            mock_telnet.RETRY_OPEN = 1
            mock_login.side_effect = ValueError
            self.dev._tty.LOGIN_RETRY = self.dev._tty.RETRY_OPEN = 1
            self.assertRaises(ValueError, self.dev.open)

    @patch("jnpr.junos.transport.tty_serial.Serial._tty_open")
    @patch("jnpr.junos.transport.tty_serial.serial.Serial.readline")
    @patch("jnpr.junos.transport.tty_serial.Serial.write")
    def test_console_serial(self, mock_write, mock_expect, mock_open):
        tty_netconf.open = MagicMock()
        mock_expect.side_effect = [
            six.b("\r\r\n Login:"******"\r\r\n password:"******"\r\r\nroot@device:~ # "),
        ]
        self.dev = Console(host="1.1.1.1", user="******", password="******", mode="serial")
        self.dev.open()
        self.assertTrue(self.dev.connected)
        self.assertFalse(self.dev._gather_facts)

    def test_wrong_mode(self):
        dev = Console(host="1.1.1.1", user="******", password="******", mode="testing")
        self.assertRaises(AttributeError, dev.open)

    @patch("jnpr.junos.transport.tty_telnet.Telnet._tty_close")
    def test_console_close_error_skip_logout(self, mock_close):
        mock_close.side_effect = RuntimeError
        self.assertRaises(RuntimeError, self.dev.close, skip_logout=True)

    @patch("jnpr.junos.transport.tty_netconf.tty_netconf.rpc")
    def test_console_zeroize(self, mock_zeroize):
        self.dev.zeroize()
        self.assertTrue(mock_zeroize.called)

    @patch("jnpr.junos.transport.tty_netconf.tty_netconf.rpc")
    @patch("jnpr.junos.console.FACT_LIST")
    @patch("jnpr.junos.device.warnings")
    def test_console_gather_facts(self, mock_warnings, mock_fact_list, mock_rpc):
        self.dev._fact_style = "old"
        from jnpr.junos.ofacts.session import facts_session

        mock_fact_list.__iter__.return_value = [facts_session]
        self.dev.facts_refresh()
        self.assertEqual(mock_rpc.call_count, 8)

    @patch("jnpr.junos.console.Console._tty_login")
    @patch("jnpr.junos.console.FACT_LIST")
    @patch("jnpr.junos.device.warnings")
    def test_console_gather_facts_true(self, mock_warnings, mock_fact_list, tty_login):
        self.dev._fact_style = "old"
        self.dev.facts = self.dev.ofacts
        from jnpr.junos.ofacts.session import facts_session

        mock_fact_list.__iter__.return_value = [facts_session]
        self.dev._gather_facts = True
        self.dev.open()
        self.assertEqual(
            self.dev.facts,
            {
                "2RE": False,
                "RE_hw_mi": False,
                "ifd_style": "CLASSIC",
                "serialnumber": "UNKNOWN",
                "model": "UNKNOWN",
                "vc_capable": False,
                "switch_style": "NONE",
                "personality": "UNKNOWN",
            },
        )

    @patch("jnpr.junos.transport.tty.sleep")
    @patch("ncclient.operations.rpc.RPCReply.parse")
    @patch("jnpr.junos.transport.tty_telnet.telnetlib.Telnet.write")
    @patch("jnpr.junos.transport.tty_netconf.select.select")
    @patch("jnpr.junos.transport.tty_telnet.telnetlib.Telnet.read_until")
    def test_load_console(
        self, mock_read_until, mock_select, mock_write, mock_parse, mock_sleep
    ):
        mock_select.return_value = ([self.dev._tty._rx], [], [])
        xml = """<policy-options>
                  <policy-statement>
                    <name>F5-in</name>
                    <term>
                        <name>testing</name>
                        <then>
                            <accept/>
                        </then>
                    </term>
                    <from>
                        <protocol>mpls</protocol>
                    </from>
                </policy-statement>
                </policy-options>"""

        mock_read_until.return_value = six.b(
            """
        <rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:junos="http://xml.juniper.net/junos/15.2I0/junos">
            <load-configuration-results>
            <ok/>
            </load-configuration-results>
            </rpc-reply>
            ]]>]]>"""
        )
        cu = Config(self.dev)
        op = cu.load(xml, format="xml")
        cu.commit()

    @patch("ncclient.operations.rpc.RPCReply.parse")
    @patch("jnpr.junos.transport.tty_netconf.tty_netconf._receive")
    @patch("jnpr.junos.transport.tty_telnet.Telnet.rawwrite")
    def test_console_rpc_call(self, mock_write, mock_rcv, mock_parse):
        self.dev._tty.nc.rpc = MagicMock(side_effect=self._mock_manager)
        op = self.dev.rpc.get_chassis_inventory()
        self.assertEqual(op.tag, "chassis-inventory")

    @patch("ncclient.operations.rpc.RPCReply.parse")
    @patch("jnpr.junos.transport.tty_netconf.tty_netconf._receive")
    @patch("jnpr.junos.transport.tty_telnet.Telnet.rawwrite")
    def test_console_rpc_call_exception(self, mock_write, mock_rcv, mock_parse):
        mock_rcv.return_value = "<output>testing</output>"
        op = self.dev.rpc.get_chassis_inventory()
        self.assertEqual(op.tag, "output")

    def test_timeout_getter_setter(self):
        self.dev.timeout = 1
        self.assertEqual(1, self.dev.timeout)

    # below 2 function will be used in future.
    def _mock_manager(self, *args, **kwargs):
        if args:
            return self._read_file(etree.XML(args[0]).tag + ".xml")

    def _read_file(self, fname):
        from ncclient.xml_ import NCElement

        fpath = os.path.join(os.path.dirname(__file__), "rpc-reply", fname)
        with open(fpath) as fp:
            return fp.read()