2016-10-26 02:20:24 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2017-02-10 01:00:38 +00:00
|
|
|
"github.com/hashicorp/consul/command/base"
|
2017-05-12 13:41:13 +00:00
|
|
|
"github.com/hashicorp/consul/testutil"
|
2016-10-26 02:20:24 +00:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
2017-02-10 01:00:38 +00:00
|
|
|
func testSnapshotRestoreCommand(t *testing.T) (*cli.MockUi, *SnapshotRestoreCommand) {
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
return ui, &SnapshotRestoreCommand{
|
|
|
|
Command: base.Command{
|
2017-04-21 00:02:42 +00:00
|
|
|
UI: ui,
|
2017-02-10 01:00:38 +00:00
|
|
|
Flags: base.FlagSetHTTP,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-26 02:20:24 +00:00
|
|
|
func TestSnapshotRestoreCommand_implements(t *testing.T) {
|
|
|
|
var _ cli.Command = &SnapshotRestoreCommand{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSnapshotRestoreCommand_noTabs(t *testing.T) {
|
|
|
|
assertNoTabs(t, new(SnapshotRestoreCommand))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSnapshotRestoreCommand_Validation(t *testing.T) {
|
2017-02-10 01:00:38 +00:00
|
|
|
ui, c := testSnapshotRestoreCommand(t)
|
2016-10-26 02:20:24 +00:00
|
|
|
|
|
|
|
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 {
|
|
|
|
// 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 TestSnapshotRestoreCommand_Run(t *testing.T) {
|
|
|
|
srv, client := testAgentWithAPIClient(t)
|
|
|
|
defer srv.Shutdown()
|
|
|
|
waitForLeader(t, srv.httpAddr)
|
|
|
|
|
2017-02-10 01:00:38 +00:00
|
|
|
ui, c := testSnapshotRestoreCommand(t)
|
2016-10-26 02:20:24 +00:00
|
|
|
|
2017-05-12 13:41:13 +00:00
|
|
|
dir := testutil.TempDir(t, "snapshot")
|
2016-10-26 02:20:24 +00:00
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
file := path.Join(dir, "backup.tgz")
|
|
|
|
args := []string{
|
|
|
|
"-http-addr=" + srv.httpAddr,
|
|
|
|
file,
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Create(file)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
snap, _, err := client.Snapshot().Save(nil)
|
|
|
|
if err != nil {
|
|
|
|
f.Close()
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if _, err := io.Copy(f, snap); err != nil {
|
|
|
|
f.Close()
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
code := c.Run(args)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
}
|