2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-02-10 01:58:20 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2017-02-10 01:58:20 +00:00
|
|
|
"github.com/mitchellh/cli"
|
2018-01-16 21:35:32 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2017-02-10 01:58:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestOperator_Raft_RemovePeers_Implements(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-02-10 01:58:20 +00:00
|
|
|
var _ cli.Command = &OperatorRaftRemoveCommand{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOperator_Raft_RemovePeer(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-01-16 21:35:32 +00:00
|
|
|
assert := assert.New(t)
|
|
|
|
s, _, addr := testServer(t, false, nil)
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
2020-10-05 14:07:41 +00:00
|
|
|
ui := cli.NewMockUi()
|
2018-01-16 21:35:32 +00:00
|
|
|
c := &OperatorRaftRemoveCommand{Meta: Meta{Ui: ui}}
|
|
|
|
args := []string{"-address=" + addr, "-peer-address=nope", "-peer-id=nope"}
|
|
|
|
|
|
|
|
// Give both an address and ID
|
|
|
|
code := c.Run(args)
|
|
|
|
if code != 1 {
|
|
|
|
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Contains(ui.ErrorWriter.String(), "cannot give both an address and id")
|
|
|
|
|
|
|
|
// Neither address nor ID present
|
|
|
|
args = args[:1]
|
|
|
|
code = c.Run(args)
|
|
|
|
if code != 1 {
|
|
|
|
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Contains(ui.ErrorWriter.String(), "an address or id is required for the peer to remove")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOperator_Raft_RemovePeerAddress(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-01-16 21:35:32 +00:00
|
|
|
assert := assert.New(t)
|
2017-07-21 04:07:32 +00:00
|
|
|
s, _, addr := testServer(t, false, nil)
|
|
|
|
defer s.Shutdown()
|
2017-02-10 01:58:20 +00:00
|
|
|
|
2020-10-05 14:07:41 +00:00
|
|
|
ui := cli.NewMockUi()
|
2017-02-10 01:58:20 +00:00
|
|
|
c := &OperatorRaftRemoveCommand{Meta: Meta{Ui: ui}}
|
|
|
|
args := []string{"-address=" + addr, "-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.
|
2018-01-16 21:35:32 +00:00
|
|
|
assert.Contains(ui.ErrorWriter.String(), "address \"nope\" was not found in the Raft configuration")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOperator_Raft_RemovePeerID(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-01-16 21:35:32 +00:00
|
|
|
assert := assert.New(t)
|
|
|
|
s, _, addr := testServer(t, false, nil)
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
2020-10-05 14:07:41 +00:00
|
|
|
ui := cli.NewMockUi()
|
2018-01-16 21:35:32 +00:00
|
|
|
c := &OperatorRaftRemoveCommand{Meta: Meta{Ui: ui}}
|
|
|
|
args := []string{"-address=" + addr, "-peer-id=nope"}
|
|
|
|
|
|
|
|
code := c.Run(args)
|
|
|
|
if code != 1 {
|
|
|
|
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
2017-02-10 01:58:20 +00:00
|
|
|
}
|
2018-01-16 21:35:32 +00:00
|
|
|
|
|
|
|
// If we get this error, it proves we sent the address all they through.
|
|
|
|
assert.Contains(ui.ErrorWriter.String(), "id \"nope\" was not found in the Raft configuration")
|
2017-02-10 01:58:20 +00:00
|
|
|
}
|