mirror of
https://github.com/restic/restic.git
synced 2025-12-13 16:07:02 +00:00
reduce os.stdout / os.stderr usage in tests
This commit is contained in:
@@ -56,13 +56,13 @@ func testBackup(t *testing.T, useFsSnapshot bool) {
|
|||||||
testListSnapshots(t, env.gopts, 1)
|
testListSnapshots(t, env.gopts, 1)
|
||||||
|
|
||||||
testRunCheck(t, env.gopts)
|
testRunCheck(t, env.gopts)
|
||||||
stat1 := dirStats(env.repo)
|
stat1 := dirStats(t, env.repo)
|
||||||
|
|
||||||
// second backup, implicit incremental
|
// second backup, implicit incremental
|
||||||
testRunBackup(t, "", []string{env.testdata}, opts, env.gopts)
|
testRunBackup(t, "", []string{env.testdata}, opts, env.gopts)
|
||||||
snapshotIDs := testListSnapshots(t, env.gopts, 2)
|
snapshotIDs := testListSnapshots(t, env.gopts, 2)
|
||||||
|
|
||||||
stat2 := dirStats(env.repo)
|
stat2 := dirStats(t, env.repo)
|
||||||
if stat2.size > stat1.size+stat1.size/10 {
|
if stat2.size > stat1.size+stat1.size/10 {
|
||||||
t.Error("repository size has grown by more than 10 percent")
|
t.Error("repository size has grown by more than 10 percent")
|
||||||
}
|
}
|
||||||
@@ -74,7 +74,7 @@ func testBackup(t *testing.T, useFsSnapshot bool) {
|
|||||||
testRunBackup(t, "", []string{env.testdata}, opts, env.gopts)
|
testRunBackup(t, "", []string{env.testdata}, opts, env.gopts)
|
||||||
snapshotIDs = testListSnapshots(t, env.gopts, 3)
|
snapshotIDs = testListSnapshots(t, env.gopts, 3)
|
||||||
|
|
||||||
stat3 := dirStats(env.repo)
|
stat3 := dirStats(t, env.repo)
|
||||||
if stat3.size > stat1.size+stat1.size/10 {
|
if stat3.size > stat1.size+stat1.size/10 {
|
||||||
t.Error("repository size has grown by more than 10 percent")
|
t.Error("repository size has grown by more than 10 percent")
|
||||||
}
|
}
|
||||||
@@ -85,7 +85,7 @@ func testBackup(t *testing.T, useFsSnapshot bool) {
|
|||||||
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)
|
||||||
testRunRestore(t, env.gopts, restoredir, snapshotID.String()+":"+toPathInSnapshot(filepath.Dir(env.testdata)))
|
testRunRestore(t, env.gopts, restoredir, snapshotID.String()+":"+toPathInSnapshot(filepath.Dir(env.testdata)))
|
||||||
diff := directoriesContentsDiff(env.testdata, filepath.Join(restoredir, "testdata"))
|
diff := directoriesContentsDiff(t, env.testdata, filepath.Join(restoredir, "testdata"))
|
||||||
rtest.Assert(t, diff == "", "directories are not equal: %v", diff)
|
rtest.Assert(t, diff == "", "directories are not equal: %v", diff)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -435,13 +435,13 @@ func TestIncrementalBackup(t *testing.T) {
|
|||||||
|
|
||||||
testRunBackup(t, "", []string{datadir}, opts, env.gopts)
|
testRunBackup(t, "", []string{datadir}, opts, env.gopts)
|
||||||
testRunCheck(t, env.gopts)
|
testRunCheck(t, env.gopts)
|
||||||
stat1 := dirStats(env.repo)
|
stat1 := dirStats(t, env.repo)
|
||||||
|
|
||||||
rtest.OK(t, appendRandomData(testfile, incrementalSecondWrite))
|
rtest.OK(t, appendRandomData(testfile, incrementalSecondWrite))
|
||||||
|
|
||||||
testRunBackup(t, "", []string{datadir}, opts, env.gopts)
|
testRunBackup(t, "", []string{datadir}, opts, env.gopts)
|
||||||
testRunCheck(t, env.gopts)
|
testRunCheck(t, env.gopts)
|
||||||
stat2 := dirStats(env.repo)
|
stat2 := dirStats(t, 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)
|
||||||
}
|
}
|
||||||
@@ -451,7 +451,7 @@ func TestIncrementalBackup(t *testing.T) {
|
|||||||
|
|
||||||
testRunBackup(t, "", []string{datadir}, opts, env.gopts)
|
testRunBackup(t, "", []string{datadir}, opts, env.gopts)
|
||||||
testRunCheck(t, env.gopts)
|
testRunCheck(t, env.gopts)
|
||||||
stat3 := dirStats(env.repo)
|
stat3 := dirStats(t, 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)
|
||||||
}
|
}
|
||||||
@@ -562,7 +562,7 @@ func TestHardLink(t *testing.T) {
|
|||||||
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)
|
||||||
testRunRestore(t, env.gopts, restoredir, snapshotID.String())
|
testRunRestore(t, env.gopts, restoredir, snapshotID.String())
|
||||||
diff := directoriesContentsDiff(env.testdata, filepath.Join(restoredir, "testdata"))
|
diff := directoriesContentsDiff(t, env.testdata, filepath.Join(restoredir, "testdata"))
|
||||||
rtest.Assert(t, diff == "", "directories are not equal %v", diff)
|
rtest.Assert(t, diff == "", "directories are not equal %v", diff)
|
||||||
|
|
||||||
linkResults := createFileSetPerHardlink(filepath.Join(restoredir, "testdata"))
|
linkResults := createFileSetPerHardlink(filepath.Join(restoredir, "testdata"))
|
||||||
|
|||||||
@@ -48,8 +48,8 @@ func TestCopy(t *testing.T) {
|
|||||||
copiedSnapshotIDs := testListSnapshots(t, env2.gopts, 3)
|
copiedSnapshotIDs := testListSnapshots(t, env2.gopts, 3)
|
||||||
|
|
||||||
// Check that the copies size seems reasonable
|
// Check that the copies size seems reasonable
|
||||||
stat := dirStats(env.repo)
|
stat := dirStats(t, env.repo)
|
||||||
stat2 := dirStats(env2.repo)
|
stat2 := dirStats(t, env2.repo)
|
||||||
sizeDiff := int64(stat.size) - int64(stat2.size)
|
sizeDiff := int64(stat.size) - int64(stat2.size)
|
||||||
if sizeDiff < 0 {
|
if sizeDiff < 0 {
|
||||||
sizeDiff = -sizeDiff
|
sizeDiff = -sizeDiff
|
||||||
@@ -72,7 +72,7 @@ func TestCopy(t *testing.T) {
|
|||||||
testRunRestore(t, env2.gopts, restoredir, snapshotID.String())
|
testRunRestore(t, env2.gopts, restoredir, snapshotID.String())
|
||||||
foundMatch := false
|
foundMatch := false
|
||||||
for cmpdir := range origRestores {
|
for cmpdir := range origRestores {
|
||||||
diff := directoriesContentsDiff(restoredir, cmpdir)
|
diff := directoriesContentsDiff(t, restoredir, cmpdir)
|
||||||
if diff == "" {
|
if diff == "" {
|
||||||
delete(origRestores, cmpdir)
|
delete(origRestores, cmpdir)
|
||||||
foundMatch = true
|
foundMatch = true
|
||||||
|
|||||||
@@ -257,7 +257,7 @@ func TestRestore(t *testing.T) {
|
|||||||
restoredir := filepath.Join(env.base, "restore")
|
restoredir := filepath.Join(env.base, "restore")
|
||||||
testRunRestoreLatest(t, env.gopts, restoredir, nil, nil)
|
testRunRestoreLatest(t, env.gopts, restoredir, nil, nil)
|
||||||
|
|
||||||
diff := directoriesContentsDiff(env.testdata, filepath.Join(restoredir, filepath.Base(env.testdata)))
|
diff := directoriesContentsDiff(t, env.testdata, filepath.Join(restoredir, filepath.Base(env.testdata)))
|
||||||
rtest.Assert(t, diff == "", "directories are not equal %v", diff)
|
rtest.Assert(t, diff == "", "directories are not equal %v", diff)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -30,19 +30,19 @@ type dirEntry struct {
|
|||||||
link uint64
|
link uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
func walkDir(dir string) <-chan *dirEntry {
|
func walkDir(t testing.TB, dir string) <-chan *dirEntry {
|
||||||
ch := make(chan *dirEntry, 100)
|
ch := make(chan *dirEntry, 100)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
t.Logf("error: %v\n", err)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
name, err := filepath.Rel(dir, path)
|
name, err := filepath.Rel(dir, path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
t.Logf("error: %v\n", err)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,7 +56,7 @@ func walkDir(dir string) <-chan *dirEntry {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Walk() error: %v\n", err)
|
t.Logf("Walk() error: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
close(ch)
|
close(ch)
|
||||||
@@ -86,10 +86,10 @@ func sameModTime(fi1, fi2 os.FileInfo) bool {
|
|||||||
|
|
||||||
// directoriesContentsDiff returns a diff between both directories. If these
|
// directoriesContentsDiff returns a diff between both directories. If these
|
||||||
// contain exactly the same contents, then the diff is an empty string.
|
// contain exactly the same contents, then the diff is an empty string.
|
||||||
func directoriesContentsDiff(dir1, dir2 string) string {
|
func directoriesContentsDiff(t testing.TB, dir1, dir2 string) string {
|
||||||
var out bytes.Buffer
|
var out bytes.Buffer
|
||||||
ch1 := walkDir(dir1)
|
ch1 := walkDir(t, dir1)
|
||||||
ch2 := walkDir(dir2)
|
ch2 := walkDir(t, dir2)
|
||||||
|
|
||||||
var a, b *dirEntry
|
var a, b *dirEntry
|
||||||
for {
|
for {
|
||||||
@@ -146,8 +146,8 @@ func isFile(fi os.FileInfo) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// dirStats walks dir and collects stats.
|
// dirStats walks dir and collects stats.
|
||||||
func dirStats(dir string) (stat dirStat) {
|
func dirStats(t testing.TB, dir string) (stat dirStat) {
|
||||||
for entry := range walkDir(dir) {
|
for entry := range walkDir(t, dir) {
|
||||||
if isFile(entry.fi) {
|
if isFile(entry.fi) {
|
||||||
stat.files++
|
stat.files++
|
||||||
stat.size += uint64(entry.fi.Size())
|
stat.size += uint64(entry.fi.Size())
|
||||||
@@ -391,19 +391,16 @@ func testLoadSnapshot(t testing.TB, gopts GlobalOptions, id restic.ID) *restic.S
|
|||||||
func appendRandomData(filename string, bytes uint) error {
|
func appendRandomData(filename string, bytes uint) error {
|
||||||
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, 0666)
|
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, 0666)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprint(os.Stderr, err)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = f.Seek(0, 2)
|
_, err = f.Seek(0, 2)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprint(os.Stderr, err)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = io.Copy(f, io.LimitReader(rand.Reader, int64(bytes)))
|
_, err = io.Copy(f, io.LimitReader(rand.Reader, int64(bytes)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprint(os.Stderr, err)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user