2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-02 03:42:31 +00:00

Merge pull request #1131 from restic/test-cleanups

Rework withTestEnvironment
This commit is contained in:
Alexander Neumann 2017-07-26 22:06:06 +02:00
commit b6790c491b
4 changed files with 759 additions and 735 deletions

View File

@ -142,44 +142,45 @@ func TestMount(t *testing.T) {
t.Skip("Skipping fuse tests")
}
withTestEnvironment(t, func(env *testEnvironment, gopts GlobalOptions) {
testRunInit(t, gopts)
env, cleanup := withTestEnvironment(t)
defer cleanup()
repo, err := OpenRepository(gopts)
OK(t, err)
testRunInit(t, env.gopts)
// We remove the mountpoint now to check that cmdMount creates it
RemoveAll(t, env.mountpoint)
repo, err := OpenRepository(env.gopts)
OK(t, err)
checkSnapshots(t, gopts, repo, env.mountpoint, env.repo, []restic.ID{})
// We remove the mountpoint now to check that cmdMount creates it
RemoveAll(t, env.mountpoint)
SetupTarTestFixture(t, env.testdata, filepath.Join("testdata", "backup-data.tar.gz"))
checkSnapshots(t, env.gopts, repo, env.mountpoint, env.repo, []restic.ID{})
// first backup
testRunBackup(t, []string{env.testdata}, BackupOptions{}, gopts)
snapshotIDs := testRunList(t, "snapshots", gopts)
Assert(t, len(snapshotIDs) == 1,
"expected one snapshot, got %v", snapshotIDs)
SetupTarTestFixture(t, env.testdata, filepath.Join("testdata", "backup-data.tar.gz"))
checkSnapshots(t, gopts, repo, env.mountpoint, env.repo, snapshotIDs)
// first backup
testRunBackup(t, []string{env.testdata}, BackupOptions{}, env.gopts)
snapshotIDs := testRunList(t, "snapshots", env.gopts)
Assert(t, len(snapshotIDs) == 1,
"expected one snapshot, got %v", snapshotIDs)
// second backup, implicit incremental
testRunBackup(t, []string{env.testdata}, BackupOptions{}, gopts)
snapshotIDs = testRunList(t, "snapshots", gopts)
Assert(t, len(snapshotIDs) == 2,
"expected two snapshots, got %v", snapshotIDs)
checkSnapshots(t, env.gopts, repo, env.mountpoint, env.repo, snapshotIDs)
checkSnapshots(t, gopts, repo, env.mountpoint, env.repo, snapshotIDs)
// second backup, implicit incremental
testRunBackup(t, []string{env.testdata}, BackupOptions{}, env.gopts)
snapshotIDs = testRunList(t, "snapshots", env.gopts)
Assert(t, len(snapshotIDs) == 2,
"expected two snapshots, got %v", snapshotIDs)
// third backup, explicit incremental
bopts := BackupOptions{Parent: snapshotIDs[0].String()}
testRunBackup(t, []string{env.testdata}, bopts, gopts)
snapshotIDs = testRunList(t, "snapshots", gopts)
Assert(t, len(snapshotIDs) == 3,
"expected three snapshots, got %v", snapshotIDs)
checkSnapshots(t, env.gopts, repo, env.mountpoint, env.repo, snapshotIDs)
checkSnapshots(t, gopts, repo, env.mountpoint, env.repo, snapshotIDs)
})
// third backup, explicit incremental
bopts := BackupOptions{Parent: snapshotIDs[0].String()}
testRunBackup(t, []string{env.testdata}, bopts, env.gopts)
snapshotIDs = testRunList(t, "snapshots", env.gopts)
Assert(t, len(snapshotIDs) == 3,
"expected three snapshots, got %v", snapshotIDs)
checkSnapshots(t, env.gopts, repo, env.mountpoint, env.repo, snapshotIDs)
}
func TestMountSameTimestamps(t *testing.T) {
@ -187,18 +188,19 @@ func TestMountSameTimestamps(t *testing.T) {
t.Skip("Skipping fuse tests")
}
withTestEnvironment(t, func(env *testEnvironment, gopts GlobalOptions) {
SetupTarTestFixture(t, env.base, filepath.Join("testdata", "repo-same-timestamps.tar.gz"))
env, cleanup := withTestEnvironment(t)
defer cleanup()
repo, err := OpenRepository(gopts)
OK(t, err)
SetupTarTestFixture(t, env.base, filepath.Join("testdata", "repo-same-timestamps.tar.gz"))
ids := []restic.ID{
restic.TestParseID("280303689e5027328889a06d718b729e96a1ce6ae9ef8290bff550459ae611ee"),
restic.TestParseID("75ad6cdc0868e082f2596d5ab8705e9f7d87316f5bf5690385eeff8dbe49d9f5"),
restic.TestParseID("5fd0d8b2ef0fa5d23e58f1e460188abb0f525c0f0c4af8365a1280c807a80a1b"),
}
repo, err := OpenRepository(env.gopts)
OK(t, err)
checkSnapshots(t, gopts, repo, env.mountpoint, env.repo, ids)
})
ids := []restic.ID{
restic.TestParseID("280303689e5027328889a06d718b729e96a1ce6ae9ef8290bff550459ae611ee"),
restic.TestParseID("75ad6cdc0868e082f2596d5ab8705e9f7d87316f5bf5690385eeff8dbe49d9f5"),
restic.TestParseID("5fd0d8b2ef0fa5d23e58f1e460188abb0f525c0f0c4af8365a1280c807a80a1b"),
}
checkSnapshots(t, env.gopts, repo, env.mountpoint, env.repo, ids)
}

View File

@ -168,11 +168,12 @@ func dirStats(dir string) (stat dirStat) {
type testEnvironment struct {
base, cache, repo, mountpoint, testdata string
gopts GlobalOptions
}
// withTestEnvironment creates a test environment and calls f with it. After f has
// returned, the temporary directory is removed.
func withTestEnvironment(t testing.TB, f func(*testEnvironment, GlobalOptions)) {
// withTestEnvironment creates a test environment and returns a cleanup
// function which removes it.
func withTestEnvironment(t testing.TB) (env *testEnvironment, cleanup func()) {
if !RunIntegrationTest {
t.Skip("integration tests disabled")
}
@ -182,7 +183,7 @@ func withTestEnvironment(t testing.TB, f func(*testEnvironment, GlobalOptions))
tempdir, err := ioutil.TempDir(TestTempDir, "restic-test-")
OK(t, err)
env := testEnvironment{
env = &testEnvironment{
base: tempdir,
cache: filepath.Join(tempdir, "cache"),
repo: filepath.Join(tempdir, "repo"),
@ -195,7 +196,7 @@ func withTestEnvironment(t testing.TB, f func(*testEnvironment, GlobalOptions))
OK(t, os.MkdirAll(env.cache, 0700))
OK(t, os.MkdirAll(env.repo, 0700))
gopts := GlobalOptions{
env.gopts = GlobalOptions{
Repo: env.repo,
Quiet: true,
ctx: context.Background(),
@ -206,14 +207,15 @@ func withTestEnvironment(t testing.TB, f func(*testEnvironment, GlobalOptions))
}
// always overwrite global options
globalOptions = gopts
globalOptions = env.gopts
f(&env, gopts)
if !TestCleanupTempDirs {
t.Logf("leaving temporary directory %v used for test", tempdir)
return
cleanup = func() {
if !TestCleanupTempDirs {
t.Logf("leaving temporary directory %v used for test", tempdir)
return
}
RemoveAll(t, tempdir)
}
RemoveAll(t, tempdir)
return env, cleanup
}

File diff suppressed because it is too large Load Diff

View File

@ -8,33 +8,34 @@ import (
)
func TestRestoreLocalLayout(t *testing.T) {
withTestEnvironment(t, func(env *testEnvironment, gopts GlobalOptions) {
var tests = []struct {
filename string
layout string
}{
{"repo-layout-default.tar.gz", ""},
{"repo-layout-s3legacy.tar.gz", ""},
{"repo-layout-default.tar.gz", "default"},
{"repo-layout-s3legacy.tar.gz", "s3legacy"},
}
env, cleanup := withTestEnvironment(t)
defer cleanup()
for _, test := range tests {
datafile := filepath.Join("..", "..", "internal", "backend", "testdata", test.filename)
var tests = []struct {
filename string
layout string
}{
{"repo-layout-default.tar.gz", ""},
{"repo-layout-s3legacy.tar.gz", ""},
{"repo-layout-default.tar.gz", "default"},
{"repo-layout-s3legacy.tar.gz", "s3legacy"},
}
SetupTarTestFixture(t, env.base, datafile)
for _, test := range tests {
datafile := filepath.Join("..", "..", "internal", "backend", "testdata", test.filename)
gopts.extended["local.layout"] = test.layout
SetupTarTestFixture(t, env.base, datafile)
// check the repo
testRunCheck(t, gopts)
env.gopts.extended["local.layout"] = test.layout
// restore latest snapshot
target := filepath.Join(env.base, "restore")
testRunRestoreLatest(t, gopts, target, nil, "")
// check the repo
testRunCheck(t, env.gopts)
RemoveAll(t, filepath.Join(env.base, "repo"))
RemoveAll(t, target)
}
})
// restore latest snapshot
target := filepath.Join(env.base, "restore")
testRunRestoreLatest(t, env.gopts, target, nil, "")
RemoveAll(t, filepath.Join(env.base, "repo"))
RemoveAll(t, target)
}
}