2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-02 11:46:36 +00:00
restic/node_test.go

149 lines
3.5 KiB
Go
Raw Normal View History

2015-05-14 16:05:15 +00:00
package restic_test
2015-05-01 21:52:36 +00:00
import (
"io/ioutil"
2015-05-14 16:05:15 +00:00
"os"
"path/filepath"
2015-05-01 21:52:36 +00:00
"testing"
2015-05-14 16:05:15 +00:00
"time"
"github.com/restic/restic"
"github.com/restic/restic/backend"
. "github.com/restic/restic/test"
2015-05-01 21:52:36 +00:00
)
func BenchmarkNodeFillUser(t *testing.B) {
tempfile, err := ioutil.TempFile("", "restic-test-temp-")
2015-05-01 22:26:32 +00:00
defer tempfile.Close()
2015-05-01 21:52:36 +00:00
if err != nil {
t.Fatal(err)
}
fi, err := tempfile.Stat()
if err != nil {
t.Fatal(err)
}
path := tempfile.Name()
t.ResetTimer()
for i := 0; i < t.N; i++ {
2015-05-14 16:05:15 +00:00
restic.NodeFromFileInfo(path, fi)
2015-05-01 21:52:36 +00:00
}
}
2015-05-01 22:26:32 +00:00
func BenchmarkNodeFromFileInfo(t *testing.B) {
tempfile, err := ioutil.TempFile("", "restic-test-temp-")
defer tempfile.Close()
if err != nil {
t.Fatal(err)
}
fi, err := tempfile.Stat()
if err != nil {
t.Fatal(err)
}
path := tempfile.Name()
t.ResetTimer()
for i := 0; i < t.N; i++ {
2015-05-14 16:05:15 +00:00
_, err := restic.NodeFromFileInfo(path, fi)
2015-05-01 22:26:32 +00:00
if err != nil {
t.Fatal(err)
}
}
}
2015-05-14 16:05:15 +00:00
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) {
2015-06-13 10:35:19 +00:00
tempdir, err := ioutil.TempDir(TestTempDir, "restic-test-")
2015-05-14 16:05:15 +00:00
OK(t, err)
defer func() {
2015-06-13 10:35:19 +00:00
if TestCleanup {
2015-05-14 16:05:15 +00:00
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,
2015-07-08 13:15:06 +00:00
"%v: name doesn't match (%v != %v)", test.Type, test.Name, n2.Name)
2015-05-14 16:05:15 +00:00
Assert(t, test.Type == n2.Type,
2015-07-08 13:15:06 +00:00
"%v: type doesn't match (%v != %v)", test.Type, test.Type, n2.Type)
2015-05-14 16:05:15 +00:00
Assert(t, test.Size == n2.Size,
2015-07-08 13:15:06 +00:00
"%v: size doesn't match (%v != %v)", test.Size, test.Size, n2.Size)
2015-05-14 16:05:15 +00:00
Assert(t, test.UID == n2.UID,
2015-07-08 13:15:06 +00:00
"%v: UID doesn't match (%v != %v)", test.Type, test.UID, n2.UID)
2015-05-14 16:05:15 +00:00
Assert(t, test.GID == n2.GID,
2015-07-08 13:15:06 +00:00
"%v: GID doesn't match (%v != %v)", test.Type, test.GID, n2.GID)
2015-05-14 16:05:15 +00:00
Assert(t, test.Mode == n2.Mode,
2015-07-08 13:15:06 +00:00
"%v: mode doesn't match (%v != %v)", test.Type, test.Mode, n2.Mode)
2015-05-14 16:05:15 +00:00
Assert(t, test.ModTime == n2.ModTime,
2015-07-08 13:15:06 +00:00
"%v: ModTime dosn't match (%v != %v)", test.Type, test.ModTime, n2.ModTime)
2015-05-14 16:05:15 +00:00
Assert(t, test.AccessTime == n2.AccessTime,
2015-07-08 13:15:06 +00:00
"%v: AccessTime doesn't match (%v != %v)", test.Type, test.AccessTime, n2.AccessTime)
2015-05-14 16:05:15 +00:00
}
}