open-consul/command/members_test.go

119 lines
2.5 KiB
Go

package command
import (
"fmt"
"strings"
"testing"
"github.com/hashicorp/consul/agent"
"github.com/mitchellh/cli"
)
func testMembersCommand(t *testing.T) (*cli.MockUi, *MembersCommand) {
ui := cli.NewMockUi()
return ui, &MembersCommand{
BaseCommand: BaseCommand{
UI: ui,
Flags: FlagSetClientHTTP,
},
}
}
func TestMembersCommand_implements(t *testing.T) {
t.Parallel()
var _ cli.Command = &MembersCommand{}
}
func TestMembersCommandRun(t *testing.T) {
t.Parallel()
a := agent.NewTestAgent(t.Name(), nil)
defer a.Shutdown()
ui, c := testMembersCommand(t)
args := []string{"-http-addr=" + a.HTTPAddr()}
code := c.Run(args)
if code != 0 {
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
}
// Name
if !strings.Contains(ui.OutputWriter.String(), a.Config.NodeName) {
t.Fatalf("bad: %#v", ui.OutputWriter.String())
}
// 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())
}
}
func TestMembersCommandRun_WAN(t *testing.T) {
t.Parallel()
a := agent.NewTestAgent(t.Name(), nil)
defer a.Shutdown()
ui, c := testMembersCommand(t)
args := []string{"-http-addr=" + a.HTTPAddr(), "-wan"}
code := c.Run(args)
if code != 0 {
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
}
if !strings.Contains(ui.OutputWriter.String(), fmt.Sprintf("%d", a.Config.Ports.SerfWan)) {
t.Fatalf("bad: %#v", ui.OutputWriter.String())
}
}
func TestMembersCommandRun_statusFilter(t *testing.T) {
t.Parallel()
a := agent.NewTestAgent(t.Name(), nil)
defer a.Shutdown()
ui, c := testMembersCommand(t)
args := []string{
"-http-addr=" + a.HTTPAddr(),
"-status=a.*e",
}
code := c.Run(args)
if code != 0 {
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
}
if !strings.Contains(ui.OutputWriter.String(), a.Config.NodeName) {
t.Fatalf("bad: %#v", ui.OutputWriter.String())
}
}
func TestMembersCommandRun_statusFilter_failed(t *testing.T) {
t.Parallel()
a := agent.NewTestAgent(t.Name(), nil)
defer a.Shutdown()
ui, c := testMembersCommand(t)
args := []string{
"-http-addr=" + a.HTTPAddr(),
"-status=(fail|left)",
}
code := c.Run(args)
if code == 1 {
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
}
if strings.Contains(ui.OutputWriter.String(), a.Config.NodeName) {
t.Fatalf("bad: %#v", ui.OutputWriter.String())
}
if code != 2 {
t.Fatalf("bad: %d", code)
}
}