Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sftpfs: Fix test with latest API of the sftp package #157

Merged
merged 1 commit into from
Mar 27, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 17 additions & 24 deletions sftpfs/sftp_test_go → sftpfs/sftp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package afero
package sftpfs

import (
"testing"
Expand Down Expand Up @@ -65,6 +65,7 @@ func SftpConnect(user, password, host string) (*SftpFsContext, error) {
Auth: []ssh.AuthMethod{
ssh.Password(password),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}

sshc, err := ssh.Dial("tcp", host, sshcfg)
Expand Down Expand Up @@ -97,7 +98,6 @@ func RunSftpServer(rootpath string) {
var (
readOnly bool
debugLevelStr string
debugLevel int
debugStderr bool
rootDir string
)
Expand All @@ -109,10 +109,6 @@ func RunSftpServer(rootpath string) {
flag.Parse()

debugStream := ioutil.Discard
if debugStderr {
debugStream = os.Stderr
debugLevel = 1
}

// An SSH server is represented by a ServerConfig, which holds
// certificate details and handles authentication of ServerConns.
Expand Down Expand Up @@ -146,7 +142,6 @@ func RunSftpServer(rootpath string) {
if err != nil {
log.Fatal("failed to listen for connection", err)
}
fmt.Printf("Listening on %v\n", listener.Addr())

nConn, err := listener.Accept()
if err != nil {
Expand All @@ -155,11 +150,11 @@ func RunSftpServer(rootpath string) {

// Before use, a handshake must be performed on the incoming
// net.Conn.
_, chans, reqs, err := ssh.NewServerConn(nConn, config)
conn, chans, reqs, err := ssh.NewServerConn(nConn, config)
if err != nil {
log.Fatal("failed to handshake", err)
}
fmt.Fprintf(debugStream, "SSH server established\n")
defer conn.Close()

// The incoming Request channel must be serviced.
go ssh.DiscardRequests(reqs)
Expand Down Expand Up @@ -200,13 +195,12 @@ func RunSftpServer(rootpath string) {
}
}(requests)

server, err := sftp.NewServer(channel, channel, debugStream, debugLevel, readOnly, rootpath)
server, err := sftp.NewServer(channel, sftp.WithDebug(debugStream))
if err != nil {
log.Fatal(err)
}
if err := server.Serve(); err != nil {
log.Fatal("sftp server completed with error:", err)
}
_ = server.Serve()
return
}
}

Expand Down Expand Up @@ -253,34 +247,33 @@ func TestSftpCreate(t *testing.T) {
}
defer ctx.Disconnect()

var AppFs Fs = SftpFs{
SftpClient: ctx.sftpc,
}
var fs = New(ctx.sftpc)

AppFs.MkdirAll("test/dir1/dir2/dir3", os.FileMode(0777))
AppFs.Mkdir("test/foo", os.FileMode(0000))
AppFs.Chmod("test/foo", os.FileMode(0700))
AppFs.Mkdir("test/bar", os.FileMode(0777))
fs.MkdirAll("test/dir1/dir2/dir3", os.FileMode(0777))
fs.Mkdir("test/foo", os.FileMode(0000))
fs.Chmod("test/foo", os.FileMode(0700))
fs.Mkdir("test/bar", os.FileMode(0777))

file, err := AppFs.Create("file1")
file, err := fs.Create("file1")
if err != nil {
t.Error(err)
}
defer file.Close()

file.Write([]byte("hello\t"))
file.Write([]byte("hello "))
file.WriteString("world!\n")

f1, err := AppFs.Open("file1")
f1, err := fs.Open("file1")
if err != nil {
log.Fatalf("open: %v", err)
}
defer f1.Close()

b := make([]byte, 100)

_, err = f1.Read(b)
_, _ = f1.Read(b)
fmt.Println(string(b))

fmt.Println("done")
// TODO check here if "hello\tworld\n" is in buffer b
}