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