2023-03-28 19:12:30 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 19:12:30 +00:00
|
|
|
|
2017-10-12 22:15:13 +00:00
|
|
|
package members
|
2013-12-31 21:06:33 +00:00
|
|
|
|
|
|
|
import (
|
2021-11-15 15:51:14 +00:00
|
|
|
"encoding/csv"
|
2013-12-31 21:06:33 +00:00
|
|
|
"fmt"
|
2021-10-28 22:27:31 +00:00
|
|
|
"math/rand"
|
|
|
|
"sort"
|
2013-12-31 21:06:33 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2017-05-12 13:41:13 +00:00
|
|
|
|
|
|
|
"github.com/mitchellh/cli"
|
2021-10-28 22:27:31 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-10-28 21:44:38 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent"
|
2021-10-28 22:27:31 +00:00
|
|
|
consulapi "github.com/hashicorp/consul/api"
|
2013-12-31 21:06:33 +00:00
|
|
|
)
|
|
|
|
|
2021-10-28 21:44:38 +00:00
|
|
|
// TODO(partitions): split these tests
|
|
|
|
|
2017-10-17 12:07:30 +00:00
|
|
|
func TestMembersCommand_noTabs(t *testing.T) {
|
2017-10-17 14:07:48 +00:00
|
|
|
t.Parallel()
|
2017-10-17 12:07:30 +00:00
|
|
|
if strings.ContainsRune(New(cli.NewMockUi()).Help(), '\t') {
|
2017-10-17 13:44:20 +00:00
|
|
|
t.Fatal("help has tabs")
|
2017-10-17 12:07:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-17 12:04:51 +00:00
|
|
|
func TestMembersCommand(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-10-17 14:07:48 +00:00
|
|
|
t.Parallel()
|
2020-03-31 19:59:56 +00:00
|
|
|
a := agent.NewTestAgent(t, ``)
|
2017-05-22 11:59:36 +00:00
|
|
|
defer a.Shutdown()
|
2013-12-31 21:06:33 +00:00
|
|
|
|
2017-10-12 22:15:13 +00:00
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := New(ui)
|
|
|
|
c.flags.SetOutput(ui.ErrorWriter)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-10-17 12:04:51 +00:00
|
|
|
func TestMembersCommand_WAN(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-10-17 14:07:48 +00:00
|
|
|
t.Parallel()
|
2020-03-31 19:59:56 +00:00
|
|
|
a := agent.NewTestAgent(t, ``)
|
2017-05-22 11:59:36 +00:00
|
|
|
defer a.Shutdown()
|
2013-12-31 21:06:33 +00:00
|
|
|
|
2017-10-12 22:15:13 +00:00
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := New(ui)
|
|
|
|
c.flags.SetOutput(ui.ErrorWriter)
|
|
|
|
|
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-09-25 18:40:42 +00:00
|
|
|
if !strings.Contains(ui.OutputWriter.String(), fmt.Sprintf("%d", a.Config.SerfPortWAN)) {
|
2013-12-31 21:06:33 +00:00
|
|
|
t.Fatalf("bad: %#v", ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-17 12:04:51 +00:00
|
|
|
func TestMembersCommand_statusFilter(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-10-17 14:07:48 +00:00
|
|
|
t.Parallel()
|
2020-03-31 19:59:56 +00:00
|
|
|
a := agent.NewTestAgent(t, ``)
|
2017-05-22 11:59:36 +00:00
|
|
|
defer a.Shutdown()
|
2013-12-31 21:06:33 +00:00
|
|
|
|
2017-10-12 22:15:13 +00:00
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := New(ui)
|
|
|
|
c.flags.SetOutput(ui.ErrorWriter)
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-17 12:04:51 +00:00
|
|
|
func TestMembersCommand_statusFilter_failed(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-10-17 14:07:48 +00:00
|
|
|
t.Parallel()
|
2020-03-31 19:59:56 +00:00
|
|
|
a := agent.NewTestAgent(t, ``)
|
2017-05-22 11:59:36 +00:00
|
|
|
defer a.Shutdown()
|
2013-12-31 21:06:33 +00:00
|
|
|
|
2017-10-12 22:15:13 +00:00
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := New(ui)
|
|
|
|
c.flags.SetOutput(ui.ErrorWriter)
|
|
|
|
|
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
|
|
|
}
|
2020-03-09 10:24:56 +00:00
|
|
|
|
|
|
|
func TestMembersCommand_verticalBar(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2020-03-09 10:24:56 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
nodeName := "name|with|bars"
|
2020-03-31 20:12:33 +00:00
|
|
|
a := agent.NewTestAgent(t, `node_name = "`+nodeName+`"`)
|
2020-03-09 10:24:56 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := New(ui)
|
|
|
|
c.flags.SetOutput(ui.ErrorWriter)
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
|
|
|
}
|
|
|
|
|
|
|
|
code := c.Run(args)
|
|
|
|
if code == 1 {
|
|
|
|
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for nodeName presense because it should not be parsed by columnize
|
|
|
|
if !strings.Contains(ui.OutputWriter.String(), nodeName) {
|
|
|
|
t.Fatalf("bad: %#v", ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
}
|
2021-10-28 22:27:31 +00:00
|
|
|
|
2021-11-15 15:51:14 +00:00
|
|
|
func decodeOutput(t *testing.T, data string) []map[string]string {
|
|
|
|
r := csv.NewReader(strings.NewReader(data))
|
|
|
|
r.Comma = ' '
|
|
|
|
r.TrimLeadingSpace = true
|
|
|
|
|
|
|
|
lines, err := r.ReadAll()
|
|
|
|
require.NoError(t, err)
|
|
|
|
if len(lines) < 2 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var out []map[string]string
|
|
|
|
for i := 1; i < len(lines); i++ {
|
|
|
|
m := zip(t, lines[0], lines[i])
|
|
|
|
out = append(out, m)
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func zip(t *testing.T, k, v []string) map[string]string {
|
|
|
|
require.Equal(t, len(k), len(v))
|
|
|
|
|
|
|
|
m := make(map[string]string)
|
|
|
|
for i := 0; i < len(k); i++ {
|
|
|
|
m[k[i]] = v[i]
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2021-10-28 22:27:31 +00:00
|
|
|
func TestSortByMemberNamePartitionAndSegment(t *testing.T) {
|
|
|
|
// For the test data we'll give them names that would sort them backwards
|
|
|
|
// if we only sorted by name.
|
|
|
|
newData := func() []*consulapi.AgentMember {
|
|
|
|
// NOTE: This should be sorted for assertions.
|
|
|
|
return []*consulapi.AgentMember{
|
|
|
|
// servers
|
|
|
|
{Name: "p-betty", Tags: map[string]string{"role": "consul"}},
|
|
|
|
{Name: "q-bob", Tags: map[string]string{"role": "consul"}},
|
|
|
|
{Name: "r-bonnie", Tags: map[string]string{"role": "consul"}},
|
|
|
|
// default clients
|
|
|
|
{Name: "m-betty", Tags: map[string]string{}},
|
|
|
|
{Name: "n-bob", Tags: map[string]string{}},
|
|
|
|
{Name: "o-bonnie", Tags: map[string]string{}},
|
|
|
|
// segment 1 clients
|
|
|
|
{Name: "j-betty", Tags: map[string]string{"segment": "alpha"}},
|
|
|
|
{Name: "k-bob", Tags: map[string]string{"segment": "alpha"}},
|
|
|
|
{Name: "l-bonnie", Tags: map[string]string{"segment": "alpha"}},
|
|
|
|
// segment 2 clients
|
|
|
|
{Name: "g-betty", Tags: map[string]string{"segment": "beta"}},
|
|
|
|
{Name: "h-bob", Tags: map[string]string{"segment": "beta"}},
|
|
|
|
{Name: "i-bonnie", Tags: map[string]string{"segment": "beta"}},
|
|
|
|
// partition 1 clients
|
|
|
|
{Name: "d-betty", Tags: map[string]string{"ap": "part1"}},
|
|
|
|
{Name: "e-bob", Tags: map[string]string{"ap": "part1"}},
|
|
|
|
{Name: "f-bonnie", Tags: map[string]string{"ap": "part1"}},
|
|
|
|
// partition 2 clients
|
|
|
|
{Name: "a-betty", Tags: map[string]string{"ap": "part2"}},
|
|
|
|
{Name: "b-bob", Tags: map[string]string{"ap": "part2"}},
|
|
|
|
{Name: "c-bonnie", Tags: map[string]string{"ap": "part2"}},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stringify := func(data []*consulapi.AgentMember) []string {
|
|
|
|
var out []string
|
|
|
|
for _, m := range data {
|
|
|
|
out = append(out, fmt.Sprintf("<%s, %s, %s, %s>",
|
|
|
|
m.Tags["role"],
|
|
|
|
m.Tags["ap"],
|
|
|
|
m.Tags["segment"],
|
|
|
|
m.Name))
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
expect := newData()
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
data := newData()
|
|
|
|
rand.Shuffle(len(data), func(i, j int) {
|
|
|
|
data[i], data[j] = data[j], data[i]
|
|
|
|
})
|
|
|
|
|
|
|
|
sort.Sort(ByMemberNamePartitionAndSegment(data))
|
|
|
|
|
|
|
|
require.Equal(t, stringify(expect), stringify(data), "iteration #%d", i)
|
|
|
|
}
|
|
|
|
}
|