Add tests for node.CreateAt()

This commit is contained in:
Alexander Neumann 2015-05-14 18:05:15 +02:00
parent 3517229844
commit af06376b5b
1 changed files with 100 additions and 4 deletions

View File

@ -1,8 +1,15 @@
package restic
package restic_test
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
"github.com/restic/restic"
"github.com/restic/restic/backend"
. "github.com/restic/restic/test"
)
func BenchmarkNodeFillUser(t *testing.B) {
@ -17,13 +24,12 @@ func BenchmarkNodeFillUser(t *testing.B) {
t.Fatal(err)
}
node := &Node{}
path := tempfile.Name()
t.ResetTimer()
for i := 0; i < t.N; i++ {
node.fillExtra(path, fi)
restic.NodeFromFileInfo(path, fi)
}
}
@ -44,9 +50,99 @@ func BenchmarkNodeFromFileInfo(t *testing.B) {
t.ResetTimer()
for i := 0; i < t.N; i++ {
_, err := NodeFromFileInfo(path, fi)
_, err := restic.NodeFromFileInfo(path, fi)
if err != nil {
t.Fatal(err)
}
}
}
func parseTime(s string) time.Time {
t, err := time.Parse("2006-01-02 15:04:05.999", s)
if err != nil {
panic(err)
}
return t.Local()
}
var nodeTests = []restic.Node{
restic.Node{
Name: "testFile",
Type: "file",
Content: []backend.ID{},
UID: uint32(os.Getuid()),
GID: uint32(os.Getgid()),
Mode: 0604,
ModTime: parseTime("2015-05-14 21:07:23.111"),
AccessTime: parseTime("2015-05-14 21:07:24.222"),
ChangeTime: parseTime("2015-05-14 21:07:25.333"),
},
restic.Node{
Name: "testDir",
Type: "dir",
Subtree: nil,
UID: uint32(os.Getuid()),
GID: uint32(os.Getgid()),
Mode: 020000000750,
ModTime: parseTime("2015-05-14 21:07:23.111"),
AccessTime: parseTime("2015-05-14 21:07:24.222"),
ChangeTime: parseTime("2015-05-14 21:07:25.333"),
},
restic.Node{
Name: "testSymlink",
Type: "symlink",
LinkTarget: "invalid",
UID: uint32(os.Getuid()),
GID: uint32(os.Getgid()),
Mode: 01000000777,
ModTime: parseTime("2015-05-14 21:07:23.111"),
AccessTime: parseTime("2015-05-14 21:07:24.222"),
ChangeTime: parseTime("2015-05-14 21:07:25.333"),
},
}
func TestNodeRestoreAt(t *testing.T) {
tempdir, err := ioutil.TempDir(*TestTempDir, "restic-test-")
OK(t, err)
defer func() {
if *TestCleanup {
OK(t, os.RemoveAll(tempdir))
} else {
t.Logf("leaving tempdir at %v", tempdir)
}
}()
for _, test := range nodeTests {
nodePath := filepath.Join(tempdir, test.Name)
OK(t, test.CreateAt(nodePath, nil))
if test.Type == "dir" {
OK(t, test.RestoreTimestamps(nodePath))
}
fi, err := os.Lstat(nodePath)
OK(t, err)
n2, err := restic.NodeFromFileInfo(nodePath, fi)
OK(t, err)
Assert(t, test.Name == n2.Name,
"%v: name doesn't match", test.Type)
Assert(t, test.Type == n2.Type,
"%v: type doesn't match", test.Type)
Assert(t, test.Size == n2.Size,
"%v: size doesn't match", test.Size)
Assert(t, test.UID == n2.UID,
"%v: UID doesn't match", test.Type)
Assert(t, test.GID == n2.GID,
"%v: GID doesn't match", test.Type)
Assert(t, test.Mode == n2.Mode,
"%v: mode doesn't match", test.Type)
Assert(t, test.ModTime == n2.ModTime,
"%v: ModTime dosn't match", test.Type)
Assert(t, test.AccessTime == n2.AccessTime,
"%v: AccessTime doesn't match", test.Type)
}
}