Merge branch 'master' into 5735-edit-description-box-size
[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         _, err = exec.Command("git", "config",
95                 "--file", s.tmpWorkdir+"/.git/config",
96                 "credential.http://"+s.testServer.Addr+"/.helper",
97                 "!cred(){ cat >/dev/null; if [ \"$1\" = get ]; then echo password=$ARVADOS_API_TOKEN; fi; };cred").Output()
98         c.Assert(err, check.Equals, nil)
99         _, err = exec.Command("git", "config",
100                 "--file", s.tmpWorkdir+"/.git/config",
101                 "credential.http://"+s.testServer.Addr+"/.username",
102                 "none").Output()
103         c.Assert(err, check.Equals, nil)
104
105         theConfig = &config{
106                 Addr:       ":",
107                 GitCommand: "/usr/bin/git",
108                 Root:       s.tmpRepoRoot,
109         }
110         err = s.testServer.Start()
111         c.Assert(err, check.Equals, nil)
112
113         // Clear ARVADOS_API_TOKEN after starting up the server, to
114         // make sure arv-git-httpd doesn't use it.
115         os.Setenv("ARVADOS_API_TOKEN", "unused-token-placates-client-library")
116 }
117
118 func (s *IntegrationSuite) TearDownTest(c *check.C) {
119         var err error
120         if s.testServer != nil {
121                 err = s.testServer.Close()
122         }
123         c.Check(err, check.Equals, nil)
124         if s.tmpRepoRoot != "" {
125                 err = os.RemoveAll(s.tmpRepoRoot)
126                 c.Check(err, check.Equals, nil)
127         }
128         if s.tmpWorkdir != "" {
129                 err = os.RemoveAll(s.tmpWorkdir)
130                 c.Check(err, check.Equals, nil)
131         }
132 }
133
134 func (s *IntegrationSuite) runGit(c *check.C, token, gitCmd, repo string, args ...string) error {
135         cwd, err := os.Getwd()
136         c.Assert(err, check.Equals, nil)
137         defer os.Chdir(cwd)
138         os.Chdir(s.tmpWorkdir)
139
140         gitargs := append([]string{
141                 gitCmd, "http://" + s.testServer.Addr + "/" + repo,
142         }, args...)
143         cmd := exec.Command("git", gitargs...)
144         cmd.Env = append(os.Environ(), "ARVADOS_API_TOKEN="+token)
145         w, err := cmd.StdinPipe()
146         c.Assert(err, check.Equals, nil)
147         w.Close()
148         output, err := cmd.CombinedOutput()
149         c.Log("git ", gitargs, " => ", err)
150         c.Log(string(output))
151         if err != nil && len(output) > 0 {
152                 // If messages appeared on stderr, they are more
153                 // helpful than the err returned by CombinedOutput().
154                 //
155                 // Easier to match error strings without newlines:
156                 err = errors.New(strings.Replace(string(output), "\n", " // ", -1))
157         }
158         return err
159 }
160
161 // Make a bare arvados repo at {tmpRepoRoot}/arvados.git
162 func (s *IntegrationSuite) makeArvadosRepo(c *check.C) {
163         msg, err := exec.Command("git", "init", "--bare", s.tmpRepoRoot+"/zzzzz-s0uqq-arvadosrepo0123.git").CombinedOutput()
164         c.Log(string(msg))
165         c.Assert(err, check.Equals, nil)
166         msg, err = exec.Command("git", "--git-dir", s.tmpRepoRoot+"/zzzzz-s0uqq-arvadosrepo0123.git", "fetch", "../../.git", "HEAD:master").CombinedOutput()
167         c.Log(string(msg))
168         c.Assert(err, check.Equals, nil)
169 }
170
171 // Gocheck boilerplate
172 func Test(t *testing.T) {
173         check.TestingT(t)
174 }