mirror of
https://github.com/rclone/rclone.git
synced 2025-12-31 07:33:33 +00:00
serve: factor out common testing parts for ftp, sftp and webdav tests
This commit is contained in:
@@ -8,15 +8,15 @@
|
||||
package ftp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"testing"
|
||||
|
||||
ftp "github.com/goftp/server"
|
||||
_ "github.com/rclone/rclone/backend/local"
|
||||
"github.com/rclone/rclone/fstest"
|
||||
"github.com/rclone/rclone/cmd/serve/servetest"
|
||||
"github.com/rclone/rclone/fs"
|
||||
"github.com/rclone/rclone/fs/config/configmap"
|
||||
"github.com/rclone/rclone/fs/config/obscure"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@@ -25,68 +25,50 @@ const (
|
||||
testHOST = "localhost"
|
||||
testPORT = "51780"
|
||||
testPASSIVEPORTRANGE = "30000-32000"
|
||||
testUSER = "rclone"
|
||||
testPASS = "password"
|
||||
)
|
||||
|
||||
// TestFTP runs the ftp server then runs the unit tests for the
|
||||
// ftp remote against it.
|
||||
func TestFTP(t *testing.T) {
|
||||
opt := DefaultOpt
|
||||
opt.ListenAddr = testHOST + ":" + testPORT
|
||||
opt.PassivePorts = testPASSIVEPORTRANGE
|
||||
opt.BasicUser = "rclone"
|
||||
opt.BasicPass = "password"
|
||||
// Configure and start the server
|
||||
start := func(f fs.Fs) (configmap.Simple, func()) {
|
||||
opt := DefaultOpt
|
||||
opt.ListenAddr = testHOST + ":" + testPORT
|
||||
opt.PassivePorts = testPASSIVEPORTRANGE
|
||||
opt.BasicUser = testUSER
|
||||
opt.BasicPass = testPASS
|
||||
|
||||
fstest.Initialise()
|
||||
|
||||
fremote, _, clean, err := fstest.RandomRemote(*fstest.RemoteName, *fstest.SubDir)
|
||||
assert.NoError(t, err)
|
||||
defer clean()
|
||||
|
||||
err = fremote.Mkdir(context.Background(), "")
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Start the server
|
||||
w, err := newServer(fremote, &opt)
|
||||
assert.NoError(t, err)
|
||||
|
||||
go func() {
|
||||
err := w.serve()
|
||||
if err != ftp.ErrServerClosed {
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
}()
|
||||
defer func() {
|
||||
err := w.close()
|
||||
w, err := newServer(f, &opt)
|
||||
assert.NoError(t, err)
|
||||
}()
|
||||
|
||||
// Change directory to run the tests
|
||||
err = os.Chdir("../../../backend/ftp")
|
||||
assert.NoError(t, err, "failed to cd to ftp remote")
|
||||
quit := make(chan struct{})
|
||||
go func() {
|
||||
err := w.serve()
|
||||
close(quit)
|
||||
if err != ftp.ErrServerClosed {
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
}()
|
||||
|
||||
// Run the ftp tests with an on the fly remote
|
||||
args := []string{"test"}
|
||||
if testing.Verbose() {
|
||||
args = append(args, "-v")
|
||||
// Config for the backend we'll use to connect to the server
|
||||
config := configmap.Simple{
|
||||
"type": "ftp",
|
||||
"host": testHOST,
|
||||
"port": testPORT,
|
||||
"user": testUSER,
|
||||
"pass": obscure.MustObscure(testPASS),
|
||||
}
|
||||
|
||||
return config, func() {
|
||||
err := w.close()
|
||||
assert.NoError(t, err)
|
||||
<-quit
|
||||
}
|
||||
}
|
||||
if *fstest.Verbose {
|
||||
args = append(args, "-verbose")
|
||||
}
|
||||
args = append(args, "-list-retries", fmt.Sprint(*fstest.ListRetries))
|
||||
args = append(args, "-remote", "ftptest:")
|
||||
cmd := exec.Command("go", args...)
|
||||
cmd.Env = append(os.Environ(),
|
||||
"RCLONE_CONFIG_FTPTEST_TYPE=ftp",
|
||||
"RCLONE_CONFIG_FTPTEST_HOST="+testHOST,
|
||||
"RCLONE_CONFIG_FTPTEST_PORT="+testPORT,
|
||||
"RCLONE_CONFIG_FTPTEST_USER=rclone",
|
||||
"RCLONE_CONFIG_FTPTEST_PASS=0HU5Hx42YiLoNGJxppOOP3QTbr-KB_MP", // ./rclone obscure password
|
||||
)
|
||||
out, err := cmd.CombinedOutput()
|
||||
if len(out) != 0 {
|
||||
t.Logf("\n----------\n%s----------\n", string(out))
|
||||
}
|
||||
assert.NoError(t, err, "Running ftp integration tests")
|
||||
|
||||
servetest.Run(t, "ftp", start)
|
||||
}
|
||||
|
||||
func TestFindID(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user