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