Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

chore(cli): avoid use of testutil.RandomPort() in prometheus test#17297

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
johnstcn merged 1 commit intomainfromcj/flake/cli/TestServer/Prometheus
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes fromall 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
15 changes: 10 additions & 5 deletionscli/agent.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
"net"
"net/http"
"net/http/pprof"
"net/url"
Expand DownExpand Up@@ -491,8 +492,6 @@ func (r *RootCmd) workspaceAgent() *serpent.Command {
}

func ServeHandler(ctx context.Context, logger slog.Logger, handler http.Handler, addr, name string) (closeFunc func()) {
logger.Debug(ctx, "http server listening", slog.F("addr", addr), slog.F("name", name))

// ReadHeaderTimeout is purposefully not enabled. It caused some issues with
// websockets over the dev tunnel.
// See: https://github.com/coder/coder/pull/3730
Expand All@@ -502,9 +501,15 @@ func ServeHandler(ctx context.Context, logger slog.Logger, handler http.Handler,
Handler: handler,
}
go func() {
err := srv.ListenAndServe()
if err != nil && !xerrors.Is(err, http.ErrServerClosed) {
logger.Error(ctx, "http server listen", slog.F("name", name), slog.Error(err))
ln, err := net.Listen("tcp", addr)
if err != nil {
logger.Error(ctx, "http server listen", slog.F("name", name), slog.F("addr", addr), slog.Error(err))
return
}
defer ln.Close()
logger.Info(ctx, "http server listening", slog.F("addr", ln.Addr()), slog.F("name", name))
if err := srv.Serve(ln); err != nil && !xerrors.Is(err, http.ErrServerClosed) {
logger.Error(ctx, "http server serve", slog.F("addr", ln.Addr()), slog.F("name", name), slog.Error(err))
}
}()

Expand Down
35 changes: 25 additions & 10 deletionscli/server_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,6 +22,7 @@ import (
"os"
"path/filepath"
"reflect"
"regexp"
"runtime"
"strconv"
"strings"
Expand DownExpand Up@@ -1217,24 +1218,31 @@ func TestServer(t *testing.T) {
t.Parallel()

ctx := testutil.Context(t, testutil.WaitLong)
randPort := testutil.RandomPort(t)
inv, cfg := clitest.New(t,
inv, _ := clitest.New(t,
"server",
"--in-memory",
"--http-address", ":0",
"--access-url", "http://example.com",
"--provisioner-daemons", "1",
"--prometheus-enable",
"--prometheus-address", ":"+strconv.Itoa(randPort),
"--prometheus-address", ":0",
// "--prometheus-collect-db-metrics", // disabled by default
"--cache-dir", t.TempDir(),
)

pty := ptytest.New(t)
inv.Stdout = pty.Output()
inv.Stderr = pty.Output()

clitest.Start(t, inv)
_ = waitAccessURL(t, cfg)

// Wait until we see the prometheus address in the logs.
addrMatchExpr := `http server listening\s+addr=(\S+)\s+name=prometheus`
lineMatch := pty.ExpectRegexMatchContext(ctx, addrMatchExpr)
promAddr := regexp.MustCompile(addrMatchExpr).FindStringSubmatch(lineMatch)[1]

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

ctx := testutil.Context(t, testutil.WaitLong)
randPort := testutil.RandomPort(t)
inv, cfg := clitest.New(t,
inv, _ := clitest.New(t,
"server",
"--in-memory",
"--http-address", ":0",
"--access-url", "http://example.com",
"--provisioner-daemons", "1",
"--prometheus-enable",
"--prometheus-address", ":"+strconv.Itoa(randPort),
"--prometheus-address", ":0",
"--prometheus-collect-db-metrics",
"--cache-dir", t.TempDir(),
)

pty := ptytest.New(t)
inv.Stdout = pty.Output()
inv.Stderr = pty.Output()

clitest.Start(t, inv)
_ = waitAccessURL(t, cfg)

// Wait until we see the prometheus address in the logs.
addrMatchExpr := `http server listening\s+addr=(\S+)\s+name=prometheus`
lineMatch := pty.ExpectRegexMatchContext(ctx, addrMatchExpr)
promAddr := regexp.MustCompile(addrMatchExpr).FindStringSubmatch(lineMatch)[1]

testutil.Eventually(ctx, t, func(ctx context.Context) bool {
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("http://127.0.0.1:%d/metrics",randPort), nil)
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("http://%s/metrics",promAddr), nil)
if err != nil {
t.Logf("error creating request: %s", err.Error())
return false
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp