2013-12-31 21:06:33 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2017-05-12 13:41:13 +00:00
|
|
|
|
2017-05-22 11:59:36 +00:00
|
|
|
"github.com/hashicorp/consul/command/agent"
|
2017-05-12 13:41:13 +00:00
|
|
|
"github.com/hashicorp/consul/command/base"
|
|
|
|
"github.com/mitchellh/cli"
|
2013-12-31 21:06:33 +00:00
|
|
|
)
|
|
|
|
|
2017-02-09 22:12:47 +00:00
|
|
|
func testMembersCommand(t *testing.T) (*cli.MockUi, *MembersCommand) {
|
2017-05-22 23:26:30 +00:00
|
|
|
ui := cli.NewMockUi()
|
2017-02-09 22:12:47 +00:00
|
|
|
return ui, &MembersCommand{
|
|
|
|
Command: base.Command{
|
2017-04-21 00:02:42 +00:00
|
|
|
UI: ui,
|
2017-02-09 22:12:47 +00:00
|
|
|
Flags: base.FlagSetClientHTTP,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-31 21:06:33 +00:00
|
|
|
func TestMembersCommand_implements(t *testing.T) {
|
2017-05-22 18:18:53 +00:00
|
|
|
t.Parallel()
|
2013-12-31 21:06:33 +00:00
|
|
|
var _ cli.Command = &MembersCommand{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMembersCommandRun(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)
|
|
|
|
defer a.Shutdown()
|
2013-12-31 21:06:33 +00:00
|
|
|
|
2017-02-09 22:12:47 +00:00
|
|
|
ui, c := testMembersCommand(t)
|
2017-05-22 11:59:36 +00:00
|
|
|
args := []string{"-http-addr=" + a.HTTPAddr()}
|
2013-12-31 21:06:33 +00:00
|
|
|
|
|
|
|
code := c.Run(args)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
2015-05-15 21:26:23 +00:00
|
|
|
// Name
|
2017-05-22 11:59:36 +00:00
|
|
|
if !strings.Contains(ui.OutputWriter.String(), a.Config.NodeName) {
|
2013-12-31 21:06:33 +00:00
|
|
|
t.Fatalf("bad: %#v", ui.OutputWriter.String())
|
|
|
|
}
|
2015-05-15 21:26:23 +00:00
|
|
|
|
|
|
|
// Agent type
|
|
|
|
if !strings.Contains(ui.OutputWriter.String(), "server") {
|
|
|
|
t.Fatalf("bad: %#v", ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Datacenter
|
|
|
|
if !strings.Contains(ui.OutputWriter.String(), "dc1") {
|
|
|
|
t.Fatalf("bad: %#v", ui.OutputWriter.String())
|
|
|
|
}
|
2013-12-31 21:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMembersCommandRun_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)
|
|
|
|
defer a.Shutdown()
|
2013-12-31 21:06:33 +00:00
|
|
|
|
2017-02-09 22:12:47 +00:00
|
|
|
ui, c := testMembersCommand(t)
|
2017-05-22 11:59:36 +00:00
|
|
|
args := []string{"-http-addr=" + a.HTTPAddr(), "-wan"}
|
2013-12-31 21:06:33 +00:00
|
|
|
|
|
|
|
code := c.Run(args)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
2017-05-22 11:59:36 +00:00
|
|
|
if !strings.Contains(ui.OutputWriter.String(), fmt.Sprintf("%d", a.Config.Ports.SerfWan)) {
|
2013-12-31 21:06:33 +00:00
|
|
|
t.Fatalf("bad: %#v", ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMembersCommandRun_statusFilter(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)
|
|
|
|
defer a.Shutdown()
|
2013-12-31 21:06:33 +00:00
|
|
|
|
2017-02-09 22:12:47 +00:00
|
|
|
ui, c := testMembersCommand(t)
|
2013-12-31 21:06:33 +00:00
|
|
|
args := []string{
|
2017-05-22 11:59:36 +00:00
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
2013-12-31 21:06:33 +00:00
|
|
|
"-status=a.*e",
|
|
|
|
}
|
|
|
|
|
|
|
|
code := c.Run(args)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
2017-05-22 11:59:36 +00:00
|
|
|
if !strings.Contains(ui.OutputWriter.String(), a.Config.NodeName) {
|
2013-12-31 21:06:33 +00:00
|
|
|
t.Fatalf("bad: %#v", ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMembersCommandRun_statusFilter_failed(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)
|
|
|
|
defer a.Shutdown()
|
2013-12-31 21:06:33 +00:00
|
|
|
|
2017-02-09 22:12:47 +00:00
|
|
|
ui, c := testMembersCommand(t)
|
2013-12-31 21:06:33 +00:00
|
|
|
args := []string{
|
2017-05-22 11:59:36 +00:00
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
2013-12-31 21:06:33 +00:00
|
|
|
"-status=(fail|left)",
|
|
|
|
}
|
|
|
|
|
|
|
|
code := c.Run(args)
|
2014-05-04 09:54:45 +00:00
|
|
|
if code == 1 {
|
2013-12-31 21:06:33 +00:00
|
|
|
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
2017-05-22 11:59:36 +00:00
|
|
|
if strings.Contains(ui.OutputWriter.String(), a.Config.NodeName) {
|
2013-12-31 21:06:33 +00:00
|
|
|
t.Fatalf("bad: %#v", ui.OutputWriter.String())
|
|
|
|
}
|
2014-05-04 09:54:45 +00:00
|
|
|
|
|
|
|
if code != 2 {
|
|
|
|
t.Fatalf("bad: %d", code)
|
|
|
|
}
|
2013-12-31 21:06:33 +00:00
|
|
|
}
|