Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8d122aa

Browse files
authored
chore(cli): avoid use of testutil.RandomPort() in prometheus test (#17297)
Should hopefully fix coder/internal#282 Instead of picking a random port for the prometheus server, listen on `:0` and read the port from the CLI stdout.
1 parent 0e65821 commit 8d122aa

File tree

2 files changed

+35
-15
lines changed

2 files changed

+35
-15
lines changed

cli/agent.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"io"
7+
"net"
78
"net/http"
89
"net/http/pprof"
910
"net/url"
@@ -491,8 +492,6 @@ func (r *RootCmd) workspaceAgent() *serpent.Command {
491492
}
492493

493494
func ServeHandler(ctx context.Context, logger slog.Logger, handler http.Handler, addr, name string) (closeFunc func()) {
494-
logger.Debug(ctx, "http server listening", slog.F("addr", addr), slog.F("name", name))
495-
496495
// ReadHeaderTimeout is purposefully not enabled. It caused some issues with
497496
// websockets over the dev tunnel.
498497
// See: https://github.com/coder/coder/pull/3730
@@ -502,9 +501,15 @@ func ServeHandler(ctx context.Context, logger slog.Logger, handler http.Handler,
502501
Handler: handler,
503502
}
504503
go func() {
505-
err := srv.ListenAndServe()
506-
if err != nil && !xerrors.Is(err, http.ErrServerClosed) {
507-
logger.Error(ctx, "http server listen", slog.F("name", name), slog.Error(err))
504+
ln, err := net.Listen("tcp", addr)
505+
if err != nil {
506+
logger.Error(ctx, "http server listen", slog.F("name", name), slog.F("addr", addr), slog.Error(err))
507+
return
508+
}
509+
defer ln.Close()
510+
logger.Info(ctx, "http server listening", slog.F("addr", ln.Addr()), slog.F("name", name))
511+
if err := srv.Serve(ln); err != nil && !xerrors.Is(err, http.ErrServerClosed) {
512+
logger.Error(ctx, "http server serve", slog.F("addr", ln.Addr()), slog.F("name", name), slog.Error(err))
508513
}
509514
}()
510515

cli/server_test.go

+25-10
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"os"
2323
"path/filepath"
2424
"reflect"
25+
"regexp"
2526
"runtime"
2627
"strconv"
2728
"strings"
@@ -1217,24 +1218,31 @@ func TestServer(t *testing.T) {
12171218
t.Parallel()
12181219

12191220
ctx := testutil.Context(t, testutil.WaitLong)
1220-
randPort := testutil.RandomPort(t)
1221-
inv, cfg := clitest.New(t,
1221+
inv, _ := clitest.New(t,
12221222
"server",
12231223
"--in-memory",
12241224
"--http-address", ":0",
12251225
"--access-url", "http://example.com",
12261226
"--provisioner-daemons", "1",
12271227
"--prometheus-enable",
1228-
"--prometheus-address", ":"+strconv.Itoa(randPort),
1228+
"--prometheus-address", ":0",
12291229
// "--prometheus-collect-db-metrics", // disabled by default
12301230
"--cache-dir", t.TempDir(),
12311231
)
12321232

1233+
pty := ptytest.New(t)
1234+
inv.Stdout = pty.Output()
1235+
inv.Stderr = pty.Output()
1236+
12331237
clitest.Start(t, inv)
1234-
_ = waitAccessURL(t, cfg)
1238+
1239+
// Wait until we see the prometheus address in the logs.
1240+
addrMatchExpr := `http server listening\s+addr=(\S+)\s+name=prometheus`
1241+
lineMatch := pty.ExpectRegexMatchContext(ctx, addrMatchExpr)
1242+
promAddr := regexp.MustCompile(addrMatchExpr).FindStringSubmatch(lineMatch)[1]
12351243

12361244
testutil.Eventually(ctx, t, func(ctx context.Context) bool {
1237-
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("http://127.0.0.1:%d/metrics", randPort), nil)
1245+
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("http://%s/metrics", promAddr), nil)
12381246
if err != nil {
12391247
t.Logf("error creating request: %s", err.Error())
12401248
return false
@@ -1272,24 +1280,31 @@ func TestServer(t *testing.T) {
12721280
t.Parallel()
12731281

12741282
ctx := testutil.Context(t, testutil.WaitLong)
1275-
randPort := testutil.RandomPort(t)
1276-
inv, cfg := clitest.New(t,
1283+
inv, _ := clitest.New(t,
12771284
"server",
12781285
"--in-memory",
12791286
"--http-address", ":0",
12801287
"--access-url", "http://example.com",
12811288
"--provisioner-daemons", "1",
12821289
"--prometheus-enable",
1283-
"--prometheus-address", ":"+strconv.Itoa(randPort),
1290+
"--prometheus-address", ":0",
12841291
"--prometheus-collect-db-metrics",
12851292
"--cache-dir", t.TempDir(),
12861293
)
12871294

1295+
pty := ptytest.New(t)
1296+
inv.Stdout = pty.Output()
1297+
inv.Stderr = pty.Output()
1298+
12881299
clitest.Start(t, inv)
1289-
_ = waitAccessURL(t, cfg)
1300+
1301+
// Wait until we see the prometheus address in the logs.
1302+
addrMatchExpr := `http server listening\s+addr=(\S+)\s+name=prometheus`
1303+
lineMatch := pty.ExpectRegexMatchContext(ctx, addrMatchExpr)
1304+
promAddr := regexp.MustCompile(addrMatchExpr).FindStringSubmatch(lineMatch)[1]
12901305

12911306
testutil.Eventually(ctx, t, func(ctx context.Context) bool {
1292-
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("http://127.0.0.1:%d/metrics", randPort), nil)
1307+
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("http://%s/metrics", promAddr), nil)
12931308
if err != nil {
12941309
t.Logf("error creating request: %s", err.Error())
12951310
return false

0 commit comments

Comments
 (0)