github.com/hbgames/consul@v1.4.5/command/forceleave/forceleave_test.go (about)

     1  package forceleave
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/consul/agent"
     8  	"github.com/hashicorp/consul/testutil/retry"
     9  	"github.com/hashicorp/serf/serf"
    10  	"github.com/mitchellh/cli"
    11  )
    12  
    13  func TestForceLeaveCommand_noTabs(t *testing.T) {
    14  	t.Parallel()
    15  	if strings.ContainsRune(New(nil).Help(), '\t') {
    16  		t.Fatal("help has tabs")
    17  	}
    18  }
    19  
    20  func TestForceLeaveCommand(t *testing.T) {
    21  	t.Parallel()
    22  	a1 := agent.NewTestAgent(t, t.Name(), ``)
    23  	a2 := agent.NewTestAgent(t, t.Name(), ``)
    24  	defer a1.Shutdown()
    25  	defer a2.Shutdown()
    26  
    27  	_, err := a2.JoinLAN([]string{a1.Config.SerfBindAddrLAN.String()})
    28  	if err != nil {
    29  		t.Fatalf("err: %s", err)
    30  	}
    31  
    32  	// Forcibly shutdown a2 so that it appears "failed" in a1
    33  	a2.Shutdown()
    34  
    35  	ui := cli.NewMockUi()
    36  	c := New(ui)
    37  	args := []string{
    38  		"-http-addr=" + a1.HTTPAddr(),
    39  		a2.Config.NodeName,
    40  	}
    41  
    42  	code := c.Run(args)
    43  	if code != 0 {
    44  		t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
    45  	}
    46  
    47  	m := a1.LANMembers()
    48  	if len(m) != 2 {
    49  		t.Fatalf("should have 2 members: %#v", m)
    50  	}
    51  	retry.Run(t, func(r *retry.R) {
    52  		m = a1.LANMembers()
    53  		if got, want := m[1].Status, serf.StatusLeft; got != want {
    54  			r.Fatalf("got status %q want %q", got, want)
    55  		}
    56  	})
    57  }
    58  
    59  func TestForceLeaveCommand_noAddrs(t *testing.T) {
    60  	t.Parallel()
    61  	ui := cli.NewMockUi()
    62  	c := New(ui)
    63  	args := []string{"-http-addr=foo"}
    64  
    65  	code := c.Run(args)
    66  	if code != 1 {
    67  		t.Fatalf("bad: %d", code)
    68  	}
    69  
    70  	if !strings.Contains(ui.ErrorWriter.String(), "node name") {
    71  		t.Fatalf("bad: %#v", ui.ErrorWriter.String())
    72  	}
    73  }