2016-08-30 02:09:57 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2017-02-09 23:19:34 +00:00
|
|
|
"github.com/hashicorp/consul/command/base"
|
2016-08-30 02:09:57 +00:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
2017-02-09 23:19:34 +00:00
|
|
|
func testOperatorCommand(t *testing.T) (*cli.MockUi, *OperatorCommand) {
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
return ui, &OperatorCommand{
|
|
|
|
Command: base.Command{
|
|
|
|
Ui: ui,
|
|
|
|
Flags: base.FlagSetHTTP,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-30 02:09:57 +00:00
|
|
|
func TestOperator_Implements(t *testing.T) {
|
|
|
|
var _ cli.Command = &OperatorCommand{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOperator_Raft_ListPeers(t *testing.T) {
|
|
|
|
a1 := testAgent(t)
|
|
|
|
defer a1.Shutdown()
|
|
|
|
waitForLeader(t, a1.httpAddr)
|
|
|
|
|
2017-02-09 23:19:34 +00:00
|
|
|
ui, c := testOperatorCommand(t)
|
2016-08-30 02:09:57 +00:00
|
|
|
args := []string{"raft", "-http-addr=" + a1.httpAddr, "-list-peers"}
|
|
|
|
|
|
|
|
code := c.Run(args)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
output := strings.TrimSpace(ui.OutputWriter.String())
|
|
|
|
if !strings.Contains(output, "leader") {
|
|
|
|
t.Fatalf("bad: %s", output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOperator_Raft_RemovePeer(t *testing.T) {
|
|
|
|
a1 := testAgent(t)
|
|
|
|
defer a1.Shutdown()
|
|
|
|
waitForLeader(t, a1.httpAddr)
|
|
|
|
|
2017-02-09 23:19:34 +00:00
|
|
|
ui, c := testOperatorCommand(t)
|
2016-08-30 02:09:57 +00:00
|
|
|
args := []string{"raft", "-http-addr=" + a1.httpAddr, "-remove-peer", "-address=nope"}
|
|
|
|
|
|
|
|
code := c.Run(args)
|
|
|
|
if code != 1 {
|
|
|
|
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we get this error, it proves we sent the address all they through.
|
|
|
|
output := strings.TrimSpace(ui.ErrorWriter.String())
|
|
|
|
if !strings.Contains(output, "address \"nope\" was not found in the Raft configuration") {
|
|
|
|
t.Fatalf("bad: %s", output)
|
|
|
|
}
|
|
|
|
}
|