1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
13 "git.arvados.org/arvados.git/lib/config"
14 "git.arvados.org/arvados.git/sdk/go/arvados"
15 "git.arvados.org/arvados.git/sdk/go/ctxlog"
16 check "gopkg.in/check.v1"
19 var _ = check.Suite(&GitoliteSuite{})
21 // GitoliteSuite tests need an API server, an arvados-git-httpd
22 // server, and a repository hosted by gitolite.
23 type GitoliteSuite struct {
28 func (s *GitoliteSuite) SetUpTest(c *check.C) {
30 s.gitoliteHome, err = ioutil.TempDir("", "githttp")
31 c.Assert(err, check.Equals, nil)
33 runGitolite := func(prog string, args ...string) {
34 c.Log(prog, " ", args)
35 cmd := exec.Command(prog, args...)
36 cmd.Dir = s.gitoliteHome
37 cmd.Env = []string{"HOME=" + s.gitoliteHome}
38 for _, e := range os.Environ() {
39 if !strings.HasPrefix(e, "HOME=") {
40 cmd.Env = append(cmd.Env, e)
43 diags, err := cmd.CombinedOutput()
45 c.Assert(err, check.Equals, nil)
48 runGitolite("gitolite", "setup", "--admin", "root")
50 s.tmpRepoRoot = s.gitoliteHome + "/repositories"
52 cfg, err := config.NewLoader(nil, ctxlog.TestLogger(c)).Load()
53 c.Assert(err, check.Equals, nil)
54 s.cluster, err = cfg.GetCluster("")
55 c.Assert(err, check.Equals, nil)
57 s.cluster.Services.GitHTTP.InternalURLs = map[arvados.URL]arvados.ServiceInstance{{Host: "localhost:0"}: {}}
58 s.cluster.TLS.Insecure = true
59 s.cluster.Git.GitCommand = "/usr/share/gitolite3/gitolite-shell"
60 s.cluster.Git.GitoliteHome = s.gitoliteHome
61 s.cluster.Git.Repositories = s.tmpRepoRoot
63 s.IntegrationSuite.SetUpTest(c)
65 // Install the gitolite hooks in the bare repo we made in
66 // (*IntegrationTest)SetUpTest() -- see 2.2.4 at
67 // http://gitolite.com/gitolite/gitolite.html
68 runGitolite("gitolite", "setup")
71 func (s *GitoliteSuite) TearDownTest(c *check.C) {
72 // We really want Unsetenv here, but it's not worth forcing an
74 os.Setenv("GITOLITE_HTTP_HOME", "")
75 os.Setenv("GL_BYPASS_ACCESS_CHECKS", "")
76 if s.gitoliteHome != "" {
77 err := os.RemoveAll(s.gitoliteHome)
78 c.Check(err, check.Equals, nil)
80 s.IntegrationSuite.TearDownTest(c)
83 func (s *GitoliteSuite) TestFetch(c *check.C) {
84 err := s.RunGit(c, activeToken, "fetch", "active/foo.git", "refs/heads/main")
85 c.Check(err, check.Equals, nil)
88 func (s *GitoliteSuite) TestFetchUnreadable(c *check.C) {
89 err := s.RunGit(c, anonymousToken, "fetch", "active/foo.git")
90 c.Check(err, check.ErrorMatches, `.* not found.*`)
93 func (s *GitoliteSuite) TestPush(c *check.C) {
94 err := s.RunGit(c, activeToken, "push", "active/foo.git", "main:gitolite-push")
95 c.Check(err, check.Equals, nil)
97 // Check that the commit hash appears in the gitolite log, as
98 // assurance that the gitolite hooks really did run.
100 sha1, err := exec.Command("git", "--git-dir", s.tmpWorkdir+"/.git",
101 "log", "-n1", "--format=%H").CombinedOutput()
102 c.Logf("git-log in workdir: %q", string(sha1))
103 c.Assert(err, check.Equals, nil)
104 c.Assert(len(sha1), check.Equals, 41)
106 gitoliteLog, err := exec.Command("grep", "-r", string(sha1[:40]), s.gitoliteHome+"/.gitolite/logs").CombinedOutput()
107 c.Check(err, check.Equals, nil)
108 c.Logf("gitolite log message: %q", string(gitoliteLog))
111 func (s *GitoliteSuite) TestPushUnwritable(c *check.C) {
112 err := s.RunGit(c, spectatorToken, "push", "active/foo.git", "main:gitolite-push-fail")
113 c.Check(err, check.ErrorMatches, `.*HTTP (code = )?403.*`)