github.com/clly/consul@v1.4.5/command/operator/autopilot/set/operator_autopilot_set_test.go (about)

     1  package set
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/hashicorp/consul/testrpc"
     9  
    10  	"github.com/hashicorp/consul/agent"
    11  	"github.com/hashicorp/consul/agent/consul/autopilot"
    12  	"github.com/hashicorp/consul/agent/structs"
    13  	"github.com/mitchellh/cli"
    14  )
    15  
    16  func TestOperatorAutopilotSetConfigCommand_noTabs(t *testing.T) {
    17  	t.Parallel()
    18  	if strings.ContainsRune(New(cli.NewMockUi()).Help(), '\t') {
    19  		t.Fatal("help has tabs")
    20  	}
    21  }
    22  
    23  func TestOperatorAutopilotSetConfigCommand(t *testing.T) {
    24  	t.Parallel()
    25  	a := agent.NewTestAgent(t, t.Name(), ``)
    26  	defer a.Shutdown()
    27  	testrpc.WaitForTestAgent(t, a.RPC, "dc1")
    28  
    29  	ui := cli.NewMockUi()
    30  	c := New(ui)
    31  	args := []string{
    32  		"-http-addr=" + a.HTTPAddr(),
    33  		"-cleanup-dead-servers=false",
    34  		"-max-trailing-logs=99",
    35  		"-last-contact-threshold=123ms",
    36  		"-server-stabilization-time=123ms",
    37  	}
    38  
    39  	code := c.Run(args)
    40  	if code != 0 {
    41  		t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
    42  	}
    43  	output := strings.TrimSpace(ui.OutputWriter.String())
    44  	if !strings.Contains(output, "Configuration updated") {
    45  		t.Fatalf("bad: %s", output)
    46  	}
    47  
    48  	req := structs.DCSpecificRequest{
    49  		Datacenter: "dc1",
    50  	}
    51  	var reply autopilot.Config
    52  	if err := a.RPC("Operator.AutopilotGetConfiguration", &req, &reply); err != nil {
    53  		t.Fatalf("err: %v", err)
    54  	}
    55  
    56  	if reply.CleanupDeadServers {
    57  		t.Fatalf("bad: %#v", reply)
    58  	}
    59  	if reply.MaxTrailingLogs != 99 {
    60  		t.Fatalf("bad: %#v", reply)
    61  	}
    62  	if reply.LastContactThreshold != 123*time.Millisecond {
    63  		t.Fatalf("bad: %#v", reply)
    64  	}
    65  	if reply.ServerStabilizationTime != 123*time.Millisecond {
    66  		t.Fatalf("bad: %#v", reply)
    67  	}
    68  }