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 25, 2022
1 parent 59aa313 commit 741a235
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 @@ -382,8 +382,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 @@ -398,7 +405,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 @@ -1186,14 +1186,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 @@ -1203,7 +1204,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 741a235

Please sign in to comment.