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