10816: Release savepoint after rollback. Use exec_query instead of execute. Move...
[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         "git.curoverse.com/arvados.git/sdk/go/arvadostest"
11         check "gopkg.in/check.v1"
12 )
13
14 var _ = check.Suite(&GitoliteSuite{})
15
16 // GitoliteSuite tests need an API server, an arv-git-httpd server,
17 // and a repository hosted by gitolite.
18 type GitoliteSuite struct {
19         IntegrationSuite
20         gitoliteHome string
21 }
22
23 func (s *GitoliteSuite) SetUpTest(c *check.C) {
24         var err error
25         s.gitoliteHome, err = ioutil.TempDir("", "arv-git-httpd")
26         c.Assert(err, check.Equals, nil)
27
28         runGitolite := func(prog string, args ...string) {
29                 c.Log(prog, " ", args)
30                 cmd := exec.Command(prog, args...)
31                 cmd.Dir = s.gitoliteHome
32                 cmd.Env = []string{"HOME=" + s.gitoliteHome}
33                 for _, e := range os.Environ() {
34                         if !strings.HasPrefix(e, "HOME=") {
35                                 cmd.Env = append(cmd.Env, e)
36                         }
37                 }
38                 diags, err := cmd.CombinedOutput()
39                 c.Log(string(diags))
40                 c.Assert(err, check.Equals, nil)
41         }
42
43         runGitolite("gitolite", "setup", "--admin", "root")
44
45         s.tmpRepoRoot = s.gitoliteHome + "/repositories"
46         s.Config = &Config{
47                 Client: arvados.Client{
48                         APIHost:  arvadostest.APIHost(),
49                         Insecure: true,
50                 },
51                 Listen:       ":0",
52                 GitCommand:   "/usr/share/gitolite3/gitolite-shell",
53                 GitoliteHome: s.gitoliteHome,
54                 RepoRoot:     s.tmpRepoRoot,
55         }
56         s.IntegrationSuite.SetUpTest(c)
57
58         // Install the gitolite hooks in the bare repo we made in
59         // (*IntegrationTest)SetUpTest() -- see 2.2.4 at
60         // http://gitolite.com/gitolite/gitolite.html
61         runGitolite("gitolite", "setup")
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 }