mirror of
https://github.com/restic/restic.git
synced 2025-11-16 15:32:53 +00:00
Add more integration tests
This commit is contained in:
@@ -2,9 +2,13 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
"testing"
|
||||
|
||||
. "github.com/restic/restic/test"
|
||||
)
|
||||
|
||||
type dirEntry struct {
|
||||
@@ -51,31 +55,33 @@ func walkDir(dir string) <-chan *dirEntry {
|
||||
|
||||
func (e *dirEntry) equals(other *dirEntry) bool {
|
||||
if e.path != other.path {
|
||||
fmt.Printf("path does not match\n")
|
||||
fmt.Fprintf(os.Stderr, "%v: path does not match\n", e.path)
|
||||
return false
|
||||
}
|
||||
|
||||
if e.fi.Mode() != other.fi.Mode() {
|
||||
fmt.Printf("mode does not match\n")
|
||||
fmt.Fprintf(os.Stderr, "%v: mode does not match\n", e.path)
|
||||
return false
|
||||
}
|
||||
|
||||
// if e.fi.ModTime() != other.fi.ModTime() {
|
||||
// fmt.Printf("%s: ModTime does not match\n", e.path)
|
||||
// // TODO: Fix ModTime for directories, return false
|
||||
// return true
|
||||
// }
|
||||
if e.fi.ModTime() != other.fi.ModTime() {
|
||||
fmt.Fprintf(os.Stderr, "%v: ModTime does not match\n", e.path)
|
||||
return false
|
||||
}
|
||||
|
||||
stat, _ := e.fi.Sys().(*syscall.Stat_t)
|
||||
stat2, _ := other.fi.Sys().(*syscall.Stat_t)
|
||||
|
||||
if stat.Uid != stat2.Uid || stat2.Gid != stat2.Gid {
|
||||
fmt.Fprintf(os.Stderr, "%v: UID or GID do not match\n", e.path)
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// directoriesEqualContents checks if both directories contain exactly the same
|
||||
// contents.
|
||||
func directoriesEqualContents(dir1, dir2 string) bool {
|
||||
ch1 := walkDir(dir1)
|
||||
ch2 := walkDir(dir2)
|
||||
@@ -136,3 +142,83 @@ func directoriesEqualContents(dir1, dir2 string) bool {
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
type dirStat struct {
|
||||
files, dirs, other uint
|
||||
size uint64
|
||||
}
|
||||
|
||||
func isFile(fi os.FileInfo) bool {
|
||||
return fi.Mode()&(os.ModeType|os.ModeCharDevice) == 0
|
||||
}
|
||||
|
||||
// dirStats walks dir and collects stats.
|
||||
func dirStats(dir string) (stat dirStat) {
|
||||
for entry := range walkDir(dir) {
|
||||
if isFile(entry.fi) {
|
||||
stat.files++
|
||||
stat.size += uint64(entry.fi.Size())
|
||||
continue
|
||||
}
|
||||
|
||||
if entry.fi.IsDir() {
|
||||
stat.dirs++
|
||||
continue
|
||||
}
|
||||
|
||||
stat.other++
|
||||
}
|
||||
|
||||
return stat
|
||||
}
|
||||
|
||||
type testEnvironment struct {
|
||||
base, cache, repo, testdata string
|
||||
}
|
||||
|
||||
func configureRestic(t testing.TB, cache, repo string) {
|
||||
opts.CacheDir = cache
|
||||
opts.Repo = repo
|
||||
opts.Quiet = true
|
||||
|
||||
opts.password = TestPassword
|
||||
}
|
||||
|
||||
func cleanupTempdir(t testing.TB, tempdir string) {
|
||||
if !TestCleanup {
|
||||
t.Logf("leaving temporary directory %v used for test", tempdir)
|
||||
return
|
||||
}
|
||||
|
||||
OK(t, os.RemoveAll(tempdir))
|
||||
}
|
||||
|
||||
// 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)) {
|
||||
if !RunIntegrationTest {
|
||||
t.Skip("integration tests disabled")
|
||||
}
|
||||
|
||||
tempdir, err := ioutil.TempDir(TestTempDir, "restic-test-")
|
||||
OK(t, err)
|
||||
|
||||
env := testEnvironment{
|
||||
base: tempdir,
|
||||
cache: filepath.Join(tempdir, "cache"),
|
||||
repo: filepath.Join(tempdir, "repo"),
|
||||
testdata: filepath.Join(tempdir, "testdata"),
|
||||
}
|
||||
|
||||
configureRestic(t, env.cache, env.repo)
|
||||
OK(t, os.MkdirAll(env.testdata, 0700))
|
||||
|
||||
f(&env)
|
||||
|
||||
if !TestCleanup {
|
||||
t.Logf("leaving temporary directory %v used for test", tempdir)
|
||||
return
|
||||
}
|
||||
|
||||
OK(t, os.RemoveAll(tempdir))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user