2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-30 00:33:57 +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") t.Skip("Skipping fuse tests")
} }
withTestEnvironment(t, func(env *testEnvironment, gopts GlobalOptions) { env, cleanup := withTestEnvironment(t)
testRunInit(t, gopts) defer cleanup()
repo, err := OpenRepository(gopts) testRunInit(t, env.gopts)
OK(t, err)
// We remove the mountpoint now to check that cmdMount creates it repo, err := OpenRepository(env.gopts)
RemoveAll(t, env.mountpoint) 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 SetupTarTestFixture(t, env.testdata, filepath.Join("testdata", "backup-data.tar.gz"))
testRunBackup(t, []string{env.testdata}, BackupOptions{}, gopts)
snapshotIDs := testRunList(t, "snapshots", gopts)
Assert(t, len(snapshotIDs) == 1,
"expected one snapshot, got %v", snapshotIDs)
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 checkSnapshots(t, env.gopts, repo, env.mountpoint, env.repo, snapshotIDs)
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, 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 checkSnapshots(t, env.gopts, repo, env.mountpoint, env.repo, snapshotIDs)
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, 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) { func TestMountSameTimestamps(t *testing.T) {
@ -187,18 +188,19 @@ func TestMountSameTimestamps(t *testing.T) {
t.Skip("Skipping fuse tests") t.Skip("Skipping fuse tests")
} }
withTestEnvironment(t, func(env *testEnvironment, gopts GlobalOptions) { env, cleanup := withTestEnvironment(t)
SetupTarTestFixture(t, env.base, filepath.Join("testdata", "repo-same-timestamps.tar.gz")) defer cleanup()
repo, err := OpenRepository(gopts) SetupTarTestFixture(t, env.base, filepath.Join("testdata", "repo-same-timestamps.tar.gz"))
OK(t, err)
ids := []restic.ID{ repo, err := OpenRepository(env.gopts)
restic.TestParseID("280303689e5027328889a06d718b729e96a1ce6ae9ef8290bff550459ae611ee"), OK(t, err)
restic.TestParseID("75ad6cdc0868e082f2596d5ab8705e9f7d87316f5bf5690385eeff8dbe49d9f5"),
restic.TestParseID("5fd0d8b2ef0fa5d23e58f1e460188abb0f525c0f0c4af8365a1280c807a80a1b"),
}
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 { type testEnvironment struct {
base, cache, repo, mountpoint, testdata string base, cache, repo, mountpoint, testdata string
gopts GlobalOptions
} }
// withTestEnvironment creates a test environment and calls f with it. After f has // withTestEnvironment creates a test environment and returns a cleanup
// returned, the temporary directory is removed. // function which removes it.
func withTestEnvironment(t testing.TB, f func(*testEnvironment, GlobalOptions)) { func withTestEnvironment(t testing.TB) (env *testEnvironment, cleanup func()) {
if !RunIntegrationTest { if !RunIntegrationTest {
t.Skip("integration tests disabled") 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-") tempdir, err := ioutil.TempDir(TestTempDir, "restic-test-")
OK(t, err) OK(t, err)
env := testEnvironment{ env = &testEnvironment{
base: tempdir, base: tempdir,
cache: filepath.Join(tempdir, "cache"), cache: filepath.Join(tempdir, "cache"),
repo: filepath.Join(tempdir, "repo"), 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.cache, 0700))
OK(t, os.MkdirAll(env.repo, 0700)) OK(t, os.MkdirAll(env.repo, 0700))
gopts := GlobalOptions{ env.gopts = GlobalOptions{
Repo: env.repo, Repo: env.repo,
Quiet: true, Quiet: true,
ctx: context.Background(), ctx: context.Background(),
@ -206,14 +207,15 @@ func withTestEnvironment(t testing.TB, f func(*testEnvironment, GlobalOptions))
} }
// always overwrite global options // always overwrite global options
globalOptions = gopts globalOptions = env.gopts
f(&env, gopts) cleanup = func() {
if !TestCleanupTempDirs {
if !TestCleanupTempDirs { t.Logf("leaving temporary directory %v used for test", tempdir)
t.Logf("leaving temporary directory %v used for test", tempdir) return
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) { func TestRestoreLocalLayout(t *testing.T) {
withTestEnvironment(t, func(env *testEnvironment, gopts GlobalOptions) { env, cleanup := withTestEnvironment(t)
var tests = []struct { defer cleanup()
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"},
}
for _, test := range tests { var tests = []struct {
datafile := filepath.Join("..", "..", "internal", "backend", "testdata", test.filename) 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 env.gopts.extended["local.layout"] = test.layout
testRunCheck(t, gopts)
// restore latest snapshot // check the repo
target := filepath.Join(env.base, "restore") testRunCheck(t, env.gopts)
testRunRestoreLatest(t, gopts, target, nil, "")
RemoveAll(t, filepath.Join(env.base, "repo")) // restore latest snapshot
RemoveAll(t, target) target := filepath.Join(env.base, "restore")
} testRunRestoreLatest(t, env.gopts, target, nil, "")
})
RemoveAll(t, filepath.Join(env.base, "repo"))
RemoveAll(t, target)
}
} }