Skip to content

Commit

Permalink
test: only listen on localhost to avoid prompting for permission (ope…
Browse files Browse the repository at this point in the history
  • Loading branch information
ewanharris authored Oct 24, 2024
1 parent 1e86401 commit f8eac9d
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cmd/run/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ func testServerMetricsReporting(t *testing.T, engine string) {
metricsPort, metricsPortReleaser := testutils.TCPRandomPort()
metricsPortReleaser()

cfg.Metrics.Addr = fmt.Sprintf("0.0.0.0:%d", metricsPort)
cfg.Metrics.Addr = fmt.Sprintf("localhost:%d", metricsPort)

cfg.MaxConcurrentReadsForCheck = 30
cfg.MaxConcurrentReadsForListObjects = 30
Expand Down
4 changes: 2 additions & 2 deletions internal/mocks/mock_oidc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (server *mockOidcServer) NewAliasMockServer(aliasURL string) *mockOidcServe
}

func createHTTPServer(issuerURL string, publicKey *rsa.PublicKey) *http.Server {
port := strings.Split(issuerURL, ":")[2]
addr := strings.Split(issuerURL, "http://")[1]

mockHandler := http.NewServeMux()

Expand Down Expand Up @@ -90,7 +90,7 @@ func createHTTPServer(issuerURL string, publicKey *rsa.PublicKey) *http.Server {
}
})

return &http.Server{Addr: ":" + port, Handler: mockHandler}
return &http.Server{Addr: addr, Handler: mockHandler}
}

func (server *mockOidcServer) start() {
Expand Down
2 changes: 1 addition & 1 deletion internal/mocks/mock_tracing_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (s *mockTracingServer) Export(context.Context, *otlpcollector.ExportTraceSe
func NewMockTracingServer(t testing.TB, port int) *mockTracingServer {
mockServer := &mockTracingServer{exportCount: 0, server: grpc.NewServer()}
otlpcollector.RegisterTraceServiceServer(mockServer.server, mockServer)
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
listener, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", port))
require.NoError(t, err)
t.Cleanup(mockServer.server.GracefulStop)

Expand Down
6 changes: 3 additions & 3 deletions pkg/testutils/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,16 +241,16 @@ func MustDefaultConfigWithRandomPorts() *serverconfig.Config {
grpcPort, grpcPortReleaser := TCPRandomPort()
defer grpcPortReleaser()

config.GRPC.Addr = fmt.Sprintf("0.0.0.0:%d", grpcPort)
config.HTTP.Addr = fmt.Sprintf("0.0.0.0:%d", httpPort)
config.GRPC.Addr = fmt.Sprintf("localhost:%d", grpcPort)
config.HTTP.Addr = fmt.Sprintf("localhost:%d", httpPort)

return config
}

// TCPRandomPort tries to find a random TCP Port. If it can't find one, it panics. Else, it returns the port and a function that releases the port.
// It is the responsibility of the caller to call the release function right before trying to listen on the given port.
func TCPRandomPort() (int, func()) {
l, err := net.Listen("tcp", "")
l, err := net.Listen("tcp", "localhost:0")
if err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions tests/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ func StartServerWithContext(t testing.TB, cfg *serverconfig.Config, serverCtx *r
ctx, cancel := context.WithCancel(context.Background())

httpPort, httpPortReleaser := testutils.TCPRandomPort()
cfg.HTTP.Addr = fmt.Sprintf("0.0.0.0:%d", httpPort)
cfg.HTTP.Addr = fmt.Sprintf("localhost:%d", httpPort)
grpcPort, grpcPortReleaser := testutils.TCPRandomPort()
cfg.GRPC.Addr = fmt.Sprintf("0.0.0.0:%d", grpcPort)
cfg.GRPC.Addr = fmt.Sprintf("localhost:%d", grpcPort)

// these two functions release the ports so that the server can start listening on them
httpPortReleaser()
Expand Down

0 comments on commit f8eac9d

Please sign in to comment.