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

View file

@ -176,12 +176,15 @@ type testEnvironment struct {
base, cache, repo, testdata string base, cache, repo, testdata string
} }
func configureRestic(t testing.TB, cache, repo string) { func configureRestic(t testing.TB, cache, repo string) GlobalOptions {
globalOpts.CacheDir = cache return GlobalOptions{
globalOpts.Repo = repo CacheDir: cache,
globalOpts.Quiet = true Repo: repo,
Quiet: true,
globalOpts.password = TestPassword password: TestPassword,
stdout: os.Stdout,
}
} }
func cleanupTempdir(t testing.TB, tempdir string) { 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 // withTestEnvironment creates a test environment and calls f with it. After f has
// returned, the temporary directory is removed. // 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 { if !RunIntegrationTest {
t.Skip("integration tests disabled") t.Skip("integration tests disabled")
} }
@ -210,10 +213,9 @@ func withTestEnvironment(t testing.TB, f func(*testEnvironment)) {
testdata: filepath.Join(tempdir, "testdata"), testdata: filepath.Join(tempdir, "testdata"),
} }
configureRestic(t, env.cache, env.repo)
OK(t, os.MkdirAll(env.testdata, 0700)) OK(t, os.MkdirAll(env.testdata, 0700))
f(&env) f(&env, configureRestic(t, env.cache, env.repo))
if !TestCleanup { if !TestCleanup {
t.Logf("leaving temporary directory %v used for test", tempdir) 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) { 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) "sh", outputDir, tarFile)
OK(t, err) OK(t, err)
} }
@ -45,15 +45,15 @@ func parseIDsFromReader(t testing.TB, rd io.Reader) backend.IDs {
return IDs return IDs
} }
func cmdInit(t testing.TB) { func cmdInit(t testing.TB, global GlobalOptions) {
cmd := &CmdInit{} cmd := &CmdInit{global: &global}
OK(t, cmd.Execute(nil)) 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) { func cmdBackup(t testing.TB, global GlobalOptions, target []string, parentID backend.ID) {
cmd := &CmdBackup{} cmd := &CmdBackup{global: &global}
cmd.Parent = parentID.String() cmd.Parent = parentID.String()
t.Logf("backing up %v", target) 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)) 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() rd, wr := io.Pipe()
global.stdout = wr
cmd := &CmdList{w: wr} cmd := &CmdList{global: &global}
go func() { go func() {
OK(t, cmd.Execute([]string{tpe})) OK(t, cmd.Execute([]string{tpe}))
@ -76,23 +76,23 @@ func cmdList(t testing.TB, tpe string) []backend.ID {
return IDs return IDs
} }
func cmdRestore(t testing.TB, dir string, snapshotID backend.ID) { func cmdRestore(t testing.TB, global GlobalOptions, dir string, snapshotID backend.ID) {
cmd := &CmdRestore{} cmd := &CmdRestore{global: &global}
cmd.Execute([]string{snapshotID.String(), dir}) cmd.Execute([]string{snapshotID.String(), dir})
} }
func cmdFsck(t testing.TB) { func cmdFsck(t testing.TB, global GlobalOptions) {
cmd := &CmdFsck{CheckData: true, Orphaned: true} cmd := &CmdFsck{global: &global, CheckData: true, Orphaned: true}
OK(t, cmd.Execute(nil)) OK(t, cmd.Execute(nil))
} }
func cmdKey(t testing.TB, args ...string) { func cmdKey(t testing.TB, global GlobalOptions, args ...string) {
cmd := &CmdKey{} cmd := &CmdKey{global: &global}
OK(t, cmd.Execute(args)) OK(t, cmd.Execute(args))
} }
func TestBackup(t *testing.T) { 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") datafile := filepath.Join("testdata", "backup-data.tar.gz")
fd, err := os.Open(datafile) fd, err := os.Open(datafile)
if os.IsNotExist(err) { if os.IsNotExist(err) {
@ -102,25 +102,24 @@ func TestBackup(t *testing.T) {
OK(t, err) OK(t, err)
OK(t, fd.Close()) OK(t, fd.Close())
cmdInit(t) cmdInit(t, global)
datadir := filepath.Join(env.base, "testdata") setupTarTestFixture(t, env.testdata, datafile)
setupTarTestFixture(t, datadir, datafile)
// first backup // first backup
cmdBackup(t, []string{datadir}, nil) cmdBackup(t, global, []string{env.testdata}, nil)
snapshotIDs := cmdList(t, "snapshots") snapshotIDs := cmdList(t, global, "snapshots")
Assert(t, len(snapshotIDs) == 1, 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) stat1 := dirStats(env.repo)
// second backup, implicit incremental // second backup, implicit incremental
cmdBackup(t, []string{datadir}, nil) cmdBackup(t, global, []string{env.testdata}, nil)
snapshotIDs = cmdList(t, "snapshots") snapshotIDs = cmdList(t, global, "snapshots")
Assert(t, len(snapshotIDs) == 2, Assert(t, len(snapshotIDs) == 2,
"more than one snapshot ID in repo") "expected two snapshots, got %v", snapshotIDs)
stat2 := dirStats(env.repo) stat2 := dirStats(env.repo)
if stat2.size > stat1.size+stat1.size/10 { 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) t.Logf("repository grown by %d bytes", stat2.size-stat1.size)
cmdFsck(t) cmdFsck(t, global)
// third backup, explicit incremental // third backup, explicit incremental
cmdBackup(t, []string{datadir}, snapshotIDs[0]) cmdBackup(t, global, []string{env.testdata}, snapshotIDs[0])
snapshotIDs = cmdList(t, "snapshots") snapshotIDs = cmdList(t, global, "snapshots")
Assert(t, len(snapshotIDs) == 3, Assert(t, len(snapshotIDs) == 3,
"more than two snapshot IDs in repo") "expected three snapshots, got %v", snapshotIDs)
stat3 := dirStats(env.repo) stat3 := dirStats(env.repo)
if stat3.size > stat1.size+stat1.size/10 { if stat3.size > stat1.size+stat1.size/10 {
@ -145,12 +144,12 @@ func TestBackup(t *testing.T) {
for i, snapshotID := range snapshotIDs { for i, snapshotID := range snapshotIDs {
restoredir := filepath.Join(env.base, fmt.Sprintf("restore%d", i)) restoredir := filepath.Join(env.base, fmt.Sprintf("restore%d", i))
t.Logf("restoring snapshot %v to %v", snapshotID.Str(), restoredir) t.Logf("restoring snapshot %v to %v", snapshotID.Str(), restoredir)
cmdRestore(t, restoredir, snapshotIDs[0]) cmdRestore(t, global, restoredir, snapshotIDs[0])
Assert(t, directoriesEqualContents(datadir, filepath.Join(restoredir, "testdata")), Assert(t, directoriesEqualContents(env.testdata, filepath.Join(restoredir, "testdata")),
"directories are not equal") "directories are not equal")
} }
cmdFsck(t) cmdFsck(t, global)
}) })
} }
@ -182,8 +181,12 @@ func appendRandomData(filename string, bytes uint) error {
return f.Close() return f.Close()
} }
func TestInit(t *testing.T) {
}
func TestIncrementalBackup(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") datafile := filepath.Join("testdata", "backup-data.tar.gz")
fd, err := os.Open(datafile) fd, err := os.Open(datafile)
if os.IsNotExist(err) { if os.IsNotExist(err) {
@ -193,21 +196,21 @@ func TestIncrementalBackup(t *testing.T) {
OK(t, err) OK(t, err)
OK(t, fd.Close()) OK(t, fd.Close())
cmdInit(t) cmdInit(t, global)
datadir := filepath.Join(env.base, "testdata") datadir := filepath.Join(env.base, "testdata")
testfile := filepath.Join(datadir, "testfile") testfile := filepath.Join(datadir, "testfile")
OK(t, appendRandomData(testfile, incrementalFirstWrite)) OK(t, appendRandomData(testfile, incrementalFirstWrite))
cmdBackup(t, []string{datadir}, nil) cmdBackup(t, global, []string{datadir}, nil)
cmdFsck(t) cmdFsck(t, global)
stat1 := dirStats(env.repo) stat1 := dirStats(env.repo)
OK(t, appendRandomData(testfile, incrementalSecondWrite)) OK(t, appendRandomData(testfile, incrementalSecondWrite))
cmdBackup(t, []string{datadir}, nil) cmdBackup(t, global, []string{datadir}, nil)
cmdFsck(t) cmdFsck(t, global)
stat2 := dirStats(env.repo) stat2 := dirStats(env.repo)
if stat2.size-stat1.size > incrementalFirstWrite { if stat2.size-stat1.size > incrementalFirstWrite {
t.Errorf("repository size has grown by more than %d bytes", 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)) OK(t, appendRandomData(testfile, incrementalThirdWrite))
cmdBackup(t, []string{datadir}, nil) cmdBackup(t, global, []string{datadir}, nil)
cmdFsck(t) cmdFsck(t, global)
stat3 := dirStats(env.repo) stat3 := dirStats(env.repo)
if stat3.size-stat2.size > incrementalFirstWrite { if stat3.size-stat2.size > incrementalFirstWrite {
t.Errorf("repository size has grown by more than %d bytes", 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) { 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") datafile := filepath.Join("testdata", "backup-data.tar.gz")
fd, err := os.Open(datafile) fd, err := os.Open(datafile)
if os.IsNotExist(err) { if os.IsNotExist(err) {
@ -237,7 +240,7 @@ func TestKeyAddRemove(t *testing.T) {
OK(t, err) OK(t, err)
OK(t, fd.Close()) OK(t, fd.Close())
cmdInit(t) cmdInit(t, global)
cmdKey(t, "list") cmdKey(t, global, "list")
}) })
} }