diff options
author | Antonio Jesus Navarro Perez <antnavper@gmail.com> | 2017-06-16 16:06:32 +0200 |
---|---|---|
committer | Antonio Jesus Navarro Perez <antnavper@gmail.com> | 2017-07-04 11:17:26 +0200 |
commit | 06f26e2d7096a7e8458e9e9b41e05c8bb83babf6 (patch) | |
tree | 98ae3a8168532ce40c06ef2e6a144c6346c42ef3 /plumbing/transport/http/receive_pack_test.go | |
parent | ce6f5b7c82fc6c2c4d41880ed6b26f921dd9c1c3 (diff) | |
download | go-git-06f26e2d7096a7e8458e9e9b41e05c8bb83babf6.tar.gz |
transport: http push
Diffstat (limited to 'plumbing/transport/http/receive_pack_test.go')
-rw-r--r-- | plumbing/transport/http/receive_pack_test.go | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/plumbing/transport/http/receive_pack_test.go b/plumbing/transport/http/receive_pack_test.go new file mode 100644 index 0000000..afc97b6 --- /dev/null +++ b/plumbing/transport/http/receive_pack_test.go @@ -0,0 +1,113 @@ +package http + +import ( + "fmt" + "io" + "io/ioutil" + "log" + "net" + "net/http" + "net/http/cgi" + "os" + "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" + . "gopkg.in/check.v1" +) + +type ReceivePackSuite struct { + test.ReceivePackSuite + fixtures.Suite + + base string +} + +var _ = Suite(&ReceivePackSuite{}) + +func (s *ReceivePackSuite) SetUpTest(c *C) { + s.ReceivePackSuite.Client = DefaultClient + + port, err := freePort() + c.Assert(err, IsNil) + + base, err := ioutil.TempDir(os.TempDir(), "go-git-http-backend-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().Base() + prepareRepo(c, dotgit) + err = os.Rename(dotgit, filepath.Join(interpolatedBase, "basic.git")) + c.Assert(err, IsNil) + + ep, err := transport.NewEndpoint(fmt.Sprintf("http://localhost:%d/basic.git", port)) + c.Assert(err, IsNil) + s.ReceivePackSuite.Endpoint = ep + + dotgit = fixtures.ByTag("empty").One().DotGit().Base() + prepareRepo(c, dotgit) + err = os.Rename(dotgit, filepath.Join(interpolatedBase, "empty.git")) + c.Assert(err, IsNil) + + ep, err = transport.NewEndpoint(fmt.Sprintf("http://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 + + var h http.Handler = &cgi.Handler{ + Path: "/usr/lib/git-core/git-http-backend", + + Env: []string{"GIT_HTTP_EXPORT_ALL=true", fmt.Sprintf("GIT_PROJECT_ROOT=%s", base)}, + } + + go func() { log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), h)) }() +} + +func (s *ReceivePackSuite) TearDownTest(c *C) { + 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` + +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) + } +} |