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