Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto/tls/test: server listens on localhost #397

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Changes from all 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
28 changes: 17 additions & 11 deletions crypto/tls/test/tls_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,21 @@ func (h *grpcHealthCheck) Watch(_ *grpc_health_v1.HealthCheckRequest, _ grpc_hea
return status.Error(codes.Unimplemented, "Watching is not supported")
}

func getLocalHostPort() (int, error) {
func getLocalHostAddr() (*net.TCPAddr, error) {
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
if err != nil {
return 0, err
return nil, err
}

l, err := net.ListenTCP("tcp", addr)
if err != nil {
return 0, err
return nil, err
}

if err := l.Close(); err != nil {
return 0, err
return nil, err
}
return l.Addr().(*net.TCPAddr).Port, nil
return l.Addr().(*net.TCPAddr), nil
}

func newIntegrationClientServer(
Expand All @@ -85,13 +85,19 @@ func newIntegrationClientServer(
prometheus.DefaultRegisterer = savedRegistry
}()

grpcPort, err := getLocalHostPort()
httpAddr, err := getLocalHostAddr()
require.NoError(t, err)
httpPort, err := getLocalHostPort()
grpcAddr, err := getLocalHostAddr()
require.NoError(t, err)

cfg.HTTPListenPort = httpPort
cfg.GRPCListenPort = grpcPort
if cfg.HTTPListenAddress == "" {
cfg.HTTPListenAddress = httpAddr.IP.String()
}
cfg.HTTPListenPort = httpAddr.Port
if cfg.GRPCListenAddress == "" {
cfg.GRPCListenAddress = grpcAddr.IP.String()
}
cfg.GRPCListenPort = grpcAddr.Port

serv, err := server.New(cfg)
require.NoError(t, err)
Expand All @@ -107,8 +113,8 @@ func newIntegrationClientServer(
require.NoError(t, err)
}()

httpURL := fmt.Sprintf("https://localhost:%d/hello", httpPort)
grpcHost := net.JoinHostPort("localhost", strconv.Itoa(grpcPort))
httpURL := fmt.Sprintf("https://localhost:%d/hello", httpAddr.Port)
grpcHost := net.JoinHostPort("localhost", strconv.Itoa(grpcAddr.Port))

for _, tc := range tcs {
tlsClientConfig, err := tc.tlsConfig.GetTLSConfig()
Expand Down