aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2017-11-20 18:29:11 +0100
committerMáximo Cuadros <mcuadros@gmail.com>2017-11-20 18:29:11 +0100
commita364785ba63465eb61bed35ded7794c05a884517 (patch)
tree8c08a4f172e4ea83ba24b726b13a39eeb8c8dda2
parent319bac952a9c8a141942d8b22b4c83d06c41a0d4 (diff)
downloadgo-git-a364785ba63465eb61bed35ded7794c05a884517.tar.gz
transport: git and http, full mocked tests
Signed-off-by: Máximo Cuadros <mcuadros@gmail.com>
-rw-r--r--plumbing/transport/git/common_test.go92
-rw-r--r--plumbing/transport/git/receive_pack_test.go129
-rw-r--r--plumbing/transport/git/upload_pack_test.go21
-rw-r--r--plumbing/transport/http/common_test.go72
-rw-r--r--plumbing/transport/http/receive_pack_test.go96
-rw-r--r--plumbing/transport/http/upload_pack_test.go44
6 files changed, 211 insertions, 243 deletions
diff --git a/plumbing/transport/git/common_test.go b/plumbing/transport/git/common_test.go
index 3f25ad9..7de41cb 100644
--- a/plumbing/transport/git/common_test.go
+++ b/plumbing/transport/git/common_test.go
@@ -1,9 +1,101 @@
package git
import (
+ "fmt"
+ "io/ioutil"
+ "net"
+ "os"
+ "os/exec"
+ "path/filepath"
"testing"
+ "time"
+ "github.com/src-d/go-git-fixtures"
. "gopkg.in/check.v1"
+ "gopkg.in/src-d/go-git.v4/plumbing/transport"
)
func Test(t *testing.T) { TestingT(t) }
+
+type BaseSuite struct {
+ fixtures.Suite
+
+ base string
+ port int
+ daemon *exec.Cmd
+}
+
+func (s *BaseSuite) SetUpTest(c *C) {
+ var err error
+ s.port, err = freePort()
+ c.Assert(err, IsNil)
+
+ s.base, err = ioutil.TempDir(os.TempDir(), fmt.Sprintf("go-git-protocol-%d", s.port))
+ c.Assert(err, IsNil)
+}
+
+func (s *BaseSuite) StartDaemon(c *C) {
+ s.daemon = exec.Command(
+ "git",
+ "daemon",
+ fmt.Sprintf("--base-path=%s", s.base),
+ "--export-all",
+ "--enable=receive-pack",
+ "--reuseaddr",
+ fmt.Sprintf("--port=%d", s.port),
+ // Unless max-connections is limited to 1, a git-receive-pack
+ // might not be seen by a subsequent operation.
+ "--max-connections=1",
+ )
+
+ // Environment must be inherited in order to acknowledge GIT_EXEC_PATH if set.
+ s.daemon.Env = os.Environ()
+
+ err := s.daemon.Start()
+ c.Assert(err, IsNil)
+
+ // Connections might be refused if we start sending request too early.
+ time.Sleep(time.Millisecond * 500)
+}
+
+func (s *BaseSuite) newEndpoint(c *C, name string) transport.Endpoint {
+ ep, err := transport.NewEndpoint(fmt.Sprintf("git://localhost:%d/%s", s.port, name))
+ c.Assert(err, IsNil)
+
+ return ep
+}
+
+func (s *BaseSuite) prepareRepository(c *C, f *fixtures.Fixture, name string) transport.Endpoint {
+ fs := f.DotGit()
+
+ err := fixtures.EnsureIsBare(fs)
+ c.Assert(err, IsNil)
+
+ path := filepath.Join(s.base, name)
+ err = os.Rename(fs.Root(), path)
+ c.Assert(err, IsNil)
+
+ return s.newEndpoint(c, name)
+}
+
+func (s *BaseSuite) TearDownTest(c *C) {
+ _ = s.daemon.Process.Signal(os.Interrupt)
+ _ = s.daemon.Wait()
+
+ err := os.RemoveAll(s.base)
+ c.Assert(err, IsNil)
+}
+
+func freePort() (int, error) {
+ addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
+ if err != nil {
+ return 0, err
+ }
+
+ l, err := net.ListenTCP("tcp", addr)
+ if err != nil {
+ return 0, err
+ }
+
+ return l.Addr().(*net.TCPAddr).Port, l.Close()
+}
diff --git a/plumbing/transport/git/receive_pack_test.go b/plumbing/transport/git/receive_pack_test.go
index 7b0fa46..c63d706 100644
--- a/plumbing/transport/git/receive_pack_test.go
+++ b/plumbing/transport/git/receive_pack_test.go
@@ -1,19 +1,9 @@
package git
import (
- "fmt"
- "io"
- "io/ioutil"
- "net"
- "os"
- "os/exec"
- "path/filepath"
"runtime"
- "strings"
- "time"
"github.com/src-d/go-git-fixtures"
- "gopkg.in/src-d/go-git.v4/plumbing/transport"
"gopkg.in/src-d/go-git.v4/plumbing/transport/test"
. "gopkg.in/check.v1"
@@ -21,10 +11,7 @@ import (
type ReceivePackSuite struct {
test.ReceivePackSuite
- fixtures.Suite
-
- base string
- daemon *exec.Cmd
+ BaseSuite
}
var _ = Suite(&ReceivePackSuite{})
@@ -35,114 +22,12 @@ func (s *ReceivePackSuite) SetUpTest(c *C) {
See https://github.com/git-for-windows/git/issues/907`)
}
- s.ReceivePackSuite.Client = DefaultClient
-
- port, err := freePort()
- c.Assert(err, IsNil)
-
- base, err := ioutil.TempDir(os.TempDir(), "go-git-daemon-test")
- c.Assert(err, IsNil)
- s.base = base
-
- host := fmt.Sprintf("localhost_%d", port)
- interpolatedBase := filepath.Join(base, host)
- err = os.MkdirAll(interpolatedBase, 0755)
- c.Assert(err, IsNil)
-
- dotgit := fixtures.Basic().One().DotGit().Root()
- prepareRepo(c, dotgit)
- err = os.Rename(dotgit, filepath.Join(interpolatedBase, "basic.git"))
- c.Assert(err, IsNil)
-
- ep, err := transport.NewEndpoint(fmt.Sprintf("git://localhost:%d/basic.git", port))
- c.Assert(err, IsNil)
- s.ReceivePackSuite.Endpoint = ep
+ s.BaseSuite.SetUpTest(c)
- dotgit = fixtures.ByTag("empty").One().DotGit().Root()
- prepareRepo(c, dotgit)
- err = os.Rename(dotgit, filepath.Join(interpolatedBase, "empty.git"))
- c.Assert(err, IsNil)
-
- ep, err = transport.NewEndpoint(fmt.Sprintf("git://localhost:%d/empty.git", port))
- c.Assert(err, IsNil)
- s.ReceivePackSuite.EmptyEndpoint = ep
-
- ep, err = transport.NewEndpoint(fmt.Sprintf("git://localhost:%d/non-existent.git", port))
- c.Assert(err, IsNil)
- s.ReceivePackSuite.NonExistentEndpoint = ep
-
- s.daemon = exec.Command(
- "git",
- "daemon",
- fmt.Sprintf("--base-path=%s", base),
- "--export-all",
- "--enable=receive-pack",
- "--reuseaddr",
- fmt.Sprintf("--port=%d", port),
- // Use interpolated paths to validate that clients are specifying
- // host and port properly.
- // Note that some git versions (e.g. v2.11.0) had a bug that prevented
- // the use of repository paths containing colons (:), so we use
- // underscore (_) instead of colon in the interpolation.
- // See https://github.com/git/git/commit/fe050334074c5132d01e1df2c1b9a82c9b8d394c
- fmt.Sprintf("--interpolated-path=%s/%%H_%%P%%D", base),
- // Unless max-connections is limited to 1, a git-receive-pack
- // might not be seen by a subsequent operation.
- "--max-connections=1",
- // Whitelist required for interpolated paths.
- fmt.Sprintf("%s/%s", interpolatedBase, "basic.git"),
- fmt.Sprintf("%s/%s", interpolatedBase, "empty.git"),
- )
-
- // Environment must be inherited in order to acknowledge GIT_EXEC_PATH if set.
- s.daemon.Env = os.Environ()
-
- err = s.daemon.Start()
- c.Assert(err, IsNil)
-
- // Connections might be refused if we start sending request too early.
- time.Sleep(time.Millisecond * 500)
-}
-
-func (s *ReceivePackSuite) TearDownTest(c *C) {
- err := s.daemon.Process.Signal(os.Kill)
- c.Assert(err, IsNil)
-
- _ = s.daemon.Wait()
-
- err = os.RemoveAll(s.base)
- c.Assert(err, IsNil)
-}
-
-func freePort() (int, error) {
- addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
- if err != nil {
- return 0, err
- }
-
- l, err := net.ListenTCP("tcp", addr)
- if err != nil {
- return 0, err
- }
-
- return l.Addr().(*net.TCPAddr).Port, l.Close()
-}
-
-const bareConfig = `[core]
-repositoryformatversion = 0
-filemode = true
-bare = true`
+ s.ReceivePackSuite.Client = DefaultClient
+ s.ReceivePackSuite.Endpoint = s.prepareRepository(c, fixtures.Basic().One(), "basic.git")
+ s.ReceivePackSuite.EmptyEndpoint = s.prepareRepository(c, fixtures.ByTag("empty").One(), "empty.git")
+ s.ReceivePackSuite.NonExistentEndpoint = s.newEndpoint(c, "non-existent.git")
-func prepareRepo(c *C, path string) {
- // git-receive-pack refuses to update refs/heads/master on non-bare repo
- // so we ensure bare repo config.
- config := filepath.Join(path, "config")
- if _, err := os.Stat(config); err == nil {
- f, err := os.OpenFile(config, os.O_TRUNC|os.O_WRONLY, 0)
- c.Assert(err, IsNil)
- content := strings.NewReader(bareConfig)
- _, err = io.Copy(f, content)
- c.Assert(err, IsNil)
- c.Assert(f.Close(), IsNil)
- }
+ s.StartDaemon(c)
}
diff --git a/plumbing/transport/git/upload_pack_test.go b/plumbing/transport/git/upload_pack_test.go
index d367a7f..6c29c8b 100644
--- a/plumbing/transport/git/upload_pack_test.go
+++ b/plumbing/transport/git/upload_pack_test.go
@@ -2,7 +2,6 @@ package git
import (
"github.com/src-d/go-git-fixtures"
- "gopkg.in/src-d/go-git.v4/plumbing/transport"
"gopkg.in/src-d/go-git.v4/plumbing/transport/test"
. "gopkg.in/check.v1"
@@ -10,26 +9,18 @@ import (
type UploadPackSuite struct {
test.UploadPackSuite
- fixtures.Suite
+ BaseSuite
}
var _ = Suite(&UploadPackSuite{})
func (s *UploadPackSuite) SetUpSuite(c *C) {
- s.Suite.SetUpSuite(c)
+ s.BaseSuite.SetUpTest(c)
s.UploadPackSuite.Client = DefaultClient
+ s.UploadPackSuite.Endpoint = s.prepareRepository(c, fixtures.Basic().One(), "basic.git")
+ s.UploadPackSuite.EmptyEndpoint = s.prepareRepository(c, fixtures.ByTag("empty").One(), "empty.git")
+ s.UploadPackSuite.NonExistentEndpoint = s.newEndpoint(c, "non-existent.git")
- ep, err := transport.NewEndpoint("git://github.com/git-fixtures/basic.git")
- c.Assert(err, IsNil)
- s.UploadPackSuite.Endpoint = ep
-
- ep, err = transport.NewEndpoint("git://github.com/git-fixtures/empty.git")
- c.Assert(err, IsNil)
- s.UploadPackSuite.EmptyEndpoint = ep
-
- ep, err = transport.NewEndpoint("git://github.com/git-fixtures/non-existent.git")
- c.Assert(err, IsNil)
- s.UploadPackSuite.NonExistentEndpoint = ep
-
+ s.StartDaemon(c)
}
diff --git a/plumbing/transport/http/common_test.go b/plumbing/transport/http/common_test.go
index d1f36d3..8dcc903 100644
--- a/plumbing/transport/http/common_test.go
+++ b/plumbing/transport/http/common_test.go
@@ -2,9 +2,20 @@ package http
import (
"crypto/tls"
+ "fmt"
+ "io/ioutil"
+ "log"
+ "net"
"net/http"
+ "net/http/cgi"
+ "os"
+ "os/exec"
+ "path/filepath"
+ "strings"
"testing"
+ fixtures "github.com/src-d/go-git-fixtures"
+
"gopkg.in/src-d/go-git.v4/plumbing/transport"
. "gopkg.in/check.v1"
@@ -95,3 +106,64 @@ func (s *ClientSuite) TestSetAuthWrongType(c *C) {
_, err := DefaultClient.NewUploadPackSession(s.Endpoint, &mockAuth{})
c.Assert(err, Equals, transport.ErrInvalidAuthMethod)
}
+
+type BaseSuite struct {
+ fixtures.Suite
+
+ base string
+ host string
+ port int
+}
+
+func (s *BaseSuite) SetUpTest(c *C) {
+ l, err := net.Listen("tcp", "localhost:0")
+ c.Assert(err, IsNil)
+
+ base, err := ioutil.TempDir(os.TempDir(), fmt.Sprintf("go-git-http-%d", s.port))
+ c.Assert(err, IsNil)
+
+ s.port = l.Addr().(*net.TCPAddr).Port
+ s.base = filepath.Join(base, s.host)
+
+ err = os.MkdirAll(s.base, 0755)
+ c.Assert(err, IsNil)
+
+ cmd := exec.Command("git", "--exec-path")
+ out, err := cmd.CombinedOutput()
+ c.Assert(err, IsNil)
+
+ server := &http.Server{
+ Handler: &cgi.Handler{
+ Path: filepath.Join(strings.Trim(string(out), "\n"), "git-http-backend"),
+ Env: []string{"GIT_HTTP_EXPORT_ALL=true", fmt.Sprintf("GIT_PROJECT_ROOT=%s", s.base)},
+ },
+ }
+ go func() {
+ log.Fatal(server.Serve(l))
+ }()
+}
+
+func (s *BaseSuite) prepareRepository(c *C, f *fixtures.Fixture, name string) transport.Endpoint {
+ fs := f.DotGit()
+
+ err := fixtures.EnsureIsBare(fs)
+ c.Assert(err, IsNil)
+
+ path := filepath.Join(s.base, name)
+ err = os.Rename(fs.Root(), path)
+ c.Assert(err, IsNil)
+
+ return s.newEndpoint(c, name)
+}
+
+func (s *BaseSuite) newEndpoint(c *C, name string) transport.Endpoint {
+ ep, err := transport.NewEndpoint(fmt.Sprintf("http://localhost:%d/%s", s.port, name))
+ c.Assert(err, IsNil)
+
+ return ep
+}
+
+func (s *BaseSuite) TearDownTest(c *C) {
+ err := os.RemoveAll(s.base)
+ c.Assert(err, IsNil)
+}
diff --git a/plumbing/transport/http/receive_pack_test.go b/plumbing/transport/http/receive_pack_test.go
index 970121d..aed375d 100644
--- a/plumbing/transport/http/receive_pack_test.go
+++ b/plumbing/transport/http/receive_pack_test.go
@@ -1,19 +1,6 @@
package http
import (
- "fmt"
- "io"
- "io/ioutil"
- "log"
- "net"
- "net/http"
- "net/http/cgi"
- "os"
- "os/exec"
- "path/filepath"
- "strings"
-
- "gopkg.in/src-d/go-git.v4/plumbing/transport"
"gopkg.in/src-d/go-git.v4/plumbing/transport/test"
"github.com/src-d/go-git-fixtures"
@@ -22,93 +9,16 @@ import (
type ReceivePackSuite struct {
test.ReceivePackSuite
- fixtures.Suite
-
- base string
- host string
- port int
+ BaseSuite
}
var _ = Suite(&ReceivePackSuite{})
func (s *ReceivePackSuite) SetUpTest(c *C) {
- s.ReceivePackSuite.Client = DefaultClient
-
- l, err := net.Listen("tcp", "localhost:0")
- c.Assert(err, IsNil)
-
- base, err := ioutil.TempDir(os.TempDir(), "go-git-http-backend-test")
- c.Assert(err, IsNil)
-
- s.port = l.Addr().(*net.TCPAddr).Port
- s.host = fmt.Sprintf("localhost_%d", s.port)
- s.base = filepath.Join(base, s.host)
-
- err = os.MkdirAll(s.base, 0755)
- c.Assert(err, IsNil)
+ s.BaseSuite.SetUpTest(c)
+ s.ReceivePackSuite.Client = DefaultClient
s.ReceivePackSuite.Endpoint = s.prepareRepository(c, fixtures.Basic().One(), "basic.git")
s.ReceivePackSuite.EmptyEndpoint = s.prepareRepository(c, fixtures.ByTag("empty").One(), "empty.git")
s.ReceivePackSuite.NonExistentEndpoint = s.newEndpoint(c, "non-existent.git")
-
- cmd := exec.Command("git", "--exec-path")
- out, err := cmd.CombinedOutput()
- c.Assert(err, IsNil)
-
- server := &http.Server{
- Handler: &cgi.Handler{
- Path: filepath.Join(strings.Trim(string(out), "\n"), "git-http-backend"),
- Env: []string{"GIT_HTTP_EXPORT_ALL=true", fmt.Sprintf("GIT_PROJECT_ROOT=%s", s.base)},
- },
- }
- go func() {
- log.Fatal(server.Serve(l))
- }()
-}
-
-func (s *ReceivePackSuite) TearDownTest(c *C) {
- err := os.RemoveAll(s.base)
- c.Assert(err, IsNil)
-}
-
-func (s *ReceivePackSuite) prepareRepository(c *C, f *fixtures.Fixture, name string) transport.Endpoint {
- path := filepath.Join(s.base, name)
-
- err := os.Rename(f.DotGit().Root(), path)
- c.Assert(err, IsNil)
-
- s.setConfigToRepository(c, path)
- return s.newEndpoint(c, name)
-}
-
-// git-receive-pack refuses to update refs/heads/master on non-bare repo
-// so we ensure bare repo config.
-func (s *ReceivePackSuite) setConfigToRepository(c *C, path string) {
- cfgPath := filepath.Join(path, "config")
- _, err := os.Stat(cfgPath)
- c.Assert(err, IsNil)
-
- cfg, err := os.OpenFile(cfgPath, os.O_TRUNC|os.O_WRONLY, 0)
- c.Assert(err, IsNil)
-
- content := strings.NewReader("" +
- "[core]\n" +
- "repositoryformatversion = 0\n" +
- "filemode = true\n" +
- "bare = true\n" +
- "[http]\n" +
- "receivepack = true\n",
- )
-
- _, err = io.Copy(cfg, content)
- c.Assert(err, IsNil)
-
- c.Assert(cfg.Close(), IsNil)
-}
-
-func (s *ReceivePackSuite) newEndpoint(c *C, name string) transport.Endpoint {
- ep, err := transport.NewEndpoint(fmt.Sprintf("http://localhost:%d/%s", s.port, name))
- c.Assert(err, IsNil)
-
- return ep
}
diff --git a/plumbing/transport/http/upload_pack_test.go b/plumbing/transport/http/upload_pack_test.go
index 57d5f46..1785c40 100644
--- a/plumbing/transport/http/upload_pack_test.go
+++ b/plumbing/transport/http/upload_pack_test.go
@@ -1,7 +1,12 @@
package http
import (
+ "fmt"
"io/ioutil"
+ "os"
+ "path/filepath"
+
+ fixtures "github.com/src-d/go-git-fixtures"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/protocol/packp"
@@ -13,24 +18,17 @@ import (
type UploadPackSuite struct {
test.UploadPackSuite
+ BaseSuite
}
var _ = Suite(&UploadPackSuite{})
func (s *UploadPackSuite) SetUpSuite(c *C) {
+ s.BaseSuite.SetUpTest(c)
s.UploadPackSuite.Client = DefaultClient
-
- ep, err := transport.NewEndpoint("https://github.com/git-fixtures/basic.git")
- c.Assert(err, IsNil)
- s.UploadPackSuite.Endpoint = ep
-
- ep, err = transport.NewEndpoint("https://github.com/git-fixtures/empty.git")
- c.Assert(err, IsNil)
- s.UploadPackSuite.EmptyEndpoint = ep
-
- ep, err = transport.NewEndpoint("https://github.com/git-fixtures/non-existent.git")
- c.Assert(err, IsNil)
- s.UploadPackSuite.NonExistentEndpoint = ep
+ s.UploadPackSuite.Endpoint = s.prepareRepository(c, fixtures.Basic().One(), "basic.git")
+ s.UploadPackSuite.EmptyEndpoint = s.prepareRepository(c, fixtures.ByTag("empty").One(), "empty.git")
+ s.UploadPackSuite.NonExistentEndpoint = s.newEndpoint(c, "non-existent.git")
}
// Overwritten, different behaviour for HTTP.
@@ -38,7 +36,7 @@ func (s *UploadPackSuite) TestAdvertisedReferencesNotExists(c *C) {
r, err := s.Client.NewUploadPackSession(s.NonExistentEndpoint, s.EmptyAuth)
c.Assert(err, IsNil)
info, err := r.AdvertisedReferences()
- c.Assert(err, Equals, transport.ErrAuthenticationRequired)
+ c.Assert(err, Equals, transport.ErrRepositoryNotFound)
c.Assert(info, IsNil)
}
@@ -58,3 +56,23 @@ func (s *UploadPackSuite) TestuploadPackRequestToReader(c *C) {
"0009done\n",
)
}
+
+func (s *UploadPackSuite) prepareRepository(c *C, f *fixtures.Fixture, name string) transport.Endpoint {
+ fs := f.DotGit()
+
+ err := fixtures.EnsureIsBare(fs)
+ c.Assert(err, IsNil)
+
+ path := filepath.Join(s.base, name)
+ err = os.Rename(fs.Root(), path)
+ c.Assert(err, IsNil)
+
+ return s.newEndpoint(c, name)
+}
+
+func (s *UploadPackSuite) newEndpoint(c *C, name string) transport.Endpoint {
+ ep, err := transport.NewEndpoint(fmt.Sprintf("http://localhost:%d/%s", s.port, name))
+ c.Assert(err, IsNil)
+
+ return ep
+}