2015-10-16 07:03:16 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/command/agent"
|
2017-02-10 00:38:06 +00:00
|
|
|
"github.com/hashicorp/consul/command/base"
|
2015-10-16 07:03:16 +00:00
|
|
|
"github.com/hashicorp/consul/consul/structs"
|
2017-04-29 16:34:02 +00:00
|
|
|
"github.com/hashicorp/consul/testutil/retry"
|
2015-10-16 07:03:16 +00:00
|
|
|
"github.com/hashicorp/serf/coordinate"
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
2017-02-10 00:38:06 +00:00
|
|
|
func testRTTCommand(t *testing.T) (*cli.MockUi, *RTTCommand) {
|
2017-05-22 23:26:30 +00:00
|
|
|
ui := cli.NewMockUi()
|
2017-02-10 00:38:06 +00:00
|
|
|
return ui, &RTTCommand{
|
|
|
|
Command: base.Command{
|
2017-04-21 00:02:42 +00:00
|
|
|
UI: ui,
|
2017-02-10 00:38:06 +00:00
|
|
|
Flags: base.FlagSetClientHTTP,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:21:08 +00:00
|
|
|
func TestRTTCommand_Implements(t *testing.T) {
|
2017-05-22 18:18:53 +00:00
|
|
|
t.Parallel()
|
2015-10-22 00:21:08 +00:00
|
|
|
var _ cli.Command = &RTTCommand{}
|
2015-10-16 07:03:16 +00:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:21:08 +00:00
|
|
|
func TestRTTCommand_Run_BadArgs(t *testing.T) {
|
2017-05-22 18:18:53 +00:00
|
|
|
t.Parallel()
|
2017-02-10 00:38:06 +00:00
|
|
|
_, c := testRTTCommand(t)
|
2015-10-16 07:03:16 +00:00
|
|
|
|
|
|
|
if code := c.Run([]string{}); code != 1 {
|
|
|
|
t.Fatalf("expected return code 1, got %d", code)
|
|
|
|
}
|
|
|
|
|
|
|
|
if code := c.Run([]string{"node1", "node2", "node3"}); code != 1 {
|
|
|
|
t.Fatalf("expected return code 1, got %d", code)
|
|
|
|
}
|
|
|
|
|
|
|
|
if code := c.Run([]string{"-wan", "node1", "node2"}); code != 1 {
|
|
|
|
t.Fatalf("expected return code 1, got %d", code)
|
|
|
|
}
|
|
|
|
|
2015-10-16 19:45:25 +00:00
|
|
|
if code := c.Run([]string{"-wan", "node1.dc1", "node2"}); code != 1 {
|
2015-10-16 07:03:16 +00:00
|
|
|
t.Fatalf("expected return code 1, got %d", code)
|
|
|
|
}
|
|
|
|
|
2015-10-16 19:45:25 +00:00
|
|
|
if code := c.Run([]string{"-wan", "node1", "node2.dc1"}); code != 1 {
|
2015-10-16 07:03:16 +00:00
|
|
|
t.Fatalf("expected return code 1, got %d", code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:21:08 +00:00
|
|
|
func TestRTTCommand_Run_LAN(t *testing.T) {
|
2017-05-22 18:18:53 +00:00
|
|
|
t.Parallel()
|
2015-10-16 07:03:16 +00:00
|
|
|
updatePeriod := 10 * time.Millisecond
|
2017-05-22 11:59:36 +00:00
|
|
|
cfg := agent.TestConfig()
|
|
|
|
cfg.ConsulConfig.CoordinateUpdatePeriod = updatePeriod
|
|
|
|
a := agent.NewTestAgent(t.Name(), cfg)
|
2015-10-16 07:03:16 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
// Inject some known coordinates.
|
|
|
|
c1 := coordinate.NewCoordinate(coordinate.DefaultConfig())
|
|
|
|
c2 := c1.Clone()
|
|
|
|
c2.Vec[0] = 0.123
|
2015-10-16 07:56:15 +00:00
|
|
|
dist_str := fmt.Sprintf("%.3f ms", c1.DistanceTo(c2).Seconds()*1000.0)
|
2015-10-23 22:19:14 +00:00
|
|
|
{
|
|
|
|
req := structs.CoordinateUpdateRequest{
|
2017-05-22 11:59:36 +00:00
|
|
|
Datacenter: a.Config.Datacenter,
|
|
|
|
Node: a.Config.NodeName,
|
2015-10-23 22:19:14 +00:00
|
|
|
Coord: c1,
|
|
|
|
}
|
|
|
|
var reply struct{}
|
2017-05-22 11:59:36 +00:00
|
|
|
if err := a.RPC("Coordinate.Update", &req, &reply); err != nil {
|
2015-10-23 22:19:14 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2015-10-16 07:03:16 +00:00
|
|
|
}
|
2015-10-23 22:19:14 +00:00
|
|
|
{
|
|
|
|
req := structs.RegisterRequest{
|
2017-05-22 11:59:36 +00:00
|
|
|
Datacenter: a.Config.Datacenter,
|
2015-10-23 22:19:14 +00:00
|
|
|
Node: "dogs",
|
|
|
|
Address: "127.0.0.2",
|
|
|
|
}
|
|
|
|
var reply struct{}
|
2017-05-22 11:59:36 +00:00
|
|
|
if err := a.RPC("Catalog.Register", &req, &reply); err != nil {
|
2015-10-23 22:19:14 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2015-10-16 07:03:16 +00:00
|
|
|
}
|
2015-10-23 22:19:14 +00:00
|
|
|
{
|
|
|
|
var reply struct{}
|
|
|
|
req := structs.CoordinateUpdateRequest{
|
2017-05-22 11:59:36 +00:00
|
|
|
Datacenter: a.Config.Datacenter,
|
2015-10-23 22:19:14 +00:00
|
|
|
Node: "dogs",
|
|
|
|
Coord: c2,
|
|
|
|
}
|
2017-05-22 11:59:36 +00:00
|
|
|
if err := a.RPC("Coordinate.Update", &req, &reply); err != nil {
|
2015-10-23 22:19:14 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2015-10-16 07:03:16 +00:00
|
|
|
}
|
|
|
|
|
2016-10-25 20:46:54 +00:00
|
|
|
// Ask for the RTT of two known nodes
|
2017-02-10 00:38:06 +00:00
|
|
|
ui, c := testRTTCommand(t)
|
2016-10-25 20:46:54 +00:00
|
|
|
args := []string{
|
2017-05-22 11:59:36 +00:00
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
|
|
|
a.Config.NodeName,
|
2016-10-25 20:46:54 +00:00
|
|
|
"dogs",
|
|
|
|
}
|
|
|
|
// Wait for the updates to get flushed to the data store.
|
2017-05-04 22:52:53 +00:00
|
|
|
retry.Run(t, func(r *retry.R) {
|
2015-10-16 07:03:16 +00:00
|
|
|
code := c.Run(args)
|
|
|
|
if code != 0 {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("bad: %d: %#v", code, ui.ErrorWriter.String())
|
2015-10-16 07:03:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the proper RTT was reported in the output.
|
2015-10-16 19:45:25 +00:00
|
|
|
expected := fmt.Sprintf("rtt: %s", dist_str)
|
2015-10-16 07:56:15 +00:00
|
|
|
if !strings.Contains(ui.OutputWriter.String(), expected) {
|
2017-04-29 16:34:02 +00:00
|
|
|
r.Fatalf("bad: %#v", ui.OutputWriter.String())
|
2015-10-16 07:03:16 +00:00
|
|
|
}
|
2017-04-29 16:34:02 +00:00
|
|
|
})
|
2015-10-16 07:56:15 +00:00
|
|
|
|
2015-10-16 19:45:25 +00:00
|
|
|
// Default to the agent's node.
|
2015-10-16 07:56:15 +00:00
|
|
|
{
|
2017-02-10 00:38:06 +00:00
|
|
|
ui, c := testRTTCommand(t)
|
2015-10-16 07:56:15 +00:00
|
|
|
args := []string{
|
2017-05-22 11:59:36 +00:00
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
2015-10-16 07:56:15 +00:00
|
|
|
"dogs",
|
|
|
|
}
|
|
|
|
code := c.Run(args)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d: %#v", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the proper RTT was reported in the output.
|
2015-10-16 19:45:25 +00:00
|
|
|
expected := fmt.Sprintf("rtt: %s", dist_str)
|
|
|
|
if !strings.Contains(ui.OutputWriter.String(), expected) {
|
2015-10-16 07:56:15 +00:00
|
|
|
t.Fatalf("bad: %#v", ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
}
|
2015-10-16 07:03:16 +00:00
|
|
|
|
|
|
|
// Try an unknown node.
|
2015-10-16 07:56:15 +00:00
|
|
|
{
|
2017-02-10 00:38:06 +00:00
|
|
|
ui, c := testRTTCommand(t)
|
2015-10-16 07:03:16 +00:00
|
|
|
args := []string{
|
2017-05-22 11:59:36 +00:00
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
|
|
|
a.Config.NodeName,
|
2015-10-16 07:03:16 +00:00
|
|
|
"nope",
|
|
|
|
}
|
|
|
|
code := c.Run(args)
|
|
|
|
if code != 1 {
|
|
|
|
t.Fatalf("bad: %d: %#v", code, ui.ErrorWriter.String())
|
|
|
|
}
|
2015-10-16 07:56:15 +00:00
|
|
|
}
|
2015-10-16 07:03:16 +00:00
|
|
|
}
|
|
|
|
|
2015-10-22 00:21:08 +00:00
|
|
|
func TestRTTCommand_Run_WAN(t *testing.T) {
|
2017-05-22 18:18:53 +00:00
|
|
|
t.Parallel()
|
2017-05-22 11:59:36 +00:00
|
|
|
a := agent.NewTestAgent(t.Name(), nil)
|
2015-10-16 07:03:16 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
|
2017-05-22 11:59:36 +00:00
|
|
|
node := fmt.Sprintf("%s.%s", a.Config.NodeName, a.Config.Datacenter)
|
2015-10-16 07:03:16 +00:00
|
|
|
|
|
|
|
// We can't easily inject WAN coordinates, so we will just query the
|
|
|
|
// node with itself.
|
2015-10-16 07:56:15 +00:00
|
|
|
{
|
2017-02-10 00:38:06 +00:00
|
|
|
ui, c := testRTTCommand(t)
|
2015-10-16 07:03:16 +00:00
|
|
|
args := []string{
|
|
|
|
"-wan",
|
2017-05-22 11:59:36 +00:00
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
2015-10-16 07:03:16 +00:00
|
|
|
node,
|
|
|
|
node,
|
|
|
|
}
|
|
|
|
code := c.Run(args)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d: %#v", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure there was some kind of RTT reported in the output.
|
2015-10-16 19:45:25 +00:00
|
|
|
if !strings.Contains(ui.OutputWriter.String(), "rtt: ") {
|
2015-10-16 07:03:16 +00:00
|
|
|
t.Fatalf("bad: %#v", ui.OutputWriter.String())
|
|
|
|
}
|
2015-10-16 07:56:15 +00:00
|
|
|
}
|
2015-10-16 07:03:16 +00:00
|
|
|
|
2015-10-16 19:45:25 +00:00
|
|
|
// Default to the agent's node.
|
2015-10-16 07:56:15 +00:00
|
|
|
{
|
2017-02-10 00:38:06 +00:00
|
|
|
ui, c := testRTTCommand(t)
|
2015-10-16 07:03:16 +00:00
|
|
|
args := []string{
|
2015-10-16 07:56:15 +00:00
|
|
|
"-wan",
|
2017-05-22 11:59:36 +00:00
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
2015-10-16 07:56:15 +00:00
|
|
|
node,
|
|
|
|
}
|
|
|
|
code := c.Run(args)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d: %#v", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure there was some kind of RTT reported in the output.
|
2015-10-16 19:45:25 +00:00
|
|
|
if !strings.Contains(ui.OutputWriter.String(), "rtt: ") {
|
2015-10-16 07:56:15 +00:00
|
|
|
t.Fatalf("bad: %#v", ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try an unknown node.
|
|
|
|
{
|
2017-02-10 00:38:06 +00:00
|
|
|
ui, c := testRTTCommand(t)
|
2015-10-16 07:56:15 +00:00
|
|
|
args := []string{
|
2015-10-16 07:03:16 +00:00
|
|
|
"-wan",
|
2017-05-22 11:59:36 +00:00
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
2015-10-16 07:03:16 +00:00
|
|
|
node,
|
|
|
|
"dc1.nope",
|
|
|
|
}
|
|
|
|
code := c.Run(args)
|
|
|
|
if code != 1 {
|
|
|
|
t.Fatalf("bad: %d: %#v", code, ui.ErrorWriter.String())
|
|
|
|
}
|
2015-10-16 07:56:15 +00:00
|
|
|
}
|
2015-10-16 07:03:16 +00:00
|
|
|
}
|