2
2
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:
Alexander Neumann 2014-08-01 20:07:38 +02:00
parent a71b49ebb9
commit 18a835bdd7
5 changed files with 198 additions and 224 deletions

View File

@ -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")
}

View File

@ -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 {
// 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()
}
}
// 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
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"},
}
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() {
Context("Static Strings", func() {
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 {
func TestWriter(t *testing.T) {
for _, test := range static_tests {
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) {
Fail("not enough bytes written")
equals(t, hex.EncodeToString(w.Hash()), test.digest)
}
if err != nil {
panic(err)
}
digest := w.Hash()
h := hex.EncodeToString(digest)
Expect(h).Should(Equal(t.digest))
}
})
})
})
})
}

View File

@ -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")
}

View File

@ -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
)
var _ = BeforeSuite(func() {
tempdir, err = ioutil.TempDir("", "khepri-test-")
func setupRepo() (*khepri.DirRepository, error) {
tempdir, err := ioutil.TempDir("", "khepri-test-")
if err != nil {
panic(err)
return nil, err
}
repo, err = khepri.NewDirRepository(tempdir)
if err != nil {
panic(err)
}
})
AfterSuite(func() {
err = os.RemoveAll(tempdir)
repo, err := khepri.NewDirRepository(tempdir)
if err != nil {
panic(err)
return nil, err
}
// fmt.Fprintf(os.Stderr, "leaving tempdir %s", tempdir)
tempdir = ""
})
Describe("Repository", func() {
Context("File Operations", func() {
It("Should detect non-existing file", func() {
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)
Expect(err).NotTo(HaveOccurred())
ok(t, err)
// try to get string out, should fail
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 {
// 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))
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
var buf bytes.Buffer
rd, err := repo.Get(test.t, id)
Expect(err).NotTo(HaveOccurred())
Expect(rd).ShouldNot(BeNil())
ok(t, err)
assert(t, rd != nil, "Get() returned nil reader")
// compare content
Expect(io.Copy(&buf, rd)).Should(Equal(int64(len(test.data))))
Expect(buf.Bytes()).Should(Equal([]byte(test.data)))
buf, err := ioutil.ReadAll(rd)
equals(t, test.data, string(buf))
}
})
It("Should Add Buffer", func() {
// add buffer
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))
ok(t, err)
equals(t, test.id, id.String())
}
})
It("Should List IDs", func() {
for _, t := range []khepri.Type{khepri.TypeBlob, khepri.TypeRef} {
// list ids
for _, tpe := range []khepri.Type{khepri.TypeBlob, khepri.TypeRef} {
IDs := khepri.IDs{}
for _, test := range TestStrings {
if test.t == t {
if test.t == tpe {
id, err := khepri.ParseID(test.id)
Expect(err).NotTo(HaveOccurred())
ok(t, err)
IDs = append(IDs, id)
}
}
ids, err := repo.ListIDs(t)
ids, err := repo.ListIDs(tpe)
ok(t, err)
sort.Sort(ids)
sort.Sort(IDs)
Expect(err).NotTo(HaveOccurred())
Expect(ids).Should(Equal(IDs))
equals(t, IDs, ids)
}
})
It("Should Remove Content", func() {
// remove content if requested
if *testCleanup {
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))
}
})
})
})
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"))
}
}
}

View File

@ -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))
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)
tree2 = new(khepri.Tree)
err = tree2.Restore(buf)
ok(t, err)
// test if tree has correctly been restored
Expect(t2).To(Equal(t))
})
})
equals(t, tree, tree2)
}