2
2
mirror of https://github.com/octoleo/restic.git synced 2024-06-03 09:30:50 +00:00

Add integration tests for mount command

This commit is contained in:
Matthieu Rakotojaona 2015-07-18 21:31:51 +02:00
parent 414ade5b3f
commit 25b090a074

View File

@ -14,8 +14,10 @@ import (
"testing"
"time"
"github.com/restic/restic"
"github.com/restic/restic/backend"
"github.com/restic/restic/debug"
"github.com/restic/restic/repository"
. "github.com/restic/restic/test"
)
@ -73,6 +75,11 @@ func cmdCheck(t testing.TB, global GlobalOptions) {
OK(t, cmd.Execute(nil))
}
func cmdMount(t testing.TB, global GlobalOptions, dir string, ready chan struct{}) {
cmd := &CmdMount{global: &global, ready: ready}
OK(t, cmd.Execute([]string{dir}))
}
func TestBackup(t *testing.T) {
withTestEnvironment(t, func(env *testEnvironment, global GlobalOptions) {
datafile := filepath.Join("testdata", "backup-data.tar.gz")
@ -483,3 +490,83 @@ func TestRestoreNoMetadataOnIgnoredIntermediateDirs(t *testing.T) {
"meta data of intermediate directory hasn't been restore")
})
}
func TestMount(t *testing.T) {
checkSnapshots := func(repo *repository.Repository, mountpoint string, snapshotIDs []backend.ID) {
stSnapshots, err := os.Open(filepath.Join(mountpoint, "snapshots"))
OK(t, err)
namesInSnapshots, err := stSnapshots.Readdirnames(-1)
OK(t, err)
Assert(t,
len(namesInSnapshots) == len(snapshotIDs),
"Invalid number of snapshots: expected %d, got %d", len(snapshotIDs), len(namesInSnapshots))
for i, id := range snapshotIDs {
snapshot, err := restic.LoadSnapshot(repo, id)
OK(t, err)
Assert(t,
namesInSnapshots[i] == snapshot.Time.Format(time.RFC3339),
"Invalid snapshot folder name: expected %s, got %s", snapshot.Time.Format(time.RFC3339), namesInSnapshots[i])
}
OK(t, stSnapshots.Close())
}
withTestEnvironment(t, func(env *testEnvironment, global GlobalOptions) {
cmdInit(t, global)
repo, err := global.OpenRepository()
OK(t, err)
mountpoint, err := ioutil.TempDir(TestTempDir, "restic-test-mount-")
OK(t, err)
defer OK(t, os.RemoveAll(mountpoint))
ready := make(chan struct{}, 1)
go cmdMount(t, global, mountpoint, ready)
<-ready
stMountPoint, err := os.Open(mountpoint)
OK(t, err)
names, err := stMountPoint.Readdirnames(-1)
OK(t, err)
Assert(t, len(names) == 1 && names[0] == "snapshots", "expected the snapshots directory to exist")
OK(t, stMountPoint.Close())
checkSnapshots(repo, mountpoint, []backend.ID{})
datafile := filepath.Join("testdata", "backup-data.tar.gz")
fd, err := os.Open(datafile)
if os.IsNotExist(err) {
t.Skipf("unable to find data file %q, skipping", datafile)
return
}
OK(t, err)
OK(t, fd.Close())
SetupTarTestFixture(t, env.testdata, datafile)
// 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)
})
}