Use port 0 as the listener port so the OS assigns one not in use (#7763)

This commit is contained in:
ncabatoff 2019-10-30 10:36:23 -04:00 committed by GitHub
parent d3bb5d60b2
commit d9205cd3c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 2 additions and 20 deletions

View File

@ -1,4 +1,4 @@
// +build !race,!hsm // +build !race !hsm
// NOTE: we can't use this with HSM. We can't set testing mode on and it's not // NOTE: we can't use this with HSM. We can't set testing mode on and it's not
// safe to use env vars since that provides an attack vector in the real world. // safe to use env vars since that provides an attack vector in the real world.
@ -12,7 +12,6 @@ import (
"crypto/x509" "crypto/x509"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net"
"os" "os"
"strings" "strings"
"sync" "sync"
@ -24,23 +23,6 @@ import (
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
) )
func testRandomPort(tb testing.TB) int {
tb.Helper()
addr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:0")
if err != nil {
tb.Fatal(err)
}
l, err := net.ListenTCP("tcp", addr)
if err != nil {
tb.Fatal(err)
}
defer l.Close()
return l.Addr().(*net.TCPAddr).Port
}
func testBaseHCL(tb testing.TB, listenerExtras string) string { func testBaseHCL(tb testing.TB, listenerExtras string) string {
tb.Helper() tb.Helper()
@ -51,7 +33,7 @@ func testBaseHCL(tb testing.TB, listenerExtras string) string {
tls_disable = "true" tls_disable = "true"
%s %s
} }
`, testRandomPort(tb), listenerExtras)) `, 0, listenerExtras))
} }
const ( const (