github.com/divyam234/rclone@v1.64.1/cmd/serve/sftp/sftp_test.go (about)

     1  // Serve sftp tests set up a server and run the integration tests
     2  // for the sftp remote against it.
     3  //
     4  // We skip tests on platforms with troublesome character mappings
     5  
     6  //go:build !windows && !darwin && !plan9
     7  // +build !windows,!darwin,!plan9
     8  
     9  package sftp
    10  
    11  import (
    12  	"context"
    13  	"strings"
    14  	"testing"
    15  
    16  	"github.com/pkg/sftp"
    17  	_ "github.com/divyam234/rclone/backend/local"
    18  	"github.com/divyam234/rclone/cmd/serve/servetest"
    19  	"github.com/divyam234/rclone/fs"
    20  	"github.com/divyam234/rclone/fs/config/configmap"
    21  	"github.com/divyam234/rclone/fs/config/obscure"
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  const (
    26  	testBindAddress = "localhost:0"
    27  	testUser        = "testuser"
    28  	testPass        = "testpass"
    29  )
    30  
    31  // check interfaces
    32  var (
    33  	_ sftp.FileReader = vfsHandler{}
    34  	_ sftp.FileWriter = vfsHandler{}
    35  	_ sftp.FileCmder  = vfsHandler{}
    36  	_ sftp.FileLister = vfsHandler{}
    37  )
    38  
    39  // TestSftp runs the sftp server then runs the unit tests for the
    40  // sftp remote against it.
    41  func TestSftp(t *testing.T) {
    42  	// Configure and start the server
    43  	start := func(f fs.Fs) (configmap.Simple, func()) {
    44  		opt := DefaultOpt
    45  		opt.ListenAddr = testBindAddress
    46  		opt.User = testUser
    47  		opt.Pass = testPass
    48  
    49  		w := newServer(context.Background(), f, &opt)
    50  		require.NoError(t, w.serve())
    51  
    52  		// Read the host and port we started on
    53  		addr := w.Addr()
    54  		colon := strings.LastIndex(addr, ":")
    55  
    56  		// Config for the backend we'll use to connect to the server
    57  		config := configmap.Simple{
    58  			"type": "sftp",
    59  			"user": testUser,
    60  			"pass": obscure.MustObscure(testPass),
    61  			"host": addr[:colon],
    62  			"port": addr[colon+1:],
    63  		}
    64  
    65  		// return a stop function
    66  		return config, func() {
    67  			w.Close()
    68  			w.Wait()
    69  		}
    70  	}
    71  
    72  	servetest.Run(t, "sftp", start)
    73  }