Skip to content

Commit

Permalink
roachtest: Set localhost as the http listener when --local flag
Browse files Browse the repository at this point in the history
is used. This prevents OSX from prompting to allow incoming
network connections when running a roachtest from an ide or
 shell.

Release justification: test-only change
Release note: None
  • Loading branch information
Miral Gadani committed Oct 24, 2022
1 parent f872901 commit 8c0a2a7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
9 changes: 8 additions & 1 deletion pkg/cmd/roachtest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,15 @@ func runTests(register func(registry.Registry), cfg cliCfg) error {

filter := registry.NewTestFilter(cfg.args)
clusterType := roachprodCluster
bindTo := ""
if local {
clusterType = localCluster

// This will suppress the annoying "Allow incoming network connections" popup from
// OSX when running a roachtest
bindTo = "localhost"

fmt.Printf("--local specified. Binding http listener to localhost only")
if cfg.parallelism != 1 {
fmt.Printf("--local specified. Overriding --parallelism to 1.\n")
cfg.parallelism = 1
Expand All @@ -392,7 +399,7 @@ func runTests(register func(registry.Registry), cfg cliCfg) error {
keepClustersOnTestFailure: cfg.debugEnabled,
clusterID: cfg.clusterID,
}
if err := runner.runHTTPServer(cfg.httpPort, os.Stdout); err != nil {
if err := runner.runHTTPServer(cfg.httpPort, os.Stdout, bindTo); err != nil {
return err
}

Expand Down
11 changes: 8 additions & 3 deletions pkg/cmd/roachtest/test_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -1187,14 +1187,15 @@ func (r *testRunner) removeWorker(ctx context.Context, name string) {
// runHTTPServer starts a server running in the background.
//
// httpPort: The port on which to serve the web interface. Pass 0 for allocating
// bindTo: The host/ip on which to bind. Leave empty to bind on all local ips
//
// a port automatically (which will be printed to stdout).
func (r *testRunner) runHTTPServer(httpPort int, stdout io.Writer) error {
func (r *testRunner) runHTTPServer(httpPort int, stdout io.Writer, bindTo string) error {
http.HandleFunc("/", r.serveHTTP)
// Run an http server in the background.
// We handle the case where httpPort is 0, which means we automatically
// allocate a port.
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", httpPort))
listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", bindTo, httpPort))
if err != nil {
return err
}
Expand All @@ -1204,7 +1205,11 @@ func (r *testRunner) runHTTPServer(httpPort int, stdout io.Writer) error {
panic(err)
}
}()
fmt.Fprintf(stdout, "HTTP server listening on all network interfaces, port %d.\n", httpPort)
bindToDesc := "all network interfaces"
if bindTo != "" {
bindToDesc = bindTo
}
fmt.Fprintf(stdout, "HTTP server listening on %s, port %d.\n", bindToDesc, httpPort)
return nil
}

Expand Down

0 comments on commit 8c0a2a7

Please sign in to comment.