Merge branch '18773-check-image-id' refs #18773
[arvados.git] / services / arv-git-httpd / server_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         "net/http"
9         "net/http/httptest"
10         "os"
11         "os/exec"
12
13         "git.arvados.org/arvados.git/sdk/go/arvadostest"
14
15         check "gopkg.in/check.v1"
16 )
17
18 var _ = check.Suite(&GitSuite{})
19
20 const (
21         spectatorToken = "zw2f4gwx8hw8cjre7yp6v1zylhrhn3m5gvjq73rtpwhmknrybu"
22         activeToken    = "3kg6k6lzmp9kj5cpkcoxie963cmvjahbt2fod9zru30k1jqdmi"
23         anonymousToken = "4kg6k6lzmp9kj4cpkcoxie964cmvjahbt4fod9zru44k4jqdmi"
24         expiredToken   = "2ym314ysp27sk7h943q6vtc378srb06se3pq6ghurylyf3pdmx"
25 )
26
27 type GitSuite struct {
28         IntegrationSuite
29 }
30
31 func (s *GitSuite) TestPathVariants(c *check.C) {
32         s.makeArvadosRepo(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)
36         }
37 }
38
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{})
46 }
47
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)
55 }
56
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.*`)
60 }
61
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.*`)
65 }
66
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.*`)
71         }
72 }
73
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).*`)
78         }
79 }
80
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.*`)
85         }
86 }
87
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).*`)
92         }
93 }
94
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.*`)
99         }
100 }
101
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()
105         c.Log(string(msg))
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()
108         c.Log(string(msg))
109         c.Assert(err, check.Equals, nil)
110 }
111
112 func (s *GitSuite) TestHealthCheckPing(c *check.C) {
113         req, err := http.NewRequest("GET",
114                 "http://"+s.testServer.Addr+"/_health/ping",
115                 nil)
116         c.Assert(err, check.Equals, nil)
117         req.Header.Set("Authorization", "Bearer "+arvadostest.ManagementToken)
118
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`)
123 }