From 81cbd6395a8a20b4fd10f161f32c3be612c4ca38 Mon Sep 17 00:00:00 2001 From: Jerry Jacobs Date: Fri, 9 Mar 2018 11:31:10 +0100 Subject: [PATCH] sftps: Fix test with latest API of the sftp package --- sftpfs/{sftp_test_go => sftp_test.go} | 41 +++++++++++---------------- 1 file changed, 17 insertions(+), 24 deletions(-) rename sftpfs/{sftp_test_go => sftp_test.go} (88%) diff --git a/sftpfs/sftp_test_go b/sftpfs/sftp_test.go similarity index 88% rename from sftpfs/sftp_test_go rename to sftpfs/sftp_test.go index bb00535..9df21c9 100644 --- a/sftpfs/sftp_test_go +++ b/sftpfs/sftp_test.go @@ -11,7 +11,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package afero +package sftpfs import ( "testing" @@ -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) @@ -97,7 +98,6 @@ func RunSftpServer(rootpath string) { var ( readOnly bool debugLevelStr string - debugLevel int debugStderr bool rootDir string ) @@ -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. @@ -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 { @@ -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) @@ -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 } } @@ -253,25 +247,23 @@ 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) } @@ -279,8 +271,9 @@ func TestSftpCreate(t *testing.T) { 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 }