Skip to content

Commit

Permalink
Allow single node clusters to override port
Browse files Browse the repository at this point in the history
Previously, the port option could only be overridden if you were
running multiple cockroach nodes. This commit changes the behavior such
that you can override from port `0` to any arbitrary port in the single
node configuration.
  • Loading branch information
pawalt committed Nov 11, 2022
1 parent f3c635a commit 5cacd4e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
4 changes: 2 additions & 2 deletions testserver/testserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ func NewTestServer(opts ...TestServerOpt) (TestServer, error) {
serverArgs.numNodes, len(serverArgs.listenAddrPorts)))
}

if len(serverArgs.listenAddrPorts) == 0 || len(serverArgs.listenAddrPorts) == 1 {
if len(serverArgs.listenAddrPorts) == 0 {
serverArgs.listenAddrPorts = []int{0}
}

Expand Down Expand Up @@ -554,7 +554,7 @@ func NewTestServer(opts ...TestServerOpt) (TestServer, error) {
"--logtostderr",
secureOpt,
"--host=localhost",
"--port=0",
"--port=" + strconv.Itoa(serverArgs.listenAddrPorts[0]),
"--http-port=" + strconv.Itoa(serverArgs.httpPorts[0]),
storeArg,
"--listening-url-file=" + nodes[i].listeningURLFile,
Expand Down
25 changes: 25 additions & 0 deletions testserver/testserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
"testing"
Expand Down Expand Up @@ -293,6 +294,30 @@ func TestPGURLWhitespace(t *testing.T) {
}
}

func TestSingleNodePort(t *testing.T) {
port, err := getFreePort()
require.NoError(t, err)

ts, err := testserver.NewTestServer(testserver.AddListenAddrPortOpt(port))
if err != nil {
t.Fatal(err)
}
defer ts.Stop()

// check that port overriding worked
url := ts.PGURL()
require.Equal(t, strconv.Itoa(port), url.Port())

db, err := sql.Open("postgres", url.String())
require.NoError(t, err)

// check that connection was successful
var out int
row := db.QueryRow("SELECT 1")
require.NoError(t, row.Scan(&out))
require.Equal(t, 1, out)
}

// tenantInterface is defined in order to use tenant-related methods on the
// TestServer.
type tenantInterface interface {
Expand Down

0 comments on commit 5cacd4e

Please sign in to comment.