restic/tree_test.go

73 lines
1.7 KiB
Go
Raw Normal View History

2014-07-28 18:20:32 +00:00
package khepri_test
2014-04-21 21:25:31 +00:00
import (
"bytes"
"strings"
2014-08-01 18:07:38 +00:00
"testing"
2014-04-21 21:25:31 +00:00
"time"
2014-07-28 18:20:32 +00:00
"github.com/fd0/khepri"
2014-04-21 21:25:31 +00:00
)
func parseTime(str string) time.Time {
t, err := time.Parse(time.RFC3339Nano, str)
if err != nil {
panic(err)
}
return t
}
2014-08-01 18:07:38 +00:00
func TestTree(t *testing.T) {
var tree = &khepri.Tree{
Nodes: []khepri.Node{
2014-07-28 18:20:32 +00:00
khepri.Node{
2014-04-27 21:59:58 +00:00
Name: "foobar",
Mode: 0755,
ModTime: parseTime("2014-04-20T22:16:54.161401+02:00"),
AccessTime: parseTime("2014-04-21T22:16:54.161401+02:00"),
User: 1000,
Group: 1001,
Content: []byte{0x41, 0x42, 0x43},
2014-04-21 21:25:31 +00:00
},
2014-07-28 18:20:32 +00:00
khepri.Node{
2014-04-27 21:59:58 +00:00
Name: "baz",
Mode: 0755,
User: 1000,
ModTime: parseTime("2014-04-20T22:16:54.161401+02:00"),
AccessTime: parseTime("2014-04-21T22:16:54.161401+02:00"),
Group: 1001,
Content: []byte("\xde\xad\xbe\xef\xba\xdc\x0d\xe0"),
2014-04-21 21:25:31 +00:00
},
2014-08-01 18:07:38 +00:00
},
}
2014-04-21 21:25:31 +00:00
2014-08-01 18:07:38 +00:00
const raw = `{"nodes":[{"name":"foobar","mode":493,"mtime":"2014-04-20T22:16:54.161401+02:00","atime":"2014-04-21T22:16:54.161401+02:00","user":1000,"group":1001,"content":"414243"},{"name":"baz","mode":493,"mtime":"2014-04-20T22:16:54.161401+02:00","atime":"2014-04-21T22:16:54.161401+02:00","user":1000,"group":1001,"content":"deadbeefbadc0de0"}]}`
2014-04-21 21:25:31 +00:00
2014-08-01 18:07:38 +00:00
// test save
buf := &bytes.Buffer{}
2014-04-21 21:25:31 +00:00
2014-08-01 18:07:38 +00:00
tree.Save(buf)
equals(t, raw, strings.TrimRight(buf.String(), "\n"))
2014-04-21 21:25:31 +00:00
2014-08-01 18:07:38 +00:00
tree2 := new(khepri.Tree)
err := tree2.Restore(buf)
ok(t, err)
equals(t, tree, tree2)
2014-04-21 21:25:31 +00:00
2014-08-01 18:07:38 +00:00
// test nodes for equality
for i, n := range tree.Nodes {
equals(t, n.Content, tree2.Nodes[i].Content)
}
2014-04-21 21:25:31 +00:00
2014-08-01 18:07:38 +00:00
// test restore
buf = bytes.NewBufferString(raw)
2014-04-21 21:25:31 +00:00
2014-08-01 18:07:38 +00:00
tree2 = new(khepri.Tree)
err = tree2.Restore(buf)
ok(t, err)
// test if tree has correctly been restored
equals(t, tree, tree2)
}