github.com/divyam234/rclone@v1.64.1/cmd/serve/ftp/ftp_test.go (about)

     1  // Serve ftp tests set up a server and run the integration tests
     2  // for the ftp 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 ftp
    10  
    11  import (
    12  	"context"
    13  	"testing"
    14  
    15  	_ "github.com/divyam234/rclone/backend/local"
    16  	"github.com/divyam234/rclone/cmd/serve/servetest"
    17  	"github.com/divyam234/rclone/fs"
    18  	"github.com/divyam234/rclone/fs/config/configmap"
    19  	"github.com/divyam234/rclone/fs/config/obscure"
    20  	"github.com/stretchr/testify/assert"
    21  	ftp "goftp.io/server/v2"
    22  )
    23  
    24  const (
    25  	testHOST             = "localhost"
    26  	testPORT             = "51780"
    27  	testPASSIVEPORTRANGE = "30000-32000"
    28  	testUSER             = "rclone"
    29  	testPASS             = "password"
    30  )
    31  
    32  // TestFTP runs the ftp server then runs the unit tests for the
    33  // ftp remote against it.
    34  func TestFTP(t *testing.T) {
    35  	// Configure and start the server
    36  	start := func(f fs.Fs) (configmap.Simple, func()) {
    37  		opt := DefaultOpt
    38  		opt.ListenAddr = testHOST + ":" + testPORT
    39  		opt.PassivePorts = testPASSIVEPORTRANGE
    40  		opt.BasicUser = testUSER
    41  		opt.BasicPass = testPASS
    42  
    43  		w, err := newServer(context.Background(), f, &opt)
    44  		assert.NoError(t, err)
    45  
    46  		quit := make(chan struct{})
    47  		go func() {
    48  			err := w.serve()
    49  			close(quit)
    50  			if err != ftp.ErrServerClosed {
    51  				assert.NoError(t, err)
    52  			}
    53  		}()
    54  
    55  		// Config for the backend we'll use to connect to the server
    56  		config := configmap.Simple{
    57  			"type": "ftp",
    58  			"host": testHOST,
    59  			"port": testPORT,
    60  			"user": testUSER,
    61  			"pass": obscure.MustObscure(testPASS),
    62  		}
    63  
    64  		return config, func() {
    65  			err := w.close()
    66  			assert.NoError(t, err)
    67  			<-quit
    68  		}
    69  	}
    70  
    71  	servetest.Run(t, "ftp", start)
    72  }