aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/transport/http/receive_pack_test.go
blob: 101a5b7a60f90580786f1c3c85654362cad12b11 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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"
	. "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().Root()
	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().Root()
	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("http://localhost:%d/non-existent.git", port))
	c.Assert(err, IsNil)
	s.ReceivePackSuite.NonExistentEndpoint = ep

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

	h := &cgi.Handler{
		Path: p,
		Env:  []string{"GIT_HTTP_EXPORT_ALL=true", fmt.Sprintf("GIT_PROJECT_ROOT=%s", interpolatedBase)},
	}

	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)
	}
}