mirror of https://github.com/hashicorp/consul
103 lines
1.8 KiB
Go
103 lines
1.8 KiB
Go
package restore
|
|
|
|
import (
|
|
"io"
|
|
"os"
|
|
"path"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/consul/agent"
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
|
"github.com/mitchellh/cli"
|
|
)
|
|
|
|
func TestSnapshotRestoreCommand_noTabs(t *testing.T) {
|
|
t.Parallel()
|
|
if strings.ContainsRune(New(cli.NewMockUi()).Help(), '\t') {
|
|
t.Fatal("help has tabs")
|
|
}
|
|
}
|
|
|
|
func TestSnapshotRestoreCommand_Validation(t *testing.T) {
|
|
t.Parallel()
|
|
ui := cli.NewMockUi()
|
|
c := New(ui)
|
|
|
|
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(t *testing.T) {
|
|
t.Parallel()
|
|
a := agent.NewTestAgent(t, ``)
|
|
defer a.Shutdown()
|
|
client := a.Client()
|
|
|
|
ui := cli.NewMockUi()
|
|
c := New(ui)
|
|
|
|
dir := testutil.TempDir(t, "snapshot")
|
|
defer os.RemoveAll(dir)
|
|
|
|
file := path.Join(dir, "backup.tgz")
|
|
args := []string{
|
|
"-http-addr=" + a.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())
|
|
}
|
|
}
|