Update step-down command

This commit is contained in:
Seth Vargo 2017-09-05 00:04:46 -04:00
parent 530144f7f7
commit c14629cb5d
No known key found for this signature in database
GPG Key ID: C921994F9C27E0FF
3 changed files with 184 additions and 55 deletions

View File

@ -1,55 +0,0 @@
package command
import (
"fmt"
"strings"
"github.com/hashicorp/vault/meta"
)
// StepDownCommand is a Command that seals the vault.
type StepDownCommand struct {
meta.Meta
}
func (c *StepDownCommand) Run(args []string) int {
flags := c.Meta.FlagSet("step-down", meta.FlagSetDefault)
flags.Usage = func() { c.Ui.Error(c.Help()) }
if err := flags.Parse(args); err != nil {
return 1
}
client, err := c.Client()
if err != nil {
c.Ui.Error(fmt.Sprintf(
"Error initializing client: %s", err))
return 2
}
if err := client.Sys().StepDown(); err != nil {
c.Ui.Error(fmt.Sprintf("Error stepping down: %s", err))
return 1
}
return 0
}
func (c *StepDownCommand) Synopsis() string {
return "Force the Vault node to give up active duty"
}
func (c *StepDownCommand) Help() string {
helpText := `
Usage: vault step-down [options]
Force the Vault node to step down from active duty.
This causes the indicated node to give up active status. Note that while the
affected node will have a short delay before attempting to grab the lock
again, if no other node grabs the lock beforehand, it is possible for the
same node to re-grab the lock and become active again.
General Options:
` + meta.GeneralOptionsUsage()
return strings.TrimSpace(helpText)
}

85
command/step_down.go Normal file
View File

@ -0,0 +1,85 @@
package command
import (
"fmt"
"strings"
"github.com/mitchellh/cli"
"github.com/posener/complete"
)
// Ensure we are implementing the right interfaces.
var _ cli.Command = (*StepDownCommand)(nil)
var _ cli.CommandAutocomplete = (*StepDownCommand)(nil)
// StepDownCommand is a Command that tells the Vault server to give up its
// leadership
type StepDownCommand struct {
*BaseCommand
}
func (c *StepDownCommand) Synopsis() string {
return "Forces Vault to resign active duty"
}
func (c *StepDownCommand) Help() string {
helpText := `
Usage: vault step-down [options]
Forces the Vault server at the given address to step down from active duty.
While the affected node will have a delay before attempting to acquire the
leader lock again, if no other Vault nodes acquire the lock beforehand, it
is possible for the same node to re-acquire the lock and become active
again.
Force Vault to step down as the leader:
$ vault step-down
For a full list of examples, please see the documentation.
` + c.Flags().Help()
return strings.TrimSpace(helpText)
}
func (c *StepDownCommand) Flags() *FlagSets {
return c.flagSet(FlagSetHTTP)
}
func (c *StepDownCommand) AutocompleteArgs() complete.Predictor {
return nil
}
func (c *StepDownCommand) AutocompleteFlags() complete.Flags {
return c.Flags().Completions()
}
func (c *StepDownCommand) Run(args []string) int {
f := c.Flags()
if err := f.Parse(args); err != nil {
c.UI.Error(err.Error())
return 1
}
args = f.Args()
if len(args) > 0 {
c.UI.Error(fmt.Sprintf("Too many arguments (expected 0, got %d)", len(args)))
return 1
}
client, err := c.Client()
if err != nil {
c.UI.Error(err.Error())
return 2
}
if err := client.Sys().StepDown(); err != nil {
c.UI.Error(fmt.Sprintf("Error stepping down: %s", err))
return 2
}
c.UI.Output(fmt.Sprintf("Success! Stepped down: %s", client.Address()))
return 0
}

99
command/step_down_test.go Normal file
View File

@ -0,0 +1,99 @@
package command
import (
"strings"
"testing"
"github.com/mitchellh/cli"
)
func testStepDownCommand(tb testing.TB) (*cli.MockUi, *StepDownCommand) {
tb.Helper()
ui := cli.NewMockUi()
return ui, &StepDownCommand{
BaseCommand: &BaseCommand{
UI: ui,
},
}
}
func TestStepDownCommand_Run(t *testing.T) {
t.Parallel()
cases := []struct {
name string
args []string
out string
code int
}{
{
"too_many_args",
[]string{"foo"},
"Too many arguments",
1,
},
{
"default",
nil,
"Success! Stepped down: ",
0,
},
}
t.Run("validations", func(t *testing.T) {
t.Parallel()
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
client, closer := testVaultServer(t)
defer closer()
ui, cmd := testStepDownCommand(t)
cmd.client = client
code := cmd.Run(tc.args)
if code != tc.code {
t.Errorf("expected %d to be %d", code, tc.code)
}
combined := ui.OutputWriter.String() + ui.ErrorWriter.String()
if !strings.Contains(combined, tc.out) {
t.Errorf("expected %q to contain %q", combined, tc.out)
}
})
}
})
t.Run("communication_failure", func(t *testing.T) {
t.Parallel()
client, closer := testVaultServerBad(t)
defer closer()
ui, cmd := testStepDownCommand(t)
cmd.client = client
code := cmd.Run([]string{})
if exp := 2; code != exp {
t.Errorf("expected %d to be %d", code, exp)
}
expected := "Error stepping down: "
combined := ui.OutputWriter.String() + ui.ErrorWriter.String()
if !strings.Contains(combined, expected) {
t.Errorf("expected %q to contain %q", combined, expected)
}
})
t.Run("no_tabs", func(t *testing.T) {
t.Parallel()
_, cmd := testStepDownCommand(t)
assertNoTabs(t, cmd)
})
}