Skip to content

Commit

Permalink
cli: make --host/--{listen,advertise,http}-addr recognize port numbers
Browse files Browse the repository at this point in the history
Prior to this patch, the various `cockroach` sub-commands would take
separate flags to specify an address/hostanme and to specify a port
number.

Meanwhile:

1. `--join` would recognize the syntax `host:port`.
2. the web UI, docs and other places often refer to a "server address"
   as the pair hostname:portnr.

For user convenience, it is thus important to make the interface more
straightforward/regular. This patch achieves this as follows:

- the flags
  `--listen-addr`/`--advertise-addr`/`--http-addr` (server-side) and
  `--host` (client-side) now recognize the syntax `host/addr:port`.
- the server-side `--port` flags are still recognized for backward
  compatibility but are marked as deprecated.
  The client-side `--port` is still recognized and not
  deprecated for now, but hidden from the contextual help.

As a side-effect of recognizing the port number inside the same flag,
the syntax with square brackets for IPv6 addresses now becomes
necessary when specifying also a port number. The syntax without
square brackets (and without port number) is temporarily still
recognized for backward compatibility, but is also marked as
deprecated.

Release note (cli change): the server-side command line flag
`--listen-addr`, which replaces the previous `--host` flag, is now
equipped to recognize both a hostname/address and port number. The
`--port` flag is deprecated as a result.

Release note (cli change): the server-side command line flag
`--http-addr`, which replaces the previous `--http-host` flag, is now
equipped to recognize both a hostname/address and port number. The
`--http-port` flag is deprecated as a result.

Release note (cli change): the server-side command line flag
`--advertise-addr`, which replaces the previous `--advertise-host`
flag, is now equipped to recognize both a hostname/address and
port number. The `--advertise-port` flag is deprecated as a result.

Release note (cli change): the client-side command line flag `--host`
is now equipped to recognize both a hostname/address and port
number. The client-side `--port` flag is still recognized,
but not documented any more; `--host` is now preferred.

Release note (cli change): the environment variable COCKROACH_PORT
that specifies the port number to use for client commands is now
deprecated. The port number can be placed in the COCKROACH_HOST
environment variable instead.

Release note (cli change): The syntax to specify IPv6 addresses with
the client-side command line flag `--host` is changed to use square
brackets, for example `--host=[::1]` instead of just `--host=::1`
previously. The previous syntax is still recognized for backward
compatibility but is deprecated.

Release note (cli change): the flag `--listen-port` which was
introduced in a recent change is now removed. (DOCS NOTE: remove both
this release note and the previous one that introduced --listen-port)
  • Loading branch information
knz committed Aug 9, 2018
1 parent 9ed0086 commit 2e99384
Show file tree
Hide file tree
Showing 17 changed files with 174 additions and 93 deletions.
6 changes: 3 additions & 3 deletions pkg/acceptance/cli_test.go
Expand Up @@ -117,9 +117,9 @@ function finish() {
trap finish EXIT
HOST=$(hostname -f)
$bin start --logtostderr=INFO --background --insecure --listen-addr="${HOST}" --listen-port=12345 &> out
$bin sql --insecure --host="${HOST}" --port=12345 -e "show databases"
$bin quit --insecure --host="${HOST}" --port=12345
$bin start --logtostderr=INFO --background --insecure --listen-addr="${HOST}":12345 &> out
$bin sql --insecure --host="${HOST}":12345 -e "show databases"
$bin quit --insecure --host="${HOST}":12345
`
containerConfig.Cmd = []string{"/bin/bash", "-c", script}
if err := testDockerOneShot(ctx, t, "start_flags_test", containerConfig); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/acceptance/cluster/cluster.go
Expand Up @@ -43,7 +43,7 @@ type Cluster interface {
// dismantle the cluster.
AssertAndStop(context.Context, testing.TB)
// ExecCLI runs `./cockroach <args>`, while filling in required flags such as
// --insecure, --certs-dir, --host or --port.
// --insecure, --certs-dir, --host.
//
// Returns stdout, stderr, and an error.
ExecCLI(ctx context.Context, i int, args []string) (string, string, error)
Expand Down
2 changes: 1 addition & 1 deletion pkg/acceptance/cluster/dockercluster.go
Expand Up @@ -541,7 +541,7 @@ func (l *DockerCluster) startNode(ctx context.Context, node *testNode) {
pprof: docker exec -it %[4]s pprof https+insecure://$(hostname):%[5]s/debug/pprof/heap
cockroach: %[6]s
cli-env: COCKROACH_INSECURE=false COCKROACH_CERTS_DIR=%[7]s COCKROACH_HOST=%s COCKROACH_PORT=%d`,
cli-env: COCKROACH_INSECURE=false COCKROACH_CERTS_DIR=%[7]s COCKROACH_HOST=%s:%d`,
node.Name(), "https://"+httpAddr.String(), localLogDir, node.Container.id[:5],
base.DefaultHTTPPort, cmd, certsDir, httpAddr.IP, httpAddr.Port)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/acceptance/localcluster/localcluster.go
Expand Up @@ -77,7 +77,7 @@ func (b *LocalCluster) AssertAndStop(ctx context.Context, t testing.TB) {
// ExecCLI implements cluster.Cluster.
func (b *LocalCluster) ExecCLI(ctx context.Context, i int, cmd []string) (string, string, error) {
cmd = append([]string{b.Cfg.Binary}, cmd...)
cmd = append(cmd, "--insecure", "--port", b.Port(ctx, i))
cmd = append(cmd, "--insecure", "--host", ":"+b.Port(ctx, i))
c := exec.CommandContext(ctx, cmd[0], cmd[1:]...)
var o, e bytes.Buffer
c.Stdout, c.Stderr = &o, &e
Expand Down
2 changes: 1 addition & 1 deletion pkg/ccl/cliccl/cli_test.go
Expand Up @@ -42,7 +42,7 @@ func newCLITest(args base.TestServerArgs) cliTest {
}
return cliTest{
TestServer: s.(*server.TestServer),
connArgs: []string{"--insecure", "--host=" + host, "--port=" + port},
connArgs: []string{"--insecure", "--host=" + host + ":" + port},
}
}

Expand Down
3 changes: 1 addition & 2 deletions pkg/cli/cli_test.go
Expand Up @@ -290,8 +290,7 @@ func (c cliTest) RunWithArgs(origArgs []string) {
args = append(args, "--insecure=false")
args = append(args, fmt.Sprintf("--certs-dir=%s", c.certsDir))
}
args = append(args, fmt.Sprintf("--host=%s", h))
args = append(args, fmt.Sprintf("--port=%s", p))
args = append(args, fmt.Sprintf("--host=%s:%s", h, p))
}
args = append(args, origArgs[1:]...)

Expand Down
64 changes: 34 additions & 30 deletions pkg/cli/cliflags/flags.go
Expand Up @@ -206,16 +206,22 @@ can also be specified (e.g. .25).`,
}

ClientHost = FlagInfo{
Name: "host",
EnvVar: "COCKROACH_HOST",
Description: `Database server host to connect to.`,
Name: "host",
EnvVar: "COCKROACH_HOST",
Description: `
CockroachDB node to connect to.
This can be specified either as an address/hostname, or
together with a port number as in -s myhost:26257.
If the port number is left unspecified, it defaults to 26257.
An IPv6 address can also be specified with the notation [...], for
example [::1]:26257 or [fe80::f6f2:::]:26257.`,
}

ClientPort = FlagInfo{
Name: "port",
Shorthand: "p",
EnvVar: "COCKROACH_PORT",
Description: `Database server port to connect to.`,
Description: `Deprecated. Use --host=<host>:<port>.`,
}

Database = FlagInfo{
Expand Down Expand Up @@ -267,8 +273,7 @@ sql_safe_updates = FALSE.`,
}

Set = FlagInfo{
Name: "set",
Shorthand: "s",
Name: "set",
Description: `
Set a client-side configuration parameter before running the SQL
shell. This flag may be specified multiple times.`,
Expand Down Expand Up @@ -327,9 +332,12 @@ or both forms can be used together, for example:
ListenAddr = FlagInfo{
Name: "listen-addr",
Description: `
The address or hostname to listen on. An IPv6 address can also be
specified with the notation [...], for example [::1] or
[fe80::f6f2:::].
The address/hostname and port to listen on, for example
--listen-addr=myhost:26257 or --listen-addr=:26257 (listen on all
interfaces). If the port part is left unspecified, it defaults
to 26257.
An IPv6 address can also be specified with the notation [...], for
example [::1]:26257 or [fe80::f6f2:::]:26257.
If --advertise-addr is left unspecified, the node will also announce
this address for use by other nodes. It is strongly recommended to use
--advertise-addr in cloud and container deployments or any setup where
Expand All @@ -341,16 +349,6 @@ NAT is present between cluster nodes.`,
Description: `Alias for --listen-addr. Deprecated.`,
}

ListenPort = FlagInfo{
Name: "listen-port",
Shorthand: "p",
Description: `
The port to bind to.
If --advertise-port is left unspecified, the node will also announce
this port for use by other nodes. If a router implements NAT or N:M
port forwarding, be sure to also use --advertise-port.`,
}

ServerPort = FlagInfo{
Name: "port",
Description: `Alias for --listen-port. Deprecated.`,
Expand All @@ -359,10 +357,14 @@ port forwarding, be sure to also use --advertise-port.`,
AdvertiseAddr = FlagInfo{
Name: "advertise-addr",
Description: `
The address or hostname to advertise to other CockroachDB nodes for intra-cluster
communication; it must resolve and be routable from other nodes in the cluster.
The address/hostname and port to advertise to other CockroachDB nodes
for intra-cluster communication. It must resolve and be routable from
other nodes in the cluster.
If left unspecified, it defaults to the setting of --listen-addr.
An IPv6 address can also be specified with the notation [...], for
example [::1] or [fe80::f6f2:::].`,
example [::1]:26257 or [fe80::f6f2:::]:26257.
The port number should be the same as in --listen-addr unless port
forwarding is set up on an intermediate firewall/router.`,
}

AdvertiseHost = FlagInfo{
Expand All @@ -371,16 +373,18 @@ example [::1] or [fe80::f6f2:::].`,
}

AdvertisePort = FlagInfo{
Name: "advertise-port",
Description: `
The port to advertise to other CockroachDB nodes for intra-cluster
communication. This should not be set differently from --listen-port
unless port forwarding is set up on an intermediate firewall/router.`,
Name: "advertise-port",
Description: `Deprecated. Use --advertise-addr=<host>:<port>.`,
}

ListenHTTPAddr = FlagInfo{
Name: "http-addr",
Description: `The hostname or IP address to bind to for HTTP requests.`,
Name: "http-addr",
Description: `
The hostname or IP address to bind to for HTTP requests.
If left unspecified, the address part defaults to the setting of
--listen-addr. The port number defaults to 8080.
An IPv6 address can also be specified with the notation [...], for
example [::1]:8080 or [fe80::f6f2:::]:8080.`,
}

ListenHTTPAddrAlias = FlagInfo{
Expand All @@ -390,7 +394,7 @@ unless port forwarding is set up on an intermediate firewall/router.`,

ListenHTTPPort = FlagInfo{
Name: "http-port",
Description: `The port to bind to for HTTP requests.`,
Description: `Deprecated. Use --http-addr=<host>:<port>.`,
}

ListeningURLFile = FlagInfo{
Expand Down
64 changes: 55 additions & 9 deletions pkg/cli/flags.go
Expand Up @@ -16,6 +16,7 @@ package cli

import (
"flag"
"fmt"
"net"
"strings"
"time"
Expand Down Expand Up @@ -151,6 +152,52 @@ func (a aliasStrVar) Set(v string) error {
// Type implements the pflag.Value interface.
func (a aliasStrVar) Type() string { return "string" }

// addrSetter wraps a address/port configuration option pair and
// enables setting them both with a single command-line flag.
type addrSetter struct {
addr *string
port *string
}

// String implements the pflag.Value interface.
func (a addrSetter) String() string {
return net.JoinHostPort(*a.addr, *a.port)
}

// Type implements the pflag.Value interface
func (a addrSetter) Type() string { return "<addr/host>[:<port>]" }

// Set implement the pflag.Value interface.
func (a addrSetter) Set(v string) error {
addr, port, err := net.SplitHostPort(v)
if err != nil {
if aerr, ok := err.(*net.AddrError); ok {
if strings.HasPrefix(aerr.Err, "too many colons") {
// Maybe this was an IPv6 address using the deprecated syntax
// without '[...]'. Try that.
// Note: the following is valid even if *a.port is empty.
// (An empty port number is always a valid listen address.)
maybeAddr := "[" + v + "]:" + *a.port
addr, port, err = net.SplitHostPort(maybeAddr)
if err == nil {
fmt.Fprintf(stderr,
"warning: the syntax \"%s\" for IPv6 addresses is deprecated; use \"[%s]\"\n", v, v)
}
} else if strings.HasPrefix(aerr.Err, "missing port") {
// It's inconvenient that SplitHostPort doesn't know how to ignore
// a missing port number. Oh well.
addr, port, err = net.SplitHostPort(v + ":" + *a.port)
}
}
}
if err != nil {
return err
}
*a.addr = addr
*a.port = port
return nil
}

func init() {
initCLIDefaults()

Expand Down Expand Up @@ -211,25 +258,23 @@ func init() {
f := StartCmd.Flags()

// Server flags.
StringFlag(f, &startCtx.serverListenAddr, cliflags.ListenAddr, startCtx.serverListenAddr)
VarFlag(f, addrSetter{&startCtx.serverListenAddr, &serverListenPort}, cliflags.ListenAddr)
VarFlag(f, aliasStrVar{&startCtx.serverListenAddr}, cliflags.ServerHost)
_ = f.MarkDeprecated(cliflags.ServerHost.Name, "use --listen-addr/--advertise-addr instead.")

StringFlag(f, &serverListenPort, cliflags.ListenPort, serverListenPort)
VarFlag(f, aliasStrVar{&serverListenPort}, cliflags.ServerPort)
_ = f.MarkDeprecated(cliflags.ServerPort.Name, "use --listen-port/--advertise-port instead.")
_ = f.MarkDeprecated(cliflags.ServerPort.Name, "use --listen-addr instead.")

StringFlag(f, &serverAdvertiseAddr, cliflags.AdvertiseAddr, serverAdvertiseAddr)
VarFlag(f, addrSetter{&serverAdvertiseAddr, &serverAdvertisePort}, cliflags.AdvertiseAddr)
VarFlag(f, aliasStrVar{&serverAdvertiseAddr}, cliflags.AdvertiseHost)
_ = f.MarkDeprecated(cliflags.AdvertiseHost.Name, "use --advertise-addr instead.")

StringFlag(f, &serverAdvertisePort, cliflags.AdvertisePort, serverAdvertisePort)
_ = f.MarkDeprecated(cliflags.AdvertisePort.Name, "use --advertise-addr=...:<port> instead.")

StringFlag(f, &serverHTTPAddr, cliflags.ListenHTTPAddr, serverHTTPAddr)
VarFlag(f, aliasStrVar{&serverHTTPAddr}, cliflags.ListenHTTPAddrAlias)
VarFlag(f, addrSetter{&serverHTTPAddr, &serverHTTPPort}, cliflags.ListenHTTPAddr)
_ = f.MarkDeprecated(cliflags.ListenHTTPAddrAlias.Name, "use --http-addr instead.")

StringFlag(f, &serverHTTPPort, cliflags.ListenHTTPPort, serverHTTPPort)
_ = f.MarkDeprecated(cliflags.ListenHTTPPort.Name, "use --http-addr=...:<port> instead.")

StringFlag(f, &serverCfg.Attrs, cliflags.Attrs, serverCfg.Attrs)
VarFlag(f, &serverCfg.Locality, cliflags.Locality)
Expand Down Expand Up @@ -317,8 +362,9 @@ func init() {
clientCmds = append(clientCmds, initCmd)
for _, cmd := range clientCmds {
f := cmd.PersistentFlags()
StringFlag(f, &clientConnHost, cliflags.ClientHost, clientConnHost)
VarFlag(f, addrSetter{&clientConnHost, &clientConnPort}, cliflags.ClientHost)
StringFlag(f, &clientConnPort, cliflags.ClientPort, clientConnPort)
_ = f.MarkHidden(cliflags.ClientPort.Name)

BoolFlag(f, &baseCfg.Insecure, cliflags.ClientInsecure, baseCfg.Insecure)

Expand Down
82 changes: 53 additions & 29 deletions pkg/cli/flags_test.go
Expand Up @@ -164,44 +164,60 @@ func TestServerConnSettings(t *testing.T) {
{[]string{"start"}, ":" + base.DefaultPort, ":" + base.DefaultPort},
{[]string{"start", "--listen-addr", "127.0.0.1"}, "127.0.0.1:" + base.DefaultPort, "127.0.0.1:" + base.DefaultPort},
{[]string{"start", "--listen-addr", "192.168.0.111"}, "192.168.0.111:" + base.DefaultPort, "192.168.0.111:" + base.DefaultPort},
{[]string{"start", "--listen-port", "12345"}, ":12345", ":12345"},
{[]string{"start", "--listen-addr", "127.0.0.1", "--listen-port", "12345"}, "127.0.0.1:12345", "127.0.0.1:12345"},
{[]string{"start", "--listen-addr", ":12345"}, ":12345", ":12345"},
{[]string{"start", "--listen-addr", "127.0.0.1:12345"}, "127.0.0.1:12345", "127.0.0.1:12345"},
{[]string{"start", "--listen-addr", "127.0.0.1:12345", "--port", "55555"}, "127.0.0.1:55555", "127.0.0.1:55555"},
{[]string{"start", "--advertise-addr", "192.168.0.111"}, ":" + base.DefaultPort, "192.168.0.111:" + base.DefaultPort},
{[]string{"start", "--advertise-addr", "192.168.0.111", "--advertise-port", "12345"}, ":" + base.DefaultPort, "192.168.0.111:12345"},
{[]string{"start", "--advertise-addr", "192.168.0.111:12345"}, ":" + base.DefaultPort, "192.168.0.111:12345"},
{[]string{"start", "--listen-addr", "127.0.0.1", "--advertise-addr", "192.168.0.111"}, "127.0.0.1:" + base.DefaultPort, "192.168.0.111:" + base.DefaultPort},
{[]string{"start", "--listen-addr", "127.0.0.1", "--advertise-addr", "192.168.0.111", "--listen-port", "12345"}, "127.0.0.1:12345", "192.168.0.111:12345"},
{[]string{"start", "--listen-addr", "127.0.0.1", "--advertise-addr", "192.168.0.111", "--advertise-port", "12345"}, "127.0.0.1:" + base.DefaultPort, "192.168.0.111:12345"},
{[]string{"start", "--listen-addr", "127.0.0.1", "--advertise-addr", "192.168.0.111", "--listen-port", "54321", "--advertise-port", "12345"}, "127.0.0.1:54321", "192.168.0.111:12345"},
{[]string{"start", "--advertise-addr", "192.168.0.111", "--listen-port", "12345"}, ":12345", "192.168.0.111:12345"},
{[]string{"start", "--advertise-addr", "192.168.0.111", "--advertise-port", "12345"}, ":" + base.DefaultPort, "192.168.0.111:12345"},
{[]string{"start", "--advertise-addr", "192.168.0.111", "--listen-port", "54321", "--advertise-port", "12345"}, ":54321", "192.168.0.111:12345"},
{[]string{"start", "--listen-addr", "127.0.0.1:12345", "--advertise-addr", "192.168.0.111"}, "127.0.0.1:12345", "192.168.0.111:12345"},
{[]string{"start", "--listen-addr", "127.0.0.1", "--advertise-addr", "192.168.0.111:12345"}, "127.0.0.1:" + base.DefaultPort, "192.168.0.111:12345"},
{[]string{"start", "--listen-addr", "127.0.0.1:54321", "--advertise-addr", "192.168.0.111:12345"}, "127.0.0.1:54321", "192.168.0.111:12345"},
{[]string{"start", "--advertise-addr", "192.168.0.111", "--listen-addr", ":12345"}, ":12345", "192.168.0.111:12345"},
{[]string{"start", "--advertise-addr", "192.168.0.111:12345", "--listen-addr", ":54321"}, ":54321", "192.168.0.111:12345"},
// confirm hostnames will work
{[]string{"start", "--listen-addr", "my.host.name"}, "my.host.name:" + base.DefaultPort, "my.host.name:" + base.DefaultPort},
{[]string{"start", "--listen-addr", "myhostname"}, "myhostname:" + base.DefaultPort, "myhostname:" + base.DefaultPort},
// confirm IPv6 works too
{[]string{"start", "--listen-addr", "::1"}, "[::1]:" + base.DefaultPort, "[::1]:" + base.DefaultPort},
{[]string{"start", "--listen-addr", "2622:6221:e663:4922:fc2b:788b:fadd:7b48"}, "[2622:6221:e663:4922:fc2b:788b:fadd:7b48]:" + base.DefaultPort, "[2622:6221:e663:4922:fc2b:788b:fadd:7b48]:" + base.DefaultPort},
// Backward-compatibility flags
{[]string{"start", "--listen-addr", "[::1]"}, "[::1]:" + base.DefaultPort, "[::1]:" + base.DefaultPort},
{[]string{"start", "--listen-addr", "[2622:6221:e663:4922:fc2b:788b:fadd:7b48]"},
"[2622:6221:e663:4922:fc2b:788b:fadd:7b48]:" + base.DefaultPort, "[2622:6221:e663:4922:fc2b:788b:fadd:7b48]:" + base.DefaultPort},

// Backward-compatibility flag combinations.
{[]string{"start", "--host", "192.168.0.111"}, "192.168.0.111:" + base.DefaultPort, "192.168.0.111:" + base.DefaultPort},
{[]string{"start", "--port", "12345"}, ":12345", ":12345"},
{[]string{"start", "--advertise-host", "192.168.0.111"}, ":" + base.DefaultPort, "192.168.0.111:" + base.DefaultPort},
{[]string{"start", "--advertise-addr", "192.168.0.111", "--advertise-port", "12345"}, ":" + base.DefaultPort, "192.168.0.111:12345"},
{[]string{"start", "--listen-addr", "::1"}, "[::1]:" + base.DefaultPort, "[::1]:" + base.DefaultPort},
{[]string{"start", "--listen-addr", "2622:6221:e663:4922:fc2b:788b:fadd:7b48"},
"[2622:6221:e663:4922:fc2b:788b:fadd:7b48]:" + base.DefaultPort, "[2622:6221:e663:4922:fc2b:788b:fadd:7b48]:" + base.DefaultPort},
{[]string{"start", "--listen-addr", "127.0.0.1", "--port", "12345"}, "127.0.0.1:12345", "127.0.0.1:12345"},
{[]string{"start", "--listen-addr", "127.0.0.1", "--advertise-addr", "192.168.0.111", "--port", "12345"}, "127.0.0.1:12345", "192.168.0.111:12345"},
{[]string{"start", "--listen-addr", "127.0.0.1", "--advertise-addr", "192.168.0.111", "--port", "12345"}, "127.0.0.1:12345", "192.168.0.111:12345"},
{[]string{"start", "--listen-addr", "127.0.0.1", "--advertise-addr", "192.168.0.111", "--advertise-port", "12345"}, "127.0.0.1:" + base.DefaultPort, "192.168.0.111:12345"},
{[]string{"start", "--listen-addr", "127.0.0.1", "--advertise-addr", "192.168.0.111", "--port", "54321", "--advertise-port", "12345"}, "127.0.0.1:54321", "192.168.0.111:12345"},
{[]string{"start", "--advertise-addr", "192.168.0.111", "--port", "12345"}, ":12345", "192.168.0.111:12345"},
{[]string{"start", "--advertise-addr", "192.168.0.111", "--advertise-port", "12345"}, ":" + base.DefaultPort, "192.168.0.111:12345"},
{[]string{"start", "--advertise-addr", "192.168.0.111", "--port", "54321", "--advertise-port", "12345"}, ":54321", "192.168.0.111:12345"},
}

for i, td := range testData {
initCLIDefaults()
if err := f.Parse(td.args); err != nil {
t.Fatalf("Parse(%#v) got unexpected error: %v", td.args, err)
}

extraServerFlagInit()
if td.expectedAddr != serverCfg.Addr {
t.Errorf("%d. serverCfg.Addr expected '%s', but got '%s'. td.args was '%#v'.",
i, td.expectedAddr, serverCfg.Addr, td.args)
}
if td.expectedAdvertiseAddr != serverCfg.AdvertiseAddr {
t.Errorf("%d. serverCfg.AdvertiseAddr expected '%s', but got '%s'. td.args was '%#v'.",
i, td.expectedAdvertiseAddr, serverCfg.AdvertiseAddr, td.args)
}
t.Run(strings.Join(td.args, " "), func(t *testing.T) {
initCLIDefaults()
if err := f.Parse(td.args); err != nil {
t.Fatalf("Parse(%#v) got unexpected error: %v", td.args, err)
}

extraServerFlagInit()
if td.expectedAddr != serverCfg.Addr {
t.Errorf("%d. serverCfg.Addr expected '%s', but got '%s'. td.args was '%#v'.",
i, td.expectedAddr, serverCfg.Addr, td.args)
}
if td.expectedAdvertiseAddr != serverCfg.AdvertiseAddr {
t.Errorf("%d. serverCfg.AdvertiseAddr expected '%s', but got '%s'. td.args was '%#v'.",
i, td.expectedAdvertiseAddr, serverCfg.AdvertiseAddr, td.args)
}
})
}
}

Expand All @@ -225,14 +241,22 @@ func TestClientConnSettings(t *testing.T) {
{[]string{"quit"}, ":" + base.DefaultPort},
{[]string{"quit", "--host", "127.0.0.1"}, "127.0.0.1:" + base.DefaultPort},
{[]string{"quit", "--host", "192.168.0.111"}, "192.168.0.111:" + base.DefaultPort},
{[]string{"quit", "--port", "12345"}, ":12345"},
{[]string{"quit", "--host", "127.0.0.1", "--port", "12345"}, "127.0.0.1:12345"},
{[]string{"quit", "--host", ":12345"}, ":12345"},
{[]string{"quit", "--host", "127.0.0.1:12345"}, "127.0.0.1:12345"},
// confirm hostnames will work
{[]string{"quit", "--host", "my.host.name"}, "my.host.name:" + base.DefaultPort},
{[]string{"quit", "--host", "myhostname"}, "myhostname:" + base.DefaultPort},
// confirm IPv6 works too
{[]string{"quit", "--host", "[::1]"}, "[::1]:" + base.DefaultPort},
{[]string{"quit", "--host", "[2622:6221:e663:4922:fc2b:788b:fadd:7b48]"},
"[2622:6221:e663:4922:fc2b:788b:fadd:7b48]:" + base.DefaultPort},

// Deprecated syntax.
{[]string{"quit", "--port", "12345"}, ":12345"},
{[]string{"quit", "--host", "127.0.0.1", "--port", "12345"}, "127.0.0.1:12345"},
{[]string{"quit", "--host", "::1"}, "[::1]:" + base.DefaultPort},
{[]string{"quit", "--host", "2622:6221:e663:4922:fc2b:788b:fadd:7b48"}, "[2622:6221:e663:4922:fc2b:788b:fadd:7b48]:" + base.DefaultPort},
{[]string{"quit", "--host", "2622:6221:e663:4922:fc2b:788b:fadd:7b48"},
"[2622:6221:e663:4922:fc2b:788b:fadd:7b48]:" + base.DefaultPort},
}

for i, td := range testData {
Expand Down

0 comments on commit 2e99384

Please sign in to comment.