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