restic/src/cmds/restic/integration_fuse_test.go

167 lines
4.3 KiB
Go
Raw Normal View History

// +build !openbsd
2015-08-17 11:02:04 +02:00
// +build !windows
package main
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
"restic"
"restic/repository"
. "restic/test"
)
2015-07-19 22:53:10 +02:00
const (
mountWait = 20
mountSleep = 100 * time.Millisecond
mountTestSubdir = "snapshots"
)
2016-09-15 21:17:20 +02:00
func snapshotsDirExists(t testing.TB, dir string) bool {
f, err := os.Open(filepath.Join(dir, mountTestSubdir))
if err != nil && os.IsNotExist(err) {
return false
}
2015-07-19 22:53:10 +02:00
2016-09-15 21:17:20 +02:00
if err != nil {
t.Error(err)
}
2015-07-19 22:53:10 +02:00
2016-09-15 21:17:20 +02:00
if err := f.Close(); err != nil {
t.Error(err)
}
2015-07-19 22:53:10 +02:00
2016-09-15 21:17:20 +02:00
return true
}
// waitForMount blocks (max mountWait * mountSleep) until the subdir
// "snapshots" appears in the dir.
func waitForMount(t testing.TB, dir string) {
for i := 0; i < mountWait; i++ {
if snapshotsDirExists(t, dir) {
t.Log("mounted directory is ready")
return
2015-07-19 22:53:10 +02:00
}
time.Sleep(mountSleep)
}
2016-09-15 21:17:20 +02:00
t.Errorf("subdir %q of dir %s never appeared", mountTestSubdir, dir)
2015-07-19 22:53:10 +02:00
}
2016-09-15 21:17:20 +02:00
func mount(t testing.TB, global GlobalOptions, dir string) {
cmd := &CmdMount{global: &global}
OK(t, cmd.Mount(dir))
}
2016-09-15 21:17:20 +02:00
func umount(t testing.TB, global GlobalOptions, dir string) {
cmd := &CmdMount{global: &global}
2016-09-15 21:32:15 +02:00
var err error
for i := 0; i < mountWait; i++ {
if err = cmd.Umount(dir); err == nil {
t.Logf("directory %v umounted", dir)
return
}
time.Sleep(mountSleep)
}
t.Errorf("unable to umount dir %v, last error was: %v", dir, err)
2016-09-15 21:17:20 +02:00
}
func listSnapshots(t testing.TB, dir string) []string {
snapshotsDir, err := os.Open(filepath.Join(dir, "snapshots"))
OK(t, err)
names, err := snapshotsDir.Readdirnames(-1)
OK(t, err)
OK(t, snapshotsDir.Close())
return names
}
func TestMount(t *testing.T) {
if !RunFuseTest {
t.Skip("Skipping fuse tests")
}
2016-09-15 21:17:20 +02:00
withTestEnvironment(t, func(env *testEnvironment, global GlobalOptions) {
checkSnapshots := func(repo *repository.Repository, mountpoint string, snapshotIDs restic.IDs) {
t.Logf("checking for %d snapshots: %v", len(snapshotIDs), snapshotIDs)
go mount(t, global, mountpoint)
waitForMount(t, mountpoint)
defer umount(t, global, mountpoint)
if !snapshotsDirExists(t, mountpoint) {
t.Fatal(`virtual directory "snapshots" doesn't exist`)
}
2016-09-15 21:17:20 +02:00
ids := listSnapshots(t, env.repo)
t.Logf("found %v snapshots in repo: %v", len(ids), ids)
2015-07-19 15:21:21 +02:00
2016-09-15 21:17:20 +02:00
namesInSnapshots := listSnapshots(t, mountpoint)
t.Logf("found %v snapshots in fuse mount: %v", len(namesInSnapshots), namesInSnapshots)
Assert(t,
len(namesInSnapshots) == len(snapshotIDs),
"Invalid number of snapshots: expected %d, got %d", len(snapshotIDs), len(namesInSnapshots))
namesMap := make(map[string]bool)
for _, name := range namesInSnapshots {
namesMap[name] = false
}
for _, id := range snapshotIDs {
snapshot, err := restic.LoadSnapshot(repo, id)
OK(t, err)
_, ok := namesMap[snapshot.Time.Format(time.RFC3339)]
Assert(t, ok, "Snapshot %s isn't present in fuse dir", snapshot.Time.Format(time.RFC3339))
namesMap[snapshot.Time.Format(time.RFC3339)] = true
}
for name, present := range namesMap {
Assert(t, present, "Directory %s is present in fuse dir but is not a snapshot", name)
}
}
cmdInit(t, global)
repo, err := global.OpenRepository()
OK(t, err)
mountpoint, err := ioutil.TempDir(TestTempDir, "restic-test-mount-")
OK(t, err)
// We remove the mountpoint now to check that cmdMount creates it
RemoveAll(t, mountpoint)
2016-09-01 16:04:29 +02:00
checkSnapshots(repo, mountpoint, []restic.ID{})
2016-09-15 21:17:20 +02:00
SetupTarTestFixture(t, env.testdata, filepath.Join("testdata", "backup-data.tar.gz"))
// first backup
cmdBackup(t, global, []string{env.testdata}, nil)
snapshotIDs := cmdList(t, global, "snapshots")
Assert(t, len(snapshotIDs) == 1,
"expected one snapshot, got %v", snapshotIDs)
checkSnapshots(repo, mountpoint, snapshotIDs)
// second backup, implicit incremental
cmdBackup(t, global, []string{env.testdata}, nil)
snapshotIDs = cmdList(t, global, "snapshots")
Assert(t, len(snapshotIDs) == 2,
"expected two snapshots, got %v", snapshotIDs)
checkSnapshots(repo, mountpoint, snapshotIDs)
// third backup, explicit incremental
cmdBackup(t, global, []string{env.testdata}, &snapshotIDs[0])
snapshotIDs = cmdList(t, global, "snapshots")
Assert(t, len(snapshotIDs) == 3,
"expected three snapshots, got %v", snapshotIDs)
checkSnapshots(repo, mountpoint, snapshotIDs)
})
}