aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/transport/test/common.go
diff options
context:
space:
mode:
authorSantiago M. Mola <santi@mola.io>2016-11-25 09:25:49 +0100
committerMáximo Cuadros <mcuadros@gmail.com>2016-11-25 09:25:49 +0100
commit9e34f68d980de57631c588aaa910c9ea95ed7c2e (patch)
treeb1bd9f867b757ca46ada2f349d122723dde3529c /plumbing/transport/test/common.go
parent8966c042795509ed17730e50d352ad69901c3da8 (diff)
downloadgo-git-9e34f68d980de57631c588aaa910c9ea95ed7c2e.tar.gz
plumbing/transport: add common tests and fixes. (#136)
* plumbing/transport: add common tests and fixes. * add common test suite for different transport implementations. * fix different behaviour on error handling for ssh and http. fixes issue #123. * support detecting unexisting repositories with SSH + GitHub/Bitbucket (apparently, there is no standard for all SSH servers). * remove ssh.NewClient (only DefaultClient makes sense at the moment). * make ssh.Client and http.Client private. * utils/ioutil: utilities to work with io interfaces. * * transport: test actual objects fetched, not just packfile size. * * fix doc typo. * * improve UploadPackRequest.IsEmpty
Diffstat (limited to 'plumbing/transport/test/common.go')
-rw-r--r--plumbing/transport/test/common.go166
1 files changed, 166 insertions, 0 deletions
diff --git a/plumbing/transport/test/common.go b/plumbing/transport/test/common.go
new file mode 100644
index 0000000..059c0c9
--- /dev/null
+++ b/plumbing/transport/test/common.go
@@ -0,0 +1,166 @@
+// Package test implements common test suite for different transport
+// implementations.
+//
+package test
+
+import (
+ "bytes"
+ "io"
+ "io/ioutil"
+
+ "gopkg.in/src-d/go-git.v4/plumbing"
+ "gopkg.in/src-d/go-git.v4/plumbing/format/packfile"
+ "gopkg.in/src-d/go-git.v4/plumbing/transport"
+ "gopkg.in/src-d/go-git.v4/storage/memory"
+
+ . "gopkg.in/check.v1"
+)
+
+type FetchPackSuite struct {
+ Endpoint transport.Endpoint
+ EmptyEndpoint transport.Endpoint
+ NonExistentEndpoint transport.Endpoint
+ Client transport.Client
+}
+
+func (s *FetchPackSuite) TestInfoEmpty(c *C) {
+ r, err := s.Client.NewFetchPackSession(s.EmptyEndpoint)
+ c.Assert(err, IsNil)
+ info, err := r.AdvertisedReferences()
+ c.Assert(err, Equals, transport.ErrEmptyRemoteRepository)
+ c.Assert(info, IsNil)
+}
+
+func (s *FetchPackSuite) TestInfoNotExists(c *C) {
+ r, err := s.Client.NewFetchPackSession(s.NonExistentEndpoint)
+ c.Assert(err, IsNil)
+ info, err := r.AdvertisedReferences()
+ c.Assert(err, Equals, transport.ErrRepositoryNotFound)
+ c.Assert(info, IsNil)
+
+ r, err = s.Client.NewFetchPackSession(s.NonExistentEndpoint)
+ c.Assert(err, IsNil)
+ req := &transport.UploadPackRequest{}
+ req.Want(plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5"))
+ reader, err := r.FetchPack(req)
+ c.Assert(err, Equals, transport.ErrRepositoryNotFound)
+ c.Assert(reader, IsNil)
+}
+
+func (s *FetchPackSuite) TestCannotCallAdvertisedReferenceTwice(c *C) {
+ r, err := s.Client.NewFetchPackSession(s.Endpoint)
+ c.Assert(err, IsNil)
+ _, err = r.AdvertisedReferences()
+ c.Assert(err, IsNil)
+ _, err = r.AdvertisedReferences()
+ c.Assert(err, Equals, transport.ErrAdvertistedReferencesAlreadyCalled)
+}
+
+func (s *FetchPackSuite) TestDefaultBranch(c *C) {
+ r, err := s.Client.NewFetchPackSession(s.Endpoint)
+ c.Assert(err, IsNil)
+ defer func() { c.Assert(r.Close(), IsNil) }()
+
+ info, err := r.AdvertisedReferences()
+ c.Assert(err, IsNil)
+ c.Assert(info.Capabilities.SymbolicReference("HEAD"), Equals, "refs/heads/master")
+}
+
+func (s *FetchPackSuite) TestCapabilities(c *C) {
+ r, err := s.Client.NewFetchPackSession(s.Endpoint)
+ c.Assert(err, IsNil)
+ defer func() { c.Assert(r.Close(), IsNil) }()
+
+ info, err := r.AdvertisedReferences()
+ c.Assert(err, IsNil)
+ c.Assert(info.Capabilities.Get("agent").Values, HasLen, 1)
+}
+
+func (s *FetchPackSuite) TestFullFetchPack(c *C) {
+ r, err := s.Client.NewFetchPackSession(s.Endpoint)
+ c.Assert(err, IsNil)
+ defer func() { c.Assert(r.Close(), IsNil) }()
+
+ info, err := r.AdvertisedReferences()
+ c.Assert(err, IsNil)
+ c.Assert(info, NotNil)
+
+ req := &transport.UploadPackRequest{}
+ req.Want(plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5"))
+
+ reader, err := r.FetchPack(req)
+ c.Assert(err, IsNil)
+
+ s.checkObjectNumber(c, reader, 28)
+}
+
+func (s *FetchPackSuite) TestFetchPack(c *C) {
+ r, err := s.Client.NewFetchPackSession(s.Endpoint)
+ c.Assert(err, IsNil)
+ defer func() { c.Assert(r.Close(), IsNil) }()
+
+ req := &transport.UploadPackRequest{}
+ req.Want(plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5"))
+
+ reader, err := r.FetchPack(req)
+ c.Assert(err, IsNil)
+
+ s.checkObjectNumber(c, reader, 28)
+}
+
+func (s *FetchPackSuite) TestFetchPackNoChanges(c *C) {
+ r, err := s.Client.NewFetchPackSession(s.Endpoint)
+ c.Assert(err, IsNil)
+ defer func() { c.Assert(r.Close(), IsNil) }()
+
+ req := &transport.UploadPackRequest{}
+ req.Want(plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5"))
+ req.Have(plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5"))
+
+ reader, err := r.FetchPack(req)
+ c.Assert(err, Equals, transport.ErrEmptyUploadPackRequest)
+ c.Assert(reader, IsNil)
+}
+
+func (s *FetchPackSuite) TestFetchPackMulti(c *C) {
+ r, err := s.Client.NewFetchPackSession(s.Endpoint)
+ c.Assert(err, IsNil)
+ defer func() { c.Assert(r.Close(), IsNil) }()
+
+ req := &transport.UploadPackRequest{}
+ req.Want(plumbing.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5"))
+ req.Want(plumbing.NewHash("e8d3ffab552895c19b9fcf7aa264d277cde33881"))
+
+ reader, err := r.FetchPack(req)
+ c.Assert(err, IsNil)
+
+ s.checkObjectNumber(c, reader, 31)
+}
+
+func (s *FetchPackSuite) TestFetchError(c *C) {
+ r, err := s.Client.NewFetchPackSession(s.Endpoint)
+ c.Assert(err, IsNil)
+
+ req := &transport.UploadPackRequest{}
+ req.Want(plumbing.NewHash("1111111111111111111111111111111111111111"))
+
+ reader, err := r.FetchPack(req)
+ c.Assert(err, Equals, transport.ErrEmptyUploadPackRequest)
+ c.Assert(reader, IsNil)
+
+ //XXX: We do not test Close error, since implementations might return
+ // different errors if a previous error was found.
+}
+
+func (s *FetchPackSuite) checkObjectNumber(c *C, r io.Reader, n int) {
+ b, err := ioutil.ReadAll(r)
+ c.Assert(err, IsNil)
+ buf := bytes.NewBuffer(b)
+ scanner := packfile.NewScanner(buf)
+ storage := memory.NewStorage()
+ d, err := packfile.NewDecoder(scanner, storage)
+ c.Assert(err, IsNil)
+ _, err = d.Decode()
+ c.Assert(err, IsNil)
+ c.Assert(len(storage.Objects), Equals, n)
+}