Fix integration tests

This commit is contained in:
Alexander Neumann 2015-06-21 13:27:56 +02:00
parent a43733d552
commit a99a460b32
3 changed files with 62 additions and 59 deletions

View File

@ -39,7 +39,7 @@ func (cmd CmdKey) listKeys(s *repository.Repository) error {
for id := range s.List(backend.Key, done) {
k, err := repository.LoadKey(s, id.String())
if err != nil {
fmt.Fprintf(os.Stderr, "LoadKey() failed: %v\n", err)
cmd.global.Warnf("LoadKey() failed: %v\n", err)
continue
}
@ -53,9 +53,7 @@ func (cmd CmdKey) listKeys(s *repository.Repository) error {
k.Username, k.Hostname, k.Created.Format(TimeFormat)})
}
tab.Write(os.Stdout)
return nil
return tab.Write(cmd.global.stdout)
}
func (cmd CmdKey) getNewPassword() (string, error) {
@ -81,7 +79,7 @@ func (cmd CmdKey) addKey(repo *repository.Repository) error {
return fmt.Errorf("creating new key failed: %v\n", err)
}
fmt.Printf("saved new key as %s\n", id)
cmd.global.Verbosef("saved new key as %s\n", id)
return nil
}
@ -96,7 +94,7 @@ func (cmd CmdKey) deleteKey(repo *repository.Repository, name string) error {
return err
}
fmt.Printf("removed key %v\n", name)
cmd.global.Verbosef("removed key %v\n", name)
return nil
}
@ -116,7 +114,7 @@ func (cmd CmdKey) changePassword(repo *repository.Repository) error {
return err
}
fmt.Printf("saved new key as %s\n", id)
cmd.global.Verbosef("saved new key as %s\n", id)
return nil
}

View File

@ -176,12 +176,15 @@ type testEnvironment struct {
base, cache, repo, testdata string
}
func configureRestic(t testing.TB, cache, repo string) {
globalOpts.CacheDir = cache
globalOpts.Repo = repo
globalOpts.Quiet = true
func configureRestic(t testing.TB, cache, repo string) GlobalOptions {
return GlobalOptions{
CacheDir: cache,
Repo: repo,
Quiet: true,
globalOpts.password = TestPassword
password: TestPassword,
stdout: os.Stdout,
}
}
func cleanupTempdir(t testing.TB, tempdir string) {
@ -195,7 +198,7 @@ func cleanupTempdir(t testing.TB, tempdir string) {
// 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)) {
func withTestEnvironment(t testing.TB, f func(*testEnvironment, GlobalOptions)) {
if !RunIntegrationTest {
t.Skip("integration tests disabled")
}
@ -210,10 +213,9 @@ func withTestEnvironment(t testing.TB, f func(*testEnvironment)) {
testdata: filepath.Join(tempdir, "testdata"),
}
configureRestic(t, env.cache, env.repo)
OK(t, os.MkdirAll(env.testdata, 0700))
f(&env)
f(&env, configureRestic(t, env.cache, env.repo))
if !TestCleanup {
t.Logf("leaving temporary directory %v used for test", tempdir)

View File

@ -15,7 +15,7 @@ import (
)
func setupTarTestFixture(t testing.TB, outputDir, tarFile string) {
err := system("sh", "-c", `mkdir "$1" && (cd "$1" && tar xz) < "$2"`,
err := system("sh", "-c", `(cd "$1" && tar xz) < "$2"`,
"sh", outputDir, tarFile)
OK(t, err)
}
@ -45,15 +45,15 @@ func parseIDsFromReader(t testing.TB, rd io.Reader) backend.IDs {
return IDs
}
func cmdInit(t testing.TB) {
cmd := &CmdInit{}
func cmdInit(t testing.TB, global GlobalOptions) {
cmd := &CmdInit{global: &global}
OK(t, cmd.Execute(nil))
t.Logf("repository initialized at %v", globalOpts.Repo)
t.Logf("repository initialized at %v", global.Repo)
}
func cmdBackup(t testing.TB, target []string, parentID backend.ID) {
cmd := &CmdBackup{}
func cmdBackup(t testing.TB, global GlobalOptions, target []string, parentID backend.ID) {
cmd := &CmdBackup{global: &global}
cmd.Parent = parentID.String()
t.Logf("backing up %v", target)
@ -61,10 +61,10 @@ func cmdBackup(t testing.TB, target []string, parentID backend.ID) {
OK(t, cmd.Execute(target))
}
func cmdList(t testing.TB, tpe string) []backend.ID {
func cmdList(t testing.TB, global GlobalOptions, tpe string) []backend.ID {
rd, wr := io.Pipe()
cmd := &CmdList{w: wr}
global.stdout = wr
cmd := &CmdList{global: &global}
go func() {
OK(t, cmd.Execute([]string{tpe}))
@ -76,23 +76,23 @@ func cmdList(t testing.TB, tpe string) []backend.ID {
return IDs
}
func cmdRestore(t testing.TB, dir string, snapshotID backend.ID) {
cmd := &CmdRestore{}
func cmdRestore(t testing.TB, global GlobalOptions, dir string, snapshotID backend.ID) {
cmd := &CmdRestore{global: &global}
cmd.Execute([]string{snapshotID.String(), dir})
}
func cmdFsck(t testing.TB) {
cmd := &CmdFsck{CheckData: true, Orphaned: true}
func cmdFsck(t testing.TB, global GlobalOptions) {
cmd := &CmdFsck{global: &global, CheckData: true, Orphaned: true}
OK(t, cmd.Execute(nil))
}
func cmdKey(t testing.TB, args ...string) {
cmd := &CmdKey{}
func cmdKey(t testing.TB, global GlobalOptions, args ...string) {
cmd := &CmdKey{global: &global}
OK(t, cmd.Execute(args))
}
func TestBackup(t *testing.T) {
withTestEnvironment(t, func(env *testEnvironment) {
withTestEnvironment(t, func(env *testEnvironment, global GlobalOptions) {
datafile := filepath.Join("testdata", "backup-data.tar.gz")
fd, err := os.Open(datafile)
if os.IsNotExist(err) {
@ -102,25 +102,24 @@ func TestBackup(t *testing.T) {
OK(t, err)
OK(t, fd.Close())
cmdInit(t)
cmdInit(t, global)
datadir := filepath.Join(env.base, "testdata")
setupTarTestFixture(t, datadir, datafile)
setupTarTestFixture(t, env.testdata, datafile)
// first backup
cmdBackup(t, []string{datadir}, nil)
snapshotIDs := cmdList(t, "snapshots")
cmdBackup(t, global, []string{env.testdata}, nil)
snapshotIDs := cmdList(t, global, "snapshots")
Assert(t, len(snapshotIDs) == 1,
"more than one snapshot ID in repo")
"expected one snapshot, got %v", snapshotIDs)
cmdFsck(t)
cmdFsck(t, global)
stat1 := dirStats(env.repo)
// second backup, implicit incremental
cmdBackup(t, []string{datadir}, nil)
snapshotIDs = cmdList(t, "snapshots")
cmdBackup(t, global, []string{env.testdata}, nil)
snapshotIDs = cmdList(t, global, "snapshots")
Assert(t, len(snapshotIDs) == 2,
"more than one snapshot ID in repo")
"expected two snapshots, got %v", snapshotIDs)
stat2 := dirStats(env.repo)
if stat2.size > stat1.size+stat1.size/10 {
@ -128,12 +127,12 @@ func TestBackup(t *testing.T) {
}
t.Logf("repository grown by %d bytes", stat2.size-stat1.size)
cmdFsck(t)
cmdFsck(t, global)
// third backup, explicit incremental
cmdBackup(t, []string{datadir}, snapshotIDs[0])
snapshotIDs = cmdList(t, "snapshots")
cmdBackup(t, global, []string{env.testdata}, snapshotIDs[0])
snapshotIDs = cmdList(t, global, "snapshots")
Assert(t, len(snapshotIDs) == 3,
"more than two snapshot IDs in repo")
"expected three snapshots, got %v", snapshotIDs)
stat3 := dirStats(env.repo)
if stat3.size > stat1.size+stat1.size/10 {
@ -145,12 +144,12 @@ func TestBackup(t *testing.T) {
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)
cmdRestore(t, restoredir, snapshotIDs[0])
Assert(t, directoriesEqualContents(datadir, filepath.Join(restoredir, "testdata")),
cmdRestore(t, global, restoredir, snapshotIDs[0])
Assert(t, directoriesEqualContents(env.testdata, filepath.Join(restoredir, "testdata")),
"directories are not equal")
}
cmdFsck(t)
cmdFsck(t, global)
})
}
@ -182,8 +181,12 @@ func appendRandomData(filename string, bytes uint) error {
return f.Close()
}
func TestInit(t *testing.T) {
}
func TestIncrementalBackup(t *testing.T) {
withTestEnvironment(t, func(env *testEnvironment) {
withTestEnvironment(t, func(env *testEnvironment, global GlobalOptions) {
datafile := filepath.Join("testdata", "backup-data.tar.gz")
fd, err := os.Open(datafile)
if os.IsNotExist(err) {
@ -193,21 +196,21 @@ func TestIncrementalBackup(t *testing.T) {
OK(t, err)
OK(t, fd.Close())
cmdInit(t)
cmdInit(t, global)
datadir := filepath.Join(env.base, "testdata")
testfile := filepath.Join(datadir, "testfile")
OK(t, appendRandomData(testfile, incrementalFirstWrite))
cmdBackup(t, []string{datadir}, nil)
cmdFsck(t)
cmdBackup(t, global, []string{datadir}, nil)
cmdFsck(t, global)
stat1 := dirStats(env.repo)
OK(t, appendRandomData(testfile, incrementalSecondWrite))
cmdBackup(t, []string{datadir}, nil)
cmdFsck(t)
cmdBackup(t, global, []string{datadir}, nil)
cmdFsck(t, global)
stat2 := dirStats(env.repo)
if stat2.size-stat1.size > incrementalFirstWrite {
t.Errorf("repository size has grown by more than %d bytes", incrementalFirstWrite)
@ -216,8 +219,8 @@ func TestIncrementalBackup(t *testing.T) {
OK(t, appendRandomData(testfile, incrementalThirdWrite))
cmdBackup(t, []string{datadir}, nil)
cmdFsck(t)
cmdBackup(t, global, []string{datadir}, nil)
cmdFsck(t, global)
stat3 := dirStats(env.repo)
if stat3.size-stat2.size > incrementalFirstWrite {
t.Errorf("repository size has grown by more than %d bytes", incrementalFirstWrite)
@ -227,7 +230,7 @@ func TestIncrementalBackup(t *testing.T) {
}
func TestKeyAddRemove(t *testing.T) {
withTestEnvironment(t, func(env *testEnvironment) {
withTestEnvironment(t, func(env *testEnvironment, global GlobalOptions) {
datafile := filepath.Join("testdata", "backup-data.tar.gz")
fd, err := os.Open(datafile)
if os.IsNotExist(err) {
@ -237,7 +240,7 @@ func TestKeyAddRemove(t *testing.T) {
OK(t, err)
OK(t, fd.Close())
cmdInit(t)
cmdKey(t, "list")
cmdInit(t, global)
cmdKey(t, global, "list")
})
}