Skip to content

Commit

Permalink
cli: fix use of IPv6 addresses with RPC client commands
Browse files Browse the repository at this point in the history
Release note (bug fix): the `cockroach` command line utilities that
internally use a RPC connection (e.g. `cockroach quit`, `cockroach
init`, etc) again properly support passing an IPv6 address literal via
the `--host` argument.
  • Loading branch information
knz committed Jun 3, 2019
1 parent 1791d2d commit ab4f77a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
4 changes: 3 additions & 1 deletion pkg/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type cliTestParams struct {
noServer bool
storeSpecs []base.StoreSpec
locality roachpb.Locality
addr string
}

func (c *cliTest) fail(err interface{}) {
Expand Down Expand Up @@ -135,6 +136,7 @@ func newCLITest(params cliTestParams) cliTest {
SSLCertsDir: c.certsDir,
StoreSpecs: params.storeSpecs,
Locality: params.locality,
Addr: params.addr,
})
if err != nil {
c.fail(err)
Expand Down Expand Up @@ -370,7 +372,7 @@ func (c cliTest) runWithArgsUnredirected(origArgs []string) {
args = append(args, "--insecure=false")
args = append(args, fmt.Sprintf("--certs-dir=%s", c.certsDir))
}
args = append(args, fmt.Sprintf("--host=%s:%s", h, p))
args = append(args, fmt.Sprintf("--host=%s", net.JoinHostPort(h, p)))
}
args = append(args, origArgs[1:]...)

Expand Down
3 changes: 0 additions & 3 deletions pkg/cli/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -1105,9 +1105,6 @@ func addrWithDefaultHost(addr string) (string, error) {
if host == "" {
host = "localhost"
}
if strings.Contains(host, ":") {
host = "[" + host + "]"
}
return net.JoinHostPort(host, port), nil
}

Expand Down
22 changes: 22 additions & 0 deletions pkg/cli/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,25 @@ func TestGCProfiles(t *testing.T) {
sum -= len(data[:i])
}
}

func TestAddrWithDefaultHost(t *testing.T) {
defer leaktest.AfterTest(t)()

testData := []struct {
inAddr string
outAddr string
}{
{"localhost:123", "localhost:123"},
{":123", "localhost:123"},
{"[::1]:123", "[::1]:123"},
}

for _, test := range testData {
addr, err := addrWithDefaultHost(test.inAddr)
if err != nil {
t.Error(err)
} else if addr != test.outAddr {
t.Errorf("expected %q, got %q", test.outAddr, addr)
}
}
}

0 comments on commit ab4f77a

Please sign in to comment.