github.phpd.cn/hashicorp/consul@v1.4.5/command/watch/watch_test.go (about)

     1  package watch
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/consul/agent"
     8  	"github.com/hashicorp/consul/testrpc"
     9  	"github.com/mitchellh/cli"
    10  )
    11  
    12  func TestWatchCommand_noTabs(t *testing.T) {
    13  	t.Parallel()
    14  	if strings.ContainsRune(New(cli.NewMockUi(), nil).Help(), '\t') {
    15  		t.Fatal("help has tabs")
    16  	}
    17  }
    18  
    19  func TestWatchCommand(t *testing.T) {
    20  	t.Parallel()
    21  	a := agent.NewTestAgent(t, t.Name(), ``)
    22  	defer a.Shutdown()
    23  	testrpc.WaitForTestAgent(t, a.RPC, "dc1")
    24  
    25  	ui := cli.NewMockUi()
    26  	c := New(ui, nil)
    27  	args := []string{"-http-addr=" + a.HTTPAddr(), "-type=nodes"}
    28  
    29  	code := c.Run(args)
    30  	if code != 0 {
    31  		t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
    32  	}
    33  
    34  	if !strings.Contains(ui.OutputWriter.String(), a.Config.NodeName) {
    35  		t.Fatalf("bad: %#v", ui.OutputWriter.String())
    36  	}
    37  }
    38  
    39  func TestWatchCommandNoConnect(t *testing.T) {
    40  	t.Parallel()
    41  	a := agent.NewTestAgent(t, t.Name(), ``)
    42  	defer a.Shutdown()
    43  	testrpc.WaitForTestAgent(t, a.RPC, "dc1")
    44  
    45  	ui := cli.NewMockUi()
    46  	c := New(ui, nil)
    47  	args := []string{"-http-addr=" + a.HTTPAddr(), "-type=connect_leaf"}
    48  
    49  	code := c.Run(args)
    50  	if code != 1 {
    51  		t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
    52  	}
    53  
    54  	if !strings.Contains(ui.ErrorWriter.String(),
    55  		"Type connect_leaf is not supported in the CLI tool") {
    56  		t.Fatalf("bad: %#v", ui.ErrorWriter.String())
    57  	}
    58  }
    59  
    60  func TestWatchCommandNoAgentService(t *testing.T) {
    61  	t.Parallel()
    62  	a := agent.NewTestAgent(t, t.Name(), ``)
    63  	defer a.Shutdown()
    64  
    65  	ui := cli.NewMockUi()
    66  	c := New(ui, nil)
    67  	args := []string{"-http-addr=" + a.HTTPAddr(), "-type=agent_service"}
    68  
    69  	code := c.Run(args)
    70  	if code != 1 {
    71  		t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
    72  	}
    73  
    74  	if !strings.Contains(ui.ErrorWriter.String(),
    75  		"Type agent_service is not supported in the CLI tool") {
    76  		t.Fatalf("bad: %#v", ui.ErrorWriter.String())
    77  	}
    78  }