Skip to content

fix: test: use monotonical port numbers #13999

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -834,12 +834,12 @@ func TestAgent_TCPRemoteForwarding(t *testing.T) {
sshClient := setupAgentSSHClient(ctx, t)

localhost := netip.MustParseAddr("127.0.0.1")
var randomPort uint16
var port uint16
var ll net.Listener
var err error
for {
randomPort = testutil.RandomPortNoListen(t)
addr := net.TCPAddrFromAddrPort(netip.AddrPortFrom(localhost, randomPort))
port = testutil.EphemeralPortNoListen()
addr := net.TCPAddrFromAddrPort(netip.AddrPortFrom(localhost, port))
ll, err = sshClient.ListenTCP(addr)
if err != nil {
t.Logf("error remote forwarding: %s", err.Error())
Expand All @@ -855,7 +855,7 @@ func TestAgent_TCPRemoteForwarding(t *testing.T) {
defer ll.Close()
go echoOnce(t, ll)

conn, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", randomPort))
conn, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", port))
require.NoError(t, err)
defer conn.Close()
requireEcho(t, conn)
Expand Down
4 changes: 1 addition & 3 deletions enterprise/cli/provisionerdaemons_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,7 @@ func TestProvisionerDaemon_SessionToken(t *testing.T) {

//nolint:paralleltest,tparallel // Prometheus endpoint tends to fail with `bind: address already in use`.
func TestProvisionerDaemon_PrometheusEnabled(t *testing.T) {
t.Skip("Flaky test - see https://github.com/coder/coder/issues/13931")

prometheusPort := testutil.RandomPortNoListen(t)
prometheusPort := testutil.EphemeralPortNoListen()

// Configure CLI client
client, admin := coderdenttest.New(t, &coderdenttest.Options{
Expand Down
32 changes: 17 additions & 15 deletions testutil/port.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package testutil

import (
"math/rand"
"net"
"sync"
"testing"
"time"

"github.com/stretchr/testify/require"
)

const (
// Overlap of windows, linux in https://en.wikipedia.org/wiki/Ephemeral_port
minPort = 49152
maxPort = 60999
)

var (
// nolint:gosec // not used for cryptography
rnd = rand.New(rand.NewSource(time.Now().Unix()))
rndMu sync.Mutex
rndMu sync.Mutex
rndPort = maxPort
)

// RandomPort is a helper function to find a free random port.
Expand All @@ -28,18 +31,17 @@ func RandomPort(t *testing.T) int {
return tcpAddr.Port
}

// RandomPortNoListen returns a random port in the ephemeral port range.
// EphemeralPortNoListen returns the next port in the ephemeral port range.
// Does not attempt to listen and close to find a port as the OS may
// reallocate the port very quickly.
func RandomPortNoListen(*testing.T) uint16 {
const (
// Overlap of windows, linux in https://en.wikipedia.org/wiki/Ephemeral_port
min = 49152
max = 60999
)
n := max - min
func EphemeralPortNoListen() uint16 {
rndMu.Lock()
x := rnd.Intn(n)
p := rndPort

rndPort--
if rndPort < minPort {
rndPort = maxPort
}
rndMu.Unlock()
return uint16(min + x)
return uint16(p)
}
Loading