1
0
Fork 0
mirror of https://github.com/vbatts/dedupe-linker.git synced 2024-11-16 13:48:38 +00:00
dedupe-linker/base/base_test.go

93 lines
1.9 KiB
Go
Raw Normal View History

2014-09-17 13:21:56 +00:00
package base
2014-10-14 20:54:28 +00:00
import (
"bytes"
"io/ioutil"
"os"
"path"
"testing"
)
2014-09-17 13:21:56 +00:00
func TestSumPath(t *testing.T) {
expected := "/var/dedup/blobs/sha1/de/deadbeef"
b := Base{Path: "/var/dedup", HashName: "sha1"}
if bp := b.blobPath("deadbeef"); bp != expected {
t.Errorf("expected %q, got %q", expected, bp)
}
}
2014-10-14 20:54:28 +00:00
func TestRand(t *testing.T) {
randmap := map[string]bool{}
for i := 0; i < 100; i++ {
r, err := randomString()
if err != nil {
t.Fatal(err)
}
if _, ok := randmap[r]; ok {
t.Errorf("expected no duplicates, but %q is a dup random string", r)
}
randmap[r] = true
}
}
2014-10-14 20:54:28 +00:00
func TestGetPut(t *testing.T) {
var (
srcDir, destDir string
err error
)
if srcDir, err = ioutil.TempDir("", "dedupe-linker-src"); err != nil {
t.Fatal(err)
}
defer os.RemoveAll(srcDir)
if destDir, err = ioutil.TempDir("", "dedupe-linker-dest"); err != nil {
t.Fatal(err)
}
defer os.RemoveAll(destDir)
b, err := NewBase(destDir, "sha1")
if err != nil {
t.Fatal(err)
}
rHash := "8f074e76e82ae6156c451019840a6f857bbe5157"
2014-10-14 20:54:28 +00:00
rMsg := "this is the dead beef"
r := bytes.NewReader([]byte(rMsg))
sum, err := b.PutBlob(r, 0666)
if err != nil {
t.Error(err)
}
if sum != rHash {
t.Errorf("expected %q; got %q", rHash, sum)
}
2014-10-14 20:54:28 +00:00
fi, err := b.Stat(rHash)
if err != nil {
t.Error(err)
}
if fi == nil {
t.Fatal("did not find the blob " + rHash)
}
//fmt.Printf("%#v\n", fi.Sys())
2014-10-14 20:54:28 +00:00
if err = b.LinkTo(path.Join(srcDir, "beef1.txt"), rHash); err != nil {
t.Error(err)
}
fi2, err := os.Stat(path.Join(srcDir, "beef1.txt"))
if err != nil {
t.Error(err)
}
if fi2 == nil {
t.Fatal("did not find the linked file " + path.Join(srcDir, "beef1.txt"))
}
//fmt.Printf("%#v\n", fi2.Sys())
2014-10-14 20:54:28 +00:00
if err = b.LinkTo(path.Join(srcDir, "beef1.txt"), rHash); err != nil && !os.IsExist(err) {
t.Error(err)
}
2016-07-18 15:23:33 +00:00
if rHash != sum {
t.Errorf("expected %s; got %s", rHash, sum)
}
2014-10-14 20:54:28 +00:00
}