Пример #1
0
 def test_send_response(self):
     conn = terminal_connection.connection()
     # no responses
     self.assertEqual(conn._send_response("abcd?", []), -1)
     # wrong question
     self.assertEqual(
         conn._send_response("abcd?", [{
             'question': 'yes?',
             'answer': 'no'
         }]), -1)
     # correct question
     conn.conn = MagicMock()
     conn.logger = MagicMock()
     conn.conn.send = Mock(return_value=2)
     conn.conn.closed = False
     conn.conn.log_file_name = False
     self.assertEqual(
         conn._send_response("continue, yes?", [{
             'question': 'yes?',
             'answer': 'no'
         }]), 14)
     conn.conn.send.assert_called_with("no")
     # question with new line response
     conn.conn.send = Mock(return_value=1)
     self.assertEqual(
         conn._send_response("continue, yes?", [{
             'question': 'yes?',
             'answer': 'n',
             'newline': True
         }]), 14)
     conn.conn.send.assert_has_calls([call("n"), call('\n')])
    def test_send_response(self):
        conn = terminal_connection.connection()

        self.assertEqual(conn._send_response("abcd?", []), -1)

        self.assertEqual(
            conn._send_response(
                "abcd?", [{
                    'question': 'yes?',
                    'answer': 'no'
                }]), -1
        )

        conn.conn = MagicMock()
        conn.logger = MagicMock()
        conn.conn.send = Mock(return_value=2)
        conn.conn.closed = False
        conn.conn.log_file_name = False

        self.assertEqual(
            conn._send_response(
                "continue, yes?", [{
                    'question': 'yes?',
                    'answer': 'no'
                }]), 14
        )

        conn.conn.send.assert_called_with("no")
Пример #3
0
    def test_run_with_closed_connection_after_third_check(self):
        conn = terminal_connection.connection()
        conn.logger = MagicMock()

        class _fake_conn(object):

            call_count = 0

            def send(self, text):
                return len(text)

            def recv(self, size):
                return "+\n"

            def close(self):
                pass

            @property
            def closed(self):
                self.call_count += 1

                return (self.call_count >= 4)

        conn.conn = _fake_conn()

        self.assertEqual(conn.run("test"), "+")
Пример #4
0
    def test_connect_with_password(self):
        conn = terminal_connection.connection()

        ssh_mock = MagicMock()
        ssh_mock.connect = MagicMock(side_effect=OSError("e"))
        with patch("paramiko.SSHClient", MagicMock(return_value=ssh_mock)):
            with self.assertRaises(OSError):
                conn.connect("ip",
                             "user",
                             "password",
                             None,
                             port=44,
                             prompt_check="prompt_check",
                             logger="logger",
                             log_file_name="log_file_name")

        ssh_mock.connect.assert_called_with('ip',
                                            allow_agent=False,
                                            look_for_keys=False,
                                            password='******',
                                            port=44,
                                            timeout=5,
                                            username='******')

        self.assertEqual(conn.logger, "logger")
        self.assertEqual(conn.log_file_name, "log_file_name")
Пример #5
0
    def test_connect_with_key(self):
        conn = terminal_connection.connection()

        ssh_mock = MagicMock()
        ssh_mock.connect = MagicMock(side_effect=OSError("e"))
        with patch("paramiko.RSAKey.from_private_key",
                   MagicMock(return_value="key_value")):
            with patch("paramiko.SSHClient", MagicMock(return_value=ssh_mock)):
                with self.assertRaises(OSError):
                    conn.connect("ip",
                                 "user",
                                 None,
                                 "key",
                                 prompt_check=None,
                                 logger="logger",
                                 log_file_name="log_file_name")

        ssh_mock.connect.assert_called_with('ip',
                                            allow_agent=False,
                                            pkey='key_value',
                                            port=22,
                                            timeout=5,
                                            username='******')

        self.assertEqual(conn.logger, "logger")
        self.assertEqual(conn.log_file_name, "log_file_name")
Пример #6
0
 def test_delete_backspace(self):
     conn = terminal_connection.connection()
     # simple case
     self.assertEqual(conn._delete_backspace("abc\bd\n$a\bbc"), "abd\n$bc")
     # \b in begging of line
     self.assertEqual(conn._delete_backspace("\bcd\n$a\bbc"), "cd\n$bc")
     # \b at the end
     self.assertEqual(conn._delete_backspace("abc\b\b\b\b\b"), "")
    def test_write_to_log_no_logfile(self):
        conn = terminal_connection.connection()
        conn.log_file_name = None
        conn.logger = MagicMock()

        conn._write_to_log("Some_text")

        conn.logger.debug.assert_called_with("'Some_text'")
Пример #8
0
    def test_recv(self):
        conn = terminal_connection.connection()
        conn.conn = MagicMock()
        conn.logger = MagicMock()
        conn.conn.recv = MagicMock(return_value="AbCd")
        conn.conn.log_file_name = False

        self.assertEqual(conn._conn_recv(4), "AbCd")

        conn.conn.recv.assert_called_with(4)
Пример #9
0
    def test_cleanup_response_with_prompt(self):
        conn = terminal_connection.connection()

        conn.logger = MagicMock()

        self.assertEqual(
            conn._cleanup_response("prompt> text ", "prompt>", ['error']),
            "text")

        conn.logger.info.assert_not_called()
Пример #10
0
    def test_cleanup_response_without_prompt(self):
        conn = terminal_connection.connection()
        conn.logger = MagicMock()

        self.assertEqual(
            conn._cleanup_response("prmpt> text ", "prompt>", ['error']),
            "prmpt> text")

        conn.logger.info.assert_called_with(
            "Have not found 'prompt>' in response: ''prmpt> text ''")
Пример #11
0
    def test_run_with_closed_connection(self):
        conn = terminal_connection.connection()
        conn.logger = MagicMock()
        conn.conn = MagicMock()
        conn.conn.closed = True
        conn.conn.send = MagicMock(return_value=5)

        self.assertEqual(conn.run("test"), "")

        conn.conn.send.assert_called_with("test\n")
Пример #12
0
    def test_send_closed_connection(self):
        conn = terminal_connection.connection()
        conn.conn = MagicMock()
        conn.conn.send = MagicMock(return_value=3)
        conn.conn.closed = True
        conn.conn.log_file_name = False

        conn._conn_send("abcd")

        conn.conn.send.assert_called_with("abcd")
Пример #13
0
    def test_send_byte_by_byte(self):
        conn = terminal_connection.connection()
        conn.conn = MagicMock()
        conn.logger = MagicMock()
        conn.conn.send = Mock(return_value=2)
        conn.conn.closed = False
        conn.conn.log_file_name = False

        conn._conn_send("abcd")

        conn.conn.send.assert_has_calls([call('abcd'), call('cd')])
Пример #14
0
    def test_cleanup_response_mess_before_prompt(self):
        conn = terminal_connection.connection()
        conn.logger = MagicMock()

        self.assertEqual(
            conn._cleanup_response("..prompt> text\n some", "prompt>",
                                   ['error']), "some")

        conn.logger.info.assert_called_with(
            "Some mess before 'prompt>' in response: ''..prompt> "
            "text\\n some''")
Пример #15
0
    def test_recv_empty(self):
        conn = terminal_connection.connection()
        conn.conn = MagicMock()
        conn.logger = MagicMock()
        conn.conn.recv = MagicMock(return_value="")
        conn.conn.log_file_name = False

        self.assertEqual(conn._conn_recv(4), "")

        conn.logger.info.assert_called_with('We have empty response.')
        conn.conn.recv.assert_called_with(4)
Пример #16
0
    def test_run_return_without_delay(self):
        conn = terminal_connection.connection()
        conn.logger = MagicMock()
        conn.conn = MagicMock()
        conn.conn.closed = False
        conn.conn.send = MagicMock(return_value=5)
        conn.conn.recv = MagicMock(return_value="\nmessage\n#")

        self.assertEqual(conn.run("test"), "message")

        conn.conn.send.assert_called_with("test\n")
Пример #17
0
    def test_send_troubles(self):
        conn = terminal_connection.connection()
        conn.conn = MagicMock()
        conn.logger = MagicMock()
        conn.conn.send = MagicMock(return_value=-1)
        conn.conn.closed = True
        conn.conn.log_file_name = False

        conn._conn_send("abcd")

        conn.logger.info.assert_called_with("We have issue with send!")
        conn.conn.send.assert_called_with("abcd")
Пример #18
0
    def test_close(self):
        conn = terminal_connection.connection()

        conn.conn = MagicMock()
        conn.conn.close = MagicMock()
        conn.ssh = MagicMock()
        conn.ssh.close = MagicMock()

        conn.close()

        conn.conn.close.assert_called_with()
        conn.ssh.close.assert_called_with()
Пример #19
0
    def test_write_to_log_write_file(self):
        conn = terminal_connection.connection()
        conn.log_file_name = '/proc/read_only_file'
        conn.logger = MagicMock()

        with patch("os.path.isdir", MagicMock(return_value=True)):
            fake_file = mock_open()
            with patch('__builtin__.open', fake_file):
                conn._write_to_log("Some_text", False)

            fake_file.assert_called_once_with('/proc/read_only_file.in', 'a+')
            fake_file().write.assert_called_with('Some_text')
Пример #20
0
    def test_is_closed(self):
        conn = terminal_connection.connection()

        conn.conn = MagicMock()

        conn.conn.closed = False
        self.assertFalse(conn.is_closed())

        conn.conn.closed = True
        self.assertTrue(conn.is_closed())

        conn.conn = None
        self.assertTrue(conn.is_closed())
Пример #21
0
    def test_write_to_log_cantcreate_dir(self):
        conn = terminal_connection.connection()
        conn.log_file_name = '/proc/read_only/file'
        conn.logger = MagicMock()

        with patch("os.path.isdir", MagicMock(return_value=False)):
            with patch(
                    "os.makedirs",
                    MagicMock(side_effect=Exception(
                        "[Errno 13] Permission denied: '/proc/read_only'"))):
                conn._write_to_log("Some_text")
        conn.logger.info.assert_called_with(
            "[Errno 13] Permission denied: '/proc/read_only'")
 def test_connect(self):
     conn = terminal_connection.connection()
     conn_mock = MagicMock()
     conn_mock.recv = MagicMock(return_value="some_prompt#")
     ssh_mock = MagicMock()
     ssh_mock.connect = MagicMock()
     ssh_mock.invoke_shell = MagicMock(return_value=conn_mock)
     with patch("paramiko.SSHClient", MagicMock(return_value=ssh_mock)):
         self.assertEqual(
             conn.connect("ip", "user", "password", None, port=44,
                          prompt_check=None, logger=MagicMock(),
                          log_file_name=None),
             "some_prompt"
         )
Пример #23
0
    def test_run_return_without_delay_with_responses(self):
        conn = terminal_connection.connection()
        conn.logger = MagicMock()
        conn.conn = MagicMock()
        conn.conn.closed = False
        conn.conn.send = MagicMock(side_effect=[5, 2])
        conn.conn.recv = MagicMock(side_effect=["\nmessage, yes?", "ok\n#"])

        self.assertEqual(
            conn.run("test", responses=[{
                'question': 'yes?',
                'answer': 'no'
            }]), "message, yes?ok")

        conn.conn.send.assert_has_calls([call("test\n"), call('no')])
    def test_cleanup_response_error(self):
        conn = terminal_connection.connection()
        conn.logger = MagicMock()

        with self.assertRaises(RecoverableError) as error:
            conn._cleanup_response(
                "prompt> text\n some\nerror", "prompt>", ['error']
            )

        conn.logger.info.assert_not_called()

        self.assertEqual(
            str(error.exception),
            'Looks as we have error in response: prompt> text\n some\nerror'
        )
Пример #25
0
    def test_run_with_closed_connection_after_twice_check(self):
        conn = terminal_connection.connection()
        conn.logger = MagicMock()
        conn.conn = MagicMock()
        conn.conn.closed = False

        conn.conn.call_count = 0

        def _recv(size):

            if conn.conn.call_count == 1:
                conn.conn.closed = True

            conn.conn.call_count += 1

            return "+"

        conn.conn.send = MagicMock(return_value=5)
        conn.conn.recv = _recv

        self.assertEqual(conn.run("test"), "++")

        conn.conn.send.assert_called_with("test\n")
Пример #26
0
 def test_empty_send(self):
     conn = terminal_connection.connection()
     conn._conn_send("")
Пример #27
0
    def test_find_any_in(self):
        conn = terminal_connection.connection()

        self.assertEqual(conn._find_any_in("abcd\n$abc", ["$", "#"]), 5)
        self.assertEqual(conn._find_any_in("abcd\n>abc", ["$", "#"]), -1)
Пример #28
0
    def test_write_to_log_no_logfile(self):
        conn = terminal_connection.connection()
        conn.log_file_name = None

        conn._write_to_log("Some_text")
Пример #29
0
    def test_cleanup_response_empty(self):
        conn = terminal_connection.connection()

        self.assertEqual(conn._cleanup_response(" text ", ":", []), "text")
    def test_delete_backspace(self):
        conn = terminal_connection.connection()

        self.assertEqual(conn._delete_backspace("abc\bd\n$a\bbc"), "abd\n$bc")