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