2015-05-09 12:45:39 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-05-10 00:41:16 +00:00
|
|
|
"bufio"
|
2015-06-21 13:01:52 +00:00
|
|
|
"bytes"
|
2018-04-22 09:57:20 +00:00
|
|
|
"context"
|
2015-06-14 13:19:11 +00:00
|
|
|
"crypto/rand"
|
2017-03-05 04:24:40 +00:00
|
|
|
"encoding/json"
|
2015-06-14 13:19:11 +00:00
|
|
|
"fmt"
|
2015-05-10 00:41:16 +00:00
|
|
|
"io"
|
2015-06-21 13:32:26 +00:00
|
|
|
"io/ioutil"
|
2017-01-24 10:51:21 +00:00
|
|
|
mrand "math/rand"
|
2015-05-09 12:45:39 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-06-21 13:01:52 +00:00
|
|
|
"regexp"
|
2015-07-19 22:13:39 +00:00
|
|
|
"strings"
|
2015-07-08 21:36:24 +00:00
|
|
|
"syscall"
|
2015-05-09 12:45:39 +00:00
|
|
|
"testing"
|
2015-07-08 21:36:24 +00:00
|
|
|
"time"
|
2015-05-09 12:45:39 +00:00
|
|
|
|
2017-10-02 13:06:39 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/filter"
|
2018-03-25 21:36:31 +00:00
|
|
|
"github.com/restic/restic/internal/fs"
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/repository"
|
2017-10-02 13:06:39 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest "github.com/restic/restic/internal/test"
|
2018-04-22 09:57:20 +00:00
|
|
|
"github.com/restic/restic/internal/ui/termstatus"
|
|
|
|
"golang.org/x/sync/errgroup"
|
2015-05-09 12:45:39 +00:00
|
|
|
)
|
|
|
|
|
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) {
|
2016-08-21 11:42:04 +00:00
|
|
|
repository.TestUseLowSecurityKDFParameters(t)
|
2018-03-11 19:59:40 +00:00
|
|
|
restic.TestDisableCheckPolynomial(t)
|
2016-09-01 19:13:06 +00:00
|
|
|
restic.TestSetLockTimeout(t, 0)
|
2016-08-21 11:42:04 +00:00
|
|
|
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, runInit(opts, nil))
|
2016-09-17 10:36:05 +00:00
|
|
|
t.Logf("repository initialized at %v", opts.Repo)
|
2015-07-19 22:13:39 +00:00
|
|
|
}
|
|
|
|
|
2018-03-25 21:36:31 +00:00
|
|
|
func testRunBackup(t testing.TB, dir string, target []string, opts BackupOptions, gopts GlobalOptions) {
|
2018-04-22 09:57:20 +00:00
|
|
|
ctx, cancel := context.WithCancel(gopts.ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
var wg errgroup.Group
|
2018-05-02 19:24:18 +00:00
|
|
|
term := termstatus.New(gopts.stdout, gopts.stderr, gopts.Quiet)
|
2018-04-22 09:57:20 +00:00
|
|
|
wg.Go(func() error { term.Run(ctx); return nil })
|
|
|
|
|
2018-04-20 19:36:05 +00:00
|
|
|
gopts.stdout = ioutil.Discard
|
2018-03-25 21:36:31 +00:00
|
|
|
t.Logf("backing up %v in %v", target, dir)
|
|
|
|
if dir != "" {
|
|
|
|
cleanup := fs.TestChdir(t, dir)
|
|
|
|
defer cleanup()
|
|
|
|
}
|
2018-04-22 09:57:20 +00:00
|
|
|
|
|
|
|
rtest.OK(t, runBackup(opts, gopts, term, target))
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
err := wg.Wait()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-05-09 12:45:39 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
func testRunList(t testing.TB, tpe string, opts GlobalOptions) restic.IDs {
|
2015-11-10 21:13:53 +00:00
|
|
|
buf := bytes.NewBuffer(nil)
|
2016-09-17 10:36:05 +00:00
|
|
|
globalOptions.stdout = buf
|
|
|
|
defer func() {
|
|
|
|
globalOptions.stdout = os.Stdout
|
|
|
|
}()
|
|
|
|
|
2018-05-17 17:41:56 +00:00
|
|
|
rtest.OK(t, runList(cmdList, opts, []string{tpe}))
|
2015-11-10 21:13:53 +00:00
|
|
|
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)
|
2015-07-19 22:38:44 +00:00
|
|
|
}
|
|
|
|
|
2020-02-26 21:17:59 +00:00
|
|
|
func testRunRestoreLatest(t testing.TB, gopts GlobalOptions, dir string, paths []string, hosts []string) {
|
2016-09-17 10:36:05 +00:00
|
|
|
opts := RestoreOptions{
|
|
|
|
Target: dir,
|
2020-02-26 21:17:59 +00:00
|
|
|
Hosts: hosts,
|
2016-09-17 10:36:05 +00:00
|
|
|
Paths: paths,
|
|
|
|
}
|
|
|
|
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, runRestore(opts, gopts, []string{"latest"}))
|
2016-05-10 19:23:18 +00:00
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.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,
|
|
|
|
}
|
|
|
|
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, runRestore(opts, gopts, []string{snapshotID.String()}))
|
2015-07-20 17:20:20 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
func testRunCheck(t testing.TB, gopts GlobalOptions) {
|
|
|
|
opts := CheckOptions{
|
2015-11-08 20:10:03 +00:00
|
|
|
ReadData: true,
|
|
|
|
CheckUnused: true,
|
|
|
|
}
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.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
|
|
|
|
}()
|
|
|
|
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.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
|
|
|
|
}()
|
2015-07-19 22:13:39 +00:00
|
|
|
|
2017-03-08 19:29:31 +00:00
|
|
|
opts := LsOptions{}
|
|
|
|
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, runLs(opts, gopts, []string{snapshotID}))
|
2015-07-19 22:13:39 +00:00
|
|
|
|
|
|
|
return strings.Split(string(buf.Bytes()), "\n")
|
|
|
|
}
|
|
|
|
|
2017-03-09 15:06:28 +00:00
|
|
|
func testRunFind(t testing.TB, wantJSON bool, gopts GlobalOptions, pattern string) []byte {
|
2016-09-17 10:36:05 +00:00
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
globalOptions.stdout = buf
|
2017-03-09 15:06:28 +00:00
|
|
|
globalOptions.JSON = wantJSON
|
2016-09-17 10:36:05 +00:00
|
|
|
defer func() {
|
|
|
|
globalOptions.stdout = os.Stdout
|
2017-03-09 15:06:28 +00:00
|
|
|
globalOptions.JSON = false
|
2016-09-17 10:36:05 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
opts := FindOptions{}
|
2015-08-28 17:31:05 +00:00
|
|
|
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, runFind(opts, gopts, []string{pattern}))
|
2015-08-28 17:31:05 +00:00
|
|
|
|
2017-03-09 15:06:28 +00:00
|
|
|
return buf.Bytes()
|
2015-08-28 17:31:05 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:50:11 +00:00
|
|
|
func testRunSnapshots(t testing.TB, gopts GlobalOptions) (newest *Snapshot, snapmap map[restic.ID]Snapshot) {
|
2017-03-05 04:24:40 +00:00
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
globalOptions.stdout = buf
|
|
|
|
globalOptions.JSON = true
|
|
|
|
defer func() {
|
|
|
|
globalOptions.stdout = os.Stdout
|
|
|
|
globalOptions.JSON = gopts.JSON
|
|
|
|
}()
|
|
|
|
|
|
|
|
opts := SnapshotOptions{}
|
|
|
|
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, runSnapshots(opts, globalOptions, []string{}))
|
2017-03-05 04:24:40 +00:00
|
|
|
|
|
|
|
snapshots := []Snapshot{}
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, json.Unmarshal(buf.Bytes(), &snapshots))
|
2017-03-05 04:24:40 +00:00
|
|
|
|
2017-03-05 16:50:11 +00:00
|
|
|
snapmap = make(map[restic.ID]Snapshot, len(snapshots))
|
2017-03-05 04:24:40 +00:00
|
|
|
for _, sn := range snapshots {
|
2017-03-05 16:50:11 +00:00
|
|
|
snapmap[*sn.ID] = sn
|
2017-03-05 04:24:40 +00:00
|
|
|
if newest == nil || sn.Time.After(newest.Time) {
|
|
|
|
newest = &sn
|
|
|
|
}
|
|
|
|
}
|
2017-03-05 16:50:11 +00:00
|
|
|
return
|
2017-03-05 04:24:40 +00:00
|
|
|
}
|
|
|
|
|
2017-01-22 21:23:30 +00:00
|
|
|
func testRunForget(t testing.TB, gopts GlobalOptions, args ...string) {
|
|
|
|
opts := ForgetOptions{}
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, runForget(opts, gopts, args))
|
2017-01-22 21:23:30 +00:00
|
|
|
}
|
|
|
|
|
2019-02-23 04:52:30 +00:00
|
|
|
func testRunForgetJSON(t testing.TB, gopts GlobalOptions, args ...string) {
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
oldJSON := gopts.JSON
|
|
|
|
gopts.stdout = buf
|
|
|
|
gopts.JSON = true
|
|
|
|
defer func() {
|
|
|
|
gopts.stdout = os.Stdout
|
|
|
|
gopts.JSON = oldJSON
|
|
|
|
}()
|
|
|
|
|
|
|
|
opts := ForgetOptions{
|
|
|
|
DryRun: true,
|
|
|
|
Last: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
rtest.OK(t, runForget(opts, gopts, args))
|
|
|
|
|
|
|
|
var forgets []*ForgetGroup
|
|
|
|
rtest.OK(t, json.Unmarshal(buf.Bytes(), &forgets))
|
|
|
|
|
|
|
|
rtest.Assert(t, len(forgets) == 1,
|
|
|
|
"Expected 1 snapshot group, got %v", len(forgets))
|
|
|
|
rtest.Assert(t, len(forgets[0].Keep) == 1,
|
|
|
|
"Expected 1 snapshot to be kept, got %v", len(forgets[0].Keep))
|
|
|
|
rtest.Assert(t, len(forgets[0].Remove) == 2,
|
|
|
|
"Expected 2 snapshots to be removed, got %v", len(forgets[0].Remove))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-01-22 21:23:30 +00:00
|
|
|
func testRunPrune(t testing.TB, gopts GlobalOptions) {
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, runPrune(gopts))
|
2017-01-22 21:23:30 +00:00
|
|
|
}
|
|
|
|
|
2015-05-09 12:45:39 +00:00
|
|
|
func TestBackup(t *testing.T) {
|
2017-07-24 19:25:49 +00:00
|
|
|
env, cleanup := withTestEnvironment(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, err)
|
|
|
|
rtest.OK(t, fd.Close())
|
2015-06-14 13:19:11 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
testRunInit(t, env.gopts)
|
2015-06-14 13:19:11 +00:00
|
|
|
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.SetupTarTestFixture(t, env.testdata, datafile)
|
2017-07-24 19:25:49 +00:00
|
|
|
opts := BackupOptions{}
|
2015-06-14 13:19:11 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
// first backup
|
2018-03-25 21:36:31 +00:00
|
|
|
testRunBackup(t, filepath.Dir(env.testdata), []string{"testdata"}, opts, env.gopts)
|
2017-07-24 19:25:49 +00:00
|
|
|
snapshotIDs := testRunList(t, "snapshots", env.gopts)
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.Assert(t, len(snapshotIDs) == 1,
|
2017-07-24 19:25:49 +00:00
|
|
|
"expected one snapshot, got %v", snapshotIDs)
|
2015-06-14 13:19:11 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
testRunCheck(t, env.gopts)
|
|
|
|
stat1 := dirStats(env.repo)
|
2015-06-14 13:19:11 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
// second backup, implicit incremental
|
2018-03-25 21:36:31 +00:00
|
|
|
testRunBackup(t, filepath.Dir(env.testdata), []string{"testdata"}, opts, env.gopts)
|
2017-07-24 19:25:49 +00:00
|
|
|
snapshotIDs = testRunList(t, "snapshots", env.gopts)
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.Assert(t, len(snapshotIDs) == 2,
|
2017-07-24 19:25:49 +00:00
|
|
|
"expected two snapshots, got %v", snapshotIDs)
|
2015-06-14 13:19:11 +00:00
|
|
|
|
2017-07-24 19:25:49 +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)
|
|
|
|
|
|
|
|
testRunCheck(t, env.gopts)
|
|
|
|
// third backup, explicit incremental
|
|
|
|
opts.Parent = snapshotIDs[0].String()
|
2018-03-25 21:36:31 +00:00
|
|
|
testRunBackup(t, filepath.Dir(env.testdata), []string{"testdata"}, opts, env.gopts)
|
2017-07-24 19:25:49 +00:00
|
|
|
snapshotIDs = testRunList(t, "snapshots", env.gopts)
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.Assert(t, len(snapshotIDs) == 3,
|
2017-07-24 19:25:49 +00:00
|
|
|
"expected three snapshots, got %v", snapshotIDs)
|
|
|
|
|
|
|
|
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)
|
|
|
|
testRunRestore(t, env.gopts, restoredir, snapshotIDs[0])
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.Assert(t, directoriesEqualContents(env.testdata, filepath.Join(restoredir, "testdata")),
|
2017-07-24 19:25:49 +00:00
|
|
|
"directories are not equal")
|
|
|
|
}
|
2015-06-14 13:19:11 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
testRunCheck(t, env.gopts)
|
2015-06-14 13:19:11 +00:00
|
|
|
}
|
|
|
|
|
2015-06-21 13:32:26 +00:00
|
|
|
func TestBackupNonExistingFile(t *testing.T) {
|
2017-07-24 19:25:49 +00:00
|
|
|
env, cleanup := withTestEnvironment(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, err)
|
|
|
|
rtest.OK(t, fd.Close())
|
2016-09-17 10:36:05 +00:00
|
|
|
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.SetupTarTestFixture(t, env.testdata, datafile)
|
2016-09-17 10:36:05 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
testRunInit(t, env.gopts)
|
|
|
|
globalOptions.stderr = ioutil.Discard
|
|
|
|
defer func() {
|
|
|
|
globalOptions.stderr = os.Stderr
|
|
|
|
}()
|
|
|
|
|
2018-03-11 20:07:47 +00:00
|
|
|
p := filepath.Join(env.testdata, "0", "0", "9")
|
2017-07-24 19:25:49 +00:00
|
|
|
dirs := []string{
|
|
|
|
filepath.Join(p, "0"),
|
|
|
|
filepath.Join(p, "1"),
|
|
|
|
filepath.Join(p, "nonexisting"),
|
|
|
|
filepath.Join(p, "5"),
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := BackupOptions{}
|
|
|
|
|
2018-03-25 21:36:31 +00:00
|
|
|
testRunBackup(t, "", dirs, opts, env.gopts)
|
2015-11-06 22:19:56 +00:00
|
|
|
}
|
|
|
|
|
2015-07-19 22:13:39 +00:00
|
|
|
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)
|
2015-07-19 22:13:39 +00:00
|
|
|
|
|
|
|
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) {
|
2017-07-24 19:25:49 +00:00
|
|
|
env, cleanup := withTestEnvironment(t)
|
|
|
|
defer cleanup()
|
2015-07-19 22:13:39 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
testRunInit(t, env.gopts)
|
2015-07-19 22:13:39 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
datadir := filepath.Join(env.base, "testdata")
|
2015-07-19 22:13:39 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
for _, filename := range backupExcludeFilenames {
|
|
|
|
fp := filepath.Join(datadir, filename)
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, os.MkdirAll(filepath.Dir(fp), 0755))
|
2015-07-19 22:13:39 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
f, err := os.Create(fp)
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, err)
|
2015-07-19 22:13:39 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
fmt.Fprintf(f, filename)
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, f.Close())
|
2017-07-24 19:25:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
snapshots := make(map[string]struct{})
|
|
|
|
|
|
|
|
opts := BackupOptions{}
|
|
|
|
|
2018-03-25 21:36:31 +00:00
|
|
|
testRunBackup(t, filepath.Dir(env.testdata), []string{"testdata"}, opts, env.gopts)
|
2017-07-24 19:25:49 +00:00
|
|
|
snapshots, snapshotID := lastSnapshot(snapshots, loadSnapshotMap(t, env.gopts))
|
|
|
|
files := testRunLs(t, env.gopts, snapshotID)
|
2018-06-09 21:58:44 +00:00
|
|
|
rtest.Assert(t, includes(files, "/testdata/foo.tar.gz"),
|
2017-07-24 19:25:49 +00:00
|
|
|
"expected file %q in first snapshot, but it's not included", "foo.tar.gz")
|
|
|
|
|
|
|
|
opts.Excludes = []string{"*.tar.gz"}
|
2018-03-25 21:36:31 +00:00
|
|
|
testRunBackup(t, filepath.Dir(env.testdata), []string{"testdata"}, opts, env.gopts)
|
2017-07-24 19:25:49 +00:00
|
|
|
snapshots, snapshotID = lastSnapshot(snapshots, loadSnapshotMap(t, env.gopts))
|
|
|
|
files = testRunLs(t, env.gopts, snapshotID)
|
2018-06-09 21:58:44 +00:00
|
|
|
rtest.Assert(t, !includes(files, "/testdata/foo.tar.gz"),
|
2017-07-24 19:25:49 +00:00
|
|
|
"expected file %q not in first snapshot, but it's included", "foo.tar.gz")
|
|
|
|
|
|
|
|
opts.Excludes = []string{"*.tar.gz", "private/secret"}
|
2018-03-25 21:36:31 +00:00
|
|
|
testRunBackup(t, filepath.Dir(env.testdata), []string{"testdata"}, opts, env.gopts)
|
2017-07-24 19:25:49 +00:00
|
|
|
_, snapshotID = lastSnapshot(snapshots, loadSnapshotMap(t, env.gopts))
|
|
|
|
files = testRunLs(t, env.gopts, snapshotID)
|
2018-06-09 21:58:44 +00:00
|
|
|
rtest.Assert(t, !includes(files, "/testdata/foo.tar.gz"),
|
2017-07-24 19:25:49 +00:00
|
|
|
"expected file %q not in first snapshot, but it's included", "foo.tar.gz")
|
2018-06-09 21:58:44 +00:00
|
|
|
rtest.Assert(t, !includes(files, "/testdata/private/secret/passwords.txt"),
|
2017-07-24 19:25:49 +00:00
|
|
|
"expected file %q not in first snapshot, but it's included", "passwords.txt")
|
2015-07-19 22:13:39 +00:00
|
|
|
}
|
|
|
|
|
2015-06-14 13:19:11 +00:00
|
|
|
const (
|
2018-04-01 11:49:42 +00:00
|
|
|
incrementalFirstWrite = 10 * 1042 * 1024
|
2018-03-11 20:17:27 +00:00
|
|
|
incrementalSecondWrite = 1 * 1042 * 1024
|
|
|
|
incrementalThirdWrite = 1 * 1042 * 1024
|
2015-06-14 13:19:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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-05 20:33:39 +00:00
|
|
|
}
|
|
|
|
|
2015-06-14 13:19:11 +00:00
|
|
|
_, err = f.Seek(0, 2)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprint(os.Stderr, err)
|
|
|
|
return err
|
2015-05-09 12:45:39 +00:00
|
|
|
}
|
|
|
|
|
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-05-09 12:45:39 +00:00
|
|
|
|
2015-06-14 13:19:11 +00:00
|
|
|
return f.Close()
|
|
|
|
}
|
2015-05-09 12:45:39 +00:00
|
|
|
|
2015-06-14 13:19:11 +00:00
|
|
|
func TestIncrementalBackup(t *testing.T) {
|
2017-07-24 19:25:49 +00:00
|
|
|
env, cleanup := withTestEnvironment(t)
|
|
|
|
defer cleanup()
|
2015-05-09 12:45:39 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
testRunInit(t, env.gopts)
|
2015-05-09 12:45:39 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
datadir := filepath.Join(env.base, "testdata")
|
|
|
|
testfile := filepath.Join(datadir, "testfile")
|
2015-05-10 00:41:16 +00:00
|
|
|
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, appendRandomData(testfile, incrementalFirstWrite))
|
2016-09-17 10:36:05 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
opts := BackupOptions{}
|
2015-05-10 00:41:16 +00:00
|
|
|
|
2018-03-25 21:36:31 +00:00
|
|
|
testRunBackup(t, "", []string{datadir}, opts, env.gopts)
|
2017-07-24 19:25:49 +00:00
|
|
|
testRunCheck(t, env.gopts)
|
|
|
|
stat1 := dirStats(env.repo)
|
2015-05-10 00:41:16 +00:00
|
|
|
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, appendRandomData(testfile, incrementalSecondWrite))
|
2015-05-10 00:41:16 +00:00
|
|
|
|
2018-03-25 21:36:31 +00:00
|
|
|
testRunBackup(t, "", []string{datadir}, opts, env.gopts)
|
2017-07-24 19:25:49 +00:00
|
|
|
testRunCheck(t, env.gopts)
|
|
|
|
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-06-14 13:19:11 +00:00
|
|
|
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, appendRandomData(testfile, incrementalThirdWrite))
|
2017-07-24 19:25:49 +00:00
|
|
|
|
2018-03-25 21:36:31 +00:00
|
|
|
testRunBackup(t, "", []string{datadir}, opts, env.gopts)
|
2017-07-24 19:25:49 +00:00
|
|
|
testRunCheck(t, env.gopts)
|
|
|
|
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-05-09 12:45:39 +00:00
|
|
|
}
|
2015-06-18 19:28:50 +00:00
|
|
|
|
2017-03-05 04:24:40 +00:00
|
|
|
func TestBackupTags(t *testing.T) {
|
2017-07-24 19:25:49 +00:00
|
|
|
env, cleanup := withTestEnvironment(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
datafile := filepath.Join("testdata", "backup-data.tar.gz")
|
|
|
|
testRunInit(t, env.gopts)
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.SetupTarTestFixture(t, env.testdata, datafile)
|
2017-07-24 19:25:49 +00:00
|
|
|
|
|
|
|
opts := BackupOptions{}
|
|
|
|
|
2018-03-25 21:36:31 +00:00
|
|
|
testRunBackup(t, "", []string{env.testdata}, opts, env.gopts)
|
2017-07-24 19:25:49 +00:00
|
|
|
testRunCheck(t, env.gopts)
|
|
|
|
newest, _ := testRunSnapshots(t, env.gopts)
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.Assert(t, newest != nil, "expected a new backup, got nil")
|
|
|
|
rtest.Assert(t, len(newest.Tags) == 0,
|
2017-07-24 19:25:49 +00:00
|
|
|
"expected no tags, got %v", newest.Tags)
|
2017-10-05 18:48:26 +00:00
|
|
|
parent := newest
|
2017-07-24 19:25:49 +00:00
|
|
|
|
|
|
|
opts.Tags = []string{"NL"}
|
2018-03-25 21:36:31 +00:00
|
|
|
testRunBackup(t, "", []string{env.testdata}, opts, env.gopts)
|
2017-07-24 19:25:49 +00:00
|
|
|
testRunCheck(t, env.gopts)
|
|
|
|
newest, _ = testRunSnapshots(t, env.gopts)
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.Assert(t, newest != nil, "expected a new backup, got nil")
|
|
|
|
rtest.Assert(t, len(newest.Tags) == 1 && newest.Tags[0] == "NL",
|
2017-07-24 19:25:49 +00:00
|
|
|
"expected one NL tag, got %v", newest.Tags)
|
2017-10-05 18:48:26 +00:00
|
|
|
// Tagged backup should have untagged backup as parent.
|
|
|
|
rtest.Assert(t, parent.ID.Equal(*newest.Parent),
|
|
|
|
"expected parent to be %v, got %v", parent.ID, newest.Parent)
|
2017-03-05 04:24:40 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 05:20:32 +00:00
|
|
|
func testRunTag(t testing.TB, opts TagOptions, gopts GlobalOptions) {
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, runTag(opts, gopts, []string{}))
|
2017-03-05 05:20:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTag(t *testing.T) {
|
2017-07-24 19:25:49 +00:00
|
|
|
env, cleanup := withTestEnvironment(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
datafile := filepath.Join("testdata", "backup-data.tar.gz")
|
|
|
|
testRunInit(t, env.gopts)
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.SetupTarTestFixture(t, env.testdata, datafile)
|
2017-07-24 19:25:49 +00:00
|
|
|
|
2018-03-25 21:36:31 +00:00
|
|
|
testRunBackup(t, "", []string{env.testdata}, BackupOptions{}, env.gopts)
|
2017-07-24 19:25:49 +00:00
|
|
|
testRunCheck(t, env.gopts)
|
|
|
|
newest, _ := testRunSnapshots(t, env.gopts)
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.Assert(t, newest != nil, "expected a new backup, got nil")
|
|
|
|
rtest.Assert(t, len(newest.Tags) == 0,
|
2017-07-24 19:25:49 +00:00
|
|
|
"expected no tags, got %v", newest.Tags)
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.Assert(t, newest.Original == nil,
|
2017-07-24 19:25:49 +00:00
|
|
|
"expected original ID to be nil, got %v", newest.Original)
|
|
|
|
originalID := *newest.ID
|
|
|
|
|
|
|
|
testRunTag(t, TagOptions{SetTags: []string{"NL"}}, env.gopts)
|
|
|
|
testRunCheck(t, env.gopts)
|
|
|
|
newest, _ = testRunSnapshots(t, env.gopts)
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.Assert(t, newest != nil, "expected a new backup, got nil")
|
|
|
|
rtest.Assert(t, len(newest.Tags) == 1 && newest.Tags[0] == "NL",
|
2017-07-24 19:25:49 +00:00
|
|
|
"set failed, expected one NL tag, got %v", newest.Tags)
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.Assert(t, newest.Original != nil, "expected original snapshot id, got nil")
|
|
|
|
rtest.Assert(t, *newest.Original == originalID,
|
2017-07-24 19:25:49 +00:00
|
|
|
"expected original ID to be set to the first snapshot id")
|
|
|
|
|
|
|
|
testRunTag(t, TagOptions{AddTags: []string{"CH"}}, env.gopts)
|
|
|
|
testRunCheck(t, env.gopts)
|
|
|
|
newest, _ = testRunSnapshots(t, env.gopts)
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.Assert(t, newest != nil, "expected a new backup, got nil")
|
|
|
|
rtest.Assert(t, len(newest.Tags) == 2 && newest.Tags[0] == "NL" && newest.Tags[1] == "CH",
|
2017-07-24 19:25:49 +00:00
|
|
|
"add failed, expected CH,NL tags, got %v", newest.Tags)
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.Assert(t, newest.Original != nil, "expected original snapshot id, got nil")
|
|
|
|
rtest.Assert(t, *newest.Original == originalID,
|
2017-07-24 19:25:49 +00:00
|
|
|
"expected original ID to be set to the first snapshot id")
|
|
|
|
|
|
|
|
testRunTag(t, TagOptions{RemoveTags: []string{"NL"}}, env.gopts)
|
|
|
|
testRunCheck(t, env.gopts)
|
|
|
|
newest, _ = testRunSnapshots(t, env.gopts)
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.Assert(t, newest != nil, "expected a new backup, got nil")
|
|
|
|
rtest.Assert(t, len(newest.Tags) == 1 && newest.Tags[0] == "CH",
|
2017-07-24 19:25:49 +00:00
|
|
|
"remove failed, expected one CH tag, got %v", newest.Tags)
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.Assert(t, newest.Original != nil, "expected original snapshot id, got nil")
|
|
|
|
rtest.Assert(t, *newest.Original == originalID,
|
2017-07-24 19:25:49 +00:00
|
|
|
"expected original ID to be set to the first snapshot id")
|
|
|
|
|
|
|
|
testRunTag(t, TagOptions{AddTags: []string{"US", "RU"}}, env.gopts)
|
|
|
|
testRunTag(t, TagOptions{RemoveTags: []string{"CH", "US", "RU"}}, env.gopts)
|
|
|
|
testRunCheck(t, env.gopts)
|
|
|
|
newest, _ = testRunSnapshots(t, env.gopts)
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.Assert(t, newest != nil, "expected a new backup, got nil")
|
|
|
|
rtest.Assert(t, len(newest.Tags) == 0,
|
2017-07-24 19:25:49 +00:00
|
|
|
"expected no tags, got %v", newest.Tags)
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.Assert(t, newest.Original != nil, "expected original snapshot id, got nil")
|
|
|
|
rtest.Assert(t, *newest.Original == originalID,
|
2017-07-24 19:25:49 +00:00
|
|
|
"expected original ID to be set to the first snapshot id")
|
|
|
|
|
|
|
|
// Check special case of removing all tags.
|
|
|
|
testRunTag(t, TagOptions{SetTags: []string{""}}, env.gopts)
|
|
|
|
testRunCheck(t, env.gopts)
|
|
|
|
newest, _ = testRunSnapshots(t, env.gopts)
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.Assert(t, newest != nil, "expected a new backup, got nil")
|
|
|
|
rtest.Assert(t, len(newest.Tags) == 0,
|
2017-07-24 19:25:49 +00:00
|
|
|
"expected no tags, got %v", newest.Tags)
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.Assert(t, newest.Original != nil, "expected original snapshot id, got nil")
|
|
|
|
rtest.Assert(t, *newest.Original == originalID,
|
2017-07-24 19:25:49 +00:00
|
|
|
"expected original ID to be set to the first snapshot id")
|
2017-03-05 05:20:32 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.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 = ""
|
|
|
|
}()
|
|
|
|
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.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 = ""
|
|
|
|
}()
|
|
|
|
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.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 {
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, runKey(gopts, []string{"remove", 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",
|
|
|
|
}
|
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
env, cleanup := withTestEnvironment(t)
|
|
|
|
defer cleanup()
|
2015-06-21 13:01:52 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
testRunInit(t, env.gopts)
|
2015-06-21 13:01:52 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
testRunKeyPasswd(t, "geheim2", env.gopts)
|
|
|
|
env.gopts.password = "geheim2"
|
|
|
|
t.Logf("changed password to %q", env.gopts.password)
|
2015-06-18 19:28:50 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
for _, newPassword := range passwordList {
|
|
|
|
testRunKeyAddNewKey(t, newPassword, env.gopts)
|
|
|
|
t.Logf("added new password %q", newPassword)
|
|
|
|
env.gopts.password = newPassword
|
|
|
|
testRunKeyRemove(t, env.gopts, testRunKeyListOtherIDs(t, env.gopts))
|
|
|
|
}
|
|
|
|
|
|
|
|
env.gopts.password = passwordList[len(passwordList)-1]
|
|
|
|
t.Logf("testing access with last password %q\n", env.gopts.password)
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, runKey(env.gopts, []string{"list"}))
|
2017-07-24 19:25:49 +00:00
|
|
|
testRunCheck(t, env.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},
|
|
|
|
}
|
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
env, cleanup := withTestEnvironment(t)
|
|
|
|
defer cleanup()
|
2015-07-08 21:36:24 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
testRunInit(t, env.gopts)
|
2015-07-08 21:36:24 +00:00
|
|
|
|
2017-10-02 13:06:39 +00:00
|
|
|
for _, testFile := range testfiles {
|
|
|
|
p := filepath.Join(env.testdata, testFile.name)
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, os.MkdirAll(filepath.Dir(p), 0755))
|
|
|
|
rtest.OK(t, appendRandomData(p, testFile.size))
|
2017-07-24 19:25:49 +00:00
|
|
|
}
|
2015-07-08 21:36:24 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
opts := BackupOptions{}
|
2016-09-17 10:36:05 +00:00
|
|
|
|
2018-03-25 21:36:31 +00:00
|
|
|
testRunBackup(t, filepath.Dir(env.testdata), []string{filepath.Base(env.testdata)}, opts, env.gopts)
|
2017-07-24 19:25:49 +00:00
|
|
|
testRunCheck(t, env.gopts)
|
2015-07-08 21:36:24 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
snapshotID := testRunList(t, "snapshots", env.gopts)[0]
|
|
|
|
|
|
|
|
// no restore filter should restore all files
|
|
|
|
testRunRestore(t, env.gopts, filepath.Join(env.base, "restore0"), snapshotID)
|
2017-10-02 13:06:39 +00:00
|
|
|
for _, testFile := range testfiles {
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, testFileSize(filepath.Join(env.base, "restore0", "testdata", testFile.name), int64(testFile.size)))
|
2017-07-24 19:25:49 +00:00
|
|
|
}
|
2015-07-08 21:36:24 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
for i, pat := range []string{"*.c", "*.exe", "*", "*file3*"} {
|
|
|
|
base := filepath.Join(env.base, fmt.Sprintf("restore%d", i+1))
|
|
|
|
testRunRestoreExcludes(t, env.gopts, base, snapshotID, []string{pat})
|
2017-10-02 13:06:39 +00:00
|
|
|
for _, testFile := range testfiles {
|
|
|
|
err := testFileSize(filepath.Join(base, "testdata", testFile.name), int64(testFile.size))
|
|
|
|
if ok, _ := filter.Match(pat, filepath.Base(testFile.name)); !ok {
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, err)
|
2017-07-24 19:25:49 +00:00
|
|
|
} else {
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.Assert(t, os.IsNotExist(errors.Cause(err)),
|
2017-10-02 13:06:39 +00:00
|
|
|
"expected %v to not exist in restore step %v, but it exists, err %v", testFile.name, i+1, err)
|
2015-07-08 21:36:24 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-24 19:25:49 +00:00
|
|
|
}
|
2015-07-08 21:36:24 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 10:51:21 +00:00
|
|
|
func TestRestore(t *testing.T) {
|
2017-07-24 19:25:49 +00:00
|
|
|
env, cleanup := withTestEnvironment(t)
|
|
|
|
defer cleanup()
|
2017-01-24 10:51:21 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
testRunInit(t, env.gopts)
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
p := filepath.Join(env.testdata, fmt.Sprintf("foo/bar/testfile%v", i))
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, os.MkdirAll(filepath.Dir(p), 0755))
|
2018-03-11 20:10:37 +00:00
|
|
|
rtest.OK(t, appendRandomData(p, uint(mrand.Intn(2<<21))))
|
2017-07-24 19:25:49 +00:00
|
|
|
}
|
2017-01-24 10:51:21 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
opts := BackupOptions{}
|
2017-01-24 10:51:21 +00:00
|
|
|
|
2018-03-25 21:36:31 +00:00
|
|
|
testRunBackup(t, filepath.Dir(env.testdata), []string{filepath.Base(env.testdata)}, opts, env.gopts)
|
2017-07-24 19:25:49 +00:00
|
|
|
testRunCheck(t, env.gopts)
|
2017-01-24 10:51:21 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
// Restore latest without any filters
|
|
|
|
restoredir := filepath.Join(env.base, "restore")
|
2020-02-26 21:17:59 +00:00
|
|
|
testRunRestoreLatest(t, env.gopts, restoredir, nil, nil)
|
2017-01-24 10:51:21 +00:00
|
|
|
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.Assert(t, directoriesEqualContents(env.testdata, filepath.Join(restoredir, filepath.Base(env.testdata))),
|
2017-07-24 19:25:49 +00:00
|
|
|
"directories are not equal")
|
2017-01-24 10:51:21 +00:00
|
|
|
}
|
|
|
|
|
2016-05-10 19:23:18 +00:00
|
|
|
func TestRestoreLatest(t *testing.T) {
|
2017-07-24 19:25:49 +00:00
|
|
|
env, cleanup := withTestEnvironment(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
testRunInit(t, env.gopts)
|
|
|
|
|
|
|
|
p := filepath.Join(env.testdata, "testfile.c")
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, os.MkdirAll(filepath.Dir(p), 0755))
|
|
|
|
rtest.OK(t, appendRandomData(p, 100))
|
2017-07-24 19:25:49 +00:00
|
|
|
|
|
|
|
opts := BackupOptions{}
|
|
|
|
|
2018-04-10 21:52:32 +00:00
|
|
|
// chdir manually here so we can get the current directory. This is not the
|
|
|
|
// same as the temp dir returned by ioutil.TempDir() on darwin.
|
|
|
|
back := fs.TestChdir(t, filepath.Dir(env.testdata))
|
|
|
|
defer back()
|
|
|
|
|
|
|
|
curdir, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
testRunBackup(t, "", []string{filepath.Base(env.testdata)}, opts, env.gopts)
|
2017-07-24 19:25:49 +00:00
|
|
|
testRunCheck(t, env.gopts)
|
|
|
|
|
|
|
|
os.Remove(p)
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, appendRandomData(p, 101))
|
2018-04-10 21:52:32 +00:00
|
|
|
testRunBackup(t, "", []string{filepath.Base(env.testdata)}, opts, env.gopts)
|
2017-07-24 19:25:49 +00:00
|
|
|
testRunCheck(t, env.gopts)
|
|
|
|
|
|
|
|
// Restore latest without any filters
|
2020-02-26 21:17:59 +00:00
|
|
|
testRunRestoreLatest(t, env.gopts, filepath.Join(env.base, "restore0"), nil, nil)
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, testFileSize(filepath.Join(env.base, "restore0", "testdata", "testfile.c"), int64(101)))
|
2017-07-24 19:25:49 +00:00
|
|
|
|
|
|
|
// Setup test files in different directories backed up in different snapshots
|
2018-04-10 21:52:32 +00:00
|
|
|
p1 := filepath.Join(curdir, filepath.FromSlash("p1/testfile.c"))
|
|
|
|
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, os.MkdirAll(filepath.Dir(p1), 0755))
|
|
|
|
rtest.OK(t, appendRandomData(p1, 102))
|
2018-04-10 21:52:32 +00:00
|
|
|
testRunBackup(t, "", []string{"p1"}, opts, env.gopts)
|
2017-07-24 19:25:49 +00:00
|
|
|
testRunCheck(t, env.gopts)
|
|
|
|
|
2018-04-10 21:52:32 +00:00
|
|
|
p2 := filepath.Join(curdir, filepath.FromSlash("p2/testfile.c"))
|
|
|
|
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, os.MkdirAll(filepath.Dir(p2), 0755))
|
|
|
|
rtest.OK(t, appendRandomData(p2, 103))
|
2018-04-10 21:52:32 +00:00
|
|
|
testRunBackup(t, "", []string{"p2"}, opts, env.gopts)
|
2017-07-24 19:25:49 +00:00
|
|
|
testRunCheck(t, env.gopts)
|
|
|
|
|
|
|
|
p1rAbs := filepath.Join(env.base, "restore1", "p1/testfile.c")
|
|
|
|
p2rAbs := filepath.Join(env.base, "restore2", "p2/testfile.c")
|
|
|
|
|
2020-02-26 21:17:59 +00:00
|
|
|
testRunRestoreLatest(t, env.gopts, filepath.Join(env.base, "restore1"), []string{filepath.Dir(p1)}, nil)
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, testFileSize(p1rAbs, int64(102)))
|
2017-07-24 19:25:49 +00:00
|
|
|
if _, err := os.Stat(p2rAbs); os.IsNotExist(errors.Cause(err)) {
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.Assert(t, os.IsNotExist(errors.Cause(err)),
|
2017-07-24 19:25:49 +00:00
|
|
|
"expected %v to not exist in restore, but it exists, err %v", p2rAbs, err)
|
|
|
|
}
|
|
|
|
|
2020-02-26 21:17:59 +00:00
|
|
|
testRunRestoreLatest(t, env.gopts, filepath.Join(env.base, "restore2"), []string{filepath.Dir(p2)}, nil)
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, testFileSize(p2rAbs, int64(103)))
|
2017-07-24 19:25:49 +00:00
|
|
|
if _, err := os.Stat(p1rAbs); os.IsNotExist(errors.Cause(err)) {
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.Assert(t, os.IsNotExist(errors.Cause(err)),
|
2017-07-24 19:25:49 +00:00
|
|
|
"expected %v to not exist in restore, but it exists, err %v", p1rAbs, err)
|
|
|
|
}
|
|
|
|
}
|
2016-05-10 19:23:18 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
func TestRestoreWithPermissionFailure(t *testing.T) {
|
|
|
|
env, cleanup := withTestEnvironment(t)
|
|
|
|
defer cleanup()
|
2016-05-10 19:23:18 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
datafile := filepath.Join("testdata", "repo-restore-permissions-test.tar.gz")
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.SetupTarTestFixture(t, env.base, datafile)
|
2016-05-10 19:23:18 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
snapshots := testRunList(t, "snapshots", env.gopts)
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.Assert(t, len(snapshots) > 0,
|
2017-07-24 19:25:49 +00:00
|
|
|
"no snapshots found in repo (%v)", datafile)
|
2016-05-10 19:23:18 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
globalOptions.stderr = ioutil.Discard
|
|
|
|
defer func() {
|
|
|
|
globalOptions.stderr = os.Stderr
|
|
|
|
}()
|
2016-05-10 19:23:18 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
testRunRestore(t, env.gopts, filepath.Join(env.base, "restore"), snapshots[0])
|
|
|
|
|
|
|
|
// make sure that all files have been restored, regardless of any
|
|
|
|
// permission errors
|
|
|
|
files := testRunLs(t, env.gopts, snapshots[0].String())
|
|
|
|
for _, filename := range files {
|
|
|
|
fi, err := os.Lstat(filepath.Join(env.base, "restore", filename))
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, err)
|
2017-07-24 19:25:49 +00:00
|
|
|
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.Assert(t, !isFile(fi) || fi.Size() > 0,
|
2017-07-24 19:25:49 +00:00
|
|
|
"file %v restored, but filesize is 0", filename)
|
|
|
|
}
|
2015-07-25 10:58:55 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2017-07-24 19:25:49 +00:00
|
|
|
env, cleanup := withTestEnvironment(t)
|
|
|
|
defer cleanup()
|
2015-07-08 21:36:24 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
testRunInit(t, env.gopts)
|
2015-07-08 21:36:24 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
p := filepath.Join(env.testdata, "subdir1", "subdir2", "subdir3", "file.ext")
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, os.MkdirAll(filepath.Dir(p), 0755))
|
|
|
|
rtest.OK(t, appendRandomData(p, 200))
|
|
|
|
rtest.OK(t, setZeroModTime(filepath.Join(env.testdata, "subdir1", "subdir2")))
|
2016-09-17 10:36:05 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
opts := BackupOptions{}
|
2015-07-08 21:36:24 +00:00
|
|
|
|
2018-03-25 21:36:31 +00:00
|
|
|
testRunBackup(t, filepath.Dir(env.testdata), []string{filepath.Base(env.testdata)}, opts, env.gopts)
|
2017-07-24 19:25:49 +00:00
|
|
|
testRunCheck(t, env.gopts)
|
2015-07-08 21:36:24 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
snapshotID := testRunList(t, "snapshots", env.gopts)[0]
|
2015-07-08 21:36:24 +00:00
|
|
|
|
2017-07-24 19:25:49 +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.
|
|
|
|
testRunRestoreIncludes(t, env.gopts, filepath.Join(env.base, "restore0"), snapshotID, []string{"*.ext"})
|
2015-07-08 21:36:24 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
f1 := filepath.Join(env.base, "restore0", "testdata", "subdir1", "subdir2")
|
|
|
|
fi, err := os.Stat(f1)
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, err)
|
2015-07-08 21:36:24 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
// restore with filter "*", this should restore meta data on everything.
|
|
|
|
testRunRestoreIncludes(t, env.gopts, filepath.Join(env.base, "restore1"), snapshotID, []string{"*"})
|
2015-07-08 21:36:24 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
f2 := filepath.Join(env.base, "restore1", "testdata", "subdir1", "subdir2")
|
|
|
|
fi, err = os.Stat(f2)
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, err)
|
2017-07-24 19:25:49 +00:00
|
|
|
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.Assert(t, fi.ModTime() == time.Unix(0, 0),
|
2017-07-24 19:25:49 +00:00
|
|
|
"meta data of intermediate directory hasn't been restore")
|
2015-07-08 21:36:24 +00:00
|
|
|
}
|
2015-08-28 17:31:05 +00:00
|
|
|
|
|
|
|
func TestFind(t *testing.T) {
|
2017-07-24 19:25:49 +00:00
|
|
|
env, cleanup := withTestEnvironment(t)
|
|
|
|
defer cleanup()
|
2015-08-28 17:31:05 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
datafile := filepath.Join("testdata", "backup-data.tar.gz")
|
|
|
|
testRunInit(t, env.gopts)
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.SetupTarTestFixture(t, env.testdata, datafile)
|
2016-09-17 10:36:05 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
opts := BackupOptions{}
|
2016-09-17 10:36:05 +00:00
|
|
|
|
2018-03-25 21:36:31 +00:00
|
|
|
testRunBackup(t, "", []string{env.testdata}, opts, env.gopts)
|
2017-07-24 19:25:49 +00:00
|
|
|
testRunCheck(t, env.gopts)
|
2017-03-09 15:06:28 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
results := testRunFind(t, false, env.gopts, "unexistingfile")
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.Assert(t, len(results) == 0, "unexisting file found in repo (%v)", datafile)
|
2017-03-09 15:06:28 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
results = testRunFind(t, false, env.gopts, "testfile")
|
|
|
|
lines := strings.Split(string(results), "\n")
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.Assert(t, len(lines) == 2, "expected one file found in repo (%v)", datafile)
|
2017-07-24 19:25:49 +00:00
|
|
|
|
|
|
|
results = testRunFind(t, false, env.gopts, "testfile*")
|
|
|
|
lines = strings.Split(string(results), "\n")
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.Assert(t, len(lines) == 4, "expected three files found in repo (%v)", datafile)
|
2017-03-09 15:06:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type testMatch struct {
|
|
|
|
Path string `json:"path,omitempty"`
|
|
|
|
Permissions string `json:"permissions,omitempty"`
|
|
|
|
Size uint64 `json:"size,omitempty"`
|
|
|
|
Date time.Time `json:"date,omitempty"`
|
|
|
|
UID uint32 `json:"uid,omitempty"`
|
|
|
|
GID uint32 `json:"gid,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type testMatches struct {
|
|
|
|
Hits int `json:"hits,omitempty"`
|
|
|
|
SnapshotID string `json:"snapshot,omitempty"`
|
|
|
|
Matches []testMatch `json:"matches,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFindJSON(t *testing.T) {
|
2017-07-24 19:25:49 +00:00
|
|
|
env, cleanup := withTestEnvironment(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
datafile := filepath.Join("testdata", "backup-data.tar.gz")
|
|
|
|
testRunInit(t, env.gopts)
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.SetupTarTestFixture(t, env.testdata, datafile)
|
2017-07-24 19:25:49 +00:00
|
|
|
|
|
|
|
opts := BackupOptions{}
|
|
|
|
|
2018-03-25 21:36:31 +00:00
|
|
|
testRunBackup(t, "", []string{env.testdata}, opts, env.gopts)
|
2017-07-24 19:25:49 +00:00
|
|
|
testRunCheck(t, env.gopts)
|
|
|
|
|
|
|
|
results := testRunFind(t, true, env.gopts, "unexistingfile")
|
|
|
|
matches := []testMatches{}
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, json.Unmarshal(results, &matches))
|
|
|
|
rtest.Assert(t, len(matches) == 0, "expected no match in repo (%v)", datafile)
|
2017-07-24 19:25:49 +00:00
|
|
|
|
|
|
|
results = testRunFind(t, true, env.gopts, "testfile")
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, json.Unmarshal(results, &matches))
|
|
|
|
rtest.Assert(t, len(matches) == 1, "expected a single snapshot in repo (%v)", datafile)
|
|
|
|
rtest.Assert(t, len(matches[0].Matches) == 1, "expected a single file to match (%v)", datafile)
|
|
|
|
rtest.Assert(t, matches[0].Hits == 1, "expected hits to show 1 match (%v)", datafile)
|
2017-07-24 19:25:49 +00:00
|
|
|
|
|
|
|
results = testRunFind(t, true, env.gopts, "testfile*")
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, json.Unmarshal(results, &matches))
|
|
|
|
rtest.Assert(t, len(matches) == 1, "expected a single snapshot in repo (%v)", datafile)
|
|
|
|
rtest.Assert(t, len(matches[0].Matches) == 3, "expected 3 files to match (%v)", datafile)
|
|
|
|
rtest.Assert(t, matches[0].Hits == 3, "expected hits to show 3 matches (%v)", datafile)
|
2015-08-28 17:31:05 +00:00
|
|
|
}
|
2015-10-25 16:24:52 +00:00
|
|
|
|
|
|
|
func TestRebuildIndex(t *testing.T) {
|
2017-07-24 19:25:49 +00:00
|
|
|
env, cleanup := withTestEnvironment(t)
|
|
|
|
defer cleanup()
|
2015-10-25 16:24:52 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
datafile := filepath.Join("..", "..", "internal", "checker", "testdata", "duplicate-packs-in-index-test-repo.tar.gz")
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.SetupTarTestFixture(t, env.base, datafile)
|
2015-10-25 16:24:52 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
out, err := testRunCheckOutput(env.gopts)
|
|
|
|
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
|
|
|
|
2017-07-24 19:25:49 +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
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
if !strings.Contains(out, "restic rebuild-index") {
|
|
|
|
t.Fatalf("did not find hint for rebuild-index command")
|
|
|
|
}
|
2015-10-25 16:24:52 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
testRunRebuildIndex(t, env.gopts)
|
2016-09-17 10:36:05 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
out, err = testRunCheckOutput(env.gopts)
|
|
|
|
if len(out) != 0 {
|
|
|
|
t.Fatalf("expected no output from the checker, got: %v", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("expected no error from checker after rebuild-index, got: %v", err)
|
|
|
|
}
|
2015-10-25 16:24:52 +00:00
|
|
|
}
|
2015-10-25 20:51:57 +00:00
|
|
|
|
|
|
|
func TestRebuildIndexAlwaysFull(t *testing.T) {
|
|
|
|
repository.IndexFull = func(*repository.Index) bool { return true }
|
|
|
|
TestRebuildIndex(t)
|
|
|
|
}
|
2015-11-08 20:10:03 +00:00
|
|
|
|
2015-11-10 20:41:22 +00:00
|
|
|
func TestCheckRestoreNoLock(t *testing.T) {
|
2017-07-24 19:25:49 +00:00
|
|
|
env, cleanup := withTestEnvironment(t)
|
|
|
|
defer cleanup()
|
2015-11-10 20:41:22 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
datafile := filepath.Join("testdata", "small-repo.tar.gz")
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.SetupTarTestFixture(t, env.base, datafile)
|
2015-11-10 20:41:22 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
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)))
|
|
|
|
})
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, err)
|
2016-09-17 10:36:05 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
env.gopts.NoLock = true
|
2015-11-10 21:13:53 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
testRunCheck(t, env.gopts)
|
2015-11-10 21:13:53 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
snapshotIDs := testRunList(t, "snapshots", env.gopts)
|
|
|
|
if len(snapshotIDs) == 0 {
|
|
|
|
t.Fatalf("found no snapshots")
|
|
|
|
}
|
|
|
|
|
|
|
|
testRunRestore(t, env.gopts, filepath.Join(env.base, "restore"), snapshotIDs[0])
|
2015-11-10 20:41:22 +00:00
|
|
|
}
|
2017-01-22 21:23:30 +00:00
|
|
|
|
|
|
|
func TestPrune(t *testing.T) {
|
2017-07-24 19:25:49 +00:00
|
|
|
env, cleanup := withTestEnvironment(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, err)
|
|
|
|
rtest.OK(t, fd.Close())
|
2017-01-22 21:23:30 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
testRunInit(t, env.gopts)
|
2017-01-22 21:23:30 +00:00
|
|
|
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.SetupTarTestFixture(t, env.testdata, datafile)
|
2017-07-24 19:25:49 +00:00
|
|
|
opts := BackupOptions{}
|
2017-01-22 21:23:30 +00:00
|
|
|
|
2018-03-25 21:36:31 +00:00
|
|
|
testRunBackup(t, "", []string{filepath.Join(env.testdata, "0", "0", "9")}, opts, env.gopts)
|
2017-07-24 19:25:49 +00:00
|
|
|
firstSnapshot := testRunList(t, "snapshots", env.gopts)
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.Assert(t, len(firstSnapshot) == 1,
|
2017-07-24 19:25:49 +00:00
|
|
|
"expected one snapshot, got %v", firstSnapshot)
|
2017-07-18 21:07:29 +00:00
|
|
|
|
2018-03-25 21:36:31 +00:00
|
|
|
testRunBackup(t, "", []string{filepath.Join(env.testdata, "0", "0", "9", "2")}, opts, env.gopts)
|
|
|
|
testRunBackup(t, "", []string{filepath.Join(env.testdata, "0", "0", "9", "3")}, opts, env.gopts)
|
2017-01-22 21:23:30 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
snapshotIDs := testRunList(t, "snapshots", env.gopts)
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.Assert(t, len(snapshotIDs) == 3,
|
2017-07-24 19:25:49 +00:00
|
|
|
"expected 3 snapshot, got %v", snapshotIDs)
|
2017-01-22 21:23:30 +00:00
|
|
|
|
2019-02-23 04:52:30 +00:00
|
|
|
testRunForgetJSON(t, env.gopts)
|
2017-07-24 19:25:49 +00:00
|
|
|
testRunForget(t, env.gopts, firstSnapshot[0].String())
|
|
|
|
testRunPrune(t, env.gopts)
|
|
|
|
testRunCheck(t, env.gopts)
|
2017-01-22 21:23:30 +00:00
|
|
|
}
|
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
|
2017-07-24 19:25:49 +00:00
|
|
|
env, cleanup := withTestEnvironment(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, err)
|
|
|
|
rtest.OK(t, fd.Close())
|
2017-01-30 23:14:20 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
testRunInit(t, env.gopts)
|
2017-01-30 23:14:20 +00:00
|
|
|
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.SetupTarTestFixture(t, env.testdata, datafile)
|
2017-01-30 23:14:20 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
linkTests := createFileSetPerHardlink(env.testdata)
|
2017-01-30 23:14:20 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
opts := BackupOptions{}
|
2017-01-30 23:14:20 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
// first backup
|
2018-03-25 21:36:31 +00:00
|
|
|
testRunBackup(t, filepath.Dir(env.testdata), []string{filepath.Base(env.testdata)}, opts, env.gopts)
|
2017-07-24 19:25:49 +00:00
|
|
|
snapshotIDs := testRunList(t, "snapshots", env.gopts)
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.Assert(t, len(snapshotIDs) == 1,
|
2017-07-24 19:25:49 +00:00
|
|
|
"expected one snapshot, got %v", snapshotIDs)
|
2017-01-30 23:14:20 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
testRunCheck(t, env.gopts)
|
2017-01-30 23:14:20 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
// 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, env.gopts, restoredir, snapshotIDs[0])
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.Assert(t, directoriesEqualContents(env.testdata, filepath.Join(restoredir, "testdata")),
|
2017-07-24 19:25:49 +00:00
|
|
|
"directories are not equal")
|
2017-01-30 23:14:20 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
linkResults := createFileSetPerHardlink(filepath.Join(restoredir, "testdata"))
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.Assert(t, linksEqual(linkTests, linkResults),
|
2017-07-24 19:25:49 +00:00
|
|
|
"links are not equal")
|
|
|
|
}
|
2017-01-30 23:14:20 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
testRunCheck(t, env.gopts)
|
2017-01-30 23:14:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2018-03-20 19:54:29 +00:00
|
|
|
|
|
|
|
func TestQuietBackup(t *testing.T) {
|
|
|
|
env, cleanup := withTestEnvironment(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
rtest.OK(t, err)
|
|
|
|
rtest.OK(t, fd.Close())
|
|
|
|
|
|
|
|
testRunInit(t, env.gopts)
|
|
|
|
|
|
|
|
rtest.SetupTarTestFixture(t, env.testdata, datafile)
|
|
|
|
opts := BackupOptions{}
|
|
|
|
|
|
|
|
env.gopts.Quiet = false
|
2018-03-25 21:36:31 +00:00
|
|
|
testRunBackup(t, "", []string{env.testdata}, opts, env.gopts)
|
2018-03-20 19:54:29 +00:00
|
|
|
snapshotIDs := testRunList(t, "snapshots", env.gopts)
|
|
|
|
rtest.Assert(t, len(snapshotIDs) == 1,
|
|
|
|
"expected one snapshot, got %v", snapshotIDs)
|
|
|
|
|
|
|
|
testRunCheck(t, env.gopts)
|
|
|
|
|
|
|
|
env.gopts.Quiet = true
|
2018-03-25 21:36:31 +00:00
|
|
|
testRunBackup(t, "", []string{env.testdata}, opts, env.gopts)
|
2018-03-20 19:54:29 +00:00
|
|
|
snapshotIDs = testRunList(t, "snapshots", env.gopts)
|
|
|
|
rtest.Assert(t, len(snapshotIDs) == 2,
|
|
|
|
"expected two snapshots, got %v", snapshotIDs)
|
|
|
|
|
|
|
|
testRunCheck(t, env.gopts)
|
|
|
|
}
|