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

View File

@ -1,60 +1,57 @@
package command
package rtt
import (
"flag"
"fmt"
"strings"
"github.com/hashicorp/consul/command/flags"
"github.com/hashicorp/consul/lib"
"github.com/hashicorp/serf/coordinate"
"github.com/mitchellh/cli"
)
// RTTCommand is a Command implementation that allows users to query the
// estimated round trip time between nodes using network coordinates.
type RTTCommand struct {
BaseCommand
func New(ui cli.Ui) *cmd {
c := &cmd{UI: ui}
c.init()
return c
}
type cmd struct {
UI cli.Ui
flags *flag.FlagSet
http *flags.HTTPFlags
usage string
// flags
wan bool
}
func (c *RTTCommand) initFlags() {
c.InitFlagSet()
c.FlagSet.BoolVar(&c.wan, "wan", false,
func (c *cmd) init() {
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
c.flags.BoolVar(&c.wan, "wan", false,
"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 {
c.initFlags()
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 *cmd) Synopsis() string {
return "Estimates network round trip time between nodes"
}
func (c *RTTCommand) Run(args []string) int {
c.initFlags()
if err := c.FlagSet.Parse(args); err != nil {
func (c *cmd) Help() string {
return c.usage
}
func (c *cmd) Run(args []string) int {
if err := c.flags.Parse(args); err != nil {
return 1
}
// They must provide at least one node.
nodes := c.FlagSet.Args()
nodes := c.flags.Args()
if len(nodes) < 1 || len(nodes) > 2 {
c.UI.Error("One or two node names must be specified")
c.UI.Error("")
@ -63,7 +60,7 @@ func (c *RTTCommand) Run(args []string) int {
}
// Create and test the HTTP client.
client, err := c.HTTPClient()
client, err := c.http.APIClient()
if err != nil {
c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
return 1
@ -185,6 +182,20 @@ SHOW_RTT:
return 0
}
func (c *RTTCommand) Synopsis() string {
return "Estimates network round trip time between nodes"
}
const usage = `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.`

View File

@ -1,4 +1,4 @@
package command
package rtt
import (
"fmt"
@ -12,19 +12,11 @@ import (
"github.com/mitchellh/cli"
)
func testRTTCommand(t *testing.T) (*cli.MockUi, *RTTCommand) {
ui := cli.NewMockUi()
return ui, &RTTCommand{
BaseCommand: BaseCommand{
UI: ui,
Flags: FlagSetClientHTTP,
},
}
}
func TestRTTCommand_Implements(t *testing.T) {
func TestRTTCommand_noTabs(t *testing.T) {
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) {
@ -42,7 +34,8 @@ func TestRTTCommand_Run_BadArgs(t *testing.T) {
for _, tt := range tests {
t.Run(strings.Join(tt.args, " "), func(t *testing.T) {
t.Parallel()
_, c := testRTTCommand(t)
ui := cli.NewMockUi()
c := New(ui)
if code := c.Run(tt.args); code != 1 {
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
ui, c := testRTTCommand(t)
ui := cli.NewMockUi()
c := New(ui)
args := []string{
"-http-addr=" + a.HTTPAddr(),
a.Config.NodeName,
@ -123,7 +117,8 @@ func TestRTTCommand_Run_LAN(t *testing.T) {
// Default to the agent's node.
{
ui, c := testRTTCommand(t)
ui := cli.NewMockUi()
c := New(ui)
args := []string{
"-http-addr=" + a.HTTPAddr(),
"dogs",
@ -142,7 +137,8 @@ func TestRTTCommand_Run_LAN(t *testing.T) {
// Try an unknown node.
{
ui, c := testRTTCommand(t)
ui := cli.NewMockUi()
c := New(ui)
args := []string{
"-http-addr=" + a.HTTPAddr(),
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
// node with itself.
{
ui, c := testRTTCommand(t)
ui := cli.NewMockUi()
c := New(ui)
args := []string{
"-wan",
"-http-addr=" + a.HTTPAddr(),
@ -185,7 +182,8 @@ func TestRTTCommand_Run_WAN(t *testing.T) {
// Default to the agent's node.
{
ui, c := testRTTCommand(t)
ui := cli.NewMockUi()
c := New(ui)
args := []string{
"-wan",
"-http-addr=" + a.HTTPAddr(),
@ -204,7 +202,8 @@ func TestRTTCommand_Run_WAN(t *testing.T) {
// Try an unknown node.
{
ui, c := testRTTCommand(t)
ui := cli.NewMockUi()
c := New(ui)
args := []string{
"-wan",
"-http-addr=" + a.HTTPAddr(),