mirror of
https://github.com/octoleo/restic.git
synced 2024-11-21 20:35:12 +00:00
Removed gingo/gomega for testing
This commit is contained in:
parent
a71b49ebb9
commit
18a835bdd7
@ -1,12 +0,0 @@
|
||||
package khepri_test
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHashing(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "Hashing Suite")
|
||||
}
|
117
hashing_test.go
117
hashing_test.go
@ -5,77 +5,74 @@ import (
|
||||
"crypto/md5"
|
||||
"crypto/sha1"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"hash"
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"testing"
|
||||
|
||||
"github.com/fd0/khepri"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = Describe("Hashing", func() {
|
||||
var static_tests = []struct {
|
||||
hash func() hash.Hash
|
||||
text string
|
||||
digest string
|
||||
}{
|
||||
{md5.New, "foobar\n", "14758f1afd44c09b7992073ccf00b43d"},
|
||||
// test data from http://www.nsrl.nist.gov/testdata/
|
||||
{sha1.New, "abc", "a9993e364706816aba3e25717850c26c9cd0d89d"},
|
||||
{sha1.New, "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", "84983e441c3bd26ebaae4aa1f95129e5e54670f1"},
|
||||
// assert fails the test if the condition is false.
|
||||
func assert(tb testing.TB, condition bool, msg string, v ...interface{}) {
|
||||
if !condition {
|
||||
_, file, line, _ := runtime.Caller(1)
|
||||
fmt.Printf("\033[31m%s:%d: "+msg+"\033[39m\n\n", append([]interface{}{filepath.Base(file), line}, v...)...)
|
||||
tb.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
Describe("Reader", func() {
|
||||
Context("Static Strings", func() {
|
||||
It("Should compute digest", func() {
|
||||
for _, t := range static_tests {
|
||||
r := khepri.NewHashingReader(bytes.NewBuffer([]byte(t.text)), t.hash)
|
||||
// ok fails the test if an err is not nil.
|
||||
func ok(tb testing.TB, err error) {
|
||||
if err != nil {
|
||||
_, file, line, _ := runtime.Caller(1)
|
||||
fmt.Printf("\033[31m%s:%d: unexpected error: %s\033[39m\n\n", filepath.Base(file), line, err.Error())
|
||||
tb.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
n, err := r.Read(make([]byte, len(t.text)+1))
|
||||
// equals fails the test if exp is not equal to act.
|
||||
func equals(tb testing.TB, exp, act interface{}) {
|
||||
if !reflect.DeepEqual(exp, act) {
|
||||
_, file, line, _ := runtime.Caller(1)
|
||||
fmt.Printf("\033[31m%s:%d:\n\n\texp: %#v\n\n\tgot: %#v\033[39m\n\n", filepath.Base(file), line, exp, act)
|
||||
tb.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
if n != len(t.text) {
|
||||
Fail("not enough bytes read")
|
||||
}
|
||||
var static_tests = []struct {
|
||||
hash func() hash.Hash
|
||||
text string
|
||||
digest string
|
||||
}{
|
||||
{md5.New, "foobar\n", "14758f1afd44c09b7992073ccf00b43d"},
|
||||
// test data from http://www.nsrl.nist.gov/testdata/
|
||||
{sha1.New, "abc", "a9993e364706816aba3e25717850c26c9cd0d89d"},
|
||||
{sha1.New, "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", "84983e441c3bd26ebaae4aa1f95129e5e54670f1"},
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
func TestReader(t *testing.T) {
|
||||
for _, test := range static_tests {
|
||||
r := khepri.NewHashingReader(bytes.NewBuffer([]byte(test.text)), test.hash)
|
||||
buf, err := ioutil.ReadAll(r)
|
||||
ok(t, err)
|
||||
equals(t, test.text, string(buf))
|
||||
|
||||
digest := r.Hash()
|
||||
equals(t, hex.EncodeToString(r.Hash()), test.digest)
|
||||
}
|
||||
}
|
||||
|
||||
h := hex.EncodeToString(digest)
|
||||
Expect(h).Should(Equal(t.digest))
|
||||
}
|
||||
})
|
||||
})
|
||||
func TestWriter(t *testing.T) {
|
||||
for _, test := range static_tests {
|
||||
var buf bytes.Buffer
|
||||
w := khepri.NewHashingWriter(&buf, test.hash)
|
||||
|
||||
Context("Random Strings", func() {
|
||||
_, err := w.Write([]byte(test.text))
|
||||
ok(t, err)
|
||||
|
||||
})
|
||||
})
|
||||
|
||||
Describe("Writer", func() {
|
||||
Context("Static Strings", func() {
|
||||
It("Should compute digest", func() {
|
||||
for _, t := range static_tests {
|
||||
var buf bytes.Buffer
|
||||
w := khepri.NewHashingWriter(&buf, t.hash)
|
||||
|
||||
n, err := w.Write([]byte(t.text))
|
||||
|
||||
if n != len(t.text) {
|
||||
Fail("not enough bytes written")
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
digest := w.Hash()
|
||||
|
||||
h := hex.EncodeToString(digest)
|
||||
Expect(h).Should(Equal(t.digest))
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
equals(t, hex.EncodeToString(w.Hash()), test.digest)
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +0,0 @@
|
||||
package khepri_test
|
||||
|
||||
import (
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestStorage(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "Storage Suite")
|
||||
}
|
@ -1,18 +1,19 @@
|
||||
package khepri_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/fd0/khepri"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var testCleanup = flag.Bool("test.cleanup", true, "clean up after running tests (remove repository directory with all content)")
|
||||
|
||||
var TestStrings = []struct {
|
||||
id string
|
||||
t khepri.Type
|
||||
@ -24,106 +25,114 @@ var TestStrings = []struct {
|
||||
{"4e54d2c721cbdb730f01b10b62dec622962b36966ec685880effa63d71c808f2", khepri.TypeBlob, "foo/../../baz"},
|
||||
}
|
||||
|
||||
var _ = Describe("Storage", func() {
|
||||
var (
|
||||
tempdir string
|
||||
repo *khepri.DirRepository
|
||||
err error
|
||||
id khepri.ID
|
||||
)
|
||||
func setupRepo() (*khepri.DirRepository, error) {
|
||||
tempdir, err := ioutil.TempDir("", "khepri-test-")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var _ = BeforeSuite(func() {
|
||||
tempdir, err = ioutil.TempDir("", "khepri-test-")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
repo, err := khepri.NewDirRepository(tempdir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return repo, nil
|
||||
}
|
||||
|
||||
func teardownRepo(repo *khepri.DirRepository) error {
|
||||
if !*testCleanup {
|
||||
fmt.Fprintf(os.Stderr, "leaving repository at %s\n", repo.Path())
|
||||
return nil
|
||||
}
|
||||
|
||||
err := os.RemoveAll(repo.Path())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestRepository(t *testing.T) {
|
||||
repo, err := setupRepo()
|
||||
ok(t, err)
|
||||
|
||||
defer func() {
|
||||
err = teardownRepo(repo)
|
||||
ok(t, err)
|
||||
}()
|
||||
|
||||
// detect non-existing files
|
||||
for _, test := range TestStrings {
|
||||
id, err := khepri.ParseID(test.id)
|
||||
ok(t, err)
|
||||
|
||||
// try to get string out, should fail
|
||||
ret, err := repo.Test(test.t, id)
|
||||
ok(t, err)
|
||||
assert(t, !ret, fmt.Sprintf("id %q was found (but should not have)", test.id))
|
||||
}
|
||||
|
||||
// add files
|
||||
for _, test := range TestStrings {
|
||||
// store string in repository
|
||||
id, err := repo.Put(test.t, strings.NewReader(test.data))
|
||||
ok(t, err)
|
||||
equals(t, test.id, id.String())
|
||||
|
||||
// try to get it out again
|
||||
rd, err := repo.Get(test.t, id)
|
||||
ok(t, err)
|
||||
assert(t, rd != nil, "Get() returned nil reader")
|
||||
|
||||
// compare content
|
||||
buf, err := ioutil.ReadAll(rd)
|
||||
equals(t, test.data, string(buf))
|
||||
}
|
||||
|
||||
// add buffer
|
||||
for _, test := range TestStrings {
|
||||
// store buf in repository
|
||||
id, err := repo.PutRaw(test.t, []byte(test.data))
|
||||
ok(t, err)
|
||||
equals(t, test.id, id.String())
|
||||
}
|
||||
|
||||
// list ids
|
||||
for _, tpe := range []khepri.Type{khepri.TypeBlob, khepri.TypeRef} {
|
||||
IDs := khepri.IDs{}
|
||||
for _, test := range TestStrings {
|
||||
if test.t == tpe {
|
||||
id, err := khepri.ParseID(test.id)
|
||||
ok(t, err)
|
||||
IDs = append(IDs, id)
|
||||
}
|
||||
}
|
||||
repo, err = khepri.NewDirRepository(tempdir)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
||||
ids, err := repo.ListIDs(tpe)
|
||||
ok(t, err)
|
||||
|
||||
sort.Sort(ids)
|
||||
sort.Sort(IDs)
|
||||
equals(t, IDs, ids)
|
||||
}
|
||||
|
||||
// remove content if requested
|
||||
if *testCleanup {
|
||||
for _, test := range TestStrings {
|
||||
id, err := khepri.ParseID(test.id)
|
||||
ok(t, err)
|
||||
|
||||
found, err := repo.Test(test.t, id)
|
||||
ok(t, err)
|
||||
assert(t, found, fmt.Sprintf("id %q was not found before removal"))
|
||||
|
||||
err = repo.Remove(test.t, id)
|
||||
ok(t, err)
|
||||
|
||||
found, err = repo.Test(test.t, id)
|
||||
ok(t, err)
|
||||
assert(t, !found, fmt.Sprintf("id %q was not found before removal"))
|
||||
}
|
||||
})
|
||||
|
||||
AfterSuite(func() {
|
||||
err = os.RemoveAll(tempdir)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
// fmt.Fprintf(os.Stderr, "leaving tempdir %s", tempdir)
|
||||
tempdir = ""
|
||||
})
|
||||
|
||||
Describe("Repository", func() {
|
||||
Context("File Operations", func() {
|
||||
It("Should detect non-existing file", func() {
|
||||
for _, test := range TestStrings {
|
||||
id, err := khepri.ParseID(test.id)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
// try to get string out, should fail
|
||||
ret, err := repo.Test(test.t, id)
|
||||
Expect(ret).Should(Equal(false))
|
||||
}
|
||||
})
|
||||
|
||||
It("Should Add File", func() {
|
||||
for _, test := range TestStrings {
|
||||
// store string in repository
|
||||
id, err = repo.Put(test.t, strings.NewReader(test.data))
|
||||
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(id.String()).Should(Equal(test.id))
|
||||
|
||||
// try to get it out again
|
||||
var buf bytes.Buffer
|
||||
rd, err := repo.Get(test.t, id)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(rd).ShouldNot(BeNil())
|
||||
|
||||
// compare content
|
||||
Expect(io.Copy(&buf, rd)).Should(Equal(int64(len(test.data))))
|
||||
Expect(buf.Bytes()).Should(Equal([]byte(test.data)))
|
||||
}
|
||||
})
|
||||
|
||||
It("Should Add Buffer", func() {
|
||||
for _, test := range TestStrings {
|
||||
// store buf in repository
|
||||
id, err := repo.PutRaw(test.t, []byte(test.data))
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(id.String()).To(Equal(test.id))
|
||||
}
|
||||
})
|
||||
|
||||
It("Should List IDs", func() {
|
||||
for _, t := range []khepri.Type{khepri.TypeBlob, khepri.TypeRef} {
|
||||
IDs := khepri.IDs{}
|
||||
for _, test := range TestStrings {
|
||||
if test.t == t {
|
||||
id, err := khepri.ParseID(test.id)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
IDs = append(IDs, id)
|
||||
}
|
||||
}
|
||||
|
||||
ids, err := repo.ListIDs(t)
|
||||
|
||||
sort.Sort(ids)
|
||||
sort.Sort(IDs)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(ids).Should(Equal(IDs))
|
||||
}
|
||||
})
|
||||
|
||||
It("Should Remove Content", func() {
|
||||
for _, test := range TestStrings {
|
||||
id, err := khepri.ParseID(test.id)
|
||||
Expect(err).ShouldNot(HaveOccurred())
|
||||
Expect(repo.Test(test.t, id)).To(Equal(true))
|
||||
Expect(repo.Remove(test.t, id))
|
||||
Expect(repo.Test(test.t, id)).To(Equal(false))
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
}
|
||||
}
|
||||
|
63
tree_test.go
63
tree_test.go
@ -2,9 +2,8 @@ package khepri_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/fd0/khepri"
|
||||
@ -19,13 +18,9 @@ func parseTime(str string) time.Time {
|
||||
return t
|
||||
}
|
||||
|
||||
var _ = Describe("Tree", func() {
|
||||
var t *khepri.Tree
|
||||
var raw string
|
||||
|
||||
BeforeEach(func() {
|
||||
t = new(khepri.Tree)
|
||||
t.Nodes = []khepri.Node{
|
||||
func TestTree(t *testing.T) {
|
||||
var tree = &khepri.Tree{
|
||||
Nodes: []khepri.Node{
|
||||
khepri.Node{
|
||||
Name: "foobar",
|
||||
Mode: 0755,
|
||||
@ -44,36 +39,34 @@ var _ = Describe("Tree", func() {
|
||||
Group: 1001,
|
||||
Content: []byte("\xde\xad\xbe\xef\xba\xdc\x0d\xe0"),
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
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"}]}`
|
||||
})
|
||||
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"}]}`
|
||||
|
||||
It("Should save", func() {
|
||||
var buf bytes.Buffer
|
||||
t.Save(&buf)
|
||||
Expect(strings.TrimRight(buf.String(), "\n")).To(Equal(raw))
|
||||
// test save
|
||||
buf := &bytes.Buffer{}
|
||||
|
||||
t2 := new(khepri.Tree)
|
||||
err := t2.Restore(&buf)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
tree.Save(buf)
|
||||
equals(t, raw, strings.TrimRight(buf.String(), "\n"))
|
||||
|
||||
// test tree for equality
|
||||
Expect(t2).To(Equal(t))
|
||||
tree2 := new(khepri.Tree)
|
||||
err := tree2.Restore(buf)
|
||||
ok(t, err)
|
||||
equals(t, tree, tree2)
|
||||
|
||||
// test nodes for equality
|
||||
for i, n := range t.Nodes {
|
||||
Expect(n.Content).To(Equal(t2.Nodes[i].Content))
|
||||
}
|
||||
})
|
||||
// test nodes for equality
|
||||
for i, n := range tree.Nodes {
|
||||
equals(t, n.Content, tree2.Nodes[i].Content)
|
||||
}
|
||||
|
||||
It("Should restore", func() {
|
||||
buf := bytes.NewBufferString(raw)
|
||||
t2 := new(khepri.Tree)
|
||||
err := t2.Restore(buf)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
// test restore
|
||||
buf = bytes.NewBufferString(raw)
|
||||
|
||||
// test if tree has correctly been restored
|
||||
Expect(t2).To(Equal(t))
|
||||
})
|
||||
})
|
||||
tree2 = new(khepri.Tree)
|
||||
err = tree2.Restore(buf)
|
||||
ok(t, err)
|
||||
|
||||
// test if tree has correctly been restored
|
||||
equals(t, tree, tree2)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user