1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
13 "git.curoverse.com/arvados.git/sdk/go/arvados"
14 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
15 check "gopkg.in/check.v1"
18 var _ = check.Suite(&GitoliteSuite{})
20 // GitoliteSuite tests need an API server, an arv-git-httpd server,
21 // and a repository hosted by gitolite.
22 type GitoliteSuite struct {
27 func (s *GitoliteSuite) SetUpTest(c *check.C) {
29 s.gitoliteHome, err = ioutil.TempDir("", "arv-git-httpd")
30 c.Assert(err, check.Equals, nil)
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)
42 diags, err := cmd.CombinedOutput()
44 c.Assert(err, check.Equals, nil)
47 runGitolite("gitolite", "setup", "--admin", "root")
49 s.tmpRepoRoot = s.gitoliteHome + "/repositories"
51 Client: arvados.Client{
52 APIHost: arvadostest.APIHost(),
56 GitCommand: "/usr/share/gitolite3/gitolite-shell",
57 GitoliteHome: s.gitoliteHome,
58 RepoRoot: s.tmpRepoRoot,
60 s.IntegrationSuite.SetUpTest(c)
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")
68 func (s *GitoliteSuite) TearDownTest(c *check.C) {
69 // We really want Unsetenv here, but it's not worth forcing an
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)
77 s.IntegrationSuite.TearDownTest(c)
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)
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.*`)
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)
94 // Check that the commit hash appears in the gitolite log, as
95 // assurance that the gitolite hooks really did run.
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)
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))
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.*`)