chore: consolidate various randomPort() implementations (#12362)

Consolidates our existing randomPort() implementations to package testutil
This commit is contained in:
Cian Johnston
2024-02-29 12:51:44 +00:00
committed by GitHub
parent 4f87ba46f9
commit eba8cd7c07
6 changed files with 52 additions and 42 deletions
+1 -12
View File
@@ -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
}
+2 -2
View File
@@ -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),
)
+1 -1
View File
@@ -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",