Example #1
0
 def test_tty_telnet_baud(self, mock_telnet):
     tel_conn = Telnet(host='1.1.1.1', user='******',
                       password='******', port=23,
                       timeout=30, baud=0)
     tel_conn._tty_open()
     tel_conn.rawwrite('<rpc>')
     tel_conn._tn.write.assert_called_with('<rpc>')
Example #2
0
class TestTTYTelnet(unittest.TestCase):
    @patch('jnpr.junos.transport.tty_telnet.telnetlib.Telnet')
    def setUp(self, mpock_telnet):
        self.tel_conn = Telnet(host='1.1.1.1',
                               user='******',
                               password='******',
                               port=23,
                               timeout=30)

    def test_open(self):
        self.tel_conn._tty_open()
        self.tel_conn._tn.open.assert_called()

    def test_open_exception(self):
        self.tel_conn._tn.open.side_effect = Exception
        Telnet.RETRY_OPEN = 1
        Telnet.RETRY_BACKOFF = 0.1
        self.assertRaises(RuntimeError, self.tel_conn._tty_open)
        # reset
        Telnet.RETRY_OPEN = 3
        Telnet.RETRY_BACKOFF = 2

    def test_close(self):
        self.tel_conn._tty_close()
        self.tel_conn._tn.close.assert_called()

    def test_read(self):
        self.tel_conn.read()
        self.tel_conn._tn.read_until.assert_called()

    @patch('jnpr.junos.transport.tty_telnet.telnetlib.Telnet')
    def test_tty_telnet_baud(self, mock_telnet):
        tel_conn = Telnet(host='1.1.1.1',
                          user='******',
                          password='******',
                          port=23,
                          timeout=30,
                          baud=0)
        tel_conn._tty_open()
        tel_conn.rawwrite('<rpc>')
        tel_conn._tn.write.assert_called_with('<rpc>')

    def test_read_prompt_RuntimeError(self):
        self.tel_conn.expect = MagicMock()
        self.tel_conn.expect = (None, None, 'port already in use')
        self.assertRaises(RuntimeError, self.tel_conn._login_state_machine)

    def test_read_prompt_in_use_RuntimeError(self):
        self.tel_conn.expect = MagicMock()
        self.tel_conn._tn.expect.return_value = (None, None,
                                                 six.b('port already in use'))
        self.assertRaises(RuntimeError, self.tel_conn._login_state_machine)

    def test_tty_telnet_rawwrite_sys_py3(self):
        with patch.object(sys.modules['sys'], 'version', '3.x') \
                as mock_sys:
            self.tel_conn._tty_open()
            content = MagicMock()
            self.tel_conn.rawwrite(content)
            content.decode.assert_called_with('utf-8')
Example #3
0
class TestTTYTelnet(unittest.TestCase):

    @patch('jnpr.junos.transport.tty_telnet.telnetlib.Telnet')
    def setUp(self, mpock_telnet):
        self.tel_conn = Telnet(host='1.1.1.1', user='******',
                               password='******', port=23,
                               timeout=30)

    def test_open(self):
        self.tel_conn._tty_open()
        self.tel_conn._tn.open.assert_called()

    def test_open_exception(self):
        self.tel_conn._tn.open.side_effect = Exception
        Telnet.RETRY_OPEN = 1
        Telnet.RETRY_BACKOFF = 0.1
        self.assertRaises(RuntimeError, self.tel_conn._tty_open)
        # reset
        Telnet.RETRY_OPEN = 3
        Telnet.RETRY_BACKOFF = 2

    def test_close(self):
        self.tel_conn._tty_close()
        self.tel_conn._tn.close.assert_called()

    def test_read(self):
        self.tel_conn.read()
        self.tel_conn._tn.read_until.assert_called()

    @patch('jnpr.junos.transport.tty_telnet.telnetlib.Telnet')
    def test_tty_telnet_baud(self, mock_telnet):
        tel_conn = Telnet(host='1.1.1.1', user='******',
                          password='******', port=23,
                          timeout=30, baud=0)
        tel_conn._tty_open()
        tel_conn.rawwrite('<rpc>')
        tel_conn._tn.write.assert_called_with('<rpc>')

    def test_read_prompt_RuntimeError(self):
        self.tel_conn.expect = MagicMock()
        self.tel_conn.expect = (None, None, 'port already in use')
        self.assertRaises(RuntimeError, self.tel_conn._login_state_machine)

    def test_read_prompt_in_use_RuntimeError(self):
        self.tel_conn.expect = MagicMock()
        self.tel_conn._tn.expect.return_value = (
            None,
            None,
            six.b('port already in use'))
        self.assertRaises(RuntimeError, self.tel_conn._login_state_machine)

    def test_tty_telnet_rawwrite_sys_py3(self):
        with patch.object(sys.modules['sys'], 'version', '3.x') \
                as mock_sys:
            self.tel_conn._tty_open()
            content = MagicMock()
            self.tel_conn.rawwrite(content)
            content.decode.assert_called_with('utf-8')
Example #4
0
    def _tty_login(self):
        tty_args = dict()
        tty_args['user'] = self._auth_user
        tty_args['passwd'] = self._auth_password
        tty_args['timeout'] = float(self._timeout)
        tty_args['attempts'] = int(self._attempts)
        tty_args['baud'] = self._baud
        tty_args['huge_tree'] = self._huge_tree
        if self._mode and self._mode.upper() == 'TELNET':
            tty_args['host'] = self._hostname
            tty_args['port'] = self._port
            tty_args['console_has_banner'] = self.console_has_banner
            self.console = ('telnet', self._hostname, self.port)
            self._tty = Telnet(**tty_args)
        elif self.cs_user is not None:
            tty_args['cs_user'] = self.cs_user
            tty_args['cs_passwd'] = self.cs_passwd
            tty_args['host'] = self._hostname
            tty_args['port'] = self._port
            tty_args['console_has_banner'] = self.console_has_banner
            tty_args['ssh_private_key_file'] = self._ssh_private_key_file
            self.console = ('ssh', self._hostname, self.port)
            self._tty = SSH(**tty_args)
        elif self._mode.upper() == 'SERIAL':
            tty_args['port'] = self._port
            self.console = ('serial', self._port)
            self._tty = Serial(**tty_args)
        else:
            logger.error('Mode should be either telnet or serial')
            raise AttributeError('Mode to be telnet/serial')

        self._tty.login()
Example #5
0
    def _tty_login(self):
        tty_args = dict()
        tty_args["user"] = self._auth_user
        tty_args["passwd"] = self._auth_password
        tty_args["timeout"] = float(self._timeout)
        tty_args["attempts"] = int(self._attempts)
        tty_args["baud"] = self._baud
        tty_args["huge_tree"] = self._huge_tree
        if self._mode and self._mode.upper() == "TELNET":
            tty_args["host"] = self._hostname
            tty_args["port"] = self._port
            tty_args["console_has_banner"] = self.console_has_banner
            self.console = ("telnet", self._hostname, self.port)
            self._tty = Telnet(**tty_args)
        elif self.cs_user is not None:
            tty_args["cs_user"] = self.cs_user
            tty_args["cs_passwd"] = self.cs_passwd
            tty_args["host"] = self._hostname
            tty_args["port"] = self._port
            tty_args["console_has_banner"] = self.console_has_banner
            tty_args["ssh_private_key_file"] = self._ssh_private_key_file
            self.console = ("ssh", self._hostname, self.port)
            self._tty = SSH(**tty_args)
        elif self._mode.upper() == "SERIAL":
            tty_args["port"] = self._port
            self.console = ("serial", self._port)
            self._tty = Serial(**tty_args)
        else:
            logger.error("Mode should be either telnet or serial")
            raise AttributeError("Mode to be telnet/serial")

        self._tty.login()
Example #6
0
class TestTTYTelnet(unittest.TestCase):
    @patch('jnpr.junos.transport.tty_telnet.telnetlib.Telnet')
    def setUp(self, mpock_telnet):
        self.tel_conn = Telnet(host='1.1.1.1',
                               user='******',
                               password='******',
                               port=23,
                               timeout=30)

    def test_open(self):
        self.tel_conn._tty_open()
        self.tel_conn._tn.open.assert_called()

    def test_open_exception(self):
        self.tel_conn._tn.open.side_effect = Exception
        Telnet.RETRY_OPEN = 1
        Telnet.RETRY_BACKOFF = 0.1
        self.assertRaises(RuntimeError, self.tel_conn._tty_open)
        # reset
        Telnet.RETRY_OPEN = 3
        Telnet.RETRY_BACKOFF = 2

    def test_close(self):
        self.tel_conn._tty_close()
        self.tel_conn._tn.close.assert_called()

    def test_read(self):
        self.tel_conn.read()
        self.tel_conn._tn.read_until.assert_called()

    def test_read_prompt_RuntimeError(self):
        self.tel_conn.expect = MagicMock()
        self.tel_conn.expect = (None, None, 'port already in use')
        self.assertRaises(RuntimeError, self.tel_conn._login_state_machine)
class TestTTYTelnet(unittest.TestCase):

    @patch('jnpr.junos.transport.tty_telnet.telnetlib.Telnet')
    def setUp(self, mpock_telnet):
        self.tel_conn = Telnet(host='1.1.1.1', user='******',
                               password='******', port=23,
                               timeout=30)

    def test_open(self):
        self.tel_conn._tty_open()
        self.tel_conn._tn.open.assert_called()

    def test_open_exception(self):
        self.tel_conn._tn.open.side_effect = Exception
        Telnet.RETRY_OPEN = 1
        Telnet.RETRY_BACKOFF = 0.1
        self.assertRaises(RuntimeError, self.tel_conn._tty_open)
        # reset
        Telnet.RETRY_OPEN = 3
        Telnet.RETRY_BACKOFF = 2

    def test_close(self):
        self.tel_conn._tty_close()
        self.tel_conn._tn.close.assert_called()

    def test_read(self):
        self.tel_conn.read()
        self.tel_conn._tn.read_until.assert_called()
Example #8
0
 def test_tty_telnet_baud(self, mock_telnet):
     tel_conn = Telnet(host='1.1.1.1',
                       user='******',
                       password='******',
                       port=23,
                       timeout=30,
                       baud=0)
     tel_conn._tty_open()
     tel_conn.rawwrite('<rpc>')
     tel_conn._tn.write.assert_called_with('<rpc>')
Example #9
0
 def test_tty_telnet_baud(self, mock_telnet):
     tel_conn = Telnet(
         host="1.1.1.1",
         user="******",
         password="******",
         port=23,
         timeout=30,
         baud=0,
     )
     tel_conn._tty_open()
     tel_conn.rawwrite("<rpc>")
     tel_conn._tn.write.assert_called_with("<rpc>")
Example #10
0
    def _tty_login(self):
        tty_args = dict()
        tty_args['user'] = self._auth_user
        tty_args['passwd'] = self._auth_password
        tty_args['timeout'] = float(self._timeout)
        tty_args['attempts'] = int(self._attempts)
        tty_args['baud'] = self._baud
        if self._mode.upper() == 'TELNET':
            tty_args['host'] = self._hostname
            tty_args['port'] = self._port
            self.console = ('telnet', self._hostname, self.port)
            self._tty = Telnet(**tty_args)
        elif self._mode.upper() == 'SERIAL':
            tty_args['port'] = self._port
            self.console = ('serial', self._port)
            self._tty = Serial(**tty_args)
        else:
            logger.error('Mode should be either telnet or serial')
            raise AttributeError('Mode to be telnet/serial')

        self._tty.login()
Example #11
0
 def setUp(self, mpock_telnet):
     self.tel_conn = Telnet(host='1.1.1.1',
                            user='******',
                            password='******',
                            port=23,
                            timeout=30)
Example #12
0
 def setUp(self, mpock_telnet):
     self.tel_conn = Telnet(host='1.1.1.1', user='******',
                            password='******', port=23,
                            timeout=30)
Example #13
0
 def setUp(self, mpock_telnet):
     self.tel_conn = Telnet(host="1.1.1.1",
                            user="******",
                            password="******",
                            port=23,
                            timeout=30)
Example #14
0
class TestTTYTelnet(unittest.TestCase):
    @patch("jnpr.junos.transport.tty_telnet.telnetlib.Telnet")
    def setUp(self, mpock_telnet):
        self.tel_conn = Telnet(host="1.1.1.1",
                               user="******",
                               password="******",
                               port=23,
                               timeout=30)

    def test_open(self):
        self.tel_conn._tty_open()
        self.tel_conn._tn.open.assert_called()

    def test_open_exception(self):
        self.tel_conn._tn.open.side_effect = Exception
        Telnet.RETRY_OPEN = 1
        Telnet.RETRY_BACKOFF = 0.1
        self.assertRaises(RuntimeError, self.tel_conn._tty_open)
        # reset
        Telnet.RETRY_OPEN = 3
        Telnet.RETRY_BACKOFF = 2

    def test_close(self):
        self.tel_conn._tty_close()
        self.tel_conn._tn.close.assert_called()

    def test_read(self):
        self.tel_conn.read()
        self.tel_conn._tn.read_until.assert_called()

    @patch("jnpr.junos.transport.tty_telnet.telnetlib.Telnet")
    def test_tty_telnet_baud(self, mock_telnet):
        tel_conn = Telnet(
            host="1.1.1.1",
            user="******",
            password="******",
            port=23,
            timeout=30,
            baud=0,
        )
        tel_conn._tty_open()
        tel_conn.rawwrite("<rpc>")
        tel_conn._tn.write.assert_called_with("<rpc>")

    def test_read_prompt_RuntimeError(self):
        self.tel_conn.expect = MagicMock()
        self.tel_conn.expect = (None, None, "port already in use")
        self.assertRaises(RuntimeError, self.tel_conn._login_state_machine)

    def test_read_prompt_in_use_RuntimeError(self):
        self.tel_conn.expect = MagicMock()
        self.tel_conn._tn.expect.return_value = (
            None,
            None,
            six.b("port already in use"),
        )
        self.assertRaises(RuntimeError, self.tel_conn._login_state_machine)

    def test_tty_telnet_rawwrite_sys_py3(self):
        with patch.object(sys.modules["sys"], "version", "3.x") as mock_sys:
            self.tel_conn._tty_open()
            content = MagicMock()
            self.tel_conn.rawwrite(content)
            content.decode.assert_called_with("utf-8")