diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2017-11-20 18:29:11 +0100 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2017-11-20 18:29:11 +0100 |
commit | a364785ba63465eb61bed35ded7794c05a884517 (patch) | |
tree | 8c08a4f172e4ea83ba24b726b13a39eeb8c8dda2 /plumbing/transport/http | |
parent | 319bac952a9c8a141942d8b22b4c83d06c41a0d4 (diff) | |
download | go-git-a364785ba63465eb61bed35ded7794c05a884517.tar.gz |
transport: git and http, full mocked tests
Signed-off-by: Máximo Cuadros <mcuadros@gmail.com>
Diffstat (limited to 'plumbing/transport/http')
-rw-r--r-- | plumbing/transport/http/common_test.go | 72 | ||||
-rw-r--r-- | plumbing/transport/http/receive_pack_test.go | 96 | ||||
-rw-r--r-- | plumbing/transport/http/upload_pack_test.go | 44 |
3 files changed, 106 insertions, 106 deletions
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 +} |