Merge branch 'master' into 5720-ajax-loading-error
[arvados.git] / services / arv-git-httpd / server_test.go
1 package main
2
3 import (
4         "errors"
5         "io/ioutil"
6         "os"
7         "os/exec"
8         "strings"
9         "testing"
10
11         "git.curoverse.com/arvados.git/sdk/go/arvadostest"
12         check "gopkg.in/check.v1"
13 )
14
15 var _ = check.Suite(&IntegrationSuite{})
16
17 const (
18         spectatorToken = "zw2f4gwx8hw8cjre7yp6v1zylhrhn3m5gvjq73rtpwhmknrybu"
19         activeToken    = "3kg6k6lzmp9kj5cpkcoxie963cmvjahbt2fod9zru30k1jqdmi"
20         anonymousToken = "4kg6k6lzmp9kj4cpkcoxie964cmvjahbt4fod9zru44k4jqdmi"
21 )
22
23 // IntegrationSuite tests need an API server and an arv-git-httpd server
24 type IntegrationSuite struct {
25         tmpRepoRoot string
26         tmpWorkdir  string
27         testServer  *server
28 }
29
30 func (s *IntegrationSuite) TestPathVariants(c *check.C) {
31         s.makeArvadosRepo(c)
32         for _, repo := range []string{"active/foo.git", "active/foo/.git", "arvados.git", "arvados/.git"} {
33                 err := s.runGit(c, spectatorToken, "fetch", repo)
34                 c.Assert(err, check.Equals, nil)
35         }
36 }
37
38 func (s *IntegrationSuite) TestReadonly(c *check.C) {
39         err := s.runGit(c, spectatorToken, "fetch", "active/foo.git")
40         c.Assert(err, check.Equals, nil)
41         err = s.runGit(c, spectatorToken, "push", "active/foo.git", "master:newbranchfail")
42         c.Assert(err, check.ErrorMatches, `.*HTTP code = 403.*`)
43         _, err = os.Stat(s.tmpRepoRoot + "/zzzzz-s0uqq-382brsig8rp3666/.git/refs/heads/newbranchfail")
44         c.Assert(err, check.FitsTypeOf, &os.PathError{})
45 }
46
47 func (s *IntegrationSuite) TestReadwrite(c *check.C) {
48         err := s.runGit(c, activeToken, "fetch", "active/foo.git")
49         c.Assert(err, check.Equals, nil)
50         err = s.runGit(c, activeToken, "push", "active/foo.git", "master:newbranch")
51         c.Assert(err, check.Equals, nil)
52         _, err = os.Stat(s.tmpRepoRoot + "/zzzzz-s0uqq-382brsig8rp3666/.git/refs/heads/newbranch")
53         c.Assert(err, check.Equals, nil)
54 }
55
56 func (s *IntegrationSuite) TestNonexistent(c *check.C) {
57         err := s.runGit(c, spectatorToken, "fetch", "thisrepodoesnotexist.git")
58         c.Assert(err, check.ErrorMatches, `.* not found.*`)
59 }
60
61 func (s *IntegrationSuite) TestMissingGitdirReadableRepository(c *check.C) {
62         err := s.runGit(c, activeToken, "fetch", "active/foo2.git")
63         c.Assert(err, check.ErrorMatches, `.* not found.*`)
64 }
65
66 func (s *IntegrationSuite) TestNoPermission(c *check.C) {
67         for _, repo := range []string{"active/foo.git", "active/foo/.git"} {
68                 err := s.runGit(c, anonymousToken, "fetch", repo)
69                 c.Assert(err, check.ErrorMatches, `.* not found.*`)
70         }
71 }
72
73 func (s *IntegrationSuite) SetUpSuite(c *check.C) {
74         arvadostest.StartAPI()
75 }
76
77 func (s *IntegrationSuite) SetUpTest(c *check.C) {
78         arvadostest.ResetEnv()
79         s.testServer = &server{}
80         var err error
81         s.tmpRepoRoot, err = ioutil.TempDir("", "arv-git-httpd")
82         c.Assert(err, check.Equals, nil)
83         s.tmpWorkdir, err = ioutil.TempDir("", "arv-git-httpd")
84         c.Assert(err, check.Equals, nil)
85         _, err = exec.Command("git", "init", s.tmpRepoRoot+"/zzzzz-s0uqq-382brsig8rp3666").Output()
86         c.Assert(err, check.Equals, nil)
87         _, err = exec.Command("sh", "-c", "cd "+s.tmpRepoRoot+"/zzzzz-s0uqq-382brsig8rp3666 && echo test >test && git add test && git -c user.name=Foo -c user.email=Foo commit -am 'foo: test'").CombinedOutput()
88         c.Assert(err, check.Equals, nil)
89         _, err = exec.Command("git", "init", s.tmpWorkdir).Output()
90         c.Assert(err, check.Equals, nil)
91         _, err = exec.Command("sh", "-c", "cd "+s.tmpWorkdir+" && echo work >work && git add work && git -c user.name=Foo -c user.email=Foo commit -am 'workdir: test'").CombinedOutput()
92         c.Assert(err, check.Equals, nil)
93
94         theConfig = &config{
95                 Addr:       ":",
96                 GitCommand: "/usr/bin/git",
97                 Root:       s.tmpRepoRoot,
98         }
99         err = s.testServer.Start()
100         c.Assert(err, check.Equals, nil)
101
102         // Clear ARVADOS_API_TOKEN after starting up the server, to
103         // make sure arv-git-httpd doesn't use it.
104         os.Setenv("ARVADOS_API_TOKEN", "unused-token-placates-client-library")
105 }
106
107 func (s *IntegrationSuite) TearDownTest(c *check.C) {
108         var err error
109         if s.testServer != nil {
110                 err = s.testServer.Close()
111         }
112         c.Check(err, check.Equals, nil)
113         if s.tmpRepoRoot != "" {
114                 err = os.RemoveAll(s.tmpRepoRoot)
115                 c.Check(err, check.Equals, nil)
116         }
117         if s.tmpWorkdir != "" {
118                 err = os.RemoveAll(s.tmpWorkdir)
119                 c.Check(err, check.Equals, nil)
120         }
121 }
122
123 func (s *IntegrationSuite) runGit(c *check.C, token, gitCmd, repo string, args ...string) error {
124         cwd, err := os.Getwd()
125         c.Assert(err, check.Equals, nil)
126         defer os.Chdir(cwd)
127         os.Chdir(s.tmpWorkdir)
128
129         gitargs := append([]string{
130                 gitCmd, "http://none:" + token + "@" + s.testServer.Addr + "/" + repo,
131         }, args...)
132         cmd := exec.Command("git", gitargs...)
133         w, err := cmd.StdinPipe()
134         c.Assert(err, check.Equals, nil)
135         w.Close()
136         output, err := cmd.CombinedOutput()
137         c.Log("git ", gitargs, " => ", err)
138         c.Log(string(output))
139         if err != nil && len(output) > 0 {
140                 // If messages appeared on stderr, they are more
141                 // helpful than the err returned by CombinedOutput().
142                 //
143                 // Easier to match error strings without newlines:
144                 err = errors.New(strings.Replace(string(output), "\n", " // ", -1))
145         }
146         return err
147 }
148
149 // Make a bare arvados repo at {tmpRepoRoot}/arvados.git
150 func (s *IntegrationSuite) makeArvadosRepo(c *check.C) {
151         msg, err := exec.Command("git", "init", "--bare", s.tmpRepoRoot+"/zzzzz-s0uqq-arvadosrepo0123.git").CombinedOutput()
152         c.Log(msg)
153         c.Assert(err, check.Equals, nil)
154         msg, err = exec.Command("git", "--git-dir", s.tmpRepoRoot+"/zzzzz-s0uqq-arvadosrepo0123.git", "fetch", "../../.git", "HEAD:master").CombinedOutput()
155         c.Log(msg)
156         c.Assert(err, check.Equals, nil)
157 }
158
159 // Gocheck boilerplate
160 func Test(t *testing.T) {
161         check.TestingT(t)
162 }