restic/cmd/restic/integration_test.go

296 lines
7.5 KiB
Go
Raw Normal View History

package main
import (
2015-05-10 00:41:16 +00:00
"bufio"
2015-06-21 13:01:52 +00:00
"bytes"
2015-06-14 13:19:11 +00:00
"crypto/rand"
"fmt"
2015-05-10 00:41:16 +00:00
"io"
"os"
"os/exec"
"path/filepath"
2015-06-21 13:01:52 +00:00
"regexp"
"testing"
2015-05-10 00:41:16 +00:00
"github.com/restic/restic/backend"
. "github.com/restic/restic/test"
)
func setupTarTestFixture(t testing.TB, outputDir, tarFile string) {
2015-06-21 11:27:56 +00:00
err := system("sh", "-c", `(cd "$1" && tar xz) < "$2"`,
"sh", outputDir, tarFile)
OK(t, err)
}
func system(command string, args ...string) error {
cmd := exec.Command(command, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
2015-05-10 00:41:16 +00:00
func parseIDsFromReader(t testing.TB, rd io.Reader) backend.IDs {
IDs := backend.IDs{}
sc := bufio.NewScanner(rd)
for sc.Scan() {
id, err := backend.ParseID(sc.Text())
if err != nil {
t.Logf("parse id %v: %v", sc.Text(), err)
continue
}
IDs = append(IDs, id)
}
return IDs
}
2015-06-21 11:27:56 +00:00
func cmdInit(t testing.TB, global GlobalOptions) {
cmd := &CmdInit{global: &global}
OK(t, cmd.Execute(nil))
2015-06-21 11:27:56 +00:00
t.Logf("repository initialized at %v", global.Repo)
}
2015-06-21 11:27:56 +00:00
func cmdBackup(t testing.TB, global GlobalOptions, target []string, parentID backend.ID) {
cmd := &CmdBackup{global: &global}
2015-05-10 00:41:16 +00:00
cmd.Parent = parentID.String()
t.Logf("backing up %v", target)
OK(t, cmd.Execute(target))
}
2015-06-21 11:27:56 +00:00
func cmdList(t testing.TB, global GlobalOptions, tpe string) []backend.ID {
2015-06-21 13:01:52 +00:00
var buf bytes.Buffer
global.stdout = &buf
2015-06-21 11:27:56 +00:00
cmd := &CmdList{global: &global}
2015-05-10 00:41:16 +00:00
2015-06-21 13:01:52 +00:00
OK(t, cmd.Execute([]string{tpe}))
IDs := parseIDsFromReader(t, &buf)
2015-05-10 00:41:16 +00:00
return IDs
}
2015-06-21 11:27:56 +00:00
func cmdRestore(t testing.TB, global GlobalOptions, dir string, snapshotID backend.ID) {
cmd := &CmdRestore{global: &global}
2015-05-10 00:41:16 +00:00
cmd.Execute([]string{snapshotID.String(), dir})
}
2015-06-21 11:27:56 +00:00
func cmdFsck(t testing.TB, global GlobalOptions) {
cmd := &CmdFsck{global: &global, CheckData: true, Orphaned: true}
2015-05-10 00:41:16 +00:00
OK(t, cmd.Execute(nil))
}
func TestBackup(t *testing.T) {
2015-06-21 11:27:56 +00:00
withTestEnvironment(t, func(env *testEnvironment, global GlobalOptions) {
2015-06-14 13:19:11 +00:00
datafile := filepath.Join("testdata", "backup-data.tar.gz")
fd, err := os.Open(datafile)
if os.IsNotExist(err) {
2015-06-18 19:28:38 +00:00
t.Skipf("unable to find data file %q, skipping", datafile)
2015-06-14 13:19:11 +00:00
return
}
OK(t, err)
OK(t, fd.Close())
2015-06-21 11:27:56 +00:00
cmdInit(t, global)
2015-06-14 13:19:11 +00:00
2015-06-21 11:27:56 +00:00
setupTarTestFixture(t, env.testdata, datafile)
2015-06-14 13:19:11 +00:00
// first backup
2015-06-21 11:27:56 +00:00
cmdBackup(t, global, []string{env.testdata}, nil)
snapshotIDs := cmdList(t, global, "snapshots")
2015-06-14 13:19:11 +00:00
Assert(t, len(snapshotIDs) == 1,
2015-06-21 11:27:56 +00:00
"expected one snapshot, got %v", snapshotIDs)
2015-06-14 13:19:11 +00:00
2015-06-21 11:27:56 +00:00
cmdFsck(t, global)
2015-06-14 13:19:11 +00:00
stat1 := dirStats(env.repo)
// second backup, implicit incremental
2015-06-21 11:27:56 +00:00
cmdBackup(t, global, []string{env.testdata}, nil)
snapshotIDs = cmdList(t, global, "snapshots")
2015-06-14 13:19:11 +00:00
Assert(t, len(snapshotIDs) == 2,
2015-06-21 11:27:56 +00:00
"expected two snapshots, got %v", snapshotIDs)
2015-06-14 13:19:11 +00:00
stat2 := dirStats(env.repo)
if stat2.size > stat1.size+stat1.size/10 {
t.Error("repository size has grown by more than 10 percent")
}
t.Logf("repository grown by %d bytes", stat2.size-stat1.size)
2015-06-21 11:27:56 +00:00
cmdFsck(t, global)
2015-06-14 13:19:11 +00:00
// third backup, explicit incremental
2015-06-21 11:27:56 +00:00
cmdBackup(t, global, []string{env.testdata}, snapshotIDs[0])
snapshotIDs = cmdList(t, global, "snapshots")
2015-06-14 13:19:11 +00:00
Assert(t, len(snapshotIDs) == 3,
2015-06-21 11:27:56 +00:00
"expected three snapshots, got %v", snapshotIDs)
2015-06-14 13:19:11 +00:00
stat3 := dirStats(env.repo)
if stat3.size > stat1.size+stat1.size/10 {
t.Error("repository size has grown by more than 10 percent")
}
t.Logf("repository grown by %d bytes", stat3.size-stat2.size)
// restore all backups and compare
for i, snapshotID := range snapshotIDs {
restoredir := filepath.Join(env.base, fmt.Sprintf("restore%d", i))
t.Logf("restoring snapshot %v to %v", snapshotID.Str(), restoredir)
2015-06-21 11:27:56 +00:00
cmdRestore(t, global, restoredir, snapshotIDs[0])
Assert(t, directoriesEqualContents(env.testdata, filepath.Join(restoredir, "testdata")),
2015-06-14 13:19:11 +00:00
"directories are not equal")
}
2015-06-21 11:27:56 +00:00
cmdFsck(t, global)
2015-06-14 13:19:11 +00:00
})
}
const (
incrementalFirstWrite = 20 * 1042 * 1024
incrementalSecondWrite = 12 * 1042 * 1024
incrementalThirdWrite = 4 * 1042 * 1024
)
func appendRandomData(filename string, bytes uint) error {
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
fmt.Fprint(os.Stderr, err)
return err
}
2015-06-14 13:19:11 +00:00
_, err = f.Seek(0, 2)
if err != nil {
fmt.Fprint(os.Stderr, err)
return err
}
2015-06-14 13:19:11 +00:00
_, err = io.Copy(f, io.LimitReader(rand.Reader, int64(bytes)))
if err != nil {
fmt.Fprint(os.Stderr, err)
return err
}
2015-06-14 13:19:11 +00:00
return f.Close()
}
2015-06-14 13:19:11 +00:00
func TestIncrementalBackup(t *testing.T) {
2015-06-21 11:27:56 +00:00
withTestEnvironment(t, func(env *testEnvironment, global GlobalOptions) {
2015-06-14 13:19:11 +00:00
datafile := filepath.Join("testdata", "backup-data.tar.gz")
fd, err := os.Open(datafile)
if os.IsNotExist(err) {
2015-06-18 19:28:38 +00:00
t.Skipf("unable to find data file %q, skipping", datafile)
2015-06-14 13:19:11 +00:00
return
}
OK(t, err)
OK(t, fd.Close())
2015-06-21 11:27:56 +00:00
cmdInit(t, global)
2015-06-14 13:19:11 +00:00
datadir := filepath.Join(env.base, "testdata")
testfile := filepath.Join(datadir, "testfile")
2015-06-14 13:19:11 +00:00
OK(t, appendRandomData(testfile, incrementalFirstWrite))
2015-05-10 00:41:16 +00:00
2015-06-21 11:27:56 +00:00
cmdBackup(t, global, []string{datadir}, nil)
cmdFsck(t, global)
2015-06-14 13:19:11 +00:00
stat1 := dirStats(env.repo)
2015-05-10 00:41:16 +00:00
2015-06-14 13:19:11 +00:00
OK(t, appendRandomData(testfile, incrementalSecondWrite))
2015-05-10 00:41:16 +00:00
2015-06-21 11:27:56 +00:00
cmdBackup(t, global, []string{datadir}, nil)
cmdFsck(t, global)
2015-06-14 13:19:11 +00:00
stat2 := dirStats(env.repo)
if stat2.size-stat1.size > incrementalFirstWrite {
t.Errorf("repository size has grown by more than %d bytes", incrementalFirstWrite)
}
t.Logf("repository grown by %d bytes", stat2.size-stat1.size)
2015-05-10 00:41:16 +00:00
2015-06-14 13:19:11 +00:00
OK(t, appendRandomData(testfile, incrementalThirdWrite))
2015-06-21 11:27:56 +00:00
cmdBackup(t, global, []string{datadir}, nil)
cmdFsck(t, global)
2015-06-14 13:19:11 +00:00
stat3 := dirStats(env.repo)
if stat3.size-stat2.size > incrementalFirstWrite {
t.Errorf("repository size has grown by more than %d bytes", incrementalFirstWrite)
}
t.Logf("repository grown by %d bytes", stat3.size-stat2.size)
})
}
2015-06-18 19:28:50 +00:00
2015-06-21 13:01:52 +00:00
func cmdKey(t testing.TB, global GlobalOptions, args ...string) string {
var buf bytes.Buffer
global.stdout = &buf
cmd := &CmdKey{global: &global}
OK(t, cmd.Execute(args))
return buf.String()
}
func cmdKeyListOtherIDs(t testing.TB, global GlobalOptions) []string {
var buf bytes.Buffer
global.stdout = &buf
cmd := &CmdKey{global: &global}
OK(t, cmd.Execute([]string{"list"}))
scanner := bufio.NewScanner(&buf)
exp := regexp.MustCompile(`^ ([a-f0-9]+) `)
IDs := []string{}
for scanner.Scan() {
if id := exp.FindStringSubmatch(scanner.Text()); id != nil {
IDs = append(IDs, id[1])
}
}
return IDs
}
func cmdKeyAddNewKey(t testing.TB, global GlobalOptions, newPassword string) {
cmd := &CmdKey{global: &global, newPassword: newPassword}
OK(t, cmd.Execute([]string{"add"}))
}
func cmdKeyPasswd(t testing.TB, global GlobalOptions, newPassword string) {
cmd := &CmdKey{global: &global, newPassword: newPassword}
OK(t, cmd.Execute([]string{"passwd"}))
}
func cmdKeyRemove(t testing.TB, global GlobalOptions, IDs []string) {
cmd := &CmdKey{global: &global}
t.Logf("remove %d keys: %q\n", len(IDs), IDs)
for _, id := range IDs {
OK(t, cmd.Execute([]string{"rm", id}))
}
}
2015-06-18 19:28:50 +00:00
func TestKeyAddRemove(t *testing.T) {
2015-06-21 13:01:52 +00:00
passwordList := []string{
"OnnyiasyatvodsEvVodyawit",
"raicneirvOjEfEigonOmLasOd",
}
2015-06-21 11:27:56 +00:00
withTestEnvironment(t, func(env *testEnvironment, global GlobalOptions) {
2015-06-21 13:01:52 +00:00
cmdInit(t, global)
cmdKeyPasswd(t, global, "geheim2")
global.password = "geheim2"
t.Logf("changed password to %q", global.password)
for _, newPassword := range passwordList {
cmdKeyAddNewKey(t, global, newPassword)
t.Logf("added new password %q", newPassword)
global.password = newPassword
cmdKeyRemove(t, global, cmdKeyListOtherIDs(t, global))
2015-06-18 19:28:50 +00:00
}
2015-06-21 13:01:52 +00:00
global.password = passwordList[len(passwordList)-1]
t.Logf("testing access with last password %q\n", global.password)
2015-06-21 11:27:56 +00:00
cmdKey(t, global, "list")
2015-06-21 13:01:52 +00:00
cmdFsck(t, global)
2015-06-18 19:28:50 +00:00
})
}