2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-15 17:47:21 +00:00
restic/src/cmds/restic/integration_test.go

1220 lines
33 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"
"encoding/json"
2015-06-14 13:19:11 +00:00
"fmt"
2015-05-10 00:41:16 +00:00
"io"
"io/ioutil"
2017-01-24 10:51:21 +00:00
mrand "math/rand"
"os"
"path/filepath"
2015-06-21 13:01:52 +00:00
"regexp"
"restic"
"strings"
2015-07-08 21:36:24 +00:00
"syscall"
"testing"
2015-07-08 21:36:24 +00:00
"time"
2016-09-01 20:17:37 +00:00
"restic/errors"
"restic/debug"
"restic/filter"
"restic/repository"
. "restic/test"
)
2016-09-01 14:04:29 +00:00
func parseIDsFromReader(t testing.TB, rd io.Reader) restic.IDs {
IDs := restic.IDs{}
2015-05-10 00:41:16 +00:00
sc := bufio.NewScanner(rd)
for sc.Scan() {
2016-09-01 17:06:53 +00:00
id, err := restic.ParseID(sc.Text())
2015-05-10 00:41:16 +00:00
if err != nil {
t.Logf("parse id %v: %v", sc.Text(), err)
continue
}
IDs = append(IDs, id)
}
return IDs
}
2016-09-17 10:36:05 +00:00
func testRunInit(t testing.TB, opts GlobalOptions) {
repository.TestUseLowSecurityKDFParameters(t)
2016-09-01 19:13:06 +00:00
restic.TestSetLockTimeout(t, 0)
2016-09-17 10:36:05 +00:00
OK(t, runInit(opts, nil))
t.Logf("repository initialized at %v", opts.Repo)
}
2016-09-17 10:36:05 +00:00
func testRunBackup(t testing.TB, target []string, opts BackupOptions, gopts GlobalOptions) {
t.Logf("backing up %v", target)
2016-09-17 10:36:05 +00:00
OK(t, runBackup(opts, gopts, target))
}
2016-09-17 10:36:05 +00:00
func testRunList(t testing.TB, tpe string, opts GlobalOptions) restic.IDs {
buf := bytes.NewBuffer(nil)
2016-09-17 10:36:05 +00:00
globalOptions.stdout = buf
defer func() {
globalOptions.stdout = os.Stdout
}()
OK(t, runList(opts, []string{tpe}))
return parseIDsFromReader(t, buf)
}
2015-05-10 00:41:16 +00:00
2016-09-17 10:36:05 +00:00
func testRunRestore(t testing.TB, opts GlobalOptions, dir string, snapshotID restic.ID) {
testRunRestoreExcludes(t, opts, dir, snapshotID, nil)
}
2016-09-17 10:36:05 +00:00
func testRunRestoreLatest(t testing.TB, gopts GlobalOptions, dir string, paths []string, host string) {
opts := RestoreOptions{
Target: dir,
Host: host,
Paths: paths,
}
OK(t, runRestore(opts, gopts, []string{"latest"}))
}
2016-09-17 10:36:05 +00:00
func testRunRestoreExcludes(t testing.TB, gopts GlobalOptions, dir string, snapshotID restic.ID, excludes []string) {
opts := RestoreOptions{
Target: dir,
Exclude: excludes,
}
OK(t, runRestore(opts, gopts, []string{snapshotID.String()}))
2015-05-10 00:41:16 +00:00
}
2016-09-17 10:36:05 +00:00
func testRunRestoreIncludes(t testing.TB, gopts GlobalOptions, dir string, snapshotID restic.ID, includes []string) {
opts := RestoreOptions{
Target: dir,
Include: includes,
}
OK(t, runRestore(opts, gopts, []string{snapshotID.String()}))
}
2016-09-17 10:36:05 +00:00
func testRunCheck(t testing.TB, gopts GlobalOptions) {
opts := CheckOptions{
ReadData: true,
CheckUnused: true,
}
2016-09-17 10:36:05 +00:00
OK(t, runCheck(opts, gopts, nil))
2015-05-10 00:41:16 +00:00
}
2016-09-17 10:36:05 +00:00
func testRunCheckOutput(gopts GlobalOptions) (string, error) {
2015-10-25 16:24:52 +00:00
buf := bytes.NewBuffer(nil)
2016-09-17 10:36:05 +00:00
globalOptions.stdout = buf
defer func() {
globalOptions.stdout = os.Stdout
}()
opts := CheckOptions{
ReadData: true,
}
err := runCheck(opts, gopts, nil)
return string(buf.Bytes()), err
2015-10-25 16:24:52 +00:00
}
2016-09-17 10:36:05 +00:00
func testRunRebuildIndex(t testing.TB, gopts GlobalOptions) {
globalOptions.stdout = ioutil.Discard
defer func() {
globalOptions.stdout = os.Stdout
}()
OK(t, runRebuildIndex(gopts))
2015-10-25 16:24:52 +00:00
}
2016-09-17 10:36:05 +00:00
func testRunLs(t testing.TB, gopts GlobalOptions, snapshotID string) []string {
buf := bytes.NewBuffer(nil)
globalOptions.stdout = buf
quiet := globalOptions.Quiet
globalOptions.Quiet = true
defer func() {
globalOptions.stdout = os.Stdout
globalOptions.Quiet = quiet
}()
2016-09-17 10:36:05 +00:00
OK(t, runLs(gopts, []string{snapshotID}))
return strings.Split(string(buf.Bytes()), "\n")
}
2016-09-17 10:36:05 +00:00
func testRunFind(t testing.TB, gopts GlobalOptions, pattern string) []string {
buf := bytes.NewBuffer(nil)
globalOptions.stdout = buf
defer func() {
globalOptions.stdout = os.Stdout
}()
opts := FindOptions{}
2015-08-28 17:31:05 +00:00
2016-09-17 10:36:05 +00:00
OK(t, runFind(opts, gopts, []string{pattern}))
2015-08-28 17:31:05 +00:00
return strings.Split(string(buf.Bytes()), "\n")
}
func testRunSnapshots(t testing.TB, gopts GlobalOptions) (*Snapshot, map[string]Snapshot) {
buf := bytes.NewBuffer(nil)
globalOptions.stdout = buf
globalOptions.JSON = true
defer func() {
globalOptions.stdout = os.Stdout
globalOptions.JSON = gopts.JSON
}()
opts := SnapshotOptions{}
OK(t, runSnapshots(opts, globalOptions, []string{}))
snapshots := []Snapshot{}
OK(t, json.Unmarshal(buf.Bytes(), &snapshots))
var newest *Snapshot
snapmap := make(map[string]Snapshot, len(snapshots))
for _, sn := range snapshots {
snapmap[sn.ID] = sn
if newest == nil || sn.Time.After(newest.Time) {
newest = &sn
}
}
return newest, snapmap
}
func testRunForget(t testing.TB, gopts GlobalOptions, args ...string) {
opts := ForgetOptions{}
OK(t, runForget(opts, gopts, args))
}
func testRunPrune(t testing.TB, gopts GlobalOptions) {
OK(t, runPrune(gopts))
}
func TestBackup(t *testing.T) {
2016-09-17 10:36:05 +00:00
withTestEnvironment(t, func(env *testEnvironment, gopts 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(errors.Cause(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())
2016-09-17 10:36:05 +00:00
testRunInit(t, gopts)
2015-06-14 13:19:11 +00:00
SetupTarTestFixture(t, env.testdata, datafile)
2016-09-17 10:36:05 +00:00
opts := BackupOptions{}
2015-06-14 13:19:11 +00:00
// first backup
2016-09-17 10:36:05 +00:00
testRunBackup(t, []string{env.testdata}, opts, gopts)
snapshotIDs := testRunList(t, "snapshots", gopts)
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
2016-09-17 10:36:05 +00:00
testRunCheck(t, gopts)
2015-06-14 13:19:11 +00:00
stat1 := dirStats(env.repo)
// second backup, implicit incremental
2016-09-17 10:36:05 +00:00
testRunBackup(t, []string{env.testdata}, opts, gopts)
snapshotIDs = testRunList(t, "snapshots", gopts)
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)
2016-09-17 10:36:05 +00:00
testRunCheck(t, gopts)
2015-06-14 13:19:11 +00:00
// third backup, explicit incremental
2016-09-17 10:36:05 +00:00
opts.Parent = snapshotIDs[0].String()
testRunBackup(t, []string{env.testdata}, opts, gopts)
snapshotIDs = testRunList(t, "snapshots", gopts)
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)
2016-09-17 10:36:05 +00:00
testRunRestore(t, gopts, restoredir, snapshotIDs[0])
2015-06-21 11:27:56 +00:00
Assert(t, directoriesEqualContents(env.testdata, filepath.Join(restoredir, "testdata")),
2015-06-14 13:19:11 +00:00
"directories are not equal")
}
2016-09-17 10:36:05 +00:00
testRunCheck(t, gopts)
2015-06-14 13:19:11 +00:00
})
}
func TestBackupNonExistingFile(t *testing.T) {
2016-09-17 10:36:05 +00:00
withTestEnvironment(t, func(env *testEnvironment, gopts GlobalOptions) {
datafile := filepath.Join("testdata", "backup-data.tar.gz")
fd, err := os.Open(datafile)
if os.IsNotExist(errors.Cause(err)) {
t.Skipf("unable to find data file %q, skipping", datafile)
return
}
OK(t, err)
OK(t, fd.Close())
SetupTarTestFixture(t, env.testdata, datafile)
2016-09-17 10:36:05 +00:00
testRunInit(t, gopts)
globalOptions.stderr = ioutil.Discard
defer func() {
globalOptions.stderr = os.Stderr
}()
p := filepath.Join(env.testdata, "0", "0")
dirs := []string{
filepath.Join(p, "0"),
filepath.Join(p, "1"),
filepath.Join(p, "nonexisting"),
filepath.Join(p, "5"),
}
2016-09-17 10:36:05 +00:00
opts := BackupOptions{}
testRunBackup(t, dirs, opts, gopts)
})
}
func TestBackupMissingFile1(t *testing.T) {
2016-09-17 10:36:05 +00:00
withTestEnvironment(t, func(env *testEnvironment, gopts GlobalOptions) {
datafile := filepath.Join("testdata", "backup-data.tar.gz")
fd, err := os.Open(datafile)
if os.IsNotExist(errors.Cause(err)) {
t.Skipf("unable to find data file %q, skipping", datafile)
return
}
OK(t, err)
OK(t, fd.Close())
SetupTarTestFixture(t, env.testdata, datafile)
2016-09-17 10:36:05 +00:00
testRunInit(t, gopts)
globalOptions.stderr = ioutil.Discard
defer func() {
globalOptions.stderr = os.Stderr
}()
ranHook := false
debug.Hook("pipe.walk1", func(context interface{}) {
pathname := context.(string)
if pathname != filepath.Join("testdata", "0", "0", "9") {
return
}
t.Logf("in hook, removing test file testdata/0/0/9/37")
ranHook = true
OK(t, os.Remove(filepath.Join(env.testdata, "0", "0", "9", "37")))
})
2016-09-17 10:36:05 +00:00
opts := BackupOptions{}
testRunBackup(t, []string{env.testdata}, opts, gopts)
testRunCheck(t, gopts)
Assert(t, ranHook, "hook did not run")
debug.RemoveHook("pipe.walk1")
})
}
func TestBackupMissingFile2(t *testing.T) {
2016-09-17 10:36:05 +00:00
withTestEnvironment(t, func(env *testEnvironment, gopts GlobalOptions) {
datafile := filepath.Join("testdata", "backup-data.tar.gz")
fd, err := os.Open(datafile)
if os.IsNotExist(errors.Cause(err)) {
t.Skipf("unable to find data file %q, skipping", datafile)
return
}
OK(t, err)
OK(t, fd.Close())
SetupTarTestFixture(t, env.testdata, datafile)
2016-09-17 10:36:05 +00:00
testRunInit(t, gopts)
globalOptions.stderr = ioutil.Discard
defer func() {
globalOptions.stderr = os.Stderr
}()
ranHook := false
debug.Hook("pipe.walk2", func(context interface{}) {
pathname := context.(string)
if pathname != filepath.Join("testdata", "0", "0", "9", "37") {
return
}
t.Logf("in hook, removing test file testdata/0/0/9/37")
ranHook = true
OK(t, os.Remove(filepath.Join(env.testdata, "0", "0", "9", "37")))
})
2016-09-17 10:36:05 +00:00
opts := BackupOptions{}
testRunBackup(t, []string{env.testdata}, opts, gopts)
testRunCheck(t, gopts)
Assert(t, ranHook, "hook did not run")
debug.RemoveHook("pipe.walk2")
})
}
func TestBackupChangedFile(t *testing.T) {
withTestEnvironment(t, func(env *testEnvironment, gopts GlobalOptions) {
datafile := filepath.Join("testdata", "backup-data.tar.gz")
fd, err := os.Open(datafile)
if os.IsNotExist(errors.Cause(err)) {
t.Skipf("unable to find data file %q, skipping", datafile)
return
}
OK(t, err)
OK(t, fd.Close())
SetupTarTestFixture(t, env.testdata, datafile)
testRunInit(t, gopts)
globalOptions.stderr = ioutil.Discard
defer func() {
globalOptions.stderr = os.Stderr
}()
modFile := filepath.Join(env.testdata, "0", "0", "6", "18")
ranHook := false
debug.Hook("archiver.SaveFile", func(context interface{}) {
pathname := context.(string)
if pathname != modFile {
return
}
t.Logf("in hook, modifying test file %v", modFile)
ranHook = true
OK(t, ioutil.WriteFile(modFile, []byte("modified"), 0600))
})
opts := BackupOptions{}
testRunBackup(t, []string{env.testdata}, opts, gopts)
testRunCheck(t, gopts)
Assert(t, ranHook, "hook did not run")
debug.RemoveHook("archiver.SaveFile")
})
}
func TestBackupDirectoryError(t *testing.T) {
2016-09-17 10:36:05 +00:00
withTestEnvironment(t, func(env *testEnvironment, gopts GlobalOptions) {
datafile := filepath.Join("testdata", "backup-data.tar.gz")
fd, err := os.Open(datafile)
if os.IsNotExist(errors.Cause(err)) {
t.Skipf("unable to find data file %q, skipping", datafile)
return
}
OK(t, err)
OK(t, fd.Close())
SetupTarTestFixture(t, env.testdata, datafile)
2016-09-17 10:36:05 +00:00
testRunInit(t, gopts)
globalOptions.stderr = ioutil.Discard
defer func() {
globalOptions.stderr = os.Stderr
}()
ranHook := false
testdir := filepath.Join(env.testdata, "0", "0", "9")
// install hook that removes the dir right before readdirnames()
debug.Hook("pipe.readdirnames", func(context interface{}) {
path := context.(string)
if path != testdir {
return
}
t.Logf("in hook, removing test file %v", testdir)
ranHook = true
OK(t, os.RemoveAll(testdir))
})
2016-09-17 10:36:05 +00:00
testRunBackup(t, []string{filepath.Join(env.testdata, "0", "0")}, BackupOptions{}, gopts)
testRunCheck(t, gopts)
Assert(t, ranHook, "hook did not run")
debug.RemoveHook("pipe.walk2")
2016-09-17 10:36:05 +00:00
snapshots := testRunList(t, "snapshots", gopts)
Assert(t, len(snapshots) > 0,
"no snapshots found in repo (%v)", datafile)
2016-09-17 10:36:05 +00:00
files := testRunLs(t, gopts, snapshots[0].String())
Assert(t, len(files) > 1, "snapshot is empty")
})
}
func includes(haystack []string, needle string) bool {
for _, s := range haystack {
if s == needle {
return true
}
}
return false
}
2016-09-17 10:36:05 +00:00
func loadSnapshotMap(t testing.TB, gopts GlobalOptions) map[string]struct{} {
snapshotIDs := testRunList(t, "snapshots", gopts)
m := make(map[string]struct{})
for _, id := range snapshotIDs {
m[id.String()] = struct{}{}
}
return m
}
func lastSnapshot(old, new map[string]struct{}) (map[string]struct{}, string) {
for k := range new {
if _, ok := old[k]; !ok {
old[k] = struct{}{}
return old, k
}
}
return old, ""
}
var backupExcludeFilenames = []string{
"testfile1",
"foo.tar.gz",
"private/secret/passwords.txt",
"work/source/test.c",
}
func TestBackupExclude(t *testing.T) {
2016-09-17 10:36:05 +00:00
withTestEnvironment(t, func(env *testEnvironment, gopts GlobalOptions) {
testRunInit(t, gopts)
datadir := filepath.Join(env.base, "testdata")
for _, filename := range backupExcludeFilenames {
fp := filepath.Join(datadir, filename)
OK(t, os.MkdirAll(filepath.Dir(fp), 0755))
f, err := os.Create(fp)
OK(t, err)
fmt.Fprintf(f, filename)
OK(t, f.Close())
}
snapshots := make(map[string]struct{})
2016-09-17 10:36:05 +00:00
opts := BackupOptions{}
testRunBackup(t, []string{datadir}, opts, gopts)
snapshots, snapshotID := lastSnapshot(snapshots, loadSnapshotMap(t, gopts))
files := testRunLs(t, gopts, snapshotID)
Assert(t, includes(files, filepath.Join(string(filepath.Separator), "testdata", "foo.tar.gz")),
"expected file %q in first snapshot, but it's not included", "foo.tar.gz")
2016-09-17 10:36:05 +00:00
opts.Excludes = []string{"*.tar.gz"}
testRunBackup(t, []string{datadir}, opts, gopts)
snapshots, snapshotID = lastSnapshot(snapshots, loadSnapshotMap(t, gopts))
files = testRunLs(t, gopts, snapshotID)
Assert(t, !includes(files, filepath.Join(string(filepath.Separator), "testdata", "foo.tar.gz")),
"expected file %q not in first snapshot, but it's included", "foo.tar.gz")
2016-09-17 10:36:05 +00:00
opts.Excludes = []string{"*.tar.gz", "private/secret"}
testRunBackup(t, []string{datadir}, opts, gopts)
snapshots, snapshotID = lastSnapshot(snapshots, loadSnapshotMap(t, gopts))
files = testRunLs(t, gopts, snapshotID)
Assert(t, !includes(files, filepath.Join(string(filepath.Separator), "testdata", "foo.tar.gz")),
"expected file %q not in first snapshot, but it's included", "foo.tar.gz")
Assert(t, !includes(files, filepath.Join(string(filepath.Separator), "testdata", "private", "secret", "passwords.txt")),
"expected file %q not in first snapshot, but it's included", "passwords.txt")
})
}
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) {
2016-09-17 10:36:05 +00:00
withTestEnvironment(t, func(env *testEnvironment, gopts GlobalOptions) {
testRunInit(t, gopts)
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
2016-09-17 10:36:05 +00:00
opts := BackupOptions{}
testRunBackup(t, []string{datadir}, opts, gopts)
testRunCheck(t, gopts)
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
2016-09-17 10:36:05 +00:00
testRunBackup(t, []string{datadir}, opts, gopts)
testRunCheck(t, gopts)
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))
2016-09-17 10:36:05 +00:00
testRunBackup(t, []string{datadir}, opts, gopts)
testRunCheck(t, gopts)
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
func TestBackupTags(t *testing.T) {
withTestEnvironment(t, func(env *testEnvironment, gopts GlobalOptions) {
datafile := filepath.Join("testdata", "backup-data.tar.gz")
testRunInit(t, gopts)
SetupTarTestFixture(t, env.testdata, datafile)
opts := BackupOptions{}
testRunBackup(t, []string{env.testdata}, opts, gopts)
testRunCheck(t, gopts)
newest, _ := testRunSnapshots(t, gopts)
Assert(t, newest != nil, "expected a new backup, got nil")
Assert(t, len(newest.Tags) == 0,
"expected no tags, got %v", newest.Tags)
opts.Tags = []string{"NL"}
testRunBackup(t, []string{env.testdata}, opts, gopts)
testRunCheck(t, gopts)
newest, _ = testRunSnapshots(t, gopts)
Assert(t, newest != nil, "expected a new backup, got nil")
Assert(t, len(newest.Tags) == 1 && newest.Tags[0] == "NL",
"expected one NL tag, got %v", newest.Tags)
})
}
func testRunTag(t testing.TB, opts TagOptions, gopts GlobalOptions) {
OK(t, runTag(opts, gopts, []string{}))
}
func TestTag(t *testing.T) {
withTestEnvironment(t, func(env *testEnvironment, gopts GlobalOptions) {
datafile := filepath.Join("testdata", "backup-data.tar.gz")
testRunInit(t, gopts)
SetupTarTestFixture(t, env.testdata, datafile)
testRunBackup(t, []string{env.testdata}, BackupOptions{}, gopts)
testRunCheck(t, gopts)
newest, _ := testRunSnapshots(t, gopts)
Assert(t, newest != nil, "expected a new backup, got nil")
Assert(t, len(newest.Tags) == 0,
"expected no tags, got %v", newest.Tags)
testRunTag(t, TagOptions{SetTags: []string{"NL"}}, gopts)
testRunCheck(t, gopts)
newest, _ = testRunSnapshots(t, gopts)
Assert(t, newest != nil, "expected a new backup, got nil")
Assert(t, len(newest.Tags) == 1 && newest.Tags[0] == "NL",
"set failed, expected one NL tag, got %v", newest.Tags)
testRunTag(t, TagOptions{AddTags: []string{"CH"}}, gopts)
testRunCheck(t, gopts)
newest, _ = testRunSnapshots(t, gopts)
Assert(t, newest != nil, "expected a new backup, got nil")
Assert(t, len(newest.Tags) == 2 && newest.Tags[0] == "NL" && newest.Tags[1] == "CH",
"add failed, expected CH,NL tags, got %v", newest.Tags)
testRunTag(t, TagOptions{RemoveTags: []string{"NL"}}, gopts)
testRunCheck(t, gopts)
newest, _ = testRunSnapshots(t, gopts)
Assert(t, newest != nil, "expected a new backup, got nil")
Assert(t, len(newest.Tags) == 1 && newest.Tags[0] == "CH",
"remove failed, expected one CH tag, got %v", newest.Tags)
testRunTag(t, TagOptions{AddTags: []string{"US", "RU"}}, gopts)
testRunTag(t, TagOptions{RemoveTags: []string{"CH", "US", "RU"}}, gopts)
testRunCheck(t, gopts)
newest, _ = testRunSnapshots(t, gopts)
Assert(t, newest != nil, "expected a new backup, got nil")
Assert(t, len(newest.Tags) == 0,
"expected no tags, got %v", newest.Tags)
// Check special case of removing all tags.
testRunTag(t, TagOptions{SetTags: []string{""}}, gopts)
testRunCheck(t, gopts)
newest, _ = testRunSnapshots(t, gopts)
Assert(t, newest != nil, "expected a new backup, got nil")
Assert(t, len(newest.Tags) == 0,
"expected no tags, got %v", newest.Tags)
})
}
2016-09-17 10:36:05 +00:00
func testRunKeyListOtherIDs(t testing.TB, gopts GlobalOptions) []string {
buf := bytes.NewBuffer(nil)
2015-06-21 13:01:52 +00:00
2016-09-17 10:36:05 +00:00
globalOptions.stdout = buf
defer func() {
globalOptions.stdout = os.Stdout
}()
2015-06-21 13:01:52 +00:00
2016-09-17 10:36:05 +00:00
OK(t, runKey(gopts, []string{"list"}))
2015-06-21 13:01:52 +00:00
2016-09-17 10:36:05 +00:00
scanner := bufio.NewScanner(buf)
2015-06-21 13:01:52 +00:00
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
}
2016-09-17 10:36:05 +00:00
func testRunKeyAddNewKey(t testing.TB, newPassword string, gopts GlobalOptions) {
testKeyNewPassword = newPassword
defer func() {
testKeyNewPassword = ""
}()
OK(t, runKey(gopts, []string{"add"}))
2015-06-21 13:01:52 +00:00
}
2016-09-17 10:36:05 +00:00
func testRunKeyPasswd(t testing.TB, newPassword string, gopts GlobalOptions) {
testKeyNewPassword = newPassword
defer func() {
testKeyNewPassword = ""
}()
OK(t, runKey(gopts, []string{"passwd"}))
2015-06-21 13:01:52 +00:00
}
2016-09-17 10:36:05 +00:00
func testRunKeyRemove(t testing.TB, gopts GlobalOptions, IDs []string) {
2015-06-21 13:01:52 +00:00
t.Logf("remove %d keys: %q\n", len(IDs), IDs)
for _, id := range IDs {
2016-09-17 10:36:05 +00:00
OK(t, runKey(gopts, []string{"rm", id}))
2015-06-21 13:01:52 +00:00
}
}
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",
}
2016-09-17 10:36:05 +00:00
withTestEnvironment(t, func(env *testEnvironment, gopts GlobalOptions) {
testRunInit(t, gopts)
2015-06-21 13:01:52 +00:00
2016-09-17 10:36:05 +00:00
testRunKeyPasswd(t, "geheim2", gopts)
gopts.password = "geheim2"
t.Logf("changed password to %q", gopts.password)
2015-06-21 13:01:52 +00:00
for _, newPassword := range passwordList {
2016-09-17 10:36:05 +00:00
testRunKeyAddNewKey(t, newPassword, gopts)
2015-06-21 13:01:52 +00:00
t.Logf("added new password %q", newPassword)
2016-09-17 10:36:05 +00:00
gopts.password = newPassword
testRunKeyRemove(t, gopts, testRunKeyListOtherIDs(t, gopts))
2015-06-18 19:28:50 +00:00
}
2016-09-17 10:36:05 +00:00
gopts.password = passwordList[len(passwordList)-1]
t.Logf("testing access with last password %q\n", gopts.password)
OK(t, runKey(gopts, []string{"list"}))
testRunCheck(t, gopts)
2015-06-18 19:28:50 +00:00
})
}
2015-07-08 21:36:24 +00:00
func testFileSize(filename string, size int64) error {
fi, err := os.Stat(filename)
if err != nil {
return err
}
if fi.Size() != size {
2016-09-01 20:17:37 +00:00
return errors.Fatalf("wrong file size for %v: expected %v, got %v", filename, size, fi.Size())
2015-07-08 21:36:24 +00:00
}
return nil
}
func TestRestoreFilter(t *testing.T) {
testfiles := []struct {
name string
size uint
}{
{"testfile1.c", 100},
{"testfile2.exe", 101},
{"subdir1/subdir2/testfile3.docx", 102},
{"subdir1/subdir2/testfile4.c", 102},
}
2016-09-17 10:36:05 +00:00
withTestEnvironment(t, func(env *testEnvironment, gopts GlobalOptions) {
testRunInit(t, gopts)
2015-07-08 21:36:24 +00:00
for _, test := range testfiles {
p := filepath.Join(env.testdata, test.name)
OK(t, os.MkdirAll(filepath.Dir(p), 0755))
OK(t, appendRandomData(p, test.size))
}
2016-09-17 10:36:05 +00:00
opts := BackupOptions{}
2015-07-08 21:36:24 +00:00
2016-09-17 10:36:05 +00:00
testRunBackup(t, []string{env.testdata}, opts, gopts)
testRunCheck(t, gopts)
snapshotID := testRunList(t, "snapshots", gopts)[0]
2015-07-08 21:36:24 +00:00
// no restore filter should restore all files
2016-09-17 10:36:05 +00:00
testRunRestore(t, gopts, filepath.Join(env.base, "restore0"), snapshotID)
2015-07-08 21:36:24 +00:00
for _, test := range testfiles {
OK(t, testFileSize(filepath.Join(env.base, "restore0", "testdata", test.name), int64(test.size)))
}
for i, pat := range []string{"*.c", "*.exe", "*", "*file3*"} {
base := filepath.Join(env.base, fmt.Sprintf("restore%d", i+1))
2016-09-17 10:36:05 +00:00
testRunRestoreExcludes(t, gopts, base, snapshotID, []string{pat})
2015-07-08 21:36:24 +00:00
for _, test := range testfiles {
err := testFileSize(filepath.Join(base, "testdata", test.name), int64(test.size))
if ok, _ := filter.Match(pat, filepath.Base(test.name)); !ok {
2015-07-08 21:36:24 +00:00
OK(t, err)
} else {
Assert(t, os.IsNotExist(errors.Cause(err)),
2015-07-08 21:36:24 +00:00
"expected %v to not exist in restore step %v, but it exists, err %v", test.name, i+1, err)
}
}
}
})
}
2017-01-24 10:51:21 +00:00
func TestRestore(t *testing.T) {
withTestEnvironment(t, func(env *testEnvironment, gopts GlobalOptions) {
testRunInit(t, gopts)
for i := 0; i < 10; i++ {
p := filepath.Join(env.testdata, fmt.Sprintf("foo/bar/testfile%v", i))
OK(t, os.MkdirAll(filepath.Dir(p), 0755))
OK(t, appendRandomData(p, uint(mrand.Intn(5<<21))))
}
opts := BackupOptions{}
testRunBackup(t, []string{env.testdata}, opts, gopts)
testRunCheck(t, gopts)
// Restore latest without any filters
restoredir := filepath.Join(env.base, "restore")
testRunRestoreLatest(t, gopts, restoredir, nil, "")
Assert(t, directoriesEqualContents(env.testdata, filepath.Join(restoredir, filepath.Base(env.testdata))),
"directories are not equal")
})
}
func TestRestoreLatest(t *testing.T) {
2016-09-17 10:36:05 +00:00
withTestEnvironment(t, func(env *testEnvironment, gopts GlobalOptions) {
testRunInit(t, gopts)
p := filepath.Join(env.testdata, "testfile.c")
OK(t, os.MkdirAll(filepath.Dir(p), 0755))
OK(t, appendRandomData(p, 100))
2016-09-17 10:36:05 +00:00
opts := BackupOptions{}
testRunBackup(t, []string{env.testdata}, opts, gopts)
testRunCheck(t, gopts)
os.Remove(p)
OK(t, appendRandomData(p, 101))
2016-09-17 10:36:05 +00:00
testRunBackup(t, []string{env.testdata}, opts, gopts)
testRunCheck(t, gopts)
// Restore latest without any filters
2016-09-17 10:36:05 +00:00
testRunRestoreLatest(t, gopts, filepath.Join(env.base, "restore0"), nil, "")
OK(t, testFileSize(filepath.Join(env.base, "restore0", "testdata", "testfile.c"), int64(101)))
// Setup test files in different directories backed up in different snapshots
p1 := filepath.Join(env.testdata, "p1/testfile.c")
OK(t, os.MkdirAll(filepath.Dir(p1), 0755))
OK(t, appendRandomData(p1, 102))
2016-09-17 10:36:05 +00:00
testRunBackup(t, []string{filepath.Dir(p1)}, opts, gopts)
testRunCheck(t, gopts)
p2 := filepath.Join(env.testdata, "p2/testfile.c")
OK(t, os.MkdirAll(filepath.Dir(p2), 0755))
OK(t, appendRandomData(p2, 103))
2016-09-17 10:36:05 +00:00
testRunBackup(t, []string{filepath.Dir(p2)}, opts, gopts)
testRunCheck(t, gopts)
2016-05-10 19:51:56 +00:00
p1rAbs := filepath.Join(env.base, "restore1", "p1/testfile.c")
p2rAbs := filepath.Join(env.base, "restore2", "p2/testfile.c")
2016-09-17 10:36:05 +00:00
testRunRestoreLatest(t, gopts, filepath.Join(env.base, "restore1"), []string{filepath.Dir(p1)}, "")
2016-05-10 19:51:56 +00:00
OK(t, testFileSize(p1rAbs, int64(102)))
if _, err := os.Stat(p2rAbs); os.IsNotExist(errors.Cause(err)) {
Assert(t, os.IsNotExist(errors.Cause(err)),
2016-05-10 19:51:56 +00:00
"expected %v to not exist in restore, but it exists, err %v", p2rAbs, err)
}
2016-09-17 10:36:05 +00:00
testRunRestoreLatest(t, gopts, filepath.Join(env.base, "restore2"), []string{filepath.Dir(p2)}, "")
2016-05-10 19:51:56 +00:00
OK(t, testFileSize(p2rAbs, int64(103)))
if _, err := os.Stat(p1rAbs); os.IsNotExist(errors.Cause(err)) {
Assert(t, os.IsNotExist(errors.Cause(err)),
2016-05-10 19:51:56 +00:00
"expected %v to not exist in restore, but it exists, err %v", p1rAbs, err)
}
})
}
func TestRestoreWithPermissionFailure(t *testing.T) {
2016-09-17 10:36:05 +00:00
withTestEnvironment(t, func(env *testEnvironment, gopts GlobalOptions) {
datafile := filepath.Join("testdata", "repo-restore-permissions-test.tar.gz")
SetupTarTestFixture(t, env.base, datafile)
2016-09-17 10:36:05 +00:00
snapshots := testRunList(t, "snapshots", gopts)
Assert(t, len(snapshots) > 0,
"no snapshots found in repo (%v)", datafile)
2016-09-17 10:36:05 +00:00
globalOptions.stderr = ioutil.Discard
defer func() {
globalOptions.stderr = os.Stderr
}()
testRunRestore(t, gopts, filepath.Join(env.base, "restore"), snapshots[0])
2017-02-08 23:43:10 +00:00
// make sure that all files have been restored, regardless of any
// permission errors
2016-09-17 10:36:05 +00:00
files := testRunLs(t, gopts, snapshots[0].String())
for _, filename := range files {
fi, err := os.Lstat(filepath.Join(env.base, "restore", filename))
OK(t, err)
Assert(t, !isFile(fi) || fi.Size() > 0,
"file %v restored, but filesize is 0", filename)
}
})
}
2015-07-08 21:36:24 +00:00
func setZeroModTime(filename string) error {
var utimes = []syscall.Timespec{
syscall.NsecToTimespec(0),
syscall.NsecToTimespec(0),
}
return syscall.UtimesNano(filename, utimes)
}
func TestRestoreNoMetadataOnIgnoredIntermediateDirs(t *testing.T) {
2016-09-17 10:36:05 +00:00
withTestEnvironment(t, func(env *testEnvironment, gopts GlobalOptions) {
testRunInit(t, gopts)
2015-07-08 21:36:24 +00:00
p := filepath.Join(env.testdata, "subdir1", "subdir2", "subdir3", "file.ext")
OK(t, os.MkdirAll(filepath.Dir(p), 0755))
OK(t, appendRandomData(p, 200))
OK(t, setZeroModTime(filepath.Join(env.testdata, "subdir1", "subdir2")))
2016-09-17 10:36:05 +00:00
opts := BackupOptions{}
testRunBackup(t, []string{env.testdata}, opts, gopts)
testRunCheck(t, gopts)
2015-07-08 21:36:24 +00:00
2016-09-17 10:36:05 +00:00
snapshotID := testRunList(t, "snapshots", gopts)[0]
2015-07-08 21:36:24 +00:00
// restore with filter "*.ext", this should restore "file.ext", but
// since the directories are ignored and only created because of
// "file.ext", no meta data should be restored for them.
2016-09-17 10:36:05 +00:00
testRunRestoreIncludes(t, gopts, filepath.Join(env.base, "restore0"), snapshotID, []string{"*.ext"})
2015-07-08 21:36:24 +00:00
f1 := filepath.Join(env.base, "restore0", "testdata", "subdir1", "subdir2")
fi, err := os.Stat(f1)
OK(t, err)
Assert(t, fi.ModTime() != time.Unix(0, 0),
"meta data of intermediate directory has been restore although it was ignored")
// restore with filter "*", this should restore meta data on everything.
2016-09-17 10:36:05 +00:00
testRunRestoreIncludes(t, gopts, filepath.Join(env.base, "restore1"), snapshotID, []string{"*"})
2015-07-08 21:36:24 +00:00
f2 := filepath.Join(env.base, "restore1", "testdata", "subdir1", "subdir2")
fi, err = os.Stat(f2)
OK(t, err)
Assert(t, fi.ModTime() == time.Unix(0, 0),
"meta data of intermediate directory hasn't been restore")
})
}
2015-08-28 17:31:05 +00:00
func TestFind(t *testing.T) {
2016-09-17 10:36:05 +00:00
withTestEnvironment(t, func(env *testEnvironment, gopts GlobalOptions) {
2015-08-28 17:31:05 +00:00
datafile := filepath.Join("testdata", "backup-data.tar.gz")
2016-09-17 10:36:05 +00:00
testRunInit(t, gopts)
2015-08-28 17:31:05 +00:00
SetupTarTestFixture(t, env.testdata, datafile)
2016-09-17 10:36:05 +00:00
opts := BackupOptions{}
testRunBackup(t, []string{env.testdata}, opts, gopts)
testRunCheck(t, gopts)
results := testRunFind(t, gopts, "unexistingfile")
2015-08-28 17:31:05 +00:00
Assert(t, len(results) != 0, "unexisting file found in repo (%v)", datafile)
2016-09-17 10:36:05 +00:00
results = testRunFind(t, gopts, "testfile")
2015-08-28 17:31:05 +00:00
Assert(t, len(results) != 1, "file not found in repo (%v)", datafile)
2016-09-17 10:36:05 +00:00
results = testRunFind(t, gopts, "test")
2015-08-28 17:31:05 +00:00
Assert(t, len(results) < 2, "less than two file found in repo (%v)", datafile)
})
}
2015-10-25 16:24:52 +00:00
func TestRebuildIndex(t *testing.T) {
2016-09-17 10:36:05 +00:00
withTestEnvironment(t, func(env *testEnvironment, gopts GlobalOptions) {
datafile := filepath.Join("..", "..", "restic", "checker", "testdata", "duplicate-packs-in-index-test-repo.tar.gz")
2015-10-25 16:24:52 +00:00
SetupTarTestFixture(t, env.base, datafile)
2016-09-17 10:36:05 +00:00
out, err := testRunCheckOutput(gopts)
2015-10-25 16:24:52 +00:00
if !strings.Contains(out, "contained in several indexes") {
t.Fatalf("did not find checker hint for packs in several indexes")
}
2016-09-17 10:36:05 +00:00
if err != nil {
t.Fatalf("expected no error from checker for test repository, got %v", err)
}
2015-10-25 16:24:52 +00:00
if !strings.Contains(out, "restic rebuild-index") {
2017-02-08 23:43:10 +00:00
t.Fatalf("did not find hint for rebuild-index command")
2015-10-25 16:24:52 +00:00
}
2016-09-17 10:36:05 +00:00
testRunRebuildIndex(t, gopts)
2015-10-25 16:24:52 +00:00
2016-09-17 10:36:05 +00:00
out, err = testRunCheckOutput(gopts)
2015-10-25 16:24:52 +00:00
if len(out) != 0 {
t.Fatalf("expected no output from the checker, got: %v", out)
}
2016-09-17 10:36:05 +00:00
if err != nil {
t.Fatalf("expected no error from checker after rebuild-index, got: %v", err)
}
2015-10-25 16:24:52 +00:00
})
}
func TestRebuildIndexAlwaysFull(t *testing.T) {
repository.IndexFull = func(*repository.Index) bool { return true }
TestRebuildIndex(t)
}
func TestCheckRestoreNoLock(t *testing.T) {
2016-09-17 10:36:05 +00:00
withTestEnvironment(t, func(env *testEnvironment, gopts GlobalOptions) {
datafile := filepath.Join("testdata", "small-repo.tar.gz")
SetupTarTestFixture(t, env.base, datafile)
err := filepath.Walk(env.repo, func(p string, fi os.FileInfo, e error) error {
if e != nil {
return e
}
return os.Chmod(p, fi.Mode() & ^(os.FileMode(0222)))
})
OK(t, err)
2016-09-17 10:36:05 +00:00
gopts.NoLock = true
testRunCheck(t, gopts)
2016-09-17 10:36:05 +00:00
snapshotIDs := testRunList(t, "snapshots", gopts)
if len(snapshotIDs) == 0 {
t.Fatalf("found no snapshots")
}
2016-09-17 10:36:05 +00:00
testRunRestore(t, gopts, filepath.Join(env.base, "restore"), snapshotIDs[0])
})
}
func TestPrune(t *testing.T) {
withTestEnvironment(t, func(env *testEnvironment, gopts GlobalOptions) {
datafile := filepath.Join("testdata", "backup-data.tar.gz")
fd, err := os.Open(datafile)
if os.IsNotExist(errors.Cause(err)) {
t.Skipf("unable to find data file %q, skipping", datafile)
return
}
OK(t, err)
OK(t, fd.Close())
testRunInit(t, gopts)
SetupTarTestFixture(t, env.testdata, datafile)
opts := BackupOptions{}
testRunBackup(t, []string{filepath.Join(env.testdata, "0", "0", "1")}, opts, gopts)
testRunBackup(t, []string{filepath.Join(env.testdata, "0", "0", "2")}, opts, gopts)
testRunBackup(t, []string{filepath.Join(env.testdata, "0", "0", "3")}, opts, gopts)
snapshotIDs := testRunList(t, "snapshots", gopts)
Assert(t, len(snapshotIDs) == 3,
"expected one snapshot, got %v", snapshotIDs)
testRunForget(t, gopts, snapshotIDs[0].String())
testRunPrune(t, gopts)
testRunCheck(t, gopts)
})
}
2017-01-30 23:14:20 +00:00
func TestHardLink(t *testing.T) {
// this test assumes a test set with a single directory containing hard linked files
withTestEnvironment(t, func(env *testEnvironment, gopts GlobalOptions) {
datafile := filepath.Join("testdata", "test.hl.tar.gz")
fd, err := os.Open(datafile)
if os.IsNotExist(errors.Cause(err)) {
t.Skipf("unable to find data file %q, skipping", datafile)
return
}
OK(t, err)
OK(t, fd.Close())
testRunInit(t, gopts)
SetupTarTestFixture(t, env.testdata, datafile)
linkTests := createFileSetPerHardlink(env.testdata)
opts := BackupOptions{}
// first backup
testRunBackup(t, []string{env.testdata}, opts, gopts)
snapshotIDs := testRunList(t, "snapshots", gopts)
Assert(t, len(snapshotIDs) == 1,
"expected one snapshot, got %v", snapshotIDs)
testRunCheck(t, gopts)
// 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)
testRunRestore(t, gopts, restoredir, snapshotIDs[0])
Assert(t, directoriesEqualContents(env.testdata, filepath.Join(restoredir, "testdata")),
"directories are not equal")
linkResults := createFileSetPerHardlink(filepath.Join(restoredir, "testdata"))
Assert(t, linksEqual(linkTests, linkResults),
"links are not equal")
}
testRunCheck(t, gopts)
})
}
func linksEqual(source, dest map[uint64][]string) bool {
for _, vs := range source {
found := false
for kd, vd := range dest {
if linkEqual(vs, vd) {
delete(dest, kd)
found = true
break
}
}
if !found {
return false
}
}
if len(dest) != 0 {
return false
}
return true
}
func linkEqual(source, dest []string) bool {
// equal if sliced are equal without considering order
if source == nil && dest == nil {
return true
}
if source == nil || dest == nil {
return false
}
if len(source) != len(dest) {
return false
}
for i := range source {
found := false
for j := range dest {
if source[i] == dest[j] {
found = true
break
}
}
if !found {
return false
}
}
return true
}