mirror of https://github.com/status-im/consul.git
Merge pull request #11677 from hashicorp/dnephin/freeport-interface
sdk: use t.Cleanup in freeport and remove unnecessary calls
This commit is contained in:
commit
81afb208ac
|
@ -18,7 +18,6 @@ import (
|
||||||
"github.com/hashicorp/consul/agent/consul/authmethod/testauth"
|
"github.com/hashicorp/consul/agent/consul/authmethod/testauth"
|
||||||
"github.com/hashicorp/consul/agent/structs"
|
"github.com/hashicorp/consul/agent/structs"
|
||||||
"github.com/hashicorp/consul/internal/go-sso/oidcauth/oidcauthtest"
|
"github.com/hashicorp/consul/internal/go-sso/oidcauth/oidcauthtest"
|
||||||
"github.com/hashicorp/consul/sdk/freeport"
|
|
||||||
"github.com/hashicorp/consul/sdk/testutil"
|
"github.com/hashicorp/consul/sdk/testutil"
|
||||||
"github.com/hashicorp/consul/testrpc"
|
"github.com/hashicorp/consul/testrpc"
|
||||||
)
|
)
|
||||||
|
@ -1658,7 +1657,7 @@ func TestACLEndpoint_LoginLogout_jwt(t *testing.T) {
|
||||||
testrpc.WaitForLeader(t, a.RPC, "dc1")
|
testrpc.WaitForLeader(t, a.RPC, "dc1")
|
||||||
|
|
||||||
// spin up a fake oidc server
|
// spin up a fake oidc server
|
||||||
oidcServer := startSSOTestServer(t)
|
oidcServer := oidcauthtest.Start(t)
|
||||||
pubKey, privKey := oidcServer.SigningKeys()
|
pubKey, privKey := oidcServer.SigningKeys()
|
||||||
|
|
||||||
type mConfig = map[string]interface{}
|
type mConfig = map[string]interface{}
|
||||||
|
@ -2330,14 +2329,6 @@ func upsertTestCustomizedBindingRule(rpc rpcFn, masterToken string, datacenter s
|
||||||
return &out, nil
|
return &out, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func startSSOTestServer(t *testing.T) *oidcauthtest.Server {
|
|
||||||
ports := freeport.MustTake(1)
|
|
||||||
return oidcauthtest.Start(t, oidcauthtest.WithPort(
|
|
||||||
ports[0],
|
|
||||||
func() { freeport.Return(ports) },
|
|
||||||
))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestHTTPHandlers_ACLReplicationStatus(t *testing.T) {
|
func TestHTTPHandlers_ACLReplicationStatus(t *testing.T) {
|
||||||
if testing.Short() {
|
if testing.Short() {
|
||||||
t.Skip("too slow for testing.Short")
|
t.Skip("too slow for testing.Short")
|
||||||
|
|
|
@ -295,10 +295,6 @@ func TestAgent_HTTPMaxHeaderBytes(t *testing.T) {
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
ports, err := freeport.Take(1)
|
|
||||||
require.NoError(t, err)
|
|
||||||
t.Cleanup(func() { freeport.Return(ports) })
|
|
||||||
|
|
||||||
caConfig := tlsutil.Config{}
|
caConfig := tlsutil.Config{}
|
||||||
tlsConf, err := tlsutil.NewConfigurator(caConfig, hclog.New(nil))
|
tlsConf, err := tlsutil.NewConfigurator(caConfig, hclog.New(nil))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -312,7 +308,7 @@ func TestAgent_HTTPMaxHeaderBytes(t *testing.T) {
|
||||||
},
|
},
|
||||||
RuntimeConfig: &config.RuntimeConfig{
|
RuntimeConfig: &config.RuntimeConfig{
|
||||||
HTTPAddrs: []net.Addr{
|
HTTPAddrs: []net.Addr{
|
||||||
&net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: ports[0]},
|
&net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: freeport.GetOne(t)},
|
||||||
},
|
},
|
||||||
HTTPMaxHeaderBytes: tt.maxHeaderBytes,
|
HTTPMaxHeaderBytes: tt.maxHeaderBytes,
|
||||||
},
|
},
|
||||||
|
@ -1738,14 +1734,12 @@ func TestAgent_RestoreServiceWithAliasCheck(t *testing.T) {
|
||||||
a := StartTestAgent(t, TestAgent{HCL: cfg})
|
a := StartTestAgent(t, TestAgent{HCL: cfg})
|
||||||
defer a.Shutdown()
|
defer a.Shutdown()
|
||||||
|
|
||||||
testCtx, testCancel := context.WithCancel(context.Background())
|
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||||
defer testCancel()
|
w.WriteHeader(http.StatusOK)
|
||||||
|
_, _ = w.Write([]byte("OK\n"))
|
||||||
testHTTPServer, returnPort := launchHTTPCheckServer(t, testCtx)
|
})
|
||||||
defer func() {
|
testHTTPServer := httptest.NewServer(handler)
|
||||||
testHTTPServer.Close()
|
t.Cleanup(testHTTPServer.Close)
|
||||||
returnPort()
|
|
||||||
}()
|
|
||||||
|
|
||||||
registerServicesAndChecks := func(t *testing.T, a *TestAgent) {
|
registerServicesAndChecks := func(t *testing.T, a *TestAgent) {
|
||||||
// add one persistent service with a simple check
|
// add one persistent service with a simple check
|
||||||
|
@ -1850,29 +1844,6 @@ node_name = "` + a.Config.NodeName + `"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func launchHTTPCheckServer(t *testing.T, ctx context.Context) (srv *httptest.Server, returnPortsFn func()) {
|
|
||||||
ports := freeport.MustTake(1)
|
|
||||||
port := ports[0]
|
|
||||||
|
|
||||||
addr := net.JoinHostPort("127.0.0.1", strconv.Itoa(port))
|
|
||||||
|
|
||||||
var lc net.ListenConfig
|
|
||||||
listener, err := lc.Listen(ctx, "tcp", addr)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
_, _ = w.Write([]byte("OK\n"))
|
|
||||||
})
|
|
||||||
|
|
||||||
srv = &httptest.Server{
|
|
||||||
Listener: listener,
|
|
||||||
Config: &http.Server{Handler: handler},
|
|
||||||
}
|
|
||||||
srv.Start()
|
|
||||||
return srv, func() { freeport.Return(ports) }
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAgent_AddCheck_Alias(t *testing.T) {
|
func TestAgent_AddCheck_Alias(t *testing.T) {
|
||||||
if testing.Short() {
|
if testing.Short() {
|
||||||
t.Skip("too slow for testing.Short")
|
t.Skip("too slow for testing.Short")
|
||||||
|
@ -4708,14 +4679,12 @@ func TestAgent_JoinWAN_viaMeshGateway(t *testing.T) {
|
||||||
|
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
gwPort := freeport.MustTake(1)
|
port := freeport.GetOne(t)
|
||||||
defer freeport.Return(gwPort)
|
gwAddr := ipaddr.FormatAddressPort("127.0.0.1", port)
|
||||||
gwAddr := ipaddr.FormatAddressPort("127.0.0.1", gwPort[0])
|
|
||||||
|
|
||||||
// Due to some ordering, we'll have to manually configure these ports in
|
// Due to some ordering, we'll have to manually configure these ports in
|
||||||
// advance.
|
// advance.
|
||||||
secondaryRPCPorts := freeport.MustTake(2)
|
secondaryRPCPorts := freeport.GetN(t, 2)
|
||||||
defer freeport.Return(secondaryRPCPorts)
|
|
||||||
|
|
||||||
a1 := StartTestAgent(t, TestAgent{Name: "bob", HCL: `
|
a1 := StartTestAgent(t, TestAgent{Name: "bob", HCL: `
|
||||||
domain = "consul"
|
domain = "consul"
|
||||||
|
@ -4769,7 +4738,7 @@ func TestAgent_JoinWAN_viaMeshGateway(t *testing.T) {
|
||||||
ID: "mesh-gateway",
|
ID: "mesh-gateway",
|
||||||
Name: "mesh-gateway",
|
Name: "mesh-gateway",
|
||||||
Meta: map[string]string{structs.MetaWANFederationKey: "1"},
|
Meta: map[string]string{structs.MetaWANFederationKey: "1"},
|
||||||
Port: gwPort[0],
|
Port: port,
|
||||||
}
|
}
|
||||||
req, err := http.NewRequest("PUT", "/v1/agent/service/register", jsonReader(args))
|
req, err := http.NewRequest("PUT", "/v1/agent/service/register", jsonReader(args))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -4883,7 +4852,7 @@ func TestAgent_JoinWAN_viaMeshGateway(t *testing.T) {
|
||||||
ID: "mesh-gateway",
|
ID: "mesh-gateway",
|
||||||
Name: "mesh-gateway",
|
Name: "mesh-gateway",
|
||||||
Meta: map[string]string{structs.MetaWANFederationKey: "1"},
|
Meta: map[string]string{structs.MetaWANFederationKey: "1"},
|
||||||
Port: gwPort[0],
|
Port: port,
|
||||||
}
|
}
|
||||||
req, err := http.NewRequest("PUT", "/v1/agent/service/register", jsonReader(args))
|
req, err := http.NewRequest("PUT", "/v1/agent/service/register", jsonReader(args))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -4898,7 +4867,7 @@ func TestAgent_JoinWAN_viaMeshGateway(t *testing.T) {
|
||||||
ID: "mesh-gateway",
|
ID: "mesh-gateway",
|
||||||
Name: "mesh-gateway",
|
Name: "mesh-gateway",
|
||||||
Meta: map[string]string{structs.MetaWANFederationKey: "1"},
|
Meta: map[string]string{structs.MetaWANFederationKey: "1"},
|
||||||
Port: gwPort[0],
|
Port: port,
|
||||||
}
|
}
|
||||||
req, err := http.NewRequest("PUT", "/v1/agent/service/register", jsonReader(args))
|
req, err := http.NewRequest("PUT", "/v1/agent/service/register", jsonReader(args))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -5281,10 +5250,7 @@ func TestAgent_ListenHTTP_MultipleAddresses(t *testing.T) {
|
||||||
t.Skip("too slow for testing.Short")
|
t.Skip("too slow for testing.Short")
|
||||||
}
|
}
|
||||||
|
|
||||||
ports, err := freeport.Take(2)
|
ports := freeport.GetN(t, 2)
|
||||||
require.NoError(t, err)
|
|
||||||
t.Cleanup(func() { freeport.Return(ports) })
|
|
||||||
|
|
||||||
caConfig := tlsutil.Config{}
|
caConfig := tlsutil.Config{}
|
||||||
tlsConf, err := tlsutil.NewConfigurator(caConfig, hclog.New(nil))
|
tlsConf, err := tlsutil.NewConfigurator(caConfig, hclog.New(nil))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
|
@ -120,11 +120,7 @@ func runTestVault(t testing.T) (*TestVaultServer, error) {
|
||||||
return nil, fmt.Errorf("%q not found on $PATH", vaultBinaryName)
|
return nil, fmt.Errorf("%q not found on $PATH", vaultBinaryName)
|
||||||
}
|
}
|
||||||
|
|
||||||
ports := freeport.MustTake(2)
|
ports := freeport.GetN(t, 2)
|
||||||
returnPortsFn := func() {
|
|
||||||
freeport.Return(ports)
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
clientAddr = fmt.Sprintf("127.0.0.1:%d", ports[0])
|
clientAddr = fmt.Sprintf("127.0.0.1:%d", ports[0])
|
||||||
clusterAddr = fmt.Sprintf("127.0.0.1:%d", ports[1])
|
clusterAddr = fmt.Sprintf("127.0.0.1:%d", ports[1])
|
||||||
|
@ -136,7 +132,6 @@ func runTestVault(t testing.T) (*TestVaultServer, error) {
|
||||||
Address: "http://" + clientAddr,
|
Address: "http://" + clientAddr,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
returnPortsFn()
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
client.SetToken(token)
|
client.SetToken(token)
|
||||||
|
@ -156,7 +151,6 @@ func runTestVault(t testing.T) (*TestVaultServer, error) {
|
||||||
cmd.Stdout = ioutil.Discard
|
cmd.Stdout = ioutil.Discard
|
||||||
cmd.Stderr = ioutil.Discard
|
cmd.Stderr = ioutil.Discard
|
||||||
if err := cmd.Start(); err != nil {
|
if err := cmd.Start(); err != nil {
|
||||||
returnPortsFn()
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -165,7 +159,6 @@ func runTestVault(t testing.T) (*TestVaultServer, error) {
|
||||||
Addr: "http://" + clientAddr,
|
Addr: "http://" + clientAddr,
|
||||||
cmd: cmd,
|
cmd: cmd,
|
||||||
client: client,
|
client: client,
|
||||||
returnPortsFn: returnPortsFn,
|
|
||||||
}
|
}
|
||||||
t.Cleanup(func() {
|
t.Cleanup(func() {
|
||||||
if err := testVault.Stop(); err != nil {
|
if err := testVault.Stop(); err != nil {
|
||||||
|
@ -181,9 +174,6 @@ type TestVaultServer struct {
|
||||||
Addr string
|
Addr string
|
||||||
cmd *exec.Cmd
|
cmd *exec.Cmd
|
||||||
client *vaultapi.Client
|
client *vaultapi.Client
|
||||||
|
|
||||||
// returnPortsFn will put the ports claimed for the test back into the
|
|
||||||
returnPortsFn func()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var printedVaultVersion sync.Once
|
var printedVaultVersion sync.Once
|
||||||
|
@ -229,11 +219,6 @@ func (v *TestVaultServer) Stop() error {
|
||||||
if err := v.cmd.Wait(); err != nil {
|
if err := v.cmd.Wait(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if v.returnPortsFn != nil {
|
|
||||||
v.returnPortsFn()
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,6 @@ import (
|
||||||
"github.com/hashicorp/consul/agent/consul/authmethod/testauth"
|
"github.com/hashicorp/consul/agent/consul/authmethod/testauth"
|
||||||
"github.com/hashicorp/consul/agent/structs"
|
"github.com/hashicorp/consul/agent/structs"
|
||||||
"github.com/hashicorp/consul/internal/go-sso/oidcauth/oidcauthtest"
|
"github.com/hashicorp/consul/internal/go-sso/oidcauth/oidcauthtest"
|
||||||
"github.com/hashicorp/consul/sdk/freeport"
|
|
||||||
"github.com/hashicorp/consul/sdk/testutil"
|
"github.com/hashicorp/consul/sdk/testutil"
|
||||||
"github.com/hashicorp/consul/sdk/testutil/retry"
|
"github.com/hashicorp/consul/sdk/testutil/retry"
|
||||||
)
|
)
|
||||||
|
@ -4868,7 +4867,7 @@ func TestACLEndpoint_Login_jwt(t *testing.T) {
|
||||||
acl := ACL{srv: srv}
|
acl := ACL{srv: srv}
|
||||||
|
|
||||||
// spin up a fake oidc server
|
// spin up a fake oidc server
|
||||||
oidcServer := startSSOTestServer(t)
|
oidcServer := oidcauthtest.Start(t)
|
||||||
pubKey, privKey := oidcServer.SigningKeys()
|
pubKey, privKey := oidcServer.SigningKeys()
|
||||||
|
|
||||||
type mConfig = map[string]interface{}
|
type mConfig = map[string]interface{}
|
||||||
|
@ -5003,14 +5002,6 @@ func TestACLEndpoint_Login_jwt(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func startSSOTestServer(t *testing.T) *oidcauthtest.Server {
|
|
||||||
ports := freeport.MustTake(1)
|
|
||||||
return oidcauthtest.Start(t, oidcauthtest.WithPort(
|
|
||||||
ports[0],
|
|
||||||
func() { freeport.Return(ports) },
|
|
||||||
))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestACLEndpoint_Logout(t *testing.T) {
|
func TestACLEndpoint_Logout(t *testing.T) {
|
||||||
if testing.Short() {
|
if testing.Short() {
|
||||||
t.Skip("too slow for testing.Short")
|
t.Skip("too slow for testing.Short")
|
||||||
|
|
|
@ -4,20 +4,16 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"encoding/pem"
|
"encoding/pem"
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"net/url"
|
"net/url"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/hashicorp/consul/sdk/freeport"
|
|
||||||
"github.com/mitchellh/go-testing-interface"
|
"github.com/mitchellh/go-testing-interface"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
authv1 "k8s.io/api/authentication/v1"
|
authv1 "k8s.io/api/authentication/v1"
|
||||||
|
@ -35,7 +31,6 @@ import (
|
||||||
type TestAPIServer struct {
|
type TestAPIServer struct {
|
||||||
srv *httptest.Server
|
srv *httptest.Server
|
||||||
caCert string
|
caCert string
|
||||||
returnFunc func()
|
|
||||||
|
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
authorizedJWT string // token review and sa read
|
authorizedJWT string // token review and sa read
|
||||||
|
@ -48,12 +43,7 @@ type TestAPIServer struct {
|
||||||
// random free port.
|
// random free port.
|
||||||
func StartTestAPIServer(t testing.T) *TestAPIServer {
|
func StartTestAPIServer(t testing.T) *TestAPIServer {
|
||||||
s := &TestAPIServer{}
|
s := &TestAPIServer{}
|
||||||
|
s.srv = httptest.NewUnstartedServer(s)
|
||||||
ports := freeport.MustTake(1)
|
|
||||||
s.returnFunc = func() {
|
|
||||||
freeport.Return(ports)
|
|
||||||
}
|
|
||||||
s.srv = httptestNewUnstartedServerWithPort(s, ports[0])
|
|
||||||
s.srv.Config.ErrorLog = log.New(ioutil.Discard, "", 0)
|
s.srv.Config.ErrorLog = log.New(ioutil.Discard, "", 0)
|
||||||
s.srv.StartTLS()
|
s.srv.StartTLS()
|
||||||
|
|
||||||
|
@ -101,10 +91,6 @@ func (s *TestAPIServer) SetAllowedServiceAccount(
|
||||||
// Stop stops the running TestAPIServer.
|
// Stop stops the running TestAPIServer.
|
||||||
func (s *TestAPIServer) Stop() {
|
func (s *TestAPIServer) Stop() {
|
||||||
s.srv.Close()
|
s.srv.Close()
|
||||||
if s.returnFunc != nil {
|
|
||||||
s.returnFunc()
|
|
||||||
s.returnFunc = nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Addr returns the current base URL for the running webserver.
|
// Addr returns the current base URL for the running webserver.
|
||||||
|
@ -545,19 +531,3 @@ func createStatus(status, message string, reason metav1.StatusReason, details *m
|
||||||
Code: code,
|
Code: code,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func httptestNewUnstartedServerWithPort(handler http.Handler, port int) *httptest.Server {
|
|
||||||
if port == 0 {
|
|
||||||
return httptest.NewUnstartedServer(handler)
|
|
||||||
}
|
|
||||||
addr := net.JoinHostPort("127.0.0.1", strconv.Itoa(port))
|
|
||||||
l, err := net.Listen("tcp", addr)
|
|
||||||
if err != nil {
|
|
||||||
panic(fmt.Sprintf("httptest: failed to listen on a port: %v", err))
|
|
||||||
}
|
|
||||||
|
|
||||||
return &httptest.Server{
|
|
||||||
Listener: l,
|
|
||||||
Config: &http.Server{Handler: handler},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -5,14 +5,14 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/hashicorp/consul/agent/consul/authmethod"
|
|
||||||
"github.com/hashicorp/consul/agent/structs"
|
|
||||||
"github.com/hashicorp/consul/internal/go-sso/oidcauth/oidcauthtest"
|
|
||||||
"github.com/hashicorp/consul/sdk/freeport"
|
|
||||||
"github.com/hashicorp/consul/sdk/testutil"
|
|
||||||
"github.com/hashicorp/go-hclog"
|
"github.com/hashicorp/go-hclog"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"gopkg.in/square/go-jose.v2/jwt"
|
"gopkg.in/square/go-jose.v2/jwt"
|
||||||
|
|
||||||
|
"github.com/hashicorp/consul/agent/consul/authmethod"
|
||||||
|
"github.com/hashicorp/consul/agent/structs"
|
||||||
|
"github.com/hashicorp/consul/internal/go-sso/oidcauth/oidcauthtest"
|
||||||
|
"github.com/hashicorp/consul/sdk/testutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestJWT_NewValidator(t *testing.T) {
|
func TestJWT_NewValidator(t *testing.T) {
|
||||||
|
@ -32,7 +32,7 @@ func TestJWT_NewValidator(t *testing.T) {
|
||||||
return method
|
return method
|
||||||
}
|
}
|
||||||
|
|
||||||
oidcServer := startTestServer(t)
|
oidcServer := oidcauthtest.Start(t)
|
||||||
|
|
||||||
// Note that we won't test ALL of the available config variations here.
|
// Note that we won't test ALL of the available config variations here.
|
||||||
// The go-sso library has exhaustive tests.
|
// The go-sso library has exhaustive tests.
|
||||||
|
@ -110,7 +110,7 @@ func TestJWT_ValidateLogin(t *testing.T) {
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
oidcServer := startTestServer(t)
|
oidcServer := oidcauthtest.Start(t)
|
||||||
pubKey, privKey := oidcServer.SigningKeys()
|
pubKey, privKey := oidcServer.SigningKeys()
|
||||||
|
|
||||||
cases := map[string]struct {
|
cases := map[string]struct {
|
||||||
|
@ -260,11 +260,3 @@ func kv(a ...string) map[string]string {
|
||||||
}
|
}
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
func startTestServer(t *testing.T) *oidcauthtest.Server {
|
|
||||||
ports := freeport.MustTake(1)
|
|
||||||
return oidcauthtest.Start(t, oidcauthtest.WithPort(
|
|
||||||
ports[0],
|
|
||||||
func() { freeport.Return(ports) },
|
|
||||||
))
|
|
||||||
}
|
|
||||||
|
|
|
@ -33,11 +33,7 @@ func testClientConfig(t *testing.T) (string, *Config) {
|
||||||
dir := testutil.TempDir(t, "consul")
|
dir := testutil.TempDir(t, "consul")
|
||||||
config := DefaultConfig()
|
config := DefaultConfig()
|
||||||
|
|
||||||
ports := freeport.MustTake(2)
|
ports := freeport.GetN(t, 2)
|
||||||
t.Cleanup(func() {
|
|
||||||
freeport.Return(ports)
|
|
||||||
})
|
|
||||||
|
|
||||||
config.Datacenter = "dc1"
|
config.Datacenter = "dc1"
|
||||||
config.DataDir = dir
|
config.DataDir = dir
|
||||||
config.NodeName = uniqueNodeName(t.Name())
|
config.NodeName = uniqueNodeName(t.Name())
|
||||||
|
|
|
@ -140,13 +140,10 @@ func TestOperator_RaftRemovePeerByAddress(t *testing.T) {
|
||||||
|
|
||||||
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
||||||
|
|
||||||
ports := freeport.MustTake(1)
|
|
||||||
defer freeport.Return(ports)
|
|
||||||
|
|
||||||
// Try to remove a peer that's not there.
|
// Try to remove a peer that's not there.
|
||||||
arg := structs.RaftRemovePeerRequest{
|
arg := structs.RaftRemovePeerRequest{
|
||||||
Datacenter: "dc1",
|
Datacenter: "dc1",
|
||||||
Address: raft.ServerAddress(fmt.Sprintf("127.0.0.1:%d", ports[0])),
|
Address: raft.ServerAddress(fmt.Sprintf("127.0.0.1:%d", freeport.GetOne(t))),
|
||||||
}
|
}
|
||||||
var reply struct{}
|
var reply struct{}
|
||||||
err := msgpackrpc.CallWithCodec(codec, "Operator.RaftRemovePeerByAddress", &arg, &reply)
|
err := msgpackrpc.CallWithCodec(codec, "Operator.RaftRemovePeerByAddress", &arg, &reply)
|
||||||
|
@ -263,10 +260,7 @@ func TestOperator_RaftRemovePeerByID(t *testing.T) {
|
||||||
|
|
||||||
// Add it manually to Raft.
|
// Add it manually to Raft.
|
||||||
{
|
{
|
||||||
ports := freeport.MustTake(1)
|
future := s1.raft.AddVoter(arg.ID, raft.ServerAddress(fmt.Sprintf("127.0.0.1:%d", freeport.GetOne(t))), 0, 0)
|
||||||
defer freeport.Return(ports)
|
|
||||||
|
|
||||||
future := s1.raft.AddVoter(arg.ID, raft.ServerAddress(fmt.Sprintf("127.0.0.1:%d", ports[0])), 0, 0)
|
|
||||||
if err := future.Error(); err != nil {
|
if err := future.Error(); err != nil {
|
||||||
t.Fatalf("err: %v", err)
|
t.Fatalf("err: %v", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -116,11 +116,7 @@ func testServerConfig(t *testing.T) (string, *Config) {
|
||||||
dir := testutil.TempDir(t, "consul")
|
dir := testutil.TempDir(t, "consul")
|
||||||
config := DefaultConfig()
|
config := DefaultConfig()
|
||||||
|
|
||||||
ports := freeport.MustTake(3)
|
ports := freeport.GetN(t, 3)
|
||||||
t.Cleanup(func() {
|
|
||||||
freeport.Return(ports)
|
|
||||||
})
|
|
||||||
|
|
||||||
config.NodeName = uniqueNodeName(t.Name())
|
config.NodeName = uniqueNodeName(t.Name())
|
||||||
config.Bootstrap = true
|
config.Bootstrap = true
|
||||||
config.Datacenter = "dc1"
|
config.Datacenter = "dc1"
|
||||||
|
@ -516,15 +512,11 @@ func TestServer_JoinWAN_SerfAllowedCIDRs(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func skipIfCannotBindToIP(t *testing.T, ip string) {
|
func skipIfCannotBindToIP(t *testing.T, ip string) {
|
||||||
ports := freeport.MustTake(1)
|
l, err := net.Listen("tcp", net.JoinHostPort(ip, "0"))
|
||||||
defer freeport.Return(ports)
|
|
||||||
|
|
||||||
addr := ipaddr.FormatAddressPort(ip, ports[0])
|
|
||||||
l, err := net.Listen("tcp", addr)
|
|
||||||
l.Close()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Skipf("Cannot bind on %s, to run on Mac OS: `sudo ifconfig lo0 alias %s up`", ip, ip)
|
t.Skipf("Cannot bind on %s, to run on Mac OS: `sudo ifconfig lo0 alias %s up`", ip, ip)
|
||||||
}
|
}
|
||||||
|
l.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestServer_LANReap(t *testing.T) {
|
func TestServer_LANReap(t *testing.T) {
|
||||||
|
@ -725,9 +717,8 @@ func TestServer_JoinWAN_viaMeshGateway(t *testing.T) {
|
||||||
|
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
gwPort := freeport.MustTake(1)
|
port := freeport.GetOne(t)
|
||||||
defer freeport.Return(gwPort)
|
gwAddr := ipaddr.FormatAddressPort("127.0.0.1", port)
|
||||||
gwAddr := ipaddr.FormatAddressPort("127.0.0.1", gwPort[0])
|
|
||||||
|
|
||||||
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
||||||
c.TLSConfig.Domain = "consul"
|
c.TLSConfig.Domain = "consul"
|
||||||
|
@ -813,7 +804,7 @@ func TestServer_JoinWAN_viaMeshGateway(t *testing.T) {
|
||||||
ID: "mesh-gateway",
|
ID: "mesh-gateway",
|
||||||
Service: "mesh-gateway",
|
Service: "mesh-gateway",
|
||||||
Meta: map[string]string{structs.MetaWANFederationKey: "1"},
|
Meta: map[string]string{structs.MetaWANFederationKey: "1"},
|
||||||
Port: gwPort[0],
|
Port: port,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -868,7 +859,7 @@ func TestServer_JoinWAN_viaMeshGateway(t *testing.T) {
|
||||||
ID: "mesh-gateway",
|
ID: "mesh-gateway",
|
||||||
Service: "mesh-gateway",
|
Service: "mesh-gateway",
|
||||||
Meta: map[string]string{structs.MetaWANFederationKey: "1"},
|
Meta: map[string]string{structs.MetaWANFederationKey: "1"},
|
||||||
Port: gwPort[0],
|
Port: port,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -885,7 +876,7 @@ func TestServer_JoinWAN_viaMeshGateway(t *testing.T) {
|
||||||
ID: "mesh-gateway",
|
ID: "mesh-gateway",
|
||||||
Service: "mesh-gateway",
|
Service: "mesh-gateway",
|
||||||
Meta: map[string]string{structs.MetaWANFederationKey: "1"},
|
Meta: map[string]string{structs.MetaWANFederationKey: "1"},
|
||||||
Port: gwPort[0],
|
Port: port,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -29,10 +28,7 @@ func useTLSForDcAlwaysTrue(_ string) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNewDialer_WithTLSWrapper(t *testing.T) {
|
func TestNewDialer_WithTLSWrapper(t *testing.T) {
|
||||||
ports := freeport.MustTake(1)
|
lis, err := net.Listen("tcp", "127.0.0.1:0")
|
||||||
defer freeport.Return(ports)
|
|
||||||
|
|
||||||
lis, err := net.Listen("tcp", net.JoinHostPort("127.0.0.1", strconv.Itoa(ports[0])))
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
t.Cleanup(logError(t, lis.Close))
|
t.Cleanup(logError(t, lis.Close))
|
||||||
|
|
||||||
|
@ -68,26 +64,18 @@ func TestNewDialer_WithTLSWrapper(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNewDialer_WithALPNWrapper(t *testing.T) {
|
func TestNewDialer_WithALPNWrapper(t *testing.T) {
|
||||||
ports := freeport.MustTake(3)
|
lis1, err := net.Listen("tcp", "127.0.0.1:0")
|
||||||
defer freeport.Return(ports)
|
|
||||||
|
|
||||||
var (
|
|
||||||
s1addr = ipaddr.FormatAddressPort("127.0.0.1", ports[0])
|
|
||||||
s2addr = ipaddr.FormatAddressPort("127.0.0.1", ports[1])
|
|
||||||
gwAddr = ipaddr.FormatAddressPort("127.0.0.1", ports[2])
|
|
||||||
)
|
|
||||||
|
|
||||||
lis1, err := net.Listen("tcp", s1addr)
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
t.Cleanup(logError(t, lis1.Close))
|
t.Cleanup(logError(t, lis1.Close))
|
||||||
|
|
||||||
lis2, err := net.Listen("tcp", s2addr)
|
lis2, err := net.Listen("tcp", "127.0.0.1:0")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
t.Cleanup(logError(t, lis2.Close))
|
t.Cleanup(logError(t, lis2.Close))
|
||||||
|
|
||||||
// Send all of the traffic to dc2's server
|
// Send all of the traffic to dc2's server
|
||||||
var p tcpproxy.Proxy
|
var p tcpproxy.Proxy
|
||||||
p.AddRoute(gwAddr, tcpproxy.To(s2addr))
|
gwAddr := ipaddr.FormatAddressPort("127.0.0.1", freeport.GetOne(t))
|
||||||
|
p.AddRoute(gwAddr, tcpproxy.To(lis2.Addr().String()))
|
||||||
p.AddStopACMESearch(gwAddr)
|
p.AddStopACMESearch(gwAddr)
|
||||||
require.NoError(t, p.Start())
|
require.NoError(t, p.Start())
|
||||||
defer func() {
|
defer func() {
|
||||||
|
@ -193,10 +181,7 @@ func TestNewDialer_IntegrationWithTLSEnabledHandler(t *testing.T) {
|
||||||
func TestNewDialer_IntegrationWithTLSEnabledHandler_viaMeshGateway(t *testing.T) {
|
func TestNewDialer_IntegrationWithTLSEnabledHandler_viaMeshGateway(t *testing.T) {
|
||||||
// if this test is failing because of expired certificates
|
// if this test is failing because of expired certificates
|
||||||
// use the procedure in test/CA-GENERATION.md
|
// use the procedure in test/CA-GENERATION.md
|
||||||
ports := freeport.MustTake(1)
|
gwAddr := ipaddr.FormatAddressPort("127.0.0.1", freeport.GetOne(t))
|
||||||
defer freeport.Return(ports)
|
|
||||||
|
|
||||||
gwAddr := ipaddr.FormatAddressPort("127.0.0.1", ports[0])
|
|
||||||
|
|
||||||
res := resolver.NewServerResolverBuilder(newConfig(t))
|
res := resolver.NewServerResolverBuilder(newConfig(t))
|
||||||
registerWithGRPC(t, res)
|
registerWithGRPC(t, res)
|
||||||
|
|
|
@ -6,7 +6,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"strconv"
|
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
@ -18,7 +17,6 @@ import (
|
||||||
"github.com/hashicorp/consul/agent/grpc/internal/testservice"
|
"github.com/hashicorp/consul/agent/grpc/internal/testservice"
|
||||||
"github.com/hashicorp/consul/agent/metadata"
|
"github.com/hashicorp/consul/agent/metadata"
|
||||||
"github.com/hashicorp/consul/agent/pool"
|
"github.com/hashicorp/consul/agent/pool"
|
||||||
"github.com/hashicorp/consul/sdk/freeport"
|
|
||||||
"github.com/hashicorp/consul/tlsutil"
|
"github.com/hashicorp/consul/tlsutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -47,12 +45,7 @@ func newTestServer(t *testing.T, name string, dc string, tlsConf *tlsutil.Config
|
||||||
testservice.RegisterSimpleServer(server, &simple{name: name, dc: dc})
|
testservice.RegisterSimpleServer(server, &simple{name: name, dc: dc})
|
||||||
})
|
})
|
||||||
|
|
||||||
ports := freeport.MustTake(1)
|
lis, err := net.Listen("tcp", "127.0.0.1:0")
|
||||||
t.Cleanup(func() {
|
|
||||||
freeport.Return(ports)
|
|
||||||
})
|
|
||||||
|
|
||||||
lis, err := net.Listen("tcp", net.JoinHostPort("127.0.0.1", strconv.Itoa(ports[0])))
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
rpc := &fakeRPCListener{t: t, handler: handler, tlsConf: tlsConf}
|
rpc := &fakeRPCListener{t: t, handler: handler, tlsConf: tlsConf}
|
||||||
|
|
|
@ -165,8 +165,7 @@ func (a *TestAgent) Start(t *testing.T) error {
|
||||||
Name: name,
|
Name: name,
|
||||||
})
|
})
|
||||||
|
|
||||||
portsConfig, returnPortsFn := randomPortsSource(a.UseTLS)
|
portsConfig := randomPortsSource(t, a.UseTLS)
|
||||||
t.Cleanup(returnPortsFn)
|
|
||||||
|
|
||||||
// Create NodeID outside the closure, so that it does not change
|
// Create NodeID outside the closure, so that it does not change
|
||||||
testHCLConfig := TestConfigHCL(NodeID())
|
testHCLConfig := TestConfigHCL(NodeID())
|
||||||
|
@ -378,8 +377,8 @@ func (a *TestAgent) consulConfig() *consul.Config {
|
||||||
// chance of port conflicts for concurrently executed test binaries.
|
// chance of port conflicts for concurrently executed test binaries.
|
||||||
// Instead of relying on one set of ports to be sufficient we retry
|
// Instead of relying on one set of ports to be sufficient we retry
|
||||||
// starting the agent with different ports on port conflict.
|
// starting the agent with different ports on port conflict.
|
||||||
func randomPortsSource(tls bool) (data string, returnPortsFn func()) {
|
func randomPortsSource(t *testing.T, tls bool) string {
|
||||||
ports := freeport.MustTake(7)
|
ports := freeport.GetN(t, 7)
|
||||||
|
|
||||||
var http, https int
|
var http, https int
|
||||||
if tls {
|
if tls {
|
||||||
|
@ -400,7 +399,7 @@ func randomPortsSource(tls bool) (data string, returnPortsFn func()) {
|
||||||
server = ` + strconv.Itoa(ports[5]) + `
|
server = ` + strconv.Itoa(ports[5]) + `
|
||||||
grpc = ` + strconv.Itoa(ports[6]) + `
|
grpc = ` + strconv.Itoa(ports[6]) + `
|
||||||
}
|
}
|
||||||
`, func() { freeport.Return(ports) }
|
`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NodeID() string {
|
func NodeID() string {
|
||||||
|
|
|
@ -8,18 +8,18 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/mitchellh/cli"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"gopkg.in/square/go-jose.v2/jwt"
|
||||||
|
|
||||||
"github.com/hashicorp/consul/agent"
|
"github.com/hashicorp/consul/agent"
|
||||||
"github.com/hashicorp/consul/agent/consul/authmethod/kubeauth"
|
"github.com/hashicorp/consul/agent/consul/authmethod/kubeauth"
|
||||||
"github.com/hashicorp/consul/agent/consul/authmethod/testauth"
|
"github.com/hashicorp/consul/agent/consul/authmethod/testauth"
|
||||||
"github.com/hashicorp/consul/api"
|
"github.com/hashicorp/consul/api"
|
||||||
"github.com/hashicorp/consul/command/acl"
|
"github.com/hashicorp/consul/command/acl"
|
||||||
"github.com/hashicorp/consul/internal/go-sso/oidcauth/oidcauthtest"
|
"github.com/hashicorp/consul/internal/go-sso/oidcauth/oidcauthtest"
|
||||||
"github.com/hashicorp/consul/sdk/freeport"
|
|
||||||
"github.com/hashicorp/consul/sdk/testutil"
|
"github.com/hashicorp/consul/sdk/testutil"
|
||||||
"github.com/hashicorp/consul/testrpc"
|
"github.com/hashicorp/consul/testrpc"
|
||||||
"github.com/mitchellh/cli"
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
"gopkg.in/square/go-jose.v2/jwt"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLoginCommand_noTabs(t *testing.T) {
|
func TestLoginCommand_noTabs(t *testing.T) {
|
||||||
|
@ -352,7 +352,7 @@ func TestLoginCommand_jwt(t *testing.T) {
|
||||||
bearerTokenFile := filepath.Join(testDir, "bearer.token")
|
bearerTokenFile := filepath.Join(testDir, "bearer.token")
|
||||||
|
|
||||||
// spin up a fake oidc server
|
// spin up a fake oidc server
|
||||||
oidcServer := startSSOTestServer(t)
|
oidcServer := oidcauthtest.Start(t)
|
||||||
pubKey, privKey := oidcServer.SigningKeys()
|
pubKey, privKey := oidcServer.SigningKeys()
|
||||||
|
|
||||||
type mConfig = map[string]interface{}
|
type mConfig = map[string]interface{}
|
||||||
|
@ -470,11 +470,3 @@ func TestLoginCommand_jwt(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func startSSOTestServer(t *testing.T) *oidcauthtest.Server {
|
|
||||||
ports := freeport.MustTake(1)
|
|
||||||
return oidcauthtest.Start(t, oidcauthtest.WithPort(
|
|
||||||
ports[0],
|
|
||||||
func() { freeport.Return(ports) },
|
|
||||||
))
|
|
||||||
}
|
|
||||||
|
|
|
@ -5,19 +5,20 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/hashicorp/consul/agent"
|
|
||||||
"github.com/hashicorp/consul/api"
|
|
||||||
"github.com/hashicorp/consul/lib"
|
|
||||||
"github.com/hashicorp/consul/sdk/testutil"
|
|
||||||
"github.com/hashicorp/consul/sdk/testutil/retry"
|
|
||||||
"github.com/mitchellh/cli"
|
"github.com/mitchellh/cli"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"github.com/hashicorp/consul/agent"
|
||||||
|
"github.com/hashicorp/consul/api"
|
||||||
|
"github.com/hashicorp/consul/sdk/testutil"
|
||||||
|
"github.com/hashicorp/consul/sdk/testutil/retry"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSnapshotSaveCommand_noTabs(t *testing.T) {
|
func TestSnapshotSaveCommand_noTabs(t *testing.T) {
|
||||||
|
@ -138,7 +139,7 @@ func TestSnapshotSaveCommand_TruncatedStream(t *testing.T) {
|
||||||
var fakeResult atomic.Value
|
var fakeResult atomic.Value
|
||||||
|
|
||||||
// Run a fake webserver to pretend to be the snapshot API.
|
// Run a fake webserver to pretend to be the snapshot API.
|
||||||
fakeAddr := lib.StartTestServer(t, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||||
if req.URL.Path != "/v1/snapshot" {
|
if req.URL.Path != "/v1/snapshot" {
|
||||||
w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
return
|
return
|
||||||
|
@ -157,10 +158,11 @@ func TestSnapshotSaveCommand_TruncatedStream(t *testing.T) {
|
||||||
data := raw.([]byte)
|
data := raw.([]byte)
|
||||||
_, _ = w.Write(data)
|
_, _ = w.Write(data)
|
||||||
}))
|
}))
|
||||||
|
t.Cleanup(srv.Close)
|
||||||
|
|
||||||
// Wait until the server is actually listening.
|
// Wait until the server is actually listening.
|
||||||
retry.Run(t, func(r *retry.R) {
|
retry.Run(t, func(r *retry.R) {
|
||||||
resp, err := http.Get("http://" + fakeAddr + "/not-real")
|
resp, err := srv.Client().Get(srv.URL + "/not-real")
|
||||||
require.NoError(r, err)
|
require.NoError(r, err)
|
||||||
require.Equal(r, http.StatusNotFound, resp.StatusCode)
|
require.Equal(r, http.StatusNotFound, resp.StatusCode)
|
||||||
})
|
})
|
||||||
|
@ -179,7 +181,7 @@ func TestSnapshotSaveCommand_TruncatedStream(t *testing.T) {
|
||||||
|
|
||||||
file := filepath.Join(dir, "backup.tgz")
|
file := filepath.Join(dir, "backup.tgz")
|
||||||
args := []string{
|
args := []string{
|
||||||
"-http-addr=" + fakeAddr, // point to the fake
|
"-http-addr=" + srv.Listener.Addr().String(), // point to the fake
|
||||||
file,
|
file,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -112,15 +112,13 @@ func TestPublicListener(t *testing.T) {
|
||||||
// Can't enable t.Parallel since we rely on the global metrics instance.
|
// Can't enable t.Parallel since we rely on the global metrics instance.
|
||||||
|
|
||||||
ca := agConnect.TestCA(t, nil)
|
ca := agConnect.TestCA(t, nil)
|
||||||
ports := freeport.MustTake(1)
|
|
||||||
defer freeport.Return(ports)
|
|
||||||
|
|
||||||
testApp := NewTestTCPServer(t)
|
testApp := NewTestTCPServer(t)
|
||||||
defer testApp.Close()
|
defer testApp.Close()
|
||||||
|
|
||||||
|
port := freeport.GetOne(t)
|
||||||
cfg := PublicListenerConfig{
|
cfg := PublicListenerConfig{
|
||||||
BindAddress: "127.0.0.1",
|
BindAddress: "127.0.0.1",
|
||||||
BindPort: ports[0],
|
BindPort: port,
|
||||||
LocalServiceAddress: testApp.Addr().String(),
|
LocalServiceAddress: testApp.Addr().String(),
|
||||||
HandshakeTimeoutMs: 100,
|
HandshakeTimeoutMs: 100,
|
||||||
LocalConnectTimeoutMs: 100,
|
LocalConnectTimeoutMs: 100,
|
||||||
|
@ -144,7 +142,7 @@ func TestPublicListener(t *testing.T) {
|
||||||
// Proxy and backend are running, play the part of a TLS client using same
|
// Proxy and backend are running, play the part of a TLS client using same
|
||||||
// cert for now.
|
// cert for now.
|
||||||
conn, err := svc.Dial(context.Background(), &connect.StaticResolver{
|
conn, err := svc.Dial(context.Background(), &connect.StaticResolver{
|
||||||
Addr: TestLocalAddr(ports[0]),
|
Addr: TestLocalAddr(port),
|
||||||
CertURI: agConnect.TestSpiffeIDService(t, "db"),
|
CertURI: agConnect.TestSpiffeIDService(t, "db"),
|
||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -166,9 +164,6 @@ func TestUpstreamListener(t *testing.T) {
|
||||||
// Can't enable t.Parallel since we rely on the global metrics instance.
|
// Can't enable t.Parallel since we rely on the global metrics instance.
|
||||||
|
|
||||||
ca := agConnect.TestCA(t, nil)
|
ca := agConnect.TestCA(t, nil)
|
||||||
ports := freeport.MustTake(1)
|
|
||||||
defer freeport.Return(ports)
|
|
||||||
|
|
||||||
// Run a test server that we can dial.
|
// Run a test server that we can dial.
|
||||||
testSvr := connect.NewTestServer(t, "db", ca)
|
testSvr := connect.NewTestServer(t, "db", ca)
|
||||||
go func() {
|
go func() {
|
||||||
|
@ -184,7 +179,7 @@ func TestUpstreamListener(t *testing.T) {
|
||||||
DestinationName: "db",
|
DestinationName: "db",
|
||||||
Config: map[string]interface{}{"connect_timeout_ms": 100},
|
Config: map[string]interface{}{"connect_timeout_ms": 100},
|
||||||
LocalBindAddress: "localhost",
|
LocalBindAddress: "localhost",
|
||||||
LocalBindPort: ports[0],
|
LocalBindPort: freeport.GetOne(t),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setup metrics to test they are recorded
|
// Setup metrics to test they are recorded
|
||||||
|
|
|
@ -27,8 +27,7 @@ func TestProxy_public(t *testing.T) {
|
||||||
t.Skip("too slow for testing.Short")
|
t.Skip("too slow for testing.Short")
|
||||||
}
|
}
|
||||||
|
|
||||||
ports := freeport.MustTake(2)
|
ports := freeport.GetN(t, 2)
|
||||||
defer freeport.Return(ports)
|
|
||||||
|
|
||||||
a := agent.NewTestAgent(t, "")
|
a := agent.NewTestAgent(t, "")
|
||||||
defer a.Shutdown()
|
defer a.Shutdown()
|
||||||
|
|
|
@ -8,10 +8,10 @@ import (
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/hashicorp/consul/connect"
|
|
||||||
"github.com/hashicorp/consul/sdk/freeport"
|
|
||||||
"github.com/mitchellh/go-testing-interface"
|
"github.com/mitchellh/go-testing-interface"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
"github.com/hashicorp/consul/connect"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestLocalAddr makes a localhost address on the given port
|
// TestLocalAddr makes a localhost address on the given port
|
||||||
|
@ -24,24 +24,17 @@ type TestTCPServer struct {
|
||||||
l net.Listener
|
l net.Listener
|
||||||
stopped int32
|
stopped int32
|
||||||
accepted, closed, active int32
|
accepted, closed, active int32
|
||||||
returnPortsFn func()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTestTCPServer opens as a listening socket on the given address and returns
|
// NewTestTCPServer opens as a listening socket on the given address and returns
|
||||||
// a TestTCPServer serving requests to it. The server is already started and can
|
// a TestTCPServer serving requests to it. The server is already started and can
|
||||||
// be stopped by calling Close().
|
// be stopped by calling Close().
|
||||||
func NewTestTCPServer(t testing.T) *TestTCPServer {
|
func NewTestTCPServer(t testing.T) *TestTCPServer {
|
||||||
ports := freeport.MustTake(1)
|
l, err := net.Listen("tcp", "127.0.0.1:0")
|
||||||
addr := TestLocalAddr(ports[0])
|
|
||||||
|
|
||||||
l, err := net.Listen("tcp", addr)
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
log.Printf("test tcp server listening on %s", addr)
|
log.Printf("test tcp server listening on %s", l.Addr())
|
||||||
s := &TestTCPServer{
|
s := &TestTCPServer{l: l}
|
||||||
l: l,
|
|
||||||
returnPortsFn: func() { freeport.Return(ports) },
|
|
||||||
}
|
|
||||||
go s.accept()
|
go s.accept()
|
||||||
|
|
||||||
return s
|
return s
|
||||||
|
@ -53,10 +46,6 @@ func (s *TestTCPServer) Close() {
|
||||||
if s.l != nil {
|
if s.l != nil {
|
||||||
s.l.Close()
|
s.l.Close()
|
||||||
}
|
}
|
||||||
if s.returnPortsFn != nil {
|
|
||||||
s.returnPortsFn()
|
|
||||||
s.returnPortsFn = nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Addr returns the address that this server is listening on.
|
// Addr returns the address that this server is listening on.
|
||||||
|
|
|
@ -97,7 +97,6 @@ type TestServer struct {
|
||||||
Listening chan struct{}
|
Listening chan struct{}
|
||||||
|
|
||||||
l net.Listener
|
l net.Listener
|
||||||
returnPortsFn func()
|
|
||||||
stopFlag int32
|
stopFlag int32
|
||||||
stopChan chan struct{}
|
stopChan chan struct{}
|
||||||
}
|
}
|
||||||
|
@ -105,15 +104,13 @@ type TestServer struct {
|
||||||
// NewTestServer returns a TestServer. It should be closed when test is
|
// NewTestServer returns a TestServer. It should be closed when test is
|
||||||
// complete.
|
// complete.
|
||||||
func NewTestServer(t testing.T, service string, ca *structs.CARoot) *TestServer {
|
func NewTestServer(t testing.T, service string, ca *structs.CARoot) *TestServer {
|
||||||
ports := freeport.MustTake(1)
|
|
||||||
return &TestServer{
|
return &TestServer{
|
||||||
Service: service,
|
Service: service,
|
||||||
CA: ca,
|
CA: ca,
|
||||||
stopChan: make(chan struct{}),
|
stopChan: make(chan struct{}),
|
||||||
TLSCfg: TestTLSConfig(t, service, ca),
|
TLSCfg: TestTLSConfig(t, service, ca),
|
||||||
Addr: fmt.Sprintf("127.0.0.1:%d", ports[0]),
|
Addr: fmt.Sprintf("127.0.0.1:%d", freeport.GetOne(t)),
|
||||||
Listening: make(chan struct{}),
|
Listening: make(chan struct{}),
|
||||||
returnPortsFn: func() { freeport.Return(ports) },
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -190,10 +187,6 @@ func (s *TestServer) Close() error {
|
||||||
if s.l != nil {
|
if s.l != nil {
|
||||||
s.l.Close()
|
s.l.Close()
|
||||||
}
|
}
|
||||||
if s.returnPortsFn != nil {
|
|
||||||
s.returnPortsFn()
|
|
||||||
s.returnPortsFn = nil
|
|
||||||
}
|
|
||||||
close(s.stopChan)
|
close(s.stopChan)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -24,11 +24,11 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/hashicorp/consul/internal/go-sso/oidcauth/internal/strutil"
|
|
||||||
"github.com/mitchellh/go-testing-interface"
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"gopkg.in/square/go-jose.v2"
|
"gopkg.in/square/go-jose.v2"
|
||||||
"gopkg.in/square/go-jose.v2/jwt"
|
"gopkg.in/square/go-jose.v2/jwt"
|
||||||
|
|
||||||
|
"github.com/hashicorp/consul/internal/go-sso/oidcauth/internal/strutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Server is local server the mocks the endpoints used by the OIDC and
|
// Server is local server the mocks the endpoints used by the OIDC and
|
||||||
|
@ -54,25 +54,17 @@ type Server struct {
|
||||||
disableUserInfo bool
|
disableUserInfo bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type startOption struct {
|
type TestingT interface {
|
||||||
port int
|
require.TestingT
|
||||||
returnFunc func()
|
Helper()
|
||||||
}
|
Cleanup(func())
|
||||||
|
|
||||||
// WithPort is a option for Start that lets the caller control the port
|
|
||||||
// allocation. The returnFunc parameter is used when the provider is stopped to
|
|
||||||
// return the port in whatever bookkeeping system the caller wants to use.
|
|
||||||
func WithPort(port int, returnFunc func()) startOption {
|
|
||||||
return startOption{
|
|
||||||
port: port,
|
|
||||||
returnFunc: returnFunc,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start creates a disposable Server. If the port provided is
|
// Start creates a disposable Server. If the port provided is
|
||||||
// zero it will bind to a random free port, otherwise the provided port is
|
// zero it will bind to a random free port, otherwise the provided port is
|
||||||
// used.
|
// used.
|
||||||
func Start(t testing.T, options ...startOption) *Server {
|
func Start(t TestingT) *Server {
|
||||||
|
t.Helper()
|
||||||
s := &Server{
|
s := &Server{
|
||||||
allowedRedirectURIs: []string{
|
allowedRedirectURIs: []string{
|
||||||
"https://example.com",
|
"https://example.com",
|
||||||
|
@ -89,23 +81,9 @@ func Start(t testing.T, options ...startOption) *Server {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
s.jwks = jwks
|
s.jwks = jwks
|
||||||
|
|
||||||
var (
|
s.httpServer = httptest.NewUnstartedServer(s)
|
||||||
port int
|
|
||||||
returnFunc func()
|
|
||||||
)
|
|
||||||
for _, option := range options {
|
|
||||||
if option.port > 0 {
|
|
||||||
port = option.port
|
|
||||||
returnFunc = option.returnFunc
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
s.httpServer = httptestNewUnstartedServerWithPort(s, port)
|
|
||||||
s.httpServer.Config.ErrorLog = log.New(ioutil.Discard, "", 0)
|
s.httpServer.Config.ErrorLog = log.New(ioutil.Discard, "", 0)
|
||||||
s.httpServer.StartTLS()
|
s.httpServer.StartTLS()
|
||||||
if returnFunc != nil {
|
|
||||||
t.Cleanup(returnFunc)
|
|
||||||
}
|
|
||||||
t.Cleanup(s.httpServer.Close)
|
t.Cleanup(s.httpServer.Close)
|
||||||
|
|
||||||
cert := s.httpServer.Certificate()
|
cert := s.httpServer.Certificate()
|
||||||
|
|
|
@ -1,36 +0,0 @@
|
||||||
package lib
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/hashicorp/consul/ipaddr"
|
|
||||||
"github.com/hashicorp/consul/sdk/freeport"
|
|
||||||
"github.com/mitchellh/go-testing-interface"
|
|
||||||
)
|
|
||||||
|
|
||||||
// StartTestServer fires up a web server on a random unused port to serve the
|
|
||||||
// given handler body. The address it is listening on is returned. When the
|
|
||||||
// test case terminates the server will be stopped via cleanup functions.
|
|
||||||
//
|
|
||||||
// We can't directly use httptest.Server here because that only thinks a port
|
|
||||||
// is free if it's not bound. Consul tests frequently reserve ports via
|
|
||||||
// `sdk/freeport` so you can have one part of the test try to use a port and
|
|
||||||
// _know_ nothing is listening. If you simply assumed unbound ports were free
|
|
||||||
// you'd end up with test cross-talk and weirdness.
|
|
||||||
func StartTestServer(t testing.T, handler http.Handler) string {
|
|
||||||
ports := freeport.MustTake(1)
|
|
||||||
t.Cleanup(func() {
|
|
||||||
freeport.Return(ports)
|
|
||||||
})
|
|
||||||
|
|
||||||
addr := ipaddr.FormatAddressPort("127.0.0.1", ports[0])
|
|
||||||
|
|
||||||
server := &http.Server{Addr: addr, Handler: handler}
|
|
||||||
t.Cleanup(func() {
|
|
||||||
server.Close()
|
|
||||||
})
|
|
||||||
|
|
||||||
go server.ListenAndServe()
|
|
||||||
|
|
||||||
return addr
|
|
||||||
}
|
|
|
@ -1,5 +1,16 @@
|
||||||
// Package freeport provides a helper for allocating free ports across multiple
|
// Package freeport provides a helper for reserving free TCP ports across multiple
|
||||||
// processes on the same machine.
|
// processes on the same machine. Each process reserves a block of ports outside
|
||||||
|
// the ephemeral port range. Tests can request one of these reserved ports
|
||||||
|
// and freeport will ensure that no other test uses that port until it is returned
|
||||||
|
// to freeport.
|
||||||
|
//
|
||||||
|
// Freeport is particularly useful when the code being tested does not accept
|
||||||
|
// a net.Listener. Any code that accepts a net.Listener (or uses net/http/httptest.Server)
|
||||||
|
// can use port 0 (ex: 127.0.0.1:0) to find an unused ephemeral port that will
|
||||||
|
// not conflict.
|
||||||
|
//
|
||||||
|
// Any code that does not accept a net.Listener or can not bind directly to port
|
||||||
|
// zero should use freeport to find an unused port.
|
||||||
package freeport
|
package freeport
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -11,8 +22,6 @@ import (
|
||||||
"runtime"
|
"runtime"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/mitchellh/go-testing-interface"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -251,6 +260,8 @@ func alloc() (int, net.Listener) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// MustTake is the same as Take except it panics on error.
|
// MustTake is the same as Take except it panics on error.
|
||||||
|
//
|
||||||
|
// Deprecated: Use GetN or GetOne instead.
|
||||||
func MustTake(n int) (ports []int) {
|
func MustTake(n int) (ports []int) {
|
||||||
ports, err := Take(n)
|
ports, err := Take(n)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -259,10 +270,12 @@ func MustTake(n int) (ports []int) {
|
||||||
return ports
|
return ports
|
||||||
}
|
}
|
||||||
|
|
||||||
// Take returns a list of free ports from the allocated port block. It is safe
|
// Take returns a list of free ports from the reserved port block. It is safe
|
||||||
// to call this method concurrently. Ports have been tested to be available on
|
// to call this method concurrently. Ports have been tested to be available on
|
||||||
// 127.0.0.1 TCP but there is no guarantee that they will remain free in the
|
// 127.0.0.1 TCP but there is no guarantee that they will remain free in the
|
||||||
// future.
|
// future.
|
||||||
|
//
|
||||||
|
// Most callers should prefer GetN or GetOne.
|
||||||
func Take(n int) (ports []int, err error) {
|
func Take(n int) (ports []int, err error) {
|
||||||
if n <= 0 {
|
if n <= 0 {
|
||||||
return nil, fmt.Errorf("freeport: cannot take %d ports", n)
|
return nil, fmt.Errorf("freeport: cannot take %d ports", n)
|
||||||
|
@ -381,11 +394,44 @@ func logf(severity string, format string, a ...interface{}) {
|
||||||
fmt.Fprintf(os.Stderr, "["+severity+"] freeport: "+format+"\n", a...)
|
fmt.Fprintf(os.Stderr, "["+severity+"] freeport: "+format+"\n", a...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestingT is the minimal set of methods implemented by *testing.T that are
|
||||||
|
// used by functions in freelist.
|
||||||
|
//
|
||||||
|
// In the future new methods may be added to this interface, but those methods
|
||||||
|
// should always be implemented by *testing.T
|
||||||
|
type TestingT interface {
|
||||||
|
Helper()
|
||||||
|
Fatalf(format string, args ...interface{})
|
||||||
|
Cleanup(func())
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetN returns n free ports from the reserved port block, and returns the
|
||||||
|
// ports to the pool when the test ends. See Take for more details.
|
||||||
|
func GetN(t TestingT, n int) []int {
|
||||||
|
t.Helper()
|
||||||
|
ports, err := Take(n)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to take %v ports: %w", n, err)
|
||||||
|
}
|
||||||
|
t.Cleanup(func() {
|
||||||
|
Return(ports)
|
||||||
|
})
|
||||||
|
return ports
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetOne returns a single free port from the reserved port block, and returns the
|
||||||
|
// port to the pool when the test ends. See Take for more details.
|
||||||
|
// Use GetN if more than a single port is required.
|
||||||
|
func GetOne(t TestingT) int {
|
||||||
|
t.Helper()
|
||||||
|
return GetN(t, 1)[0]
|
||||||
|
}
|
||||||
|
|
||||||
// Deprecated: Please use Take/Return calls instead.
|
// Deprecated: Please use Take/Return calls instead.
|
||||||
func Get(n int) (ports []int) { return MustTake(n) }
|
func Get(n int) (ports []int) { return MustTake(n) }
|
||||||
|
|
||||||
// Deprecated: Please use Take/Return calls instead.
|
// Deprecated: Please use Take/Return calls instead.
|
||||||
func GetT(t testing.T, n int) (ports []int) { return MustTake(n) }
|
func GetT(t TestingT, n int) (ports []int) { return MustTake(n) }
|
||||||
|
|
||||||
// Deprecated: Please use Take/Return calls instead.
|
// Deprecated: Please use Take/Return calls instead.
|
||||||
func Free(n int) (ports []int, err error) { return MustTake(n), nil }
|
func Free(n int) (ports []int, err error) { return MustTake(n), nil }
|
||||||
|
|
|
@ -29,11 +29,12 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/hashicorp/consul/sdk/freeport"
|
|
||||||
"github.com/hashicorp/consul/sdk/testutil/retry"
|
|
||||||
"github.com/hashicorp/go-cleanhttp"
|
"github.com/hashicorp/go-cleanhttp"
|
||||||
"github.com/hashicorp/go-uuid"
|
"github.com/hashicorp/go-uuid"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
|
"github.com/hashicorp/consul/sdk/freeport"
|
||||||
|
"github.com/hashicorp/consul/sdk/testutil/retry"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestPerformanceConfig configures the performance parameters.
|
// TestPerformanceConfig configures the performance parameters.
|
||||||
|
@ -142,7 +143,11 @@ func defaultServerConfig(t TestingTB) *TestServerConfig {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
ports := freeport.MustTake(6)
|
ports, err := freeport.Take(6)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to take ports: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
logBuffer := NewLogBuffer(t)
|
logBuffer := NewLogBuffer(t)
|
||||||
|
|
||||||
return &TestServerConfig{
|
return &TestServerConfig{
|
||||||
|
|
|
@ -8,4 +8,5 @@ type TestingTB interface {
|
||||||
Failed() bool
|
Failed() bool
|
||||||
Logf(format string, args ...interface{})
|
Logf(format string, args ...interface{})
|
||||||
Name() string
|
Name() string
|
||||||
|
Fatalf(fmt string, args ...interface{})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue