commands: move rtt command to separate pkg

This commit is contained in:
Frank Schroeder 2017-10-17 08:52:50 +02:00 committed by Frank Schröder
parent 1ba816b0ae
commit 602e896fb9
3 changed files with 70 additions and 64 deletions

View File

@ -35,6 +35,7 @@ import (
"github.com/hashicorp/consul/command/operraftlist" "github.com/hashicorp/consul/command/operraftlist"
"github.com/hashicorp/consul/command/operraftremove" "github.com/hashicorp/consul/command/operraftremove"
"github.com/hashicorp/consul/command/reload" "github.com/hashicorp/consul/command/reload"
"github.com/hashicorp/consul/command/rtt"
"github.com/hashicorp/consul/command/validate" "github.com/hashicorp/consul/command/validate"
"github.com/hashicorp/consul/version" "github.com/hashicorp/consul/version"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
@ -182,12 +183,7 @@ func init() {
}, },
"rtt": func() (cli.Command, error) { "rtt": func() (cli.Command, error) {
return &RTTCommand{ return rtt.New(ui), nil
BaseCommand: BaseCommand{
Flags: FlagSetClientHTTP,
UI: ui,
},
}, nil
}, },
"snapshot": func() (cli.Command, error) { "snapshot": func() (cli.Command, error) {

View File

@ -1,60 +1,57 @@
package command package rtt
import ( import (
"flag"
"fmt" "fmt"
"strings" "strings"
"github.com/hashicorp/consul/command/flags"
"github.com/hashicorp/consul/lib" "github.com/hashicorp/consul/lib"
"github.com/hashicorp/serf/coordinate" "github.com/hashicorp/serf/coordinate"
"github.com/mitchellh/cli"
) )
// RTTCommand is a Command implementation that allows users to query the func New(ui cli.Ui) *cmd {
// estimated round trip time between nodes using network coordinates. c := &cmd{UI: ui}
type RTTCommand struct { c.init()
BaseCommand return c
}
type cmd struct {
UI cli.Ui
flags *flag.FlagSet
http *flags.HTTPFlags
usage string
// flags // flags
wan bool wan bool
} }
func (c *RTTCommand) initFlags() { func (c *cmd) init() {
c.InitFlagSet() c.flags = flag.NewFlagSet("", flag.ContinueOnError)
c.FlagSet.BoolVar(&c.wan, "wan", false, c.flags.BoolVar(&c.wan, "wan", false,
"Use WAN coordinates instead of LAN coordinates.") "Use WAN coordinates instead of LAN coordinates.")
c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
c.usage = flags.Usage(usage, c.flags, c.http.ClientFlags(), nil)
} }
func (c *RTTCommand) Help() string { func (c *cmd) Synopsis() string {
c.initFlags() return "Estimates network round trip time between nodes"
return c.HelpCommand(`
Usage: consul rtt [options] node1 [node2]
Estimates the round trip time between two nodes using Consul's network
coordinate model of the cluster.
At least one node name is required. If the second node name isn't given, it
is set to the agent's node name. Note that these are node names as known to
Consul as "consul members" would show, not IP addresses.
By default, the two nodes are assumed to be nodes in the local datacenter
and the LAN coordinates are used. If the -wan option is given, then the WAN
coordinates are used, and the node names must be suffixed by a period and
the datacenter (eg. "myserver.dc1").
It is not possible to measure between LAN coordinates and WAN coordinates
because they are maintained by independent Serf gossip areas, so they are
not compatible.
`)
} }
func (c *RTTCommand) Run(args []string) int { func (c *cmd) Help() string {
c.initFlags() return c.usage
if err := c.FlagSet.Parse(args); err != nil { }
func (c *cmd) Run(args []string) int {
if err := c.flags.Parse(args); err != nil {
return 1 return 1
} }
// They must provide at least one node. // They must provide at least one node.
nodes := c.FlagSet.Args() nodes := c.flags.Args()
if len(nodes) < 1 || len(nodes) > 2 { if len(nodes) < 1 || len(nodes) > 2 {
c.UI.Error("One or two node names must be specified") c.UI.Error("One or two node names must be specified")
c.UI.Error("") c.UI.Error("")
@ -63,7 +60,7 @@ func (c *RTTCommand) Run(args []string) int {
} }
// Create and test the HTTP client. // Create and test the HTTP client.
client, err := c.HTTPClient() client, err := c.http.APIClient()
if err != nil { if err != nil {
c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err)) c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
return 1 return 1
@ -185,6 +182,20 @@ SHOW_RTT:
return 0 return 0
} }
func (c *RTTCommand) Synopsis() string { const usage = `Usage: consul rtt [options] node1 [node2]
return "Estimates network round trip time between nodes"
} Estimates the round trip time between two nodes using Consul's network
coordinate model of the cluster.
At least one node name is required. If the second node name isn't given, it
is set to the agent's node name. Note that these are node names as known to
Consul as "consul members" would show, not IP addresses.
By default, the two nodes are assumed to be nodes in the local datacenter
and the LAN coordinates are used. If the -wan option is given, then the WAN
coordinates are used, and the node names must be suffixed by a period and
the datacenter (eg. "myserver.dc1").
It is not possible to measure between LAN coordinates and WAN coordinates
because they are maintained by independent Serf gossip areas, so they are
not compatible.`

View File

@ -1,4 +1,4 @@
package command package rtt
import ( import (
"fmt" "fmt"
@ -12,19 +12,11 @@ import (
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
) )
func testRTTCommand(t *testing.T) (*cli.MockUi, *RTTCommand) { func TestRTTCommand_noTabs(t *testing.T) {
ui := cli.NewMockUi()
return ui, &RTTCommand{
BaseCommand: BaseCommand{
UI: ui,
Flags: FlagSetClientHTTP,
},
}
}
func TestRTTCommand_Implements(t *testing.T) {
t.Parallel() t.Parallel()
var _ cli.Command = &RTTCommand{} if strings.ContainsRune(New(cli.NewMockUi()).Help(), '\t') {
t.Fatal("usage has tabs")
}
} }
func TestRTTCommand_Run_BadArgs(t *testing.T) { func TestRTTCommand_Run_BadArgs(t *testing.T) {
@ -42,7 +34,8 @@ func TestRTTCommand_Run_BadArgs(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(strings.Join(tt.args, " "), func(t *testing.T) { t.Run(strings.Join(tt.args, " "), func(t *testing.T) {
t.Parallel() t.Parallel()
_, c := testRTTCommand(t) ui := cli.NewMockUi()
c := New(ui)
if code := c.Run(tt.args); code != 1 { if code := c.Run(tt.args); code != 1 {
t.Fatalf("expected return code 1, got %d", code) t.Fatalf("expected return code 1, got %d", code)
} }
@ -101,7 +94,8 @@ func TestRTTCommand_Run_LAN(t *testing.T) {
} }
// Ask for the RTT of two known nodes // Ask for the RTT of two known nodes
ui, c := testRTTCommand(t) ui := cli.NewMockUi()
c := New(ui)
args := []string{ args := []string{
"-http-addr=" + a.HTTPAddr(), "-http-addr=" + a.HTTPAddr(),
a.Config.NodeName, a.Config.NodeName,
@ -123,7 +117,8 @@ func TestRTTCommand_Run_LAN(t *testing.T) {
// Default to the agent's node. // Default to the agent's node.
{ {
ui, c := testRTTCommand(t) ui := cli.NewMockUi()
c := New(ui)
args := []string{ args := []string{
"-http-addr=" + a.HTTPAddr(), "-http-addr=" + a.HTTPAddr(),
"dogs", "dogs",
@ -142,7 +137,8 @@ func TestRTTCommand_Run_LAN(t *testing.T) {
// Try an unknown node. // Try an unknown node.
{ {
ui, c := testRTTCommand(t) ui := cli.NewMockUi()
c := New(ui)
args := []string{ args := []string{
"-http-addr=" + a.HTTPAddr(), "-http-addr=" + a.HTTPAddr(),
a.Config.NodeName, a.Config.NodeName,
@ -165,7 +161,8 @@ func TestRTTCommand_Run_WAN(t *testing.T) {
// We can't easily inject WAN coordinates, so we will just query the // We can't easily inject WAN coordinates, so we will just query the
// node with itself. // node with itself.
{ {
ui, c := testRTTCommand(t) ui := cli.NewMockUi()
c := New(ui)
args := []string{ args := []string{
"-wan", "-wan",
"-http-addr=" + a.HTTPAddr(), "-http-addr=" + a.HTTPAddr(),
@ -185,7 +182,8 @@ func TestRTTCommand_Run_WAN(t *testing.T) {
// Default to the agent's node. // Default to the agent's node.
{ {
ui, c := testRTTCommand(t) ui := cli.NewMockUi()
c := New(ui)
args := []string{ args := []string{
"-wan", "-wan",
"-http-addr=" + a.HTTPAddr(), "-http-addr=" + a.HTTPAddr(),
@ -204,7 +202,8 @@ func TestRTTCommand_Run_WAN(t *testing.T) {
// Try an unknown node. // Try an unknown node.
{ {
ui, c := testRTTCommand(t) ui := cli.NewMockUi()
c := New(ui)
args := []string{ args := []string{
"-wan", "-wan",
"-http-addr=" + a.HTTPAddr(), "-http-addr=" + a.HTTPAddr(),