Add 'build/' from commit '555b039609a3c8700c27767c255fdfe00eb42063'
[arvados.git] / services / arv-git-httpd / gitolite_test.go
1 package main
2
3 import (
4         "io/ioutil"
5         "os"
6         "os/exec"
7         "strings"
8
9         check "gopkg.in/check.v1"
10 )
11
12 var _ = check.Suite(&GitoliteSuite{})
13
14 // GitoliteSuite tests need an API server, an arv-git-httpd server,
15 // and a repository hosted by gitolite.
16 type GitoliteSuite struct {
17         IntegrationSuite
18         gitoliteHome string
19 }
20
21 func (s *GitoliteSuite) SetUpTest(c *check.C) {
22         var err error
23         s.gitoliteHome, err = ioutil.TempDir("", "arv-git-httpd")
24         c.Assert(err, check.Equals, nil)
25
26         runGitolite := func(prog string, args ...string) {
27                 c.Log(prog, " ", args)
28                 cmd := exec.Command(prog, args...)
29                 cmd.Dir = s.gitoliteHome
30                 cmd.Env = []string{"HOME=" + s.gitoliteHome}
31                 for _, e := range os.Environ() {
32                         if !strings.HasPrefix(e, "HOME=") {
33                                 cmd.Env = append(cmd.Env, e)
34                         }
35                 }
36                 diags, err := cmd.CombinedOutput()
37                 c.Log(string(diags))
38                 c.Assert(err, check.Equals, nil)
39         }
40
41         runGitolite("gitolite", "setup", "--admin", "root")
42
43         s.tmpRepoRoot = s.gitoliteHome + "/repositories"
44         s.Config = &config{
45                 Addr:       ":0",
46                 GitCommand: "/usr/share/gitolite3/gitolite-shell",
47                 Root:       s.tmpRepoRoot,
48         }
49         s.IntegrationSuite.SetUpTest(c)
50
51         // Install the gitolite hooks in the bare repo we made in
52         // (*IntegrationTest)SetUpTest() -- see 2.2.4 at
53         // http://gitolite.com/gitolite/gitolite.html
54         runGitolite("gitolite", "setup")
55
56         os.Setenv("GITOLITE_HTTP_HOME", s.gitoliteHome)
57         os.Setenv("GL_BYPASS_ACCESS_CHECKS", "1")
58 }
59
60 func (s *GitoliteSuite) TearDownTest(c *check.C) {
61         // We really want Unsetenv here, but it's not worth forcing an
62         // upgrade to Go 1.4.
63         os.Setenv("GITOLITE_HTTP_HOME", "")
64         os.Setenv("GL_BYPASS_ACCESS_CHECKS", "")
65         s.IntegrationSuite.TearDownTest(c)
66 }
67
68 func (s *GitoliteSuite) TestFetch(c *check.C) {
69         err := s.RunGit(c, activeToken, "fetch", "active/foo.git")
70         c.Check(err, check.Equals, nil)
71 }
72
73 func (s *GitoliteSuite) TestFetchUnreadable(c *check.C) {
74         err := s.RunGit(c, anonymousToken, "fetch", "active/foo.git")
75         c.Check(err, check.ErrorMatches, `.* not found.*`)
76 }
77
78 func (s *GitoliteSuite) TestPush(c *check.C) {
79         err := s.RunGit(c, activeToken, "push", "active/foo.git", "master:gitolite-push")
80         c.Check(err, check.Equals, nil)
81
82         // Check that the commit hash appears in the gitolite log, as
83         // assurance that the gitolite hooks really did run.
84
85         sha1, err := exec.Command("git", "--git-dir", s.tmpWorkdir+"/.git",
86                 "log", "-n1", "--format=%H").CombinedOutput()
87         c.Logf("git-log in workdir: %q", string(sha1))
88         c.Assert(err, check.Equals, nil)
89         c.Assert(len(sha1), check.Equals, 41)
90
91         gitoliteLog, err := exec.Command("grep", "-r", string(sha1[:40]), s.gitoliteHome+"/.gitolite/logs").CombinedOutput()
92         c.Check(err, check.Equals, nil)
93         c.Logf("gitolite log message: %q", string(gitoliteLog))
94 }
95
96 func (s *GitoliteSuite) TestPushUnwritable(c *check.C) {
97         err := s.RunGit(c, spectatorToken, "push", "active/foo.git", "master:gitolite-push-fail")
98         c.Check(err, check.ErrorMatches, `.*HTTP code = 403.*`)
99 }