mirror of
https://github.com/status-im/consul.git
synced 2025-01-10 05:45:46 +00:00
a49711b8bf
This patch refactors the commands that use the mitchellh/cli library to populate the command line flag set in both the Run() and the Help() method. Earlier versions of the mitchellh/cli library relied on the Run() method to populuate the flagset for generating the usage screen. This has changed in later versions and was previously solved with a small monkey patch to the library to restore the old behavior. However, this makes upgrading the library difficult since the patch has to be restored every time. This patch addresses this by moving the command line flags into an initFlags() method where appropriate and also moving all variables for the flags from the Run() method into the command itself. Fixes #3536
106 lines
2.0 KiB
Go
106 lines
2.0 KiB
Go
package command
|
|
|
|
import (
|
|
"os"
|
|
"path"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/consul/agent"
|
|
"github.com/hashicorp/consul/testutil"
|
|
"github.com/mitchellh/cli"
|
|
)
|
|
|
|
func testSnapshotSaveCommand(t *testing.T) (*cli.MockUi, *SnapshotSaveCommand) {
|
|
ui := cli.NewMockUi()
|
|
return ui, &SnapshotSaveCommand{
|
|
BaseCommand: BaseCommand{
|
|
UI: ui,
|
|
Flags: FlagSetHTTP,
|
|
},
|
|
}
|
|
}
|
|
|
|
func TestSnapshotSaveCommand_implements(t *testing.T) {
|
|
t.Parallel()
|
|
var _ cli.Command = &SnapshotSaveCommand{}
|
|
}
|
|
|
|
func TestSnapshotSaveCommand_noTabs(t *testing.T) {
|
|
t.Parallel()
|
|
assertNoTabs(t, new(SnapshotSaveCommand))
|
|
}
|
|
|
|
func TestSnapshotSaveCommand_Validation(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
cases := map[string]struct {
|
|
args []string
|
|
output string
|
|
}{
|
|
"no file": {
|
|
[]string{},
|
|
"Missing FILE argument",
|
|
},
|
|
"extra args": {
|
|
[]string{"foo", "bar", "baz"},
|
|
"Too many arguments",
|
|
},
|
|
}
|
|
|
|
for name, tc := range cases {
|
|
ui, c := testSnapshotSaveCommand(t)
|
|
|
|
// Ensure our buffer is always clear
|
|
if ui.ErrorWriter != nil {
|
|
ui.ErrorWriter.Reset()
|
|
}
|
|
if ui.OutputWriter != nil {
|
|
ui.OutputWriter.Reset()
|
|
}
|
|
|
|
code := c.Run(tc.args)
|
|
if code == 0 {
|
|
t.Errorf("%s: expected non-zero exit", name)
|
|
}
|
|
|
|
output := ui.ErrorWriter.String()
|
|
if !strings.Contains(output, tc.output) {
|
|
t.Errorf("%s: expected %q to contain %q", name, output, tc.output)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSnapshotSaveCommand_Run(t *testing.T) {
|
|
t.Parallel()
|
|
a := agent.NewTestAgent(t.Name(), ``)
|
|
defer a.Shutdown()
|
|
client := a.Client()
|
|
|
|
ui, c := testSnapshotSaveCommand(t)
|
|
|
|
dir := testutil.TempDir(t, "snapshot")
|
|
defer os.RemoveAll(dir)
|
|
|
|
file := path.Join(dir, "backup.tgz")
|
|
args := []string{
|
|
"-http-addr=" + a.HTTPAddr(),
|
|
file,
|
|
}
|
|
|
|
code := c.Run(args)
|
|
if code != 0 {
|
|
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
|
}
|
|
|
|
f, err := os.Open(file)
|
|
if err != nil {
|
|
t.Fatalf("err: %v", err)
|
|
}
|
|
defer f.Close()
|
|
|
|
if err := client.Snapshot().Restore(nil, f); err != nil {
|
|
t.Fatalf("err: %v", err)
|
|
}
|
|
}
|