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) TestMissingGitdirReadableRepository(c *check.C) {
64         // Active user token
65         os.Setenv("ARVADOS_API_TOKEN", "3kg6k6lzmp9kj5cpkcoxie963cmvjahbt2fod9zru30k1jqdmi")
66         err := s.runGit(c, "fetch", "active/foo2.git")
67         c.Assert(err, check.ErrorMatches, `.* not found.*`)
68 }
69
70 func (s *IntegrationSuite) TestNoPermission(c *check.C) {
71         // Anonymous token
72         os.Setenv("ARVADOS_API_TOKEN", "4kg6k6lzmp9kj4cpkcoxie964cmvjahbt4fod9zru44k4jqdmi")
73         for _, repo := range []string{"active/foo.git", "active/foo/.git"} {
74                 err := s.runGit(c, "fetch", repo)
75                 c.Assert(err, check.ErrorMatches, `.* not found.*`)
76         }
77 }
78
79 func (s *IntegrationSuite) SetUpSuite(c *check.C) {
80         arvadostest.StartAPI()
81 }
82
83 func (s *IntegrationSuite) SetUpTest(c *check.C) {
84         arvadostest.ResetEnv()
85         s.testServer = &server{}
86         var err error
87         s.tmpRepoRoot, err = ioutil.TempDir("", "arv-git-httpd")
88         c.Assert(err, check.Equals, nil)
89         s.tmpWorkdir, err = ioutil.TempDir("", "arv-git-httpd")
90         c.Assert(err, check.Equals, nil)
91         _, err = exec.Command("git", "init", s.tmpRepoRoot+"/zzzzz-s0uqq-382brsig8rp3666").Output()
92         c.Assert(err, check.Equals, nil)
93         _, 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()
94         c.Assert(err, check.Equals, nil)
95         _, err = exec.Command("git", "init", s.tmpWorkdir).Output()
96         c.Assert(err, check.Equals, nil)
97         _, 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()
98         c.Assert(err, check.Equals, nil)
99
100         theConfig = &config{
101                 Addr:       ":",
102                 GitCommand: "/usr/bin/git",
103                 Root:       s.tmpRepoRoot,
104         }
105         err = s.testServer.Start()
106         c.Assert(err, check.Equals, nil)
107
108         // Clear ARVADOS_API_TOKEN after starting up the server, to
109         // make sure arv-git-httpd doesn't use it.
110         os.Setenv("ARVADOS_API_TOKEN", "")
111
112         _, err = exec.Command("git", "config",
113                 "--file", s.tmpWorkdir+"/.git/config",
114                 "credential.http://"+s.testServer.Addr+"/.helper",
115                 "!cred(){ echo password=$ARVADOS_API_TOKEN; };cred").Output()
116         c.Assert(err, check.Equals, nil)
117         _, err = exec.Command("git", "config",
118                 "--file", s.tmpWorkdir+"/.git/config",
119                 "credential.http://"+s.testServer.Addr+"/.username",
120                 "none").Output()
121         c.Assert(err, check.Equals, nil)
122 }
123
124 func (s *IntegrationSuite) TearDownTest(c *check.C) {
125         var err error
126         if s.testServer != nil {
127                 err = s.testServer.Close()
128         }
129         c.Check(err, check.Equals, nil)
130         if s.tmpRepoRoot != "" {
131                 err = os.RemoveAll(s.tmpRepoRoot)
132                 c.Check(err, check.Equals, nil)
133         }
134         if s.tmpWorkdir != "" {
135                 err = os.RemoveAll(s.tmpWorkdir)
136                 c.Check(err, check.Equals, nil)
137         }
138 }
139
140 func (s *IntegrationSuite) runGit(c *check.C, gitCmd, repo string, args ...string) error {
141         cwd, err := os.Getwd()
142         c.Assert(err, check.Equals, nil)
143         defer os.Chdir(cwd)
144         os.Chdir(s.tmpWorkdir)
145
146         gitargs := append([]string{
147                 gitCmd, "http://" + s.testServer.Addr + "/" + repo,
148         }, args...)
149         cmd := exec.Command("git", gitargs...)
150         w, err := cmd.StdinPipe()
151         c.Assert(err, check.Equals, nil)
152         w.Close()
153         output, err := cmd.CombinedOutput()
154         c.Log("git ", gitargs, " => ", err)
155         c.Log(string(output))
156         if err != nil && len(output) > 0 {
157                 // If messages appeared on stderr, they are more
158                 // helpful than the err returned by CombinedOutput().
159                 //
160                 // Easier to match error strings without newlines:
161                 err = errors.New(strings.Replace(string(output), "\n", " // ", -1))
162         }
163         return err
164 }
165
166 // Make a bare arvados repo at {tmpRepoRoot}/arvados.git
167 func (s *IntegrationSuite) makeArvadosRepo(c *check.C) {
168         _, err := exec.Command("git", "init", "--bare", s.tmpRepoRoot+"/zzzzz-s0uqq-arvadosrepo0123.git").Output()
169         c.Assert(err, check.Equals, nil)
170         _, err = exec.Command("git", "--git-dir", s.tmpRepoRoot+"/zzzzz-s0uqq-arvadosrepo0123.git", "fetch", "../../.git", "master:master").Output()
171         c.Assert(err, check.Equals, nil)
172 }
173
174 // Gocheck boilerplate
175 func Test(t *testing.T) {
176         check.TestingT(t)
177 }