diff --git a/agent/agent_test.go b/agent/agent_test.go index a7e39e560..29eb03cf8 100644 --- a/agent/agent_test.go +++ b/agent/agent_test.go @@ -308,7 +308,7 @@ func TestAgent_HTTPMaxHeaderBytes(t *testing.T) { }, RuntimeConfig: &config.RuntimeConfig{ HTTPAddrs: []net.Addr{ - &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: freeport.Port(t)}, + &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: freeport.GetOne(t)}, }, HTTPMaxHeaderBytes: tt.maxHeaderBytes, }, @@ -4679,7 +4679,7 @@ func TestAgent_JoinWAN_viaMeshGateway(t *testing.T) { t.Parallel() - port := freeport.Port(t) + port := freeport.GetOne(t) gwAddr := ipaddr.FormatAddressPort("127.0.0.1", port) // Due to some ordering, we'll have to manually configure these ports in diff --git a/agent/consul/operator_raft_endpoint_test.go b/agent/consul/operator_raft_endpoint_test.go index 63db469cf..778dcbf0f 100644 --- a/agent/consul/operator_raft_endpoint_test.go +++ b/agent/consul/operator_raft_endpoint_test.go @@ -143,7 +143,7 @@ func TestOperator_RaftRemovePeerByAddress(t *testing.T) { // Try to remove a peer that's not there. arg := structs.RaftRemovePeerRequest{ Datacenter: "dc1", - Address: raft.ServerAddress(fmt.Sprintf("127.0.0.1:%d", freeport.Port(t))), + Address: raft.ServerAddress(fmt.Sprintf("127.0.0.1:%d", freeport.GetOne(t))), } var reply struct{} err := msgpackrpc.CallWithCodec(codec, "Operator.RaftRemovePeerByAddress", &arg, &reply) @@ -260,7 +260,7 @@ func TestOperator_RaftRemovePeerByID(t *testing.T) { // Add it manually to Raft. { - future := s1.raft.AddVoter(arg.ID, raft.ServerAddress(fmt.Sprintf("127.0.0.1:%d", freeport.Port(t))), 0, 0) + future := s1.raft.AddVoter(arg.ID, raft.ServerAddress(fmt.Sprintf("127.0.0.1:%d", freeport.GetOne(t))), 0, 0) if err := future.Error(); err != nil { t.Fatalf("err: %v", err) } diff --git a/agent/consul/server_test.go b/agent/consul/server_test.go index 5b1bdabfd..bf68d8696 100644 --- a/agent/consul/server_test.go +++ b/agent/consul/server_test.go @@ -717,7 +717,7 @@ func TestServer_JoinWAN_viaMeshGateway(t *testing.T) { t.Parallel() - port := freeport.Port(t) + port := freeport.GetOne(t) gwAddr := ipaddr.FormatAddressPort("127.0.0.1", port) dir1, s1 := testServerWithConfig(t, func(c *Config) { diff --git a/agent/grpc/client_test.go b/agent/grpc/client_test.go index 8baf751de..3fa90e218 100644 --- a/agent/grpc/client_test.go +++ b/agent/grpc/client_test.go @@ -74,7 +74,7 @@ func TestNewDialer_WithALPNWrapper(t *testing.T) { // Send all of the traffic to dc2's server var p tcpproxy.Proxy - gwAddr := ipaddr.FormatAddressPort("127.0.0.1", freeport.Port(t)) + gwAddr := ipaddr.FormatAddressPort("127.0.0.1", freeport.GetOne(t)) p.AddRoute(gwAddr, tcpproxy.To(lis2.Addr().String())) p.AddStopACMESearch(gwAddr) require.NoError(t, p.Start()) @@ -181,7 +181,7 @@ func TestNewDialer_IntegrationWithTLSEnabledHandler(t *testing.T) { func TestNewDialer_IntegrationWithTLSEnabledHandler_viaMeshGateway(t *testing.T) { // if this test is failing because of expired certificates // use the procedure in test/CA-GENERATION.md - gwAddr := ipaddr.FormatAddressPort("127.0.0.1", freeport.Port(t)) + gwAddr := ipaddr.FormatAddressPort("127.0.0.1", freeport.GetOne(t)) res := resolver.NewServerResolverBuilder(newConfig(t)) registerWithGRPC(t, res) diff --git a/connect/proxy/listener_test.go b/connect/proxy/listener_test.go index 8e5a07f32..8a3006b69 100644 --- a/connect/proxy/listener_test.go +++ b/connect/proxy/listener_test.go @@ -115,7 +115,7 @@ func TestPublicListener(t *testing.T) { testApp := NewTestTCPServer(t) defer testApp.Close() - port := freeport.Port(t) + port := freeport.GetOne(t) cfg := PublicListenerConfig{ BindAddress: "127.0.0.1", BindPort: port, @@ -179,7 +179,7 @@ func TestUpstreamListener(t *testing.T) { DestinationName: "db", Config: map[string]interface{}{"connect_timeout_ms": 100}, LocalBindAddress: "localhost", - LocalBindPort: freeport.Port(t), + LocalBindPort: freeport.GetOne(t), } // Setup metrics to test they are recorded diff --git a/connect/testing.go b/connect/testing.go index 180b15d6c..d054c0dee 100644 --- a/connect/testing.go +++ b/connect/testing.go @@ -109,7 +109,7 @@ func NewTestServer(t testing.T, service string, ca *structs.CARoot) *TestServer CA: ca, stopChan: make(chan struct{}), TLSCfg: TestTLSConfig(t, service, ca), - Addr: fmt.Sprintf("127.0.0.1:%d", freeport.Port(t)), + Addr: fmt.Sprintf("127.0.0.1:%d", freeport.GetOne(t)), Listening: make(chan struct{}), } } diff --git a/sdk/freeport/freeport.go b/sdk/freeport/freeport.go index b978d2ee2..e35d662ad 100644 --- a/sdk/freeport/freeport.go +++ b/sdk/freeport/freeport.go @@ -261,7 +261,7 @@ func alloc() (int, net.Listener) { // MustTake is the same as Take except it panics on error. // -// Deprecated: Use GetN or Port instead. +// Deprecated: Use GetN or GetOne instead. func MustTake(n int) (ports []int) { ports, err := Take(n) if err != nil { @@ -275,7 +275,7 @@ func MustTake(n int) (ports []int) { // 127.0.0.1 TCP but there is no guarantee that they will remain free in the // future. // -// Most callers should prefer GetN or Port. +// Most callers should prefer GetN or GetOne. func Take(n int) (ports []int, err error) { if n <= 0 { return nil, fmt.Errorf("freeport: cannot take %d ports", n) @@ -419,10 +419,10 @@ func GetN(t TestingT, n int) []int { return ports } -// Port returns a single free port from the reserved port block, and returns the +// 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 Port(t TestingT) int { +func GetOne(t TestingT) int { t.Helper() return GetN(t, 1)[0] }