def test_write_to_log_no_logfile(self):
        conn = base_connection.SSHConnection()
        conn.log_file_name = None
        conn.logger = mock.Mock()

        conn._write_to_log("Some_text")
        conn.logger.debug.assert_not_called()
    def test_recv(self):
        conn = base_connection.SSHConnection()
        conn.conn = mock.Mock()
        conn.logger = mock.Mock()
        conn.conn.recv = mock.Mock(return_value="AbCd")
        conn.conn.log_file_name = False

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

        conn.conn.recv.assert_called_with(4)
    def test_send_closed_connection(self):
        conn = base_connection.SSHConnection()
        conn.conn = mock.Mock()
        conn.conn.send = mock.Mock(return_value=3)
        conn.conn.closed = True
        conn.conn.log_file_name = False

        conn._conn_send("abcd")

        conn.conn.send.assert_called_with("abcd")
    def test_recv_empty(self):
        conn = base_connection.SSHConnection()
        conn.conn = mock.Mock()
        conn.logger = mock.Mock()
        conn.conn.recv = mock.Mock(return_value="")
        conn.conn.log_file_name = False

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

        conn.logger.warn.assert_called_with('We have empty response.')
        conn.conn.recv.assert_called_with(4)
    def test_send_byte_by_byte(self):
        conn = base_connection.SSHConnection()
        conn.conn = mock.Mock()
        conn.logger = mock.Mock()
        conn.conn.send = mock.Mock(return_value=2)
        conn.conn.closed = False
        conn.conn.log_file_name = False

        conn._conn_send("abcd")

        conn.conn.send.assert_has_calls([mock.call('abcd'), mock.call('cd')])
    def test_send_troubles(self):
        conn = base_connection.SSHConnection()
        conn.conn = mock.Mock()
        conn.logger = mock.Mock()
        conn.conn.send = mock.Mock(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")
    def test_write_to_log_cantcreate_dir(self):
        conn = base_connection.SSHConnection()
        conn.log_file_name = '/proc/read_only/file'
        conn.logger = mock.Mock()

        with mock.patch("os.path.isdir", mock.Mock(return_value=False)):
            with mock.patch(
                    "os.makedirs",
                    mock.Mock(side_effect=Exception(
                        "[Errno 13] Permission denied: '/proc/read_only'"))):
                conn._write_to_log("Some_text")
        conn.logger.info.assert_called_with(
            "Can\'t write to log: Exception(\"[Errno 13] Permission denied: "
            "\'/proc/read_only\'\",)")
    def test_reuse_connection(self):
        "Check resuse exteranl connection with close on delete"
        conn = base_connection.SSHConnection()
        ssh_fake = mock.Mock()
        ssh_fake.close = mock.Mock()
        conn_fake = mock.Mock()
        conn_fake.close = mock.Mock()

        conn.reuse_connection(ssh_fake, conn_fake)

        self.assertEqual(conn.ssh, ssh_fake)
        self.assertEqual(conn.conn, conn_fake)
        # check close
        conn._ssh_close()

        conn_fake.close.assert_called_once_with()
        ssh_fake.close.assert_called_once_with()
    def test_write_to_log_write_file_output(self):
        conn = base_connection.SSHConnection()
        conn.log_file_name = '/proc/read_only_file'
        conn.logger = mock.Mock()

        with mock.patch("os.path.isdir", mock.Mock(return_value=True)):
            fake_file = mock.mock_open()
            if six.PY3:
                # python 3
                with mock.patch('builtins.open', fake_file):
                    conn._write_to_log("Some_text")
            else:
                # python 2
                with mock.patch('__builtin__.open', fake_file):
                    conn._write_to_log("Some_text")
            fake_file.assert_called_once_with('/proc/read_only_file', 'a+')
            fake_file().write.assert_called_with('Some_text')
 def test_empty_send(self):
     conn = base_connection.SSHConnection()
     conn._conn_send("")