Skip to content

Commit

Permalink
Support custom host on listen address
Browse files Browse the repository at this point in the history
Previously, the testserver could only listen on localhost. This meant
that there was no way to spin up an instance of the testserver on one
machine and access it from another. This commit adds support for custom
hosts so that the testserver can be accessed from outside hosts if
needed.
  • Loading branch information
pawalt committed Jan 31, 2023
1 parent 83aaaf2 commit 5a67afd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
26 changes: 23 additions & 3 deletions testserver/testserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ const defaultStoreMemSize = 0.2

const defaultInitTimeout = 60
const defaultPollListenURLTimeout = 60
const defaultListenAddrHost = "localhost"

const testserverMessagePrefix = "cockroach-go testserver"
const tenantserverMessagePrefix = "cockroach-go tenantserver"
Expand Down Expand Up @@ -225,6 +226,7 @@ type testServerArgs struct {
storeMemSize float64 // the proportion of available memory allocated to test server
httpPorts []int
listenAddrPorts []int
listenAddrHost string
testConfig TestConfig
nonStableDB bool
customVersion string // custom cockroach version to use
Expand Down Expand Up @@ -335,6 +337,15 @@ func AddListenAddrPortOpt(port int) TestServerOpt {
}
}

// ListenAddrHostOpt is a TestServer option that can be passed to
// NewTestServer to specify the host for Cockroach to listen on. By default,
// this is `localhost`, and the most common override is 0.0.0.0.
func ListenAddrHostOpt(host string) TestServerOpt {
return func(args *testServerArgs) {
args.listenAddrHost = host
}
}

// StopDownloadInMiddleOpt is a TestServer option used only in testing.
// It is used to test the flock over downloaded CRDB binary.
// It should not be used in production.
Expand Down Expand Up @@ -397,6 +408,7 @@ func NewTestServer(opts ...TestServerOpt) (TestServer, error) {
serverArgs.storeMemSize = defaultStoreMemSize
serverArgs.initTimeoutSeconds = defaultInitTimeout
serverArgs.pollListenURLTimeoutSeconds = defaultPollListenURLTimeout
serverArgs.listenAddrHost = defaultListenAddrHost
for _, applyOptToArgs := range opts {
applyOptToArgs(serverArgs)
}
Expand Down Expand Up @@ -550,8 +562,16 @@ func NewTestServer(opts ...TestServerOpt) (TestServer, error) {
startCmd,
secureOpt,
storeArg,
fmt.Sprintf("--listen-addr=localhost:%d", serverArgs.listenAddrPorts[i]),
fmt.Sprintf("--http-addr=localhost:%d", serverArgs.httpPorts[i]),
fmt.Sprintf(
"--listen-addr=%s:%d",
serverArgs.listenAddrHost,
serverArgs.listenAddrPorts[i],
),
fmt.Sprintf(
"--http-addr=%s:%d",
serverArgs.listenAddrHost,
serverArgs.httpPorts[i],
),
"--listening-url-file=" + nodes[i].listeningURLFile,
joinArg,
"--external-io-dir=" + serverArgs.externalIODir,
Expand All @@ -562,7 +582,7 @@ func NewTestServer(opts ...TestServerOpt) (TestServer, error) {
startCmd,
"--logtostderr",
secureOpt,
"--host=localhost",
fmt.Sprintf("--host=%s", serverArgs.listenAddrHost),
"--port=" + strconv.Itoa(serverArgs.listenAddrPorts[0]),
"--http-port=" + strconv.Itoa(serverArgs.httpPorts[0]),
storeArg,
Expand Down
6 changes: 6 additions & 0 deletions testserver/testserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ func TestRunServer(t *testing.T) {
name: "Insecure",
instantiation: func(t *testing.T) (*sql.DB, func()) { return testserver.NewDBForTest(t) },
},
{
name: "InsecureWithHostOverride",
instantiation: func(t *testing.T) (*sql.DB, func()) {
return testserver.NewDBForTest(t, testserver.ListenAddrHostOpt("0.0.0.0"))
},
},
{
name: "InsecureWithCustomizedMemSize",
instantiation: func(t *testing.T) (*sql.DB, func()) {
Expand Down

0 comments on commit 5a67afd

Please sign in to comment.