Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit 3888c7f

Browse files
committed
transport: http, fixes random failing test, #644
Signed-off-by: Máximo Cuadros <[email protected]>
1 parent 63f4806 commit 3888c7f

File tree

1 file changed

+53
-60
lines changed

1 file changed

+53
-60
lines changed

plumbing/transport/http/receive_pack_test.go

+53-60
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"os/exec"
1313
"path/filepath"
1414
"strings"
15+
"time"
1516

1617
"gopkg.in/src-d/go-git.v4/plumbing/transport"
1718
"gopkg.in/src-d/go-git.v4/plumbing/transport/test"
@@ -25,59 +26,44 @@ type ReceivePackSuite struct {
2526
fixtures.Suite
2627

2728
base string
29+
host string
30+
port int
2831
}
2932

3033
var _ = Suite(&ReceivePackSuite{})
3134

3235
func (s *ReceivePackSuite) SetUpTest(c *C) {
3336
s.ReceivePackSuite.Client = DefaultClient
3437

35-
port, err := freePort()
38+
l, err := net.Listen("tcp", "localhost:0")
3639
c.Assert(err, IsNil)
3740

3841
base, err := ioutil.TempDir(os.TempDir(), "go-git-http-backend-test")
3942
c.Assert(err, IsNil)
40-
s.base = base
4143

42-
host := fmt.Sprintf("localhost_%d", port)
43-
interpolatedBase := filepath.Join(base, host)
44-
err = os.MkdirAll(interpolatedBase, 0755)
45-
c.Assert(err, IsNil)
46-
47-
dotgit := fixtures.Basic().One().DotGit().Root()
48-
prepareRepo(c, dotgit)
49-
err = os.Rename(dotgit, filepath.Join(interpolatedBase, "basic.git"))
50-
c.Assert(err, IsNil)
51-
52-
ep, err := transport.NewEndpoint(fmt.Sprintf("http://localhost:%d/basic.git", port))
53-
c.Assert(err, IsNil)
54-
s.ReceivePackSuite.Endpoint = ep
55-
56-
dotgit = fixtures.ByTag("empty").One().DotGit().Root()
57-
prepareRepo(c, dotgit)
58-
err = os.Rename(dotgit, filepath.Join(interpolatedBase, "empty.git"))
59-
c.Assert(err, IsNil)
44+
s.port = l.Addr().(*net.TCPAddr).Port
45+
s.host = fmt.Sprintf("localhost_%d", s.port)
46+
s.base = filepath.Join(base, s.host)
6047

61-
ep, err = transport.NewEndpoint(fmt.Sprintf("http://localhost:%d/empty.git", port))
48+
err = os.MkdirAll(s.base, 0755)
6249
c.Assert(err, IsNil)
63-
s.ReceivePackSuite.EmptyEndpoint = ep
6450

65-
ep, err = transport.NewEndpoint(fmt.Sprintf("http://localhost:%d/non-existent.git", port))
66-
c.Assert(err, IsNil)
67-
s.ReceivePackSuite.NonExistentEndpoint = ep
51+
s.ReceivePackSuite.Endpoint = s.prepareRepository(c, fixtures.Basic().One(), "basic.git")
52+
s.ReceivePackSuite.EmptyEndpoint = s.prepareRepository(c, fixtures.ByTag("empty").One(), "empty.git")
53+
s.ReceivePackSuite.NonExistentEndpoint = s.newEndpoint(c, "non-existent.git")
6854

6955
cmd := exec.Command("git", "--exec-path")
7056
out, err := cmd.CombinedOutput()
7157
c.Assert(err, IsNil)
72-
p := filepath.Join(strings.Trim(string(out), "\n"), "git-http-backend")
7358

74-
h := &cgi.Handler{
75-
Path: p,
76-
Env: []string{"GIT_HTTP_EXPORT_ALL=true", fmt.Sprintf("GIT_PROJECT_ROOT=%s", interpolatedBase)},
59+
server := &http.Server{
60+
Handler: &cgi.Handler{
61+
Path: filepath.Join(strings.Trim(string(out), "\n"), "git-http-backend"),
62+
Env: []string{"GIT_HTTP_EXPORT_ALL=true", fmt.Sprintf("GIT_PROJECT_ROOT=%s", s.base)},
63+
},
7764
}
78-
7965
go func() {
80-
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), h))
66+
log.Fatal(server.Serve(l))
8167
}()
8268
}
8369

@@ -86,37 +72,44 @@ func (s *ReceivePackSuite) TearDownTest(c *C) {
8672
c.Assert(err, IsNil)
8773
}
8874

89-
func freePort() (int, error) {
90-
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
91-
if err != nil {
92-
return 0, err
93-
}
75+
func (s *ReceivePackSuite) prepareRepository(c *C, f *fixtures.Fixture, name string) transport.Endpoint {
76+
path := filepath.Join(s.base, name)
9477

95-
l, err := net.ListenTCP("tcp", addr)
96-
if err != nil {
97-
return 0, err
98-
}
78+
err := os.Rename(f.DotGit().Root(), path)
79+
c.Assert(err, IsNil)
9980

100-
return l.Addr().(*net.TCPAddr).Port, l.Close()
81+
s.setConfigToRepository(c, path)
82+
return s.newEndpoint(c, name)
10183
}
10284

103-
const bareConfig = `[core]
104-
repositoryformatversion = 0
105-
filemode = true
106-
bare = true
107-
[http]
108-
receivepack = true`
109-
110-
func prepareRepo(c *C, path string) {
111-
// git-receive-pack refuses to update refs/heads/master on non-bare repo
112-
// so we ensure bare repo config.
113-
config := filepath.Join(path, "config")
114-
if _, err := os.Stat(config); err == nil {
115-
f, err := os.OpenFile(config, os.O_TRUNC|os.O_WRONLY, 0)
116-
c.Assert(err, IsNil)
117-
content := strings.NewReader(bareConfig)
118-
_, err = io.Copy(f, content)
119-
c.Assert(err, IsNil)
120-
c.Assert(f.Close(), IsNil)
121-
}
85+
// git-receive-pack refuses to update refs/heads/master on non-bare repo
86+
// so we ensure bare repo config.
87+
func (s *ReceivePackSuite) setConfigToRepository(c *C, path string) {
88+
cfgPath := filepath.Join(path, "config")
89+
_, err := os.Stat(cfgPath)
90+
c.Assert(err, IsNil)
91+
92+
cfg, err := os.OpenFile(cfgPath, os.O_TRUNC|os.O_WRONLY, 0)
93+
c.Assert(err, IsNil)
94+
95+
content := strings.NewReader("" +
96+
"[core]\n" +
97+
"repositoryformatversion = 0\n" +
98+
"filemode = true\n" +
99+
"bare = true\n" +
100+
"[http]\n" +
101+
"receivepack = true\n",
102+
)
103+
104+
_, err = io.Copy(cfg, content)
105+
c.Assert(err, IsNil)
106+
107+
c.Assert(cfg.Close(), IsNil)
108+
}
109+
110+
func (s *ReceivePackSuite) newEndpoint(c *C, name string) transport.Endpoint {
111+
ep, err := transport.NewEndpoint(fmt.Sprintf("http://localhost:%d/%s", s.port, name))
112+
c.Assert(err, IsNil)
113+
114+
return ep
122115
}

0 commit comments

Comments
 (0)