tests: make test servers choose a random port to make more reliable

Tests have been randomly failing with messages like

    listen tcp 127.0.0.1:51778: bind: address already in use

Rework all the test servers so they choose a random free port on
startup and use that for the tests to avoid.
This commit is contained in:
Nick Craig-Wood 2019-05-01 11:16:22 +01:00
parent 8f89b03d7b
commit 9df322e889
5 changed files with 14 additions and 15 deletions

View File

@ -20,11 +20,11 @@ import (
var ( var (
dlnaServer *server dlnaServer *server
testURL string
) )
const ( const (
testBindAddress = "localhost:51777" testBindAddress = "localhost:0"
testURL = "http://" + testBindAddress + "/"
) )
func startServer(t *testing.T, f fs.Fs) { func startServer(t *testing.T, f fs.Fs) {
@ -32,6 +32,7 @@ func startServer(t *testing.T, f fs.Fs) {
opt.ListenAddr = testBindAddress opt.ListenAddr = testBindAddress
dlnaServer = newServer(f, &opt) dlnaServer = newServer(f, &opt)
assert.NoError(t, dlnaServer.Serve()) assert.NoError(t, dlnaServer.Serve())
testURL = "http://" + dlnaServer.HTTPConn.Addr().String() + "/"
} }
func TestInit(t *testing.T) { func TestInit(t *testing.T) {

View File

@ -3,7 +3,6 @@ package http
import ( import (
"flag" "flag"
"io/ioutil" "io/ioutil"
"net"
"net/http" "net/http"
"strings" "strings"
"testing" "testing"
@ -21,11 +20,11 @@ import (
var ( var (
updateGolden = flag.Bool("updategolden", false, "update golden files for regression test") updateGolden = flag.Bool("updategolden", false, "update golden files for regression test")
httpServer *server httpServer *server
testURL string
) )
const ( const (
testBindAddress = "localhost:51777" testBindAddress = "localhost:0"
testURL = "http://" + testBindAddress + "/"
) )
func startServer(t *testing.T, f fs.Fs) { func startServer(t *testing.T, f fs.Fs) {
@ -33,13 +32,14 @@ func startServer(t *testing.T, f fs.Fs) {
opt.ListenAddr = testBindAddress opt.ListenAddr = testBindAddress
httpServer = newServer(f, &opt) httpServer = newServer(f, &opt)
assert.NoError(t, httpServer.Serve()) assert.NoError(t, httpServer.Serve())
testURL = httpServer.Server.URL()
// try to connect to the test server // try to connect to the test server
pause := time.Millisecond pause := time.Millisecond
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
conn, err := net.Dial("tcp", testBindAddress) resp, err := http.Head(testURL)
if err == nil { if err == nil {
_ = conn.Close() _ = resp.Body.Close()
return return
} }
// t.Logf("couldn't connect, sleeping for %v: %v", pause, err) // t.Logf("couldn't connect, sleeping for %v: %v", pause, err)

View File

@ -17,8 +17,7 @@ import (
) )
const ( const (
testBindAddress = "localhost:51779" testBindAddress = "localhost:0"
testURL = "http://" + testBindAddress + "/"
resticSource = "../../../../../restic/restic" resticSource = "../../../../../restic/restic"
) )
@ -62,7 +61,7 @@ func TestRestic(t *testing.T) {
} }
cmd := exec.Command("go", args...) cmd := exec.Command("go", args...)
cmd.Env = append(os.Environ(), cmd.Env = append(os.Environ(),
"RESTIC_TEST_REST_REPOSITORY=rest:"+testURL+path, "RESTIC_TEST_REST_REPOSITORY=rest:"+w.Server.URL()+path,
) )
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
if len(out) != 0 { if len(out) != 0 {

View File

@ -20,8 +20,7 @@ import (
) )
const ( const (
testBindAddress = "localhost:51778" testBindAddress = "localhost:0"
testURL = "http://" + testBindAddress + "/"
) )
// check interfaces // check interfaces
@ -70,7 +69,7 @@ func TestWebDav(t *testing.T) {
cmd := exec.Command("go", args...) cmd := exec.Command("go", args...)
cmd.Env = append(os.Environ(), cmd.Env = append(os.Environ(),
"RCLONE_CONFIG_WEBDAVTEST_TYPE=webdav", "RCLONE_CONFIG_WEBDAVTEST_TYPE=webdav",
"RCLONE_CONFIG_WEBDAVTEST_URL="+testURL, "RCLONE_CONFIG_WEBDAVTEST_URL="+w.Server.URL(),
"RCLONE_CONFIG_WEBDAVTEST_VENDOR=other", "RCLONE_CONFIG_WEBDAVTEST_VENDOR=other",
) )
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()

View File

@ -18,8 +18,7 @@ import (
) )
const ( const (
testBindAddress = "localhost:51781" testBindAddress = "localhost:0"
testURL = "http://" + testBindAddress + "/"
testFs = "testdata/files" testFs = "testdata/files"
remoteURL = "[" + testFs + "]/" // initial URL path to fetch from that remote remoteURL = "[" + testFs + "]/" // initial URL path to fetch from that remote
) )
@ -39,6 +38,7 @@ func TestRcServer(t *testing.T) {
rcServer.Close() rcServer.Close()
rcServer.Wait() rcServer.Wait()
}() }()
testURL := rcServer.Server.URL()
// Do the simplest possible test to check the server is alive // Do the simplest possible test to check the server is alive
// Do it a few times to wait for the server to start // Do it a few times to wait for the server to start