github.com/marwan-at-work/consul@v1.4.5/command/members/members_test.go (about)

     1  package members
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/consul/agent"
     9  	"github.com/mitchellh/cli"
    10  )
    11  
    12  func TestMembersCommand_noTabs(t *testing.T) {
    13  	t.Parallel()
    14  	if strings.ContainsRune(New(cli.NewMockUi()).Help(), '\t') {
    15  		t.Fatal("help has tabs")
    16  	}
    17  }
    18  
    19  func TestMembersCommand(t *testing.T) {
    20  	t.Parallel()
    21  	a := agent.NewTestAgent(t, t.Name(), ``)
    22  	defer a.Shutdown()
    23  
    24  	ui := cli.NewMockUi()
    25  	c := New(ui)
    26  	c.flags.SetOutput(ui.ErrorWriter)
    27  
    28  	args := []string{"-http-addr=" + a.HTTPAddr()}
    29  
    30  	code := c.Run(args)
    31  	if code != 0 {
    32  		t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
    33  	}
    34  
    35  	// Name
    36  	if !strings.Contains(ui.OutputWriter.String(), a.Config.NodeName) {
    37  		t.Fatalf("bad: %#v", ui.OutputWriter.String())
    38  	}
    39  
    40  	// Agent type
    41  	if !strings.Contains(ui.OutputWriter.String(), "server") {
    42  		t.Fatalf("bad: %#v", ui.OutputWriter.String())
    43  	}
    44  
    45  	// Datacenter
    46  	if !strings.Contains(ui.OutputWriter.String(), "dc1") {
    47  		t.Fatalf("bad: %#v", ui.OutputWriter.String())
    48  	}
    49  }
    50  
    51  func TestMembersCommand_WAN(t *testing.T) {
    52  	t.Parallel()
    53  	a := agent.NewTestAgent(t, t.Name(), ``)
    54  	defer a.Shutdown()
    55  
    56  	ui := cli.NewMockUi()
    57  	c := New(ui)
    58  	c.flags.SetOutput(ui.ErrorWriter)
    59  
    60  	args := []string{"-http-addr=" + a.HTTPAddr(), "-wan"}
    61  
    62  	code := c.Run(args)
    63  	if code != 0 {
    64  		t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
    65  	}
    66  
    67  	if !strings.Contains(ui.OutputWriter.String(), fmt.Sprintf("%d", a.Config.SerfPortWAN)) {
    68  		t.Fatalf("bad: %#v", ui.OutputWriter.String())
    69  	}
    70  }
    71  
    72  func TestMembersCommand_statusFilter(t *testing.T) {
    73  	t.Parallel()
    74  	a := agent.NewTestAgent(t, t.Name(), ``)
    75  	defer a.Shutdown()
    76  
    77  	ui := cli.NewMockUi()
    78  	c := New(ui)
    79  	c.flags.SetOutput(ui.ErrorWriter)
    80  
    81  	args := []string{
    82  		"-http-addr=" + a.HTTPAddr(),
    83  		"-status=a.*e",
    84  	}
    85  
    86  	code := c.Run(args)
    87  	if code != 0 {
    88  		t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
    89  	}
    90  
    91  	if !strings.Contains(ui.OutputWriter.String(), a.Config.NodeName) {
    92  		t.Fatalf("bad: %#v", ui.OutputWriter.String())
    93  	}
    94  }
    95  
    96  func TestMembersCommand_statusFilter_failed(t *testing.T) {
    97  	t.Parallel()
    98  	a := agent.NewTestAgent(t, t.Name(), ``)
    99  	defer a.Shutdown()
   100  
   101  	ui := cli.NewMockUi()
   102  	c := New(ui)
   103  	c.flags.SetOutput(ui.ErrorWriter)
   104  
   105  	args := []string{
   106  		"-http-addr=" + a.HTTPAddr(),
   107  		"-status=(fail|left)",
   108  	}
   109  
   110  	code := c.Run(args)
   111  	if code == 1 {
   112  		t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
   113  	}
   114  
   115  	if strings.Contains(ui.OutputWriter.String(), a.Config.NodeName) {
   116  		t.Fatalf("bad: %#v", ui.OutputWriter.String())
   117  	}
   118  
   119  	if code != 2 {
   120  		t.Fatalf("bad: %d", code)
   121  	}
   122  }