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/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{}) {
hash func() hash.Hash if !condition {
text string _, file, line, _ := runtime.Caller(1)
digest string fmt.Printf("\033[31m%s:%d: "+msg+"\033[39m\n\n", append([]interface{}{filepath.Base(file), line}, v...)...)
}{ tb.FailNow()
{md5.New, "foobar\n", "14758f1afd44c09b7992073ccf00b43d"},
// test data from http://www.nsrl.nist.gov/testdata/
{sha1.New, "abc", "a9993e364706816aba3e25717850c26c9cd0d89d"},
{sha1.New, "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", "84983e441c3bd26ebaae4aa1f95129e5e54670f1"},
} }
}
Describe("Reader", func() { // ok fails the test if an err is not nil.
Context("Static Strings", func() { func ok(tb testing.TB, err error) {
It("Should compute digest", func() { if err != nil {
for _, t := range static_tests { _, file, line, _ := runtime.Caller(1)
r := khepri.NewHashingReader(bytes.NewBuffer([]byte(t.text)), t.hash) 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) { var static_tests = []struct {
Fail("not enough bytes read") 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 { func TestReader(t *testing.T) {
panic(err) 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) func TestWriter(t *testing.T) {
Expect(h).Should(Equal(t.digest)) 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)
}) equals(t, hex.EncodeToString(w.Hash()), test.digest)
}) }
}
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))
}
})
})
})
})

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 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 if err != nil {
repo *khepri.DirRepository return nil, err
err error }
id khepri.ID
)
var _ = BeforeSuite(func() { repo, err := khepri.NewDirRepository(tempdir)
tempdir, err = ioutil.TempDir("", "khepri-test-") if err != nil {
if err != nil { return nil, err
panic(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 { ids, err := repo.ListIDs(tpe)
panic(err) 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))
}
})
})
})
})

View File

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