1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
13 "git.arvados.org/arvados.git/sdk/go/arvadostest"
15 check "gopkg.in/check.v1"
18 var _ = check.Suite(&GitSuite{})
21 spectatorToken = "zw2f4gwx8hw8cjre7yp6v1zylhrhn3m5gvjq73rtpwhmknrybu"
22 activeToken = "3kg6k6lzmp9kj5cpkcoxie963cmvjahbt2fod9zru30k1jqdmi"
23 anonymousToken = "4kg6k6lzmp9kj4cpkcoxie964cmvjahbt4fod9zru44k4jqdmi"
24 expiredToken = "2ym314ysp27sk7h943q6vtc378srb06se3pq6ghurylyf3pdmx"
27 type GitSuite struct {
31 func (s *GitSuite) TestPathVariants(c *check.C) {
33 for _, repo := range []string{"active/foo.git", "active/foo/.git", "arvados.git", "arvados/.git"} {
34 err := s.RunGit(c, spectatorToken, "fetch", repo, "refs/heads/main")
35 c.Assert(err, check.Equals, nil)
39 func (s *GitSuite) TestReadonly(c *check.C) {
40 err := s.RunGit(c, spectatorToken, "fetch", "active/foo.git", "refs/heads/main")
41 c.Assert(err, check.Equals, nil)
42 err = s.RunGit(c, spectatorToken, "push", "active/foo.git", "main:newbranchfail")
43 c.Assert(err, check.ErrorMatches, `.*HTTP (code = )?403.*`)
44 _, err = os.Stat(s.tmpRepoRoot + "/zzzzz-s0uqq-382brsig8rp3666.git/refs/heads/newbranchfail")
45 c.Assert(err, check.FitsTypeOf, &os.PathError{})
48 func (s *GitSuite) TestReadwrite(c *check.C) {
49 err := s.RunGit(c, activeToken, "fetch", "active/foo.git", "refs/heads/main")
50 c.Assert(err, check.Equals, nil)
51 err = s.RunGit(c, activeToken, "push", "active/foo.git", "main:newbranch")
52 c.Assert(err, check.Equals, nil)
53 _, err = os.Stat(s.tmpRepoRoot + "/zzzzz-s0uqq-382brsig8rp3666.git/refs/heads/newbranch")
54 c.Assert(err, check.Equals, nil)
57 func (s *GitSuite) TestNonexistent(c *check.C) {
58 err := s.RunGit(c, spectatorToken, "fetch", "thisrepodoesnotexist.git", "refs/heads/main")
59 c.Assert(err, check.ErrorMatches, `.* not found.*`)
62 func (s *GitSuite) TestMissingGitdirReadableRepository(c *check.C) {
63 err := s.RunGit(c, activeToken, "fetch", "active/foo2.git", "refs/heads/main")
64 c.Assert(err, check.ErrorMatches, `.* not found.*`)
67 func (s *GitSuite) TestNoPermission(c *check.C) {
68 for _, repo := range []string{"active/foo.git", "active/foo/.git"} {
69 err := s.RunGit(c, anonymousToken, "fetch", repo, "refs/heads/main")
70 c.Assert(err, check.ErrorMatches, `.* not found.*`)
74 func (s *GitSuite) TestExpiredToken(c *check.C) {
75 for _, repo := range []string{"active/foo.git", "active/foo/.git"} {
76 err := s.RunGit(c, expiredToken, "fetch", repo, "refs/heads/main")
77 c.Assert(err, check.ErrorMatches, `.* (500 while accessing|requested URL returned error: 500).*`)
81 func (s *GitSuite) TestInvalidToken(c *check.C) {
82 for _, repo := range []string{"active/foo.git", "active/foo/.git"} {
83 err := s.RunGit(c, "s3cr3tp@ssw0rd", "fetch", repo, "refs/heads/main")
84 c.Assert(err, check.ErrorMatches, `.* requested URL returned error.*`)
88 func (s *GitSuite) TestShortToken(c *check.C) {
89 for _, repo := range []string{"active/foo.git", "active/foo/.git"} {
90 err := s.RunGit(c, "s3cr3t", "fetch", repo, "refs/heads/main")
91 c.Assert(err, check.ErrorMatches, `.* (500 while accessing|requested URL returned error: 500).*`)
95 func (s *GitSuite) TestShortTokenBadReq(c *check.C) {
96 for _, repo := range []string{"bogus"} {
97 err := s.RunGit(c, "s3cr3t", "fetch", repo, "refs/heads/main")
98 c.Assert(err, check.ErrorMatches, `.*not found.*`)
102 // Make a bare arvados repo at {tmpRepoRoot}/arvados.git
103 func (s *GitSuite) makeArvadosRepo(c *check.C) {
104 msg, err := exec.Command("git", "init", "--bare", s.tmpRepoRoot+"/zzzzz-s0uqq-arvadosrepo0123.git").CombinedOutput()
106 c.Assert(err, check.Equals, nil)
107 msg, err = exec.Command("git", "--git-dir", s.tmpRepoRoot+"/zzzzz-s0uqq-arvadosrepo0123.git", "fetch", "../../.git", "HEAD:main").CombinedOutput()
109 c.Assert(err, check.Equals, nil)
112 func (s *GitSuite) TestHealthCheckPing(c *check.C) {
113 req, err := http.NewRequest("GET",
114 "http://"+s.testServer.Addr+"/_health/ping",
116 c.Assert(err, check.Equals, nil)
117 req.Header.Set("Authorization", "Bearer "+arvadostest.ManagementToken)
119 resp := httptest.NewRecorder()
120 s.testServer.Handler.ServeHTTP(resp, req)
121 c.Check(resp.Code, check.Equals, 200)
122 c.Check(resp.Body.String(), check.Matches, `{"health":"OK"}\n`)