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
|
|
|
|
2016-09-08 18:13:53 +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)
|
|
|
|
}
|
|
|
|
|
2016-09-13 13:53:47 +00:00
|
|
|
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)
|
|
|
|
}
|
2016-09-13 13:53:47 +00:00
|
|
|
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)
|
|
|
|
}
|
2016-09-13 13:53:47 +00:00
|
|
|
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)
|
|
|
|
}
|
2016-09-13 13:53:47 +00:00
|
|
|
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
|
|
|
}
|