mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
chore: consolidate various randomPort() implementations (#12362)
Consolidates our existing randomPort() implementations to package testutil
This commit is contained in:
@@ -4,7 +4,6 @@ import (
|
||||
"bufio"
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -170,7 +169,7 @@ func TestProvisionerDaemon_SessionToken(t *testing.T) {
|
||||
t.Run("PrometheusEnabled", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
prometheusPort := randomPort(t)
|
||||
prometheusPort := testutil.RandomPort(t)
|
||||
|
||||
// Configure CLI client
|
||||
client, admin := coderdenttest.New(t, &coderdenttest.Options{
|
||||
@@ -242,13 +241,3 @@ func TestProvisionerDaemon_SessionToken(t *testing.T) {
|
||||
require.True(t, hasPromHTTP, "Prometheus HTTP metrics are missing")
|
||||
})
|
||||
}
|
||||
|
||||
// randomPort is a helper function to find a free random port, for instance to spawn Prometheus endpoint.
|
||||
func randomPort(t *testing.T) int {
|
||||
random, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
require.NoError(t, err)
|
||||
_ = random.Close()
|
||||
tcpAddr, valid := random.Addr().(*net.TCPAddr)
|
||||
require.True(t, valid)
|
||||
return tcpAddr.Port
|
||||
}
|
||||
|
||||
@@ -63,7 +63,7 @@ func Test_Headers(t *testing.T) {
|
||||
func TestWorkspaceProxy_Server_PrometheusEnabled(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
prometheusPort := randomPort(t)
|
||||
prometheusPort := testutil.RandomPort(t)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(1)
|
||||
@@ -96,7 +96,7 @@ func TestWorkspaceProxy_Server_PrometheusEnabled(t *testing.T) {
|
||||
"--primary-access-url", srv.URL,
|
||||
"--proxy-session-token", "test-token",
|
||||
"--access-url", "http://foobar:3001",
|
||||
"--http-address", fmt.Sprintf("127.0.0.1:%d", randomPort(t)),
|
||||
"--http-address", fmt.Sprintf("127.0.0.1:%d", testutil.RandomPort(t)),
|
||||
"--prometheus-enable",
|
||||
"--prometheus-address", fmt.Sprintf("127.0.0.1:%d", prometheusPort),
|
||||
)
|
||||
|
||||
@@ -22,7 +22,7 @@ func TestServer(t *testing.T) {
|
||||
var root cli.RootCmd
|
||||
cmd, err := root.Command(root.EnterpriseSubcommands())
|
||||
require.NoError(t, err)
|
||||
port := randomPort(t)
|
||||
port := testutil.RandomPort(t)
|
||||
inv, _ := clitest.NewWithCommand(t, cmd,
|
||||
"server",
|
||||
"--in-memory",
|
||||
|
||||
Reference in New Issue
Block a user